diff --git a/.gitignore b/.gitignore index 51378cb..460eb97 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ vendor composer.lock composer.phar build +.idea \ No newline at end of file diff --git a/src/StrokerCache/Listener/CacheListener.php b/src/StrokerCache/Listener/CacheListener.php index 5769dd3..19dd9b8 100644 --- a/src/StrokerCache/Listener/CacheListener.php +++ b/src/StrokerCache/Listener/CacheListener.php @@ -69,7 +69,7 @@ public function onRoute(MvcEvent $e) return; } - $data = $this->getCacheService()->load(); + $data = $this->getCacheService()->load($e); if ($data !== null) { $this->loadedFromCache = true; diff --git a/src/StrokerCache/Service/CacheService.php b/src/StrokerCache/Service/CacheService.php index e4bc8c3..63d8dae 100644 --- a/src/StrokerCache/Service/CacheService.php +++ b/src/StrokerCache/Service/CacheService.php @@ -62,14 +62,14 @@ public function __construct(StorageInterface $cacheStorage, ModuleOptions $optio /** * Check if a page is saved in the cache and return contents. Return null when no item is found. */ - public function load() + public function load(MvcEvent $mvcEvent) { $id = $this->getIdGenerator()->generate(); if (!$this->getCacheStorage()->hasItem($id)) { return null; }; - $event = $this->createCacheEvent(CacheEvent::EVENT_LOAD); + $event = $this->createCacheEvent(CacheEvent::EVENT_LOAD, $mvcEvent); $event->setCacheKey($id); $results = $this->getEventManager()->trigger($event, function ($result) { diff --git a/tests/StrokerCacheTest/Service/CacheServiceTest.php b/tests/StrokerCacheTest/Service/CacheServiceTest.php index 19c873e..0024130 100644 --- a/tests/StrokerCacheTest/Service/CacheServiceTest.php +++ b/tests/StrokerCacheTest/Service/CacheServiceTest.php @@ -62,6 +62,10 @@ public function tearDown() public function testLoadPageFromCache() { + // Setup + $mvcEvent = new MvcEvent(); + + // Expectations $expectedContent = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; $this->storageMock @@ -72,20 +76,26 @@ public function testLoadPageFromCache() ->with('/foo/bar') ->andReturn($expectedContent); - $content = $this->cacheService->load(); + $content = $this->cacheService->load($mvcEvent); $this->assertEquals($expectedContent, $content); } public function testLoadReturnsNullWhenPageIsNotInCache() { + // Setup + $mvcEvent = new MvcEvent(); + $this->storageMock ->shouldReceive('hasItem') ->andReturn(false); - $this->assertNull($this->cacheService->load()); + $this->assertNull($this->cacheService->load($mvcEvent)); } public function testCancelLoadingUsingLoadEvent() { + // Setup + $mvcEvent = new MvcEvent(); + $this->storageMock ->shouldReceive('hasItem') ->with('/foo/bar') @@ -93,7 +103,7 @@ public function testCancelLoadingUsingLoadEvent() $this->cacheService->getEventManager()->attach(CacheEvent::EVENT_LOAD, function () { return false; }); - $this->assertNull($this->cacheService->load()); + $this->assertNull($this->cacheService->load($mvcEvent)); } public function testSaveResponseIsNotCached() @@ -218,6 +228,9 @@ public function testSaveEventIsTriggered() public function testLoadEventIsTriggered() { + // Setup + $mvcEvent = new MvcEvent(); + $this->idGeneratorMock->shouldReceive('generate')->andReturn('foo-bar'); $self = $this; @@ -230,7 +243,7 @@ public function testLoadEventIsTriggered() ->shouldReceive('hasItem') ->andReturn(true); - $this->cacheService->load(); + $this->cacheService->load($mvcEvent); } public function testSettersProvideFluentInterface()