Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #3 from AmaraLiving/finish-tests
Browse files Browse the repository at this point in the history
Finish unit tests
  • Loading branch information
rossmotley committed Jan 16, 2017
2 parents 0a82b73 + 93717d7 commit 82e5998
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions tests/unit/SymfonyTaggedCacheStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public function testFetchBlockReturnsFalseFromCacheItemWhenNotAHit()
$cacheContent = 'My cached content';
$cacheCreateFunction = $this->createCacheCreateFunction();

$cacheItemMock = $cacheCreateFunction('key', $cacheContent, false);
$cacheItem = $cacheCreateFunction('key', $cacheContent, false);

$this->adapterMock->getItem('key')->willReturn($cacheItemMock);
$this->adapterMock->getItem('key')->willReturn($cacheItem);

$actual = $this->symfonyTaggedCacheStrategy->fetchBlock($key);

Expand Down Expand Up @@ -120,6 +120,55 @@ public function testGenerateKeyLooksCorrectWithPassedValues()
$this->assertEquals('__SF2__annotation', $keyModel->key);
}

public function testSaveBlockThrowsExceptionWhenNonKeyModelPassed()
{
$this->setExpectedException(InvalidValueException::class);

$this->symfonyTaggedCacheStrategy->saveBlock('not a key model', 'Content to cache');
}

public function testSaveBlockWithPsrCacheItemInterface()
{
$block = 'Content to cache';
$lifetime = 23;

$keyModel = new KeyModel('key', $lifetime);

$cacheItemPsr = $this->prophesize(\Psr\Cache\CacheItemInterface::class);
$cacheItemPsr->set($block)->shouldBeCalled();
$cacheItemPsr->expiresAfter($lifetime)->shouldBeCalled();

$this->adapterMock->getItem('key')->willReturn($cacheItemPsr);

$this->adapterMock->save($cacheItemPsr)->shouldBeCalled();

$this->symfonyTaggedCacheStrategy->saveBlock($keyModel, $block);
}

public function testSaveBlockWithCacheItem()
{
$block = 'Content to cache';
$lifetime = 23;
$cacheCreateFunction = $this->createCacheCreateFunction();

// CacheItem is final, so we cannot prophesize it
$cacheItem = $cacheCreateFunction('key', '', false);

$keyModel = new KeyModel('key', $lifetime, ['tag1', 'tag2']);

$this->adapterMock->getItem('key')->willReturn($cacheItem);
$this->adapterMock->save($cacheItem)->shouldBeCalled();

$this->symfonyTaggedCacheStrategy->saveBlock($keyModel, $block);

// We just check the tags, we assume that the others are okay from the PSR cache item test
PHPUnit_Framework_Assert::assertAttributeEquals(
['tag1' => 'tag1', 'tag2' => 'tag2'],
'tags',
$cacheItem
);
}

/**
* @return Closure
*/
Expand Down

0 comments on commit 82e5998

Please sign in to comment.