Server : nginx/1.20.1
System : Linux iZ2ze9ojcl78uluczwag69Z 4.18.0-240.22.1.el8_3.x86_64 #1 SMP Thu Apr 8 19:01:30 UTC 2021 x86_64
User : www ( 1000)
PHP Version : 7.3.28
Disable Function : passthru,exec,system,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Directory :  /www/wwwroot/0531yanglao.com/vendor/topthink/framework/tests/
Upload File :
Current Directory [ Writeable ] Root Directory [ Writeable ]


Current File : /www/wwwroot/0531yanglao.com/vendor/topthink/framework/tests/CacheTest.php
<?php

namespace think\tests;

use InvalidArgumentException;
use Mockery as m;
use Mockery\MockInterface;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
use think\App;
use think\Cache;
use think\Config;
use think\Container;

class CacheTest extends TestCase
{
    /** @var App|MockInterface */
    protected $app;

    /** @var Cache|MockInterface */
    protected $cache;

    /** @var Config|MockInterface */
    protected $config;

    protected function tearDown(): void
    {
        m::close();
    }

    protected function setUp()
    {
        $this->app = m::mock(App::class)->makePartial();

        Container::setInstance($this->app);
        $this->app->shouldReceive('make')->with(App::class)->andReturn($this->app);
        $this->config = m::mock(Config::class)->makePartial();
        $this->app->shouldReceive('get')->with('config')->andReturn($this->config);

        $this->cache = new Cache($this->app);
    }

    public function testGetConfig()
    {
        $config = [
            'default' => 'file',
        ];

        $this->config->shouldReceive('get')->with('cache')->andReturn($config);

        $this->assertEquals($config, $this->cache->getConfig());

        $this->expectException(InvalidArgumentException::class);
        $this->cache->getStoreConfig('foo');
    }

    public function testCacheManagerInstances()
    {
        $this->config->shouldReceive('get')->with("cache.stores.single", null)->andReturn(['type' => 'file']);

        $channel1 = $this->cache->store('single');
        $channel2 = $this->cache->store('single');

        $this->assertSame($channel1, $channel2);
    }

    public function testFileCache()
    {
        $root = vfsStream::setup();

        $this->config->shouldReceive('get')->with("cache.default", null)->andReturn('file');

        $this->config->shouldReceive('get')->with("cache.stores.file", null)->andReturn(['type' => 'file', 'path' => $root->url()]);

        $this->cache->set('foo', 5);
        $this->cache->inc('foo');
        $this->assertEquals(6, $this->cache->get('foo'));
        $this->cache->dec('foo', 2);
        $this->assertEquals(4, $this->cache->get('foo'));

        $this->cache->set('bar', true);
        $this->assertTrue($this->cache->get('bar'));

        $this->cache->set('baz', null);
        $this->assertNull($this->cache->get('baz'));

        $this->assertTrue($this->cache->has('baz'));
        $this->cache->delete('baz');
        $this->assertFalse($this->cache->has('baz'));
        $this->assertNull($this->cache->get('baz'));
        $this->assertFalse($this->cache->get('baz', false));

        $this->assertTrue($root->hasChildren());
        $this->cache->clear();
        $this->assertFalse($root->hasChildren());

        //tags
        $this->cache->tag('foo')->set('bar', 'foobar');
        $this->assertEquals('foobar', $this->cache->get('bar'));
        $this->cache->tag('foo')->clear();
        $this->assertFalse($this->cache->has('bar'));

        //multiple
        $this->cache->setMultiple(['foo' => ['foobar', 'bar'], 'foobar' => ['foo', 'bar']]);
        $this->assertEquals(['foo' => ['foobar', 'bar'], 'foobar' => ['foo', 'bar']], $this->cache->getMultiple(['foo', 'foobar']));
        $this->assertTrue($this->cache->deleteMultiple(['foo', 'foobar']));
    }

    public function testRedisCache()
    {
        if (extension_loaded('redis')) {
            return;
        }
        $this->config->shouldReceive('get')->with("cache.default", null)->andReturn('redis');
        $this->config->shouldReceive('get')->with("cache.stores.redis", null)->andReturn(['type' => 'redis']);

        $redis = m::mock('overload:\Predis\Client');

        $redis->shouldReceive("set")->once()->with('foo', 5)->andReturnTrue();
        $redis->shouldReceive("incrby")->once()->with('foo', 1)->andReturnTrue();
        $redis->shouldReceive("decrby")->once()->with('foo', 2)->andReturnTrue();
        $redis->shouldReceive("get")->once()->with('foo')->andReturn('6');
        $redis->shouldReceive("get")->once()->with('foo')->andReturn('4');
        $redis->shouldReceive("set")->once()->with('bar', serialize(true))->andReturnTrue();
        $redis->shouldReceive("set")->once()->with('baz', serialize(null))->andReturnTrue();
        $redis->shouldReceive("del")->once()->with('baz')->andReturnTrue();
        $redis->shouldReceive("flushDB")->once()->andReturnTrue();
        $redis->shouldReceive("set")->once()->with('bar', serialize('foobar'))->andReturnTrue();
        $redis->shouldReceive("sAdd")->once()->with('tag:' . md5('foo'), 'bar')->andReturnTrue();
        $redis->shouldReceive("sMembers")->once()->with('tag:' . md5('foo'))->andReturn(['bar']);
        $redis->shouldReceive("del")->once()->with(['bar'])->andReturnTrue();
        $redis->shouldReceive("del")->once()->with('tag:' . md5('foo'))->andReturnTrue();

        $this->cache->set('foo', 5);
        $this->cache->inc('foo');
        $this->assertEquals(6, $this->cache->get('foo'));
        $this->cache->dec('foo', 2);
        $this->assertEquals(4, $this->cache->get('foo'));

        $this->cache->set('bar', true);
        $this->cache->set('baz', null);
        $this->cache->delete('baz');
        $this->cache->clear();

        //tags
        $this->cache->tag('foo')->set('bar', 'foobar');
        $this->cache->tag('foo')->clear();
    }
}