CacheArrayStoreTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace Illuminate\Tests\Cache;
  3. use Illuminate\Cache\ArrayStore;
  4. use Illuminate\Support\Carbon;
  5. use PHPUnit\Framework\TestCase;
  6. use stdClass;
  7. class CacheArrayStoreTest extends TestCase
  8. {
  9. public function testItemsCanBeSetAndRetrieved()
  10. {
  11. $store = new ArrayStore;
  12. $result = $store->put('foo', 'bar', 10);
  13. $this->assertTrue($result);
  14. $this->assertSame('bar', $store->get('foo'));
  15. }
  16. public function testMultipleItemsCanBeSetAndRetrieved()
  17. {
  18. $store = new ArrayStore;
  19. $result = $store->put('foo', 'bar', 10);
  20. $resultMany = $store->putMany([
  21. 'fizz' => 'buz',
  22. 'quz' => 'baz',
  23. ], 10);
  24. $this->assertTrue($result);
  25. $this->assertTrue($resultMany);
  26. $this->assertEquals([
  27. 'foo' => 'bar',
  28. 'fizz' => 'buz',
  29. 'quz' => 'baz',
  30. 'norf' => null,
  31. ], $store->many(['foo', 'fizz', 'quz', 'norf']));
  32. }
  33. public function testItemsCanExpire()
  34. {
  35. Carbon::setTestNow(Carbon::now());
  36. $store = new ArrayStore;
  37. $store->put('foo', 'bar', 10);
  38. Carbon::setTestNow(Carbon::now()->addSeconds(10)->addSecond());
  39. $result = $store->get('foo');
  40. $this->assertNull($result);
  41. Carbon::setTestNow(null);
  42. }
  43. public function testStoreItemForeverProperlyStoresInArray()
  44. {
  45. $mock = $this->getMockBuilder(ArrayStore::class)->onlyMethods(['put'])->getMock();
  46. $mock->expects($this->once())
  47. ->method('put')->with($this->equalTo('foo'), $this->equalTo('bar'), $this->equalTo(0))
  48. ->willReturn(true);
  49. $result = $mock->forever('foo', 'bar');
  50. $this->assertTrue($result);
  51. }
  52. public function testValuesCanBeIncremented()
  53. {
  54. $store = new ArrayStore;
  55. $store->put('foo', 1, 10);
  56. $result = $store->increment('foo');
  57. $this->assertEquals(2, $result);
  58. $this->assertEquals(2, $store->get('foo'));
  59. }
  60. public function testNonExistingKeysCanBeIncremented()
  61. {
  62. $store = new ArrayStore;
  63. $result = $store->increment('foo');
  64. $this->assertEquals(1, $result);
  65. $this->assertEquals(1, $store->get('foo'));
  66. }
  67. public function testExpiredKeysAreIncrementedLikeNonExistingKeys()
  68. {
  69. Carbon::setTestNow(Carbon::now());
  70. $store = new ArrayStore;
  71. $store->put('foo', 999, 10);
  72. Carbon::setTestNow(Carbon::now()->addSeconds(10)->addSecond());
  73. $result = $store->increment('foo');
  74. $this->assertEquals(1, $result);
  75. Carbon::setTestNow(null);
  76. }
  77. public function testValuesCanBeDecremented()
  78. {
  79. $store = new ArrayStore;
  80. $store->put('foo', 1, 10);
  81. $result = $store->decrement('foo');
  82. $this->assertEquals(0, $result);
  83. $this->assertEquals(0, $store->get('foo'));
  84. }
  85. public function testItemsCanBeRemoved()
  86. {
  87. $store = new ArrayStore;
  88. $store->put('foo', 'bar', 10);
  89. $this->assertTrue($store->forget('foo'));
  90. $this->assertNull($store->get('foo'));
  91. $this->assertFalse($store->forget('foo'));
  92. }
  93. public function testItemsCanBeFlushed()
  94. {
  95. $store = new ArrayStore;
  96. $store->put('foo', 'bar', 10);
  97. $store->put('baz', 'boom', 10);
  98. $result = $store->flush();
  99. $this->assertTrue($result);
  100. $this->assertNull($store->get('foo'));
  101. $this->assertNull($store->get('baz'));
  102. }
  103. public function testCacheKey()
  104. {
  105. $store = new ArrayStore;
  106. $this->assertEmpty($store->getPrefix());
  107. }
  108. public function testCannotAcquireLockTwice()
  109. {
  110. $store = new ArrayStore;
  111. $lock = $store->lock('foo', 10);
  112. $this->assertTrue($lock->acquire());
  113. $this->assertFalse($lock->acquire());
  114. }
  115. public function testCanAcquireLockAgainAfterExpiry()
  116. {
  117. Carbon::setTestNow(Carbon::now());
  118. $store = new ArrayStore;
  119. $lock = $store->lock('foo', 10);
  120. $lock->acquire();
  121. Carbon::setTestNow(Carbon::now()->addSeconds(10));
  122. $this->assertTrue($lock->acquire());
  123. }
  124. public function testLockExpirationLowerBoundary()
  125. {
  126. Carbon::setTestNow(Carbon::now());
  127. $store = new ArrayStore;
  128. $lock = $store->lock('foo', 10);
  129. $lock->acquire();
  130. Carbon::setTestNow(Carbon::now()->addSeconds(10)->subMicrosecond());
  131. $this->assertFalse($lock->acquire());
  132. }
  133. public function testLockWithNoExpirationNeverExpires()
  134. {
  135. Carbon::setTestNow(Carbon::now());
  136. $store = new ArrayStore;
  137. $lock = $store->lock('foo');
  138. $lock->acquire();
  139. Carbon::setTestNow(Carbon::now()->addYears(100));
  140. $this->assertFalse($lock->acquire());
  141. }
  142. public function testCanAcquireLockAfterRelease()
  143. {
  144. $store = new ArrayStore;
  145. $lock = $store->lock('foo', 10);
  146. $lock->acquire();
  147. $this->assertTrue($lock->release());
  148. $this->assertTrue($lock->acquire());
  149. }
  150. public function testAnotherOwnerCannotReleaseLock()
  151. {
  152. $store = new ArrayStore;
  153. $owner = $store->lock('foo', 10);
  154. $wannabeOwner = $store->lock('foo', 10);
  155. $owner->acquire();
  156. $this->assertFalse($wannabeOwner->release());
  157. }
  158. public function testAnotherOwnerCanForceReleaseALock()
  159. {
  160. $store = new ArrayStore;
  161. $owner = $store->lock('foo', 10);
  162. $wannabeOwner = $store->lock('foo', 10);
  163. $owner->acquire();
  164. $wannabeOwner->forceRelease();
  165. $this->assertTrue($wannabeOwner->acquire());
  166. }
  167. public function testValuesAreNotStoredByReference()
  168. {
  169. $store = new ArrayStore($serialize = true);
  170. $object = new stdClass;
  171. $object->foo = true;
  172. $store->put('object', $object, 10);
  173. $object->bar = true;
  174. $this->assertObjectNotHasAttribute('bar', $store->get('object'));
  175. }
  176. public function testValuesAreStoredByReferenceIfSerializationIsDisabled()
  177. {
  178. $store = new ArrayStore;
  179. $object = new stdClass;
  180. $object->foo = true;
  181. $store->put('object', $object, 10);
  182. $object->bar = true;
  183. $this->assertObjectHasAttribute('bar', $store->get('object'));
  184. }
  185. public function testReleasingLockAfterAlreadyForceReleasedByAnotherOwnerFails()
  186. {
  187. $store = new ArrayStore;
  188. $owner = $store->lock('foo', 10);
  189. $wannabeOwner = $store->lock('foo', 10);
  190. $owner->acquire();
  191. $wannabeOwner->forceRelease();
  192. $this->assertFalse($wannabeOwner->release());
  193. }
  194. }