CacheMemcachedStoreTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace Illuminate\Tests\Cache;
  3. use Illuminate\Cache\MemcachedStore;
  4. use Illuminate\Support\Carbon;
  5. use Memcached;
  6. use Mockery as m;
  7. use PHPUnit\Framework\TestCase;
  8. use stdClass;
  9. /**
  10. * @requires extension memcached
  11. */
  12. class CacheMemcachedStoreTest extends TestCase
  13. {
  14. protected function tearDown(): void
  15. {
  16. m::close();
  17. parent::tearDown();
  18. }
  19. public function testGetReturnsNullWhenNotFound()
  20. {
  21. $memcache = $this->getMockBuilder(stdClass::class)->addMethods(['get', 'getResultCode'])->getMock();
  22. $memcache->expects($this->once())->method('get')->with($this->equalTo('foo:bar'))->willReturn(null);
  23. $memcache->expects($this->once())->method('getResultCode')->willReturn(1);
  24. $store = new MemcachedStore($memcache, 'foo');
  25. $this->assertNull($store->get('bar'));
  26. }
  27. public function testMemcacheValueIsReturned()
  28. {
  29. $memcache = $this->getMockBuilder(stdClass::class)->addMethods(['get', 'getResultCode'])->getMock();
  30. $memcache->expects($this->once())->method('get')->willReturn('bar');
  31. $memcache->expects($this->once())->method('getResultCode')->willReturn(0);
  32. $store = new MemcachedStore($memcache);
  33. $this->assertSame('bar', $store->get('foo'));
  34. }
  35. public function testMemcacheGetMultiValuesAreReturnedWithCorrectKeys()
  36. {
  37. $memcache = $this->getMockBuilder(stdClass::class)->addMethods(['getMulti', 'getResultCode'])->getMock();
  38. $memcache->expects($this->once())->method('getMulti')->with(
  39. ['foo:foo', 'foo:bar', 'foo:baz']
  40. )->willReturn([
  41. 'fizz', 'buzz', 'norf',
  42. ]);
  43. $memcache->expects($this->once())->method('getResultCode')->willReturn(0);
  44. $store = new MemcachedStore($memcache, 'foo');
  45. $this->assertEquals([
  46. 'foo' => 'fizz',
  47. 'bar' => 'buzz',
  48. 'baz' => 'norf',
  49. ], $store->many([
  50. 'foo', 'bar', 'baz',
  51. ]));
  52. }
  53. public function testSetMethodProperlyCallsMemcache()
  54. {
  55. Carbon::setTestNow($now = Carbon::now());
  56. $memcache = $this->getMockBuilder(Memcached::class)->onlyMethods(['set'])->getMock();
  57. $memcache->expects($this->once())->method('set')->with($this->equalTo('foo'), $this->equalTo('bar'), $this->equalTo($now->timestamp + 60))->willReturn(true);
  58. $store = new MemcachedStore($memcache);
  59. $result = $store->put('foo', 'bar', 60);
  60. $this->assertTrue($result);
  61. Carbon::setTestNow();
  62. }
  63. public function testIncrementMethodProperlyCallsMemcache()
  64. {
  65. /* @link https://github.com/php-memcached-dev/php-memcached/pull/468 */
  66. if (version_compare(phpversion(), '8.0.0', '>=')) {
  67. $this->markTestSkipped('Test broken due to parse error in PHP Memcached.');
  68. }
  69. $memcached = m::mock(Memcached::class);
  70. $memcached->shouldReceive('increment')->with('foo', 5)->once()->andReturn(5);
  71. $store = new MemcachedStore($memcached);
  72. $store->increment('foo', 5);
  73. }
  74. public function testDecrementMethodProperlyCallsMemcache()
  75. {
  76. /* @link https://github.com/php-memcached-dev/php-memcached/pull/468 */
  77. if (version_compare(phpversion(), '8.0.0', '>=')) {
  78. $this->markTestSkipped('Test broken due to parse error in PHP Memcached.');
  79. }
  80. $memcached = m::mock(Memcached::class);
  81. $memcached->shouldReceive('decrement')->with('foo', 5)->once()->andReturn(0);
  82. $store = new MemcachedStore($memcached);
  83. $store->decrement('foo', 5);
  84. }
  85. public function testStoreItemForeverProperlyCallsMemcached()
  86. {
  87. $memcache = $this->getMockBuilder(Memcached::class)->onlyMethods(['set'])->getMock();
  88. $memcache->expects($this->once())->method('set')->with($this->equalTo('foo'), $this->equalTo('bar'), $this->equalTo(0))->willReturn(true);
  89. $store = new MemcachedStore($memcache);
  90. $result = $store->forever('foo', 'bar');
  91. $this->assertTrue($result);
  92. }
  93. public function testForgetMethodProperlyCallsMemcache()
  94. {
  95. $memcache = $this->getMockBuilder(Memcached::class)->onlyMethods(['delete'])->getMock();
  96. $memcache->expects($this->once())->method('delete')->with($this->equalTo('foo'));
  97. $store = new MemcachedStore($memcache);
  98. $store->forget('foo');
  99. }
  100. public function testFlushesCached()
  101. {
  102. $memcache = $this->getMockBuilder(Memcached::class)->onlyMethods(['flush'])->getMock();
  103. $memcache->expects($this->once())->method('flush')->willReturn(true);
  104. $store = new MemcachedStore($memcache);
  105. $result = $store->flush();
  106. $this->assertTrue($result);
  107. }
  108. public function testGetAndSetPrefix()
  109. {
  110. $store = new MemcachedStore(new Memcached, 'bar');
  111. $this->assertSame('bar:', $store->getPrefix());
  112. $store->setPrefix('foo');
  113. $this->assertSame('foo:', $store->getPrefix());
  114. $store->setPrefix(null);
  115. $this->assertEmpty($store->getPrefix());
  116. }
  117. }