This extension is inspired by SymfonyDependencyInjectionTest, and used for simplify testing of Symfony DI.
namespace Vendor\MyProjectBundle\DependencyInjection\Tests\Units;
use atoum;
use Vendor\MyProjectBundle\DependencyInjection\VendorMyBundleExtension as TestedClass;
use Symfony\Component\DependencyInjection;
class VendorMyBundleExtension extends atoum
{
public function testLoad()
{
$this
->given(
$container = new DependencyInjection\ContainerBuilder(),
$testedClass = new TestedClass(),
$testedClass->load([], $container)
)
->then
->containerBuilder($container)
->hasService('myServiceName')
->hasParameter('myParameterName')
;
}
}
Install extension using composer:
composer require --dev jdecool/atoum-symfony-di-extension
Enable the extension using atoum configuration file:
<?php
// .atoum.php
require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
use jdecool\atoum\symfonyDependencyInjection;
$runner->addExtension(new symfonyDependencyInjection\extension($script));
This extension adds two new asserters : containerBuilder
and serviceDefinition
.
In the following examples we will assume we have a container definition like this one, available in a $container
variable:
<parameters>
<parameter key="mailer.transport">sendmail</parameter>
</parameters>
<services>
<service id="myAwesomeAlias" alias="serviceid" />
<service id="serviceid" class="ArrayObject" >
<argument type="collection">
<argument type="service"><service class="stdClass" /></argument>
</argument>
<call method="append">
<argument type="service"><service class="stdClass" /></argument>
</call>
<tag name="myCustomTag" />
</service>
<service id="app.synthetic_service" synthetic="true" />
</services>
On the containerBuilder
asserter, all assertions from the variable asserter are available.
hasService
checks if a service is present in the container.
$this
->containerBuilder($container)
->hasService('serviceId') // passes
->hasService('service_id') // fails
;
hasAlias
checks if an alias is present in the container.
$this
->containerBuilder($container)
->hasAlias('myAwesomeAlias') // passes
->hasAlias('anUnexistingAlias') // fails
;
hasSyntheticService
checks if a service is present in the container and is synthetic.
$this
->containerBuilder($container)
->hasSyntheticService('app.synthetic_service') // passes
->hasService('app.synthetic_service') // fails
->hasSyntheticService('app_synthetic_service') // fails
;
hasNotService
checks if a service is not present in the container.
$this
->containerBuilder($container)
->hasService('service_id') // passes
->hasService('serviceId') // fails
;
hasParameter
checks if a parameter is present in the container.
$this
->containerBuilder($container)
->hasParameter('mailer.transport') // passes
->hasParameter('mailer_transport') // fails
;
hasNotParameter
checks if a parameter is not present in the container.
$this
->containerBuilder($container)
->hasNotParameter('mailer_transport') // passes
->hasNotParameter('mailer.transport') // fails
;
On the serviceDefinition
asserter, all assertions from the variable asserter are available.
isInstanceOf
checks if the defined service class is the same as tested.
Warning : this will not check if the class inherits from another one.
$this
->serviceDefinition($container->getDefinition('serviceid'))
->isInstanceOf('ArrayObject') // passes
->isInstanceOf('SplFileObject') // fails
;
isInstanceOf
checks if the defined service class is not the same as tested.
Warning : this will not check if the class inherits from another one.
$this
->serviceDefinition($container->getDefinition('serviceid'))
->isNotInstanceOf('SplFileObject') // passes
->isNotInstanceOf('ArrayObject') // fails
;
hasArgument
checks if the service definition has an argument in the index passed has parameter.
$this
->serviceDefinition($container->getDefinition('serviceid'))
->hasArgument(0)
;
hasTag
checks if the service definition has a given tag.
$this
->serviceDefinition($container->getDefinition('serviceid'))
->hasTag('myCustomTag')
;
hasMethodCall
checks if a service definition it a method call defined on the method name passed as parameter.
$this
->serviceDefinition($container->getDefinition('serviceid'))
->hasTag('myCustomTag')
;
This extension is released under the MIT License. See the bundled LICENSE file for details.