CacheTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace think\tests;
  3. use InvalidArgumentException;
  4. use Mockery as m;
  5. use Mockery\MockInterface;
  6. use org\bovigo\vfs\vfsStream;
  7. use PHPUnit\Framework\TestCase;
  8. use think\App;
  9. use think\Cache;
  10. use think\Config;
  11. use think\Container;
  12. class CacheTest extends TestCase
  13. {
  14. /** @var App|MockInterface */
  15. protected $app;
  16. /** @var Cache|MockInterface */
  17. protected $cache;
  18. /** @var Config|MockInterface */
  19. protected $config;
  20. protected function tearDown(): void
  21. {
  22. m::close();
  23. }
  24. protected function setUp()
  25. {
  26. $this->app = m::mock(App::class)->makePartial();
  27. Container::setInstance($this->app);
  28. $this->app->shouldReceive('make')->with(App::class)->andReturn($this->app);
  29. $this->config = m::mock(Config::class)->makePartial();
  30. $this->app->shouldReceive('get')->with('config')->andReturn($this->config);
  31. $this->cache = new Cache($this->app);
  32. }
  33. public function testGetConfig()
  34. {
  35. $config = [
  36. 'default' => 'file',
  37. ];
  38. $this->config->shouldReceive('get')->with('cache')->andReturn($config);
  39. $this->assertEquals($config, $this->cache->getConfig());
  40. $this->expectException(InvalidArgumentException::class);
  41. $this->cache->getStoreConfig('foo');
  42. }
  43. public function testCacheManagerInstances()
  44. {
  45. $this->config->shouldReceive('get')->with("cache.stores.single", null)->andReturn(['type' => 'file']);
  46. $channel1 = $this->cache->store('single');
  47. $channel2 = $this->cache->store('single');
  48. $this->assertSame($channel1, $channel2);
  49. }
  50. public function testFileCache()
  51. {
  52. $root = vfsStream::setup();
  53. $this->config->shouldReceive('get')->with("cache.default", null)->andReturn('file');
  54. $this->config->shouldReceive('get')->with("cache.stores.file", null)->andReturn(['type' => 'file', 'path' => $root->url()]);
  55. $this->cache->set('foo', 5);
  56. $this->cache->inc('foo');
  57. $this->assertEquals(6, $this->cache->get('foo'));
  58. $this->cache->dec('foo', 2);
  59. $this->assertEquals(4, $this->cache->get('foo'));
  60. $this->cache->set('bar', true);
  61. $this->assertTrue($this->cache->get('bar'));
  62. $this->cache->set('baz', null);
  63. $this->assertNull($this->cache->get('baz'));
  64. $this->assertTrue($this->cache->has('baz'));
  65. $this->cache->delete('baz');
  66. $this->assertFalse($this->cache->has('baz'));
  67. $this->assertNull($this->cache->get('baz'));
  68. $this->assertFalse($this->cache->get('baz', false));
  69. $this->assertTrue($root->hasChildren());
  70. $this->cache->clear();
  71. $this->assertFalse($root->hasChildren());
  72. //tags
  73. $this->cache->tag('foo')->set('bar', 'foobar');
  74. $this->assertEquals('foobar', $this->cache->get('bar'));
  75. $this->cache->tag('foo')->clear();
  76. $this->assertFalse($this->cache->has('bar'));
  77. //multiple
  78. $this->cache->setMultiple(['foo' => ['foobar', 'bar'], 'foobar' => ['foo', 'bar']]);
  79. $this->assertEquals(['foo' => ['foobar', 'bar'], 'foobar' => ['foo', 'bar']], $this->cache->getMultiple(['foo', 'foobar']));
  80. $this->assertTrue($this->cache->deleteMultiple(['foo', 'foobar']));
  81. }
  82. public function testRedisCache()
  83. {
  84. if (extension_loaded('redis')) {
  85. return;
  86. }
  87. $this->config->shouldReceive('get')->with("cache.default", null)->andReturn('redis');
  88. $this->config->shouldReceive('get')->with("cache.stores.redis", null)->andReturn(['type' => 'redis']);
  89. $redis = m::mock('overload:\Predis\Client');
  90. $redis->shouldReceive("set")->once()->with('foo', 5)->andReturnTrue();
  91. $redis->shouldReceive("incrby")->once()->with('foo', 1)->andReturnTrue();
  92. $redis->shouldReceive("decrby")->once()->with('foo', 2)->andReturnTrue();
  93. $redis->shouldReceive("get")->once()->with('foo')->andReturn(6);
  94. $redis->shouldReceive("get")->once()->with('foo')->andReturn(4);
  95. $redis->shouldReceive("set")->once()->with('bar', \Opis\Closure\serialize(true))->andReturnTrue();
  96. $redis->shouldReceive("set")->once()->with('baz', \Opis\Closure\serialize(null))->andReturnTrue();
  97. $redis->shouldReceive("del")->once()->with('baz')->andReturnTrue();
  98. $redis->shouldReceive("flushDB")->once()->andReturnTrue();
  99. $redis->shouldReceive("set")->once()->with('bar', \Opis\Closure\serialize('foobar'))->andReturnTrue();
  100. $redis->shouldReceive("sAdd")->once()->with('tag:' . md5('foo'), 'bar')->andReturnTrue();
  101. $redis->shouldReceive("sMembers")->once()->with('tag:' . md5('foo'))->andReturn(['bar']);
  102. $redis->shouldReceive("del")->once()->with(['bar'])->andReturnTrue();
  103. $redis->shouldReceive("del")->once()->with('tag:' . md5('foo'))->andReturnTrue();
  104. $this->cache->set('foo', 5);
  105. $this->cache->inc('foo');
  106. $this->assertEquals(6, $this->cache->get('foo'));
  107. $this->cache->dec('foo', 2);
  108. $this->assertEquals(4, $this->cache->get('foo'));
  109. $this->cache->set('bar', true);
  110. $this->cache->set('baz', null);
  111. $this->cache->delete('baz');
  112. $this->cache->clear();
  113. //tags
  114. $this->cache->tag('foo')->set('bar', 'foobar');
  115. $this->cache->tag('foo')->clear();
  116. }
  117. }