diff --git a/Strategy/ProfilerStrategy.php b/Strategy/ProfilerStrategy.php index b2e5390..8d98361 100644 --- a/Strategy/ProfilerStrategy.php +++ b/Strategy/ProfilerStrategy.php @@ -35,7 +35,7 @@ public function __construct(CacheStrategyInterface $cacheStrategy, ProfilerExten * * @param mixed $key * - * @return string + * @return mixed */ public function fetchBlock($key) { @@ -65,6 +65,8 @@ public function generateKey($annotation, $value) * * @param mixed $key * @param string $block + * + * @return mixed */ public function saveBlock($key, $block) { diff --git a/Tests/AppKernel.php b/Tests/AppKernel.php deleted file mode 100644 index 8f387f6..0000000 --- a/Tests/AppKernel.php +++ /dev/null @@ -1,31 +0,0 @@ -getEnvironment().'.yml'; - - if (file_exists($config)) { - $loader->load($config); - - return; - } - - $loader->load(__DIR__.'/Resources/config/config.yml'); - } -} diff --git a/Tests/DependencyInjection/TwigCacheExtensionTest.php b/Tests/DependencyInjection/TwigCacheExtensionTest.php index 084a8dc..2ca074b 100644 --- a/Tests/DependencyInjection/TwigCacheExtensionTest.php +++ b/Tests/DependencyInjection/TwigCacheExtensionTest.php @@ -2,54 +2,58 @@ namespace EmanueleMinotto\TwigCacheBundle\Tests\DependencyInjection; -use EmanueleMinotto\TwigCacheBundle\Tests\AppKernel; -use PHPUnit_Framework_TestCase; +use EmanueleMinotto\TwigCacheBundle\DependencyInjection\TwigCacheExtension; +use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; +use Symfony\Component\DependencyInjection\Definition; -/** - * @coversDefaultClass \EmanueleMinotto\TwigCacheBundle\DependencyInjection\TwigCacheExtension - */ -class TwigCacheExtensionTest extends PHPUnit_Framework_TestCase +class TwigCacheExtensionTest extends AbstractExtensionTestCase { /** - * @var \Symfony\Component\HttpKernel\Kernel + * Return an array of container extensions you need to be registered for each test (usually just the container + * extension you are testing. + * + * @return ExtensionInterface[] */ - private $kernel; + protected function getContainerExtensions() + { + return [ + new TwigCacheExtension(), + ]; + } - /** - * {@inheritdoc} - */ protected function setUp() { - $this->kernel = new AppKernel('TwigCacheExtensionTest', true); - $this->kernel->boot(); + parent::setUp(); + + $serviceId = sha1(rand()); + + $this->setDefinition($serviceId, new Definition( + 'Doctrine\Common\Cache\ArrayCache' + )); + $this->load([ + 'service' => $serviceId, + ]); } /** - * @covers ::load + * Test services. */ public function testService() { - $container = $this->kernel->getContainer(); - - $this->assertTrue($container->has('twig_cache.extension')); - $this->assertInstanceOf('Twig_Extension', $container->get('twig_cache.extension')); - - $this->assertTrue($container->has('twig_cache.service')); - $this->assertInstanceOf('Doctrine\\Common\\Cache\\Cache', $container->get('twig_cache.service')); + $this->assertContainerBuilderHasService('twig_cache.extension'); + $this->assertContainerBuilderHasService('twig_cache.service'); } /** - * @covers ::load + * Test parameters. */ public function testParameter() { - $container = $this->kernel->getContainer(); - - $this->assertTrue($container->hasParameter('twig_cache.adapter.class')); - $this->assertTrue($container->hasParameter('twig_cache.extension.class')); - $this->assertTrue($container->hasParameter('twig_cache.strategy.class')); - $this->assertTrue($container->hasParameter('twig_cache.strategy.generational.class')); - $this->assertTrue($container->hasParameter('twig_cache.strategy.lifetime.class')); - $this->assertTrue($container->hasParameter('twig_cache.strategy.spl_object_hash_key_generator.class')); + $this->assertContainerBuilderHasParameter('twig_cache.adapter.class'); + $this->assertContainerBuilderHasParameter('twig_cache.extension.class'); + $this->assertContainerBuilderHasParameter('twig_cache.strategy.class'); + $this->assertContainerBuilderHasParameter('twig_cache.strategy.generational.class'); + $this->assertContainerBuilderHasParameter('twig_cache.strategy.lifetime.class'); + $this->assertContainerBuilderHasParameter('twig_cache.strategy.spl_object_hash_key_generator.class'); } } diff --git a/Tests/KeyGenerator/SplObjectHashKeyGeneratorTest.php b/Tests/KeyGenerator/SplObjectHashKeyGeneratorTest.php index f9876e5..c471b54 100644 --- a/Tests/KeyGenerator/SplObjectHashKeyGeneratorTest.php +++ b/Tests/KeyGenerator/SplObjectHashKeyGeneratorTest.php @@ -4,43 +4,34 @@ use EmanueleMinotto\TwigCacheBundle\KeyGenerator\SplObjectHashKeyGenerator; use PHPUnit_Framework_TestCase; +use stdClass; -/** - * @coversDefaultClass \EmanueleMinotto\TwigCacheBundle\KeyGenerator\SplObjectHashKeyGenerator - */ class SplObjectHashKeyGeneratorTest extends PHPUnit_Framework_TestCase { - /** - * @covers ::generateKey - */ public function testGenerateKeySingle() { $generator = new SplObjectHashKeyGenerator(); - $interface = 'Asm89\\Twig\\CacheExtension\\CacheStrategy\\KeyGeneratorInterface'; - $this->assertInstanceOf($interface, $generator); + $this->assertInstanceOf( + 'Asm89\Twig\CacheExtension\CacheStrategy\KeyGeneratorInterface', + $generator + ); $result = $generator->generateKey('foo'); $this->assertSame(sha1(serialize('foo')), $result); - $result = $generator->generateKey(new \stdClass()); - $this->assertSame(spl_object_hash(new \stdClass()), $result); + $result = $generator->generateKey(new stdClass()); + $this->assertSame(spl_object_hash(new stdClass()), $result); } /** - * @covers ::generateKey * @dataProvider valueProvider */ public function testGenerateKey($value) { $generator = new SplObjectHashKeyGenerator(); - $interface = 'Asm89\\Twig\\CacheExtension\\CacheStrategy\\KeyGeneratorInterface'; - $this->assertInstanceOf($interface, $generator); - - $result = $generator->generateKey($value); - - $this->assertRegExp('/[a-z0-9]{20,20}/', $result); + $this->assertRegExp('/[a-z0-9]{20,20}/', $generator->generateKey($value)); } /** @@ -49,7 +40,7 @@ public function testGenerateKey($value) public function valueProvider() { return [ - [new \stdClass()], + [new stdClass()], [[]], ['foo'], ['bar'], diff --git a/Tests/ProfilerExtension/ProfilerExtensionTest.php b/Tests/ProfilerExtension/ProfilerExtensionTest.php deleted file mode 100644 index ab00a8f..0000000 --- a/Tests/ProfilerExtension/ProfilerExtensionTest.php +++ /dev/null @@ -1,54 +0,0 @@ -kernel = new AppKernel('TwigCacheExtensionTest', true); - $this->kernel->boot(); - } - - /** - * @covers ::serialize - * @covers ::unserialize - */ - public function testSerialization() - { - $container = $this->kernel->getContainer(); - - $cacheStrategy = $container->get('twig_cache.strategy.generational'); - $extension = new ProfilerExtension($cacheStrategy); - - $extension->addFetchBlock('foo', true); - $extension->addGenerateKey('generation_key', 123); - - $data = $extension->getData(); - - $serializedData = serialize($extension); - - /** @var ProfilerExtension $unserialized */ - $unserialized = unserialize($serializedData); - - $this->assertInstanceOf('EmanueleMinotto\TwigCacheBundle\Twig\ProfilerExtension', $unserialized); - $dataUnserialized = $unserialized->getData(); - - $this->assertEquals($data, $dataUnserialized); - } -} diff --git a/Tests/Resources/config/config.yml b/Tests/Resources/config/config.yml deleted file mode 100644 index 735b77b..0000000 --- a/Tests/Resources/config/config.yml +++ /dev/null @@ -1,17 +0,0 @@ -framework: - router: - resource: ruoting.yml - templating: - engines: - - twig - test: null - -parameters: - kernel.secret: ThisTokenIsNotSoSecretChangeIt - -services: - test_cache: - class: Doctrine\Common\Cache\ArrayCache - -twig_cache: - service: test_cache diff --git a/Tests/Resources/config/routing.yml b/Tests/Resources/config/routing.yml deleted file mode 100644 index e69de29..0000000 diff --git a/Tests/Strategy/ProfilerStrategyTest.php b/Tests/Strategy/ProfilerStrategyTest.php new file mode 100644 index 0000000..19e8020 --- /dev/null +++ b/Tests/Strategy/ProfilerStrategyTest.php @@ -0,0 +1,108 @@ +cacheStrategy = $this->getMock('Asm89\Twig\CacheExtension\CacheStrategyInterface'); + + $this->profilerExtension = $this + ->getMockBuilder('EmanueleMinotto\TwigCacheBundle\Twig\ProfilerExtension') + ->disableOriginalConstructor() + ->getMock() + ; + + $this->object = new ProfilerStrategy( + $this->cacheStrategy, + $this->profilerExtension + ); + } + + public function testFetchBlock() + { + $key = sha1(rand()); + $output = sha1(rand()); + + $this->cacheStrategy + ->method('fetchBlock') + ->will($this->returnCallback(function ($a) use ($key, $output) { + $this->assertSame($key, $a); + + return $output; + })) + ; + + $this->profilerExtension + ->method('addFetchBlock') + ->will($this->returnCallback(function ($a, $b) use ($key, $output) { + $this->assertSame($key, $a); + $this->assertSame($output, $b); + })) + ; + + $this->object->fetchBlock($key); + } + + public function testGenerateKey() + { + $annotation = sha1(rand()); + $value = sha1(rand()); + + $this->profilerExtension + ->method('addGenerateKey') + ->will($this->returnCallback(function ($a, $b) use ($annotation, $value) { + $this->assertSame($annotation, $a); + $this->assertSame($value, $b); + })) + ; + + $this->cacheStrategy + ->method('generateKey') + ->will($this->returnCallback(function ($a, $b) use ($annotation, $value) { + $this->assertSame($annotation, $a); + $this->assertSame($value, $b); + })) + ; + + $this->object->generateKey($annotation, $value); + } + + public function testSaveBlock() + { + $key = sha1(rand()); + $block = sha1(rand()); + + $this->cacheStrategy + ->method('saveBlock') + ->will($this->returnCallback(function ($a, $b) use ($key, $block) { + $this->assertSame($key, $a); + $this->assertSame($block, $b); + })) + ; + + $this->object->saveBlock($key, $block); + } +} diff --git a/composer.json b/composer.json index ca9e347..f4d965a 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ }, "require-dev": { "doctrine/cache": "^1.4", + "matthiasnoback/symfony-dependency-injection-test": "^0.7.6", "phpunit/phpunit": "^4.4|^5.0", "symfony/framework-bundle": "^2.6|^3.0", "symfony/twig-bundle": "^2.6|^3.0"