123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- namespace Illuminate\Tests\Cache;
- use Illuminate\Cache\ArrayStore;
- use Illuminate\Cache\Events\CacheHit;
- use Illuminate\Cache\Events\CacheMissed;
- use Illuminate\Cache\Events\KeyForgotten;
- use Illuminate\Cache\Events\KeyWritten;
- use Illuminate\Cache\Repository;
- use Illuminate\Contracts\Cache\Store;
- use Illuminate\Events\Dispatcher;
- use Mockery as m;
- use PHPUnit\Framework\TestCase;
- class CacheEventsTest extends TestCase
- {
- protected function tearDown(): void
- {
- m::close();
- }
- public function testHasTriggersEvents()
- {
- $dispatcher = $this->getDispatcher();
- $repository = $this->getRepository($dispatcher);
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo']));
- $this->assertFalse($repository->has('foo'));
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheHit::class, ['key' => 'baz', 'value' => 'qux']));
- $this->assertTrue($repository->has('baz'));
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo', 'tags' => ['taylor']]));
- $this->assertFalse($repository->tags('taylor')->has('foo'));
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheHit::class, ['key' => 'baz', 'value' => 'qux', 'tags' => ['taylor']]));
- $this->assertTrue($repository->tags('taylor')->has('baz'));
- }
- public function testGetTriggersEvents()
- {
- $dispatcher = $this->getDispatcher();
- $repository = $this->getRepository($dispatcher);
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo']));
- $this->assertNull($repository->get('foo'));
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheHit::class, ['key' => 'baz', 'value' => 'qux']));
- $this->assertSame('qux', $repository->get('baz'));
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo', 'tags' => ['taylor']]));
- $this->assertNull($repository->tags('taylor')->get('foo'));
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheHit::class, ['key' => 'baz', 'value' => 'qux', 'tags' => ['taylor']]));
- $this->assertSame('qux', $repository->tags('taylor')->get('baz'));
- }
- public function testPullTriggersEvents()
- {
- $dispatcher = $this->getDispatcher();
- $repository = $this->getRepository($dispatcher);
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheHit::class, ['key' => 'baz', 'value' => 'qux']));
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyForgotten::class, ['key' => 'baz']));
- $this->assertSame('qux', $repository->pull('baz'));
- }
- public function testPullTriggersEventsUsingTags()
- {
- $dispatcher = $this->getDispatcher();
- $repository = $this->getRepository($dispatcher);
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheHit::class, ['key' => 'baz', 'value' => 'qux', 'tags' => ['taylor']]));
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyForgotten::class, ['key' => 'baz', 'tags' => ['taylor']]));
- $this->assertSame('qux', $repository->tags('taylor')->pull('baz'));
- }
- public function testPutTriggersEvents()
- {
- $dispatcher = $this->getDispatcher();
- $repository = $this->getRepository($dispatcher);
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => 99]));
- $repository->put('foo', 'bar', 99);
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => 99, 'tags' => ['taylor']]));
- $repository->tags('taylor')->put('foo', 'bar', 99);
- }
- public function testAddTriggersEvents()
- {
- $dispatcher = $this->getDispatcher();
- $repository = $this->getRepository($dispatcher);
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo']));
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => 99]));
- $this->assertTrue($repository->add('foo', 'bar', 99));
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo', 'tags' => ['taylor']]));
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => 99, 'tags' => ['taylor']]));
- $this->assertTrue($repository->tags('taylor')->add('foo', 'bar', 99));
- }
- public function testForeverTriggersEvents()
- {
- $dispatcher = $this->getDispatcher();
- $repository = $this->getRepository($dispatcher);
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => null]));
- $repository->forever('foo', 'bar');
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => null, 'tags' => ['taylor']]));
- $repository->tags('taylor')->forever('foo', 'bar');
- }
- public function testRememberTriggersEvents()
- {
- $dispatcher = $this->getDispatcher();
- $repository = $this->getRepository($dispatcher);
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo']));
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => 99]));
- $this->assertSame('bar', $repository->remember('foo', 99, function () {
- return 'bar';
- }));
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo', 'tags' => ['taylor']]));
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => 99, 'tags' => ['taylor']]));
- $this->assertSame('bar', $repository->tags('taylor')->remember('foo', 99, function () {
- return 'bar';
- }));
- }
- public function testRememberForeverTriggersEvents()
- {
- $dispatcher = $this->getDispatcher();
- $repository = $this->getRepository($dispatcher);
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo']));
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => null]));
- $this->assertSame('bar', $repository->rememberForever('foo', function () {
- return 'bar';
- }));
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(CacheMissed::class, ['key' => 'foo', 'tags' => ['taylor']]));
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyWritten::class, ['key' => 'foo', 'value' => 'bar', 'seconds' => null, 'tags' => ['taylor']]));
- $this->assertSame('bar', $repository->tags('taylor')->rememberForever('foo', function () {
- return 'bar';
- }));
- }
- public function testForgetTriggersEvents()
- {
- $dispatcher = $this->getDispatcher();
- $repository = $this->getRepository($dispatcher);
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyForgotten::class, ['key' => 'baz']));
- $this->assertTrue($repository->forget('baz'));
- $dispatcher->shouldReceive('dispatch')->once()->with($this->assertEventMatches(KeyForgotten::class, ['key' => 'baz', 'tags' => ['taylor']]));
- $this->assertTrue($repository->tags('taylor')->forget('baz'));
- }
- public function testForgetDoesNotTriggerEventOnFailure()
- {
- $dispatcher = $this->getDispatcher();
- $store = m::mock(Store::class);
- $store->shouldReceive('forget')->andReturn(false);
- $repository = new Repository($store);
- $repository->setEventDispatcher($dispatcher);
- $dispatcher->shouldReceive('dispatch')->never();
- $this->assertFalse($repository->forget('baz'));
- }
- protected function assertEventMatches($eventClass, $properties = [])
- {
- return m::on(function ($event) use ($eventClass, $properties) {
- if (! $event instanceof $eventClass) {
- return false;
- }
- foreach ($properties as $name => $value) {
- if ($event->$name != $value) {
- return false;
- }
- }
- return true;
- });
- }
- protected function getDispatcher()
- {
- return m::mock(Dispatcher::class);
- }
- protected function getRepository($dispatcher)
- {
- $repository = new Repository(new ArrayStore);
- $repository->put('baz', 'qux', 99);
- $repository->tags('taylor')->put('baz', 'qux', 99);
- $repository->setEventDispatcher($dispatcher);
- return $repository;
- }
- }
|