Skip to content

Commit

Permalink
Merge pull request #40 from duepi/master
Browse files Browse the repository at this point in the history
Added MvcEvent to EVENT_LOAD event handler
  • Loading branch information
bramstroker committed Mar 27, 2017
2 parents d80ad3e + cf81cec commit 103a865
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,3 +5,4 @@ vendor
composer.lock
composer.phar
build
.idea
2 changes: 1 addition & 1 deletion src/StrokerCache/Listener/CacheListener.php
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/StrokerCache/Service/CacheService.php
Expand Up @@ -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) {
Expand Down
21 changes: 17 additions & 4 deletions tests/StrokerCacheTest/Service/CacheServiceTest.php
Expand Up @@ -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
Expand All @@ -72,28 +76,34 @@ 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')
->andReturn(true);

$this->cacheService->getEventManager()->attach(CacheEvent::EVENT_LOAD, function () { return false; });

$this->assertNull($this->cacheService->load());
$this->assertNull($this->cacheService->load($mvcEvent));
}

public function testSaveResponseIsNotCached()
Expand Down Expand Up @@ -218,6 +228,9 @@ public function testSaveEventIsTriggered()

public function testLoadEventIsTriggered()
{
// Setup
$mvcEvent = new MvcEvent();

$this->idGeneratorMock->shouldReceive('generate')->andReturn('foo-bar');

$self = $this;
Expand All @@ -230,7 +243,7 @@ public function testLoadEventIsTriggered()
->shouldReceive('hasItem')
->andReturn(true);

$this->cacheService->load();
$this->cacheService->load($mvcEvent);
}

public function testSettersProvideFluentInterface()
Expand Down

0 comments on commit 103a865

Please sign in to comment.