CacheApcStoreTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Illuminate\Tests\Cache;
  3. use Illuminate\Cache\ApcStore;
  4. use Illuminate\Cache\ApcWrapper;
  5. use PHPUnit\Framework\TestCase;
  6. class CacheApcStoreTest extends TestCase
  7. {
  8. public function testGetReturnsNullWhenNotFound()
  9. {
  10. $apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['get'])->getMock();
  11. $apc->expects($this->once())->method('get')->with($this->equalTo('foobar'))->willReturn(null);
  12. $store = new ApcStore($apc, 'foo');
  13. $this->assertNull($store->get('bar'));
  14. }
  15. public function testAPCValueIsReturned()
  16. {
  17. $apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['get'])->getMock();
  18. $apc->expects($this->once())->method('get')->willReturn('bar');
  19. $store = new ApcStore($apc);
  20. $this->assertSame('bar', $store->get('foo'));
  21. }
  22. public function testGetMultipleReturnsNullWhenNotFoundAndValueWhenFound()
  23. {
  24. $apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['get'])->getMock();
  25. $apc->expects($this->exactly(3))->method('get')->willReturnMap([
  26. ['foo', 'qux'],
  27. ['bar', null],
  28. ['baz', 'norf'],
  29. ]);
  30. $store = new ApcStore($apc);
  31. $this->assertEquals([
  32. 'foo' => 'qux',
  33. 'bar' => null,
  34. 'baz' => 'norf',
  35. ], $store->many(['foo', 'bar', 'baz']));
  36. }
  37. public function testSetMethodProperlyCallsAPC()
  38. {
  39. $apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['put'])->getMock();
  40. $apc->expects($this->once())
  41. ->method('put')->with($this->equalTo('foo'), $this->equalTo('bar'), $this->equalTo(60))
  42. ->willReturn(true);
  43. $store = new ApcStore($apc);
  44. $result = $store->put('foo', 'bar', 60);
  45. $this->assertTrue($result);
  46. }
  47. public function testSetMultipleMethodProperlyCallsAPC()
  48. {
  49. $apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['put'])->getMock();
  50. $apc->expects($this->exactly(3))->method('put')->withConsecutive([
  51. $this->equalTo('foo'), $this->equalTo('bar'), $this->equalTo(60),
  52. ], [
  53. $this->equalTo('baz'), $this->equalTo('qux'), $this->equalTo(60),
  54. ], [
  55. $this->equalTo('bar'), $this->equalTo('norf'), $this->equalTo(60),
  56. ])->willReturn(true);
  57. $store = new ApcStore($apc);
  58. $result = $store->putMany([
  59. 'foo' => 'bar',
  60. 'baz' => 'qux',
  61. 'bar' => 'norf',
  62. ], 60);
  63. $this->assertTrue($result);
  64. }
  65. public function testIncrementMethodProperlyCallsAPC()
  66. {
  67. $apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['increment'])->getMock();
  68. $apc->expects($this->once())->method('increment')->with($this->equalTo('foo'), $this->equalTo(5));
  69. $store = new ApcStore($apc);
  70. $store->increment('foo', 5);
  71. }
  72. public function testDecrementMethodProperlyCallsAPC()
  73. {
  74. $apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['decrement'])->getMock();
  75. $apc->expects($this->once())->method('decrement')->with($this->equalTo('foo'), $this->equalTo(5));
  76. $store = new ApcStore($apc);
  77. $store->decrement('foo', 5);
  78. }
  79. public function testStoreItemForeverProperlyCallsAPC()
  80. {
  81. $apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['put'])->getMock();
  82. $apc->expects($this->once())
  83. ->method('put')->with($this->equalTo('foo'), $this->equalTo('bar'), $this->equalTo(0))
  84. ->willReturn(true);
  85. $store = new ApcStore($apc);
  86. $result = $store->forever('foo', 'bar');
  87. $this->assertTrue($result);
  88. }
  89. public function testForgetMethodProperlyCallsAPC()
  90. {
  91. $apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['delete'])->getMock();
  92. $apc->expects($this->once())->method('delete')->with($this->equalTo('foo'))->willReturn(true);
  93. $store = new ApcStore($apc);
  94. $result = $store->forget('foo');
  95. $this->assertTrue($result);
  96. }
  97. public function testFlushesCached()
  98. {
  99. $apc = $this->getMockBuilder(ApcWrapper::class)->onlyMethods(['flush'])->getMock();
  100. $apc->expects($this->once())->method('flush')->willReturn(true);
  101. $store = new ApcStore($apc);
  102. $result = $store->flush();
  103. $this->assertTrue($result);
  104. }
  105. }