Skip to content

Commit

Permalink
Added some tests for the DI extension
Browse files Browse the repository at this point in the history
  • Loading branch information
stof committed Sep 11, 2011
1 parent 274341c commit ca2b88a
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/phpunit.xml
153 changes: 153 additions & 0 deletions Tests/DependencyInjection/AvalancheImagineExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* 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 = <<<EOF
driver: imagick
web_root: ../foo/bar
cache_prefix: /imagine/cache
cache: false
formats: ['json', 'xml', 'jpg', 'png', 'gif']
filters:
small:
type: thumbnail
options: { size: [100, ~], mode: inset, quality: 80 }
medium_small_cropped:
type: thumbnail
options: { size: [223, 173], mode: outbound, quality: 80 }
medium_cropped:
type: thumbnail
options: { size: [232, 180], mode: outbound, quality: 80 }
medium:
type: thumbnail
options: { size: [232, 180], mode: inset, quality: 80 }
large_cropped:
type: thumbnail
options: { size: [483, 350], mode: outbound, quality: 100 }
large:
type: thumbnail
options: { size: [483, ~], mode: inset, quality: 100 }
xxl:
type: thumbnail
options: { size: [660, ~], mode: inset, quality: 100 }
'':
type: ~
options: { quality: 100 }
loader: acme_imagine.loader
EOF;
$parser = new Parser();

return $parser->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);
}
}
26 changes: 26 additions & 0 deletions Tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

spl_autoload_register(function($class) {
$class = ltrim($class, '\\');
if (0 === strpos($class, 'Avalanche\Bundle\ImagineBundle\\')) {
$file = __DIR__.'/../'.str_replace('\\', '/', substr($class, strlen('Avalanche\Bundle\ImagineBundle\\'))).'.php';
if (file_exists($file)) {
require $file;
}
}
});

if (!defined('SYMFONY_SRC_DIR') || 'NOT_SET' === SYMFONY_SRC_DIR) {
throw new \RuntimeException('You must set the Symfony src dir');
}

if (!defined('IMAGINE_SRC_DIR') || 'NOT_SET' === IMAGINE_SRC_DIR) {
throw new \RuntimeException('You must set the Imagine src dir');
}

require_once SYMFONY_SRC_DIR.'/Symfony/Component/ClassLoader/UniversalClassLoader.php';

$loader = new \Symfony\Component\ClassLoader\UniversalClassLoader();
$loader->registerNamespace('Symfony', SYMFONY_SRC_DIR);
$loader->registerNamespace('Imagine', IMAGINE_SRC_DIR);
$loader->register();
36 changes: 36 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="Tests/bootstrap.php"
>
<testsuites>
<testsuite name="AvalancheImagineBundle Test Suite">
<directory>./Tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
</exclude>
</whitelist>
</filter>

<php>
<!-- path to the Symfony src dir -->
<const name="SYMFONY_SRC_DIR" value="../../../../symfony/src" />
<!-- path to the Imagine src dir -->
<const name="IMAGINE_SRC_DIR" value="../../../../imagine/src" />
</php>
</phpunit>

0 comments on commit ca2b88a

Please sign in to comment.