Permalink
Browse files

Initial commit

  • Loading branch information...
matthiasnoback committed Sep 4, 2013
0 parents commit bffed2bd33e5d4434d1e5c5271fe829a09a02a12
@@ -0,0 +1,3 @@
composer.lock
phpunit.xml
vendor
19 LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2013 Matthias Noback
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@@ -0,0 +1,37 @@
<?php
namespace Matthias\SymfonyDependencyInjectionTest\PhpUnit;
use Symfony\Component\DependencyInjection\ContainerBuilder;
abstract class AbstractCompilerPassTest extends ContainerBuilderTestCase
{
/**
* Register the compiler pass under test, just like you would do inside a bundle's load()
* method:
*
* $container->addCompilerPass(new MyCompilerPass());
*/
abstract protected function registerCompilerPass(ContainerBuilder $container);
/**
* This test will run the compile method
*
* @test
*/
public function compilation_should_not_fail_with_empty_container()
{
try {
$this->compile();
} catch (\Exception $e) {
$this->fail('The compiler pass should not fail with an empty container.');
}
}
protected function setUp()
{
parent::setUp();
$this->registerCompilerPass($this->container);
}
}
@@ -0,0 +1,80 @@
<?php
namespace Matthias\SymfonyDependencyInjectionTest\PhpUnit;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
abstract class AbstractExtensionTestCase extends ContainerBuilderTestCase
{
/**
* Return an array of container extensions you need to be registered for each test (usually just the container
* extension you are testing.
*
* @return ExtensionInterface[]
*/
abstract protected function getContainerExtensions();
/**
* Optionally override this method to return an array that will be used as the minimal configuration for loading
* the container extension under test, to prevent a test from failing because of a missing required
* configuration value for the container extension.
*/
protected function getMinimalConfiguration()
{
return array();
}
/**
* Setup for each test: creates a new ContainerBuilder,
* registers the ValidateServiceDefinitionsPass which will validate all defined services when
* the container is compiled
*
* @see AbstractExtensionTestCase::tearDown()
*/
protected function setUp()
{
parent::setUp();
foreach ($this->getContainerExtensions() as $extension) {
$this->container->registerExtension($extension);
}
}
/**
* Inside each test case, you can modify the ContainerBuilder ($this->container), then call $this->load() to load
* the container extension(s) you are testing. After each test the container will be compiled. This will cause
* exceptions in the case of problems with any container extension.
*/
protected function tearDown()
{
// this will trigger any errors that are likely to occur at compile time in the real application
$this->compile();
parent::tearDown();
}
/**
* Call this method from within your test after you have (optionally) modified the ContainerBuilder for this test
* ($this->container).
*
* @param array $specificConfiguration
*/
protected function load(array $configurationValues = array())
{
$configs = array($this->getMinimalConfiguration(), $configurationValues);
foreach ($this->container->getExtensions() as $extension) {
$extension->load($configs, $this->container);
}
}
/**
* Call this method if you want to compile the ContainerBuilder *inside* the test instead of *after* the test.
*/
protected function compile()
{
$this->container->compile();
}
}
@@ -0,0 +1,82 @@
<?php
namespace Matthias\SymfonyDependencyInjectionTest\PhpUnit;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class ContainerBuilderHasAliasConstraint extends \PHPUnit_Framework_Constraint
{
private $aliasId;
private $expectedServiceId;
public function __construct($aliasId, $expectedServiceId)
{
$this->aliasId = $aliasId;
$this->expectedServiceId = $expectedServiceId;
}
public function toString()
{
return 'has an alias "' . $this->aliasId . '" for service "' . $this->expectedServiceId . '"';
}
public function evaluate($other, $description = '', $returnResult = false)
{
if (!($other instanceof ContainerBuilder)) {
throw new \InvalidArgumentException(
'Expected an instance of Symfony\Component\DependencyInjection\ContainerBuilder'
);
}
if (!$this->evaluateAliasId($other, $returnResult)) {
return false;
}
if (!$this->evaluateServiceId($other, $returnResult)) {
return false;
}
return true;
}
private function evaluateAliasId(ContainerBuilder $containerBuilder, $returnResult)
{
if (!$containerBuilder->hasAlias($this->aliasId)) {
if ($returnResult) {
return false;
}
$this->fail(
$containerBuilder,
sprintf(
'The container builder has no alias "%s"',
$this->aliasId
)
);
}
return true;
}
private function evaluateServiceId(ContainerBuilder $containerBuilder, $returnResult)
{
$alias = $containerBuilder->getAlias($this->aliasId);
/*
* The aliases service id can be retrieved by casting the alias to a string,
* see Alias::__toString()
*/
$actualServiceId = (string) $alias;
$constraint = new \PHPUnit_Framework_Constraint_IsEqual($this->expectedServiceId);
return $constraint->evaluate(
$actualServiceId,
sprintf('"%s" is not an alias for "%s"',
$this->aliasId,
$this->expectedServiceId
),
$returnResult
);
}
}
@@ -0,0 +1,80 @@
<?php
namespace Matthias\SymfonyDependencyInjectionTest\PhpUnit;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class ContainerBuilderHasServiceDefinitionConstraint extends \PHPUnit_Framework_Constraint
{
private $serviceId;
private $expectedClass;
public function __construct($serviceId, $expectedClass)
{
$this->serviceId = $serviceId;
$this->expectedClass = $expectedClass;
}
public function toString()
{
return sprintf(
'has a service definition "%s" with class "%s"',
$this->serviceId,
$this->expectedClass
);
}
public function evaluate($other, $description = '', $returnResult = false)
{
if (!($other instanceof ContainerBuilder)) {
throw new \InvalidArgumentException(
'Expected an instance of Symfony\Component\DependencyInjection\ContainerBuilder'
);
}
if (!$this->evaluateServiceDefinition($other, $returnResult)) {
return false;
}
if (!$this->evaluateClass($other, $returnResult)) {
return false;
}
return true;
}
private function evaluateServiceDefinition(ContainerBuilder $containerBuilder, $returnResult)
{
if (!$containerBuilder->has($this->serviceId)) {
if ($returnResult) {
return false;
}
$this->fail(
$containerBuilder,
sprintf(
'The container builder has no service "%s"',
$this->serviceId
)
);
}
return true;
}
private function evaluateClass(ContainerBuilder $containerBuilder, $returnResult)
{
$definition = $containerBuilder->findDefinition($this->serviceId);
$constraint = new \PHPUnit_Framework_Constraint_IsEqual($this->expectedClass);
return $constraint->evaluate(
$definition->getClass(),
sprintf(
'The class of the service definition of "%s" does not match the expected value',
$this->serviceId
),
$returnResult
);
}
}
Oops, something went wrong.

0 comments on commit bffed2b

Please sign in to comment.