Skip to content

Commit

Permalink
improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EmanueleMinotto committed Dec 6, 2015
1 parent 0abb953 commit 18f6853
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 151 deletions.
4 changes: 3 additions & 1 deletion Strategy/ProfilerStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(CacheStrategyInterface $cacheStrategy, ProfilerExten
*
* @param mixed $key
*
* @return string
* @return mixed
*/
public function fetchBlock($key)
{
Expand Down Expand Up @@ -65,6 +65,8 @@ public function generateKey($annotation, $value)
*
* @param mixed $key
* @param string $block
*
* @return mixed
*/
public function saveBlock($key, $block)
{
Expand Down
31 changes: 0 additions & 31 deletions Tests/AppKernel.php

This file was deleted.

64 changes: 34 additions & 30 deletions Tests/DependencyInjection/TwigCacheExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
27 changes: 9 additions & 18 deletions Tests/KeyGenerator/SplObjectHashKeyGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand All @@ -49,7 +40,7 @@ public function testGenerateKey($value)
public function valueProvider()
{
return [
[new \stdClass()],
[new stdClass()],
[[]],
['foo'],
['bar'],
Expand Down
54 changes: 0 additions & 54 deletions Tests/ProfilerExtension/ProfilerExtensionTest.php

This file was deleted.

17 changes: 0 additions & 17 deletions Tests/Resources/config/config.yml

This file was deleted.

Empty file removed Tests/Resources/config/routing.yml
Empty file.
108 changes: 108 additions & 0 deletions Tests/Strategy/ProfilerStrategyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace EmanueleMinotto\TwigCacheBundle\Tests\Strategy;

use PHPUnit_Framework_TestCase;
use EmanueleMinotto\TwigCacheBundle\Strategy\ProfilerStrategy;

class ProfilerStrategyTest extends PHPUnit_Framework_TestCase
{
/**
* @var ProfilerStrategy
*/
protected $object;

/**
* @var \Asm89\Twig\CacheExtension\CacheStrategyInterface
*/
private $cacheStrategy;

/**
* @var \EmanueleMinotto\TwigCacheBundle\Twig\ProfilerExtension
*/
private $profilerExtension;

/**
* {@inheritdoc}
*/
public function setUp()
{
$this->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);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 18f6853

Please sign in to comment.