From ca2b88acee092f177851474ac0e69d611d554b43 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Sun, 11 Sep 2011 20:10:55 +0200 Subject: [PATCH] Added some tests for the DI extension --- .gitignore | 1 + .../AvalancheImagineExtensionTest.php | 153 ++++++++++++++++++ Tests/bootstrap.php | 26 +++ phpunit.xml.dist | 36 +++++ 4 files changed, 216 insertions(+) create mode 100644 .gitignore create mode 100644 Tests/DependencyInjection/AvalancheImagineExtensionTest.php create mode 100644 Tests/bootstrap.php create mode 100644 phpunit.xml.dist diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..319b3826f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/phpunit.xml diff --git a/Tests/DependencyInjection/AvalancheImagineExtensionTest.php b/Tests/DependencyInjection/AvalancheImagineExtensionTest.php new file mode 100644 index 000000000..cb142b5a5 --- /dev/null +++ b/Tests/DependencyInjection/AvalancheImagineExtensionTest.php @@ -0,0 +1,153 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Avalanche\Bundle\ImagineBundle\Tests\DependencyInjection; + +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Avalanche\Bundle\ImagineBundle\DependencyInjection\AvalancheImagineExtension; +use Symfony\Component\Yaml\Parser; +use Symfony\Component\DependencyInjection\Reference; + +class AvalancheImagineExtensionTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Symfony\Component\DependencyInjection\ContainerBuilder + */ + protected $containerBuilder; + + /** + * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException + */ + public function testUserLoadThrowsExceptionUnlessDriverIsValid() + { + $loader = new AvalancheImagineExtension(); + $config = array('driver' => 'foo'); + $loader->load(array($config), new ContainerBuilder()); + } + + public function testLoadWithDefaults() + { + $this->createEmptyConfiguration(); + + $this->assertParameter(true, 'imagine.cache'); + $this->assertAlias('imagine.gd', 'imagine'); + $this->assertHasDefinition('imagine.controller'); + $this->assertDICConstructorArguments( + $this->containerBuilder->getDefinition('imagine.controller'), + array(new Reference('imagine.loader.filesystem'), new Reference('imagine.filter.manager'), new Reference('imagine.cache.path.resolver')) + ); + } + + public function testLoad() + { + $this->createFullConfiguration(); + + $this->assertParameter(false, 'imagine.cache'); + $this->assertAlias('imagine.imagick', 'imagine'); + $this->assertHasDefinition('imagine.controller'); + $this->assertDICConstructorArguments( + $this->containerBuilder->getDefinition('imagine.controller'), + array(new Reference('acme_imagine.loader'), new Reference('imagine.filter.manager')) + ); + } + + /** + * @return ContainerBuilder + */ + protected function createEmptyConfiguration() + { + $this->containerBuilder = new ContainerBuilder(); + $loader = new AvalancheImagineExtension(); + $loader->load(array(array()), $this->containerBuilder); + $this->assertTrue($this->containerBuilder instanceof ContainerBuilder); + } + + /** + * @return ContainerBuilder + */ + protected function createFullConfiguration() + { + $this->containerBuilder = new ContainerBuilder(); + $loader = new AvalancheImagineExtension(); + $loader->load(array($this->getFullConfig()), $this->containerBuilder); + $this->assertTrue($this->containerBuilder instanceof ContainerBuilder); + } + + protected function getFullConfig() + { + $yaml = <<parse($yaml); + } + + private function assertAlias($value, $key) + { + $this->assertEquals($value, (string) $this->containerBuilder->getAlias($key), sprintf('%s alias is correct', $key)); + } + + private function assertParameter($value, $key) + { + $this->assertEquals($value, $this->containerBuilder->getParameter($key), sprintf('%s parameter is correct', $key)); + } + + private function assertHasDefinition($id) + { + $this->assertTrue(($this->containerBuilder->hasDefinition($id) ?: $this->containerBuilder->hasAlias($id))); + } + + private function assertNotHasDefinition($id) + { + $this->assertFalse(($this->containerBuilder->hasDefinition($id) ?: $this->containerBuilder->hasAlias($id))); + } + + private function assertDICConstructorArguments($definition, $args) + { + $this->assertEquals($args, $definition->getArguments(), "Expected and actual DIC Service constructor arguments of definition '".$definition->getClass()."' don't match."); + } + + protected function tearDown() + { + unset($this->containerBuilder); + } +} diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php new file mode 100644 index 000000000..b52ec5c1e --- /dev/null +++ b/Tests/bootstrap.php @@ -0,0 +1,26 @@ +registerNamespace('Symfony', SYMFONY_SRC_DIR); +$loader->registerNamespace('Imagine', IMAGINE_SRC_DIR); +$loader->register(); diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 000000000..1380396d4 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,36 @@ + + + + + + ./Tests/ + + + + + + ./ + + ./Resources + ./Tests + + + + + + + + + + +