CacheEventsTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace Illuminate\Tests\Cache;
  3. use Illuminate\Cache\ArrayStore;
  4. use Illuminate\Cache\Events\CacheHit;
  5. use Illuminate\Cache\Events\CacheMissed;
  6. use Illuminate\Cache\Events\KeyForgotten;
  7. use Illuminate\Cache\Events\KeyWritten;
  8. use Illuminate\Cache\Repository;
  9. use Illuminate\Contracts\Cache\Store;
  10. use Illuminate\Events\Dispatcher;
  11. use Mockery as m;
  12. use PHPUnit\Framework\TestCase;
  13. class CacheEventsTest extends TestCase
  14. {
  15. protected function tearDown(): void
  16. {
  17. m::close();
  18. }
  19. public function testHasTriggersEvents()
  20. {
  21. $dispatcher = $this->getDispatcher();
  22. $repository = $this->getRepository($dispatcher);
  23. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo']));
  24. $this->assertFalse($repository->has('foo'));
  25. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheHit::class, ['key' => 'baz', 'value' => 'qux']));
  26. $this->assertTrue($repository->has('baz'));
  27. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo', 'tags' => ['taylor']]));
  28. $this->assertFalse($repository->tags('taylor')->has('foo'));
  29. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheHit::class, ['key' => 'baz', 'value' => 'qux', 'tags' => ['taylor']]));
  30. $this->assertTrue($repository->tags('taylor')->has('baz'));
  31. }
  32. public function testGetTriggersEvents()
  33. {
  34. $dispatcher = $this->getDispatcher();
  35. $repository = $this->getRepository($dispatcher);
  36. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo']));
  37. $this->assertNull($repository->get('foo'));
  38. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheHit::class, ['key' => 'baz', 'value' => 'qux']));
  39. $this->assertSame('qux', $repository->get('baz'));
  40. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo', 'tags' => ['taylor']]));
  41. $this->assertNull($repository->tags('taylor')->get('foo'));
  42. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheHit::class, ['key' => 'baz', 'value' => 'qux', 'tags' => ['taylor']]));
  43. $this->assertSame('qux', $repository->tags('taylor')->get('baz'));
  44. }
  45. public function testPullTriggersEvents()
  46. {
  47. $dispatcher = $this->getDispatcher();
  48. $repository = $this->getRepository($dispatcher);
  49. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheHit::class, ['key' => 'baz', 'value' => 'qux']));
  50. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyForgotten::class, ['key' => 'baz']));
  51. $this->assertSame('qux', $repository->pull('baz'));
  52. }
  53. public function testPullTriggersEventsUsingTags()
  54. {
  55. $dispatcher = $this->getDispatcher();
  56. $repository = $this->getRepository($dispatcher);
  57. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheHit::class, ['key' => 'baz', 'value' => 'qux', 'tags' => ['taylor']]));
  58. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyForgotten::class, ['key' => 'baz', 'tags' => ['taylor']]));
  59. $this->assertSame('qux', $repository->tags('taylor')->pull('baz'));
  60. }
  61. public function testPutTriggersEvents()
  62. {
  63. $dispatcher = $this->getDispatcher();
  64. $repository = $this->getRepository($dispatcher);
  65. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => 99]));
  66. $repository->put('foo', 'bar', 99);
  67. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => 99, 'tags' => ['taylor']]));
  68. $repository->tags('taylor')->put('foo', 'bar', 99);
  69. }
  70. public function testAddTriggersEvents()
  71. {
  72. $dispatcher = $this->getDispatcher();
  73. $repository = $this->getRepository($dispatcher);
  74. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo']));
  75. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => 99]));
  76. $this->assertTrue($repository->add('foo', 'bar', 99));
  77. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo', 'tags' => ['taylor']]));
  78. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => 99, 'tags' => ['taylor']]));
  79. $this->assertTrue($repository->tags('taylor')->add('foo', 'bar', 99));
  80. }
  81. public function testForeverTriggersEvents()
  82. {
  83. $dispatcher = $this->getDispatcher();
  84. $repository = $this->getRepository($dispatcher);
  85. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => null]));
  86. $repository->forever('foo', 'bar');
  87. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => null, 'tags' => ['taylor']]));
  88. $repository->tags('taylor')->forever('foo', 'bar');
  89. }
  90. public function testRememberTriggersEvents()
  91. {
  92. $dispatcher = $this->getDispatcher();
  93. $repository = $this->getRepository($dispatcher);
  94. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo']));
  95. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => 99]));
  96. $this->assertSame('bar', $repository->remember('foo', 99, function () {
  97. return 'bar';
  98. }));
  99. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo', 'tags' => ['taylor']]));
  100. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => 99, 'tags' => ['taylor']]));
  101. $this->assertSame('bar', $repository->tags('taylor')->remember('foo', 99, function () {
  102. return 'bar';
  103. }));
  104. }
  105. public function testRememberForeverTriggersEvents()
  106. {
  107. $dispatcher = $this->getDispatcher();
  108. $repository = $this->getRepository($dispatcher);
  109. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo']));
  110. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => null]));
  111. $this->assertSame('bar', $repository->rememberForever('foo', function () {
  112. return 'bar';
  113. }));
  114. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo', 'tags' => ['taylor']]));
  115. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => null, 'tags' => ['taylor']]));
  116. $this->assertSame('bar', $repository->tags('taylor')->rememberForever('foo', function () {
  117. return 'bar';
  118. }));
  119. }
  120. public function testForgetTriggersEvents()
  121. {
  122. $dispatcher = $this->getDispatcher();
  123. $repository = $this->getRepository($dispatcher);
  124. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyForgotten::class, ['key' => 'baz']));
  125. $this->assertTrue($repository->forget('baz'));
  126. $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyForgotten::class, ['key' => 'baz', 'tags' => ['taylor']]));
  127. $this->assertTrue($repository->tags('taylor')->forget('baz'));
  128. }
  129. public function testForgetDoesNotTriggerEventOnFailure()
  130. {
  131. $dispatcher = $this->getDispatcher();
  132. $store = m::mock(Store::class);
  133. $store->shouldReceive('forget')->andReturn(false);
  134. $repository = new Repository($store);
  135. $repository->setEventDispatcher($dispatcher);
  136. $dispatcher->shouldReceive('dispatch')->never();
  137. $this->assertFalse($repository->forget('baz'));
  138. }
  139. protected function assertEventMatches($eventClass, $properties = [])
  140. {
  141. return m::on(function ($event) use ($eventClass, $properties) {
  142. if (! $event instanceof $eventClass) {
  143. return false;
  144. }
  145. foreach ($properties as $name => $value) {
  146. if ($event->$name != $value) {
  147. return false;
  148. }
  149. }
  150. return true;
  151. });
  152. }
  153. protected function getDispatcher()
  154. {
  155. return m::mock(Dispatcher::class);
  156. }
  157. protected function getRepository($dispatcher)
  158. {
  159. $repository = new Repository(new ArrayStore);
  160. $repository->put('baz', 'qux', 99);
  161. $repository->tags('taylor')->put('baz', 'qux', 99);
  162. $repository->setEventDispatcher($dispatcher);
  163. return $repository;
  164. }
  165. }