Skip to content

Factory service test #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions PhpUnit/AbstractContainerBuilderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ protected function assertContainerBuilderHasAlias($aliasId, $expectedServiceId =
);
}

/**
* Assert that the ContainerBuilder for this test has a service which is created by other service
*
* @param $serivceId
* @param $expectedFactoryClass
* @param $expectedFactoryMethod
*/
protected function assertContainerBuilderHasCreatedByFactoryService($serviceId, $expectedFactoryClass = null, $expectedFactoryMethod)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that the library requires PHP 7 we can use scalar type hints here

{
self::assertThat(
$this->container,
new ContainerBuilderHasFactoryConstraint($serviceId, $expectedFactoryClass, $expectedFactoryMethod)
);
}

/**
* Assert that the ContainerBuilder for this test has a parameter and that its value is the given value.
*
Expand Down
215 changes: 215 additions & 0 deletions PhpUnit/ContainerBuilderHasFactoryConstraint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
<?php

namespace Matthias\SymfonyDependencyInjectionTest\PhpUnit;

use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsEqual;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;

class ContainerBuilderHasFactoryConstraint extends Constraint
{
private $serviceId;
private $expectedFactoryClass;
private $expectedFactoryMethod;

public function __construct($serviceId, $expectedFactoryClass = null, $expectedFactoryMethod = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

{
parent::__construct();

if (!is_string($serviceId)) {
throw new \InvalidArgumentException('The $serviceId argument should be a string');
}

if ($expectedFactoryClass !== null && !is_string($expectedFactoryClass)) {
throw new \InvalidArgumentException('The $expectedFactoryClass argument should be a string');
}

if (null !== $expectedFactoryMethod && null === $expectedFactoryClass) {
throw new \InvalidArgumentException('When argument $expectedFactoryMethod is set, must inform $expectedFactoryClass');
}

if (null !== $expectedFactoryMethod && !is_string($expectedFactoryMethod)) {
throw new \InvalidArgumentException('The $expectedFactoryMethod argument should be a string');
}

$this->serviceId = $serviceId;
$this->expectedFactoryClass = $expectedFactoryClass;
$this->expectedFactoryMethod = $expectedFactoryMethod;
}

public function toString()
{
if (null === $this->expectedFactoryClass) {
return sprintf('"%s" has factory', $this->serviceId);
}

return sprintf('"%s" has factory "@%s:%s"', $this->serviceId, $this->expectedFactoryClass, $this->expectedFactoryMethod);
}

public function evaluate($other, $description = '', $returnResult = false)
{
if (!($other instanceof ContainerBuilder)) {
throw new \InvalidArgumentException(
'Expected an instance of Symfony\Component\DependencyInjection\ContainerBuilder'
);
}

if (!$this->evaluateServiceId($other, $returnResult)) {
return false;
}

if (!$this->evaluateFactory($other, $returnResult)) {
return false;
}

if ($this->expectedFactoryClass !== null && !$this->evaluateFactoryClass($other, $returnResult)) {
return false;
}

return true;
}

private function evaluateServiceId(ContainerBuilder $containerBuilder, $returnResult)
{
if (!$containerBuilder->hasDefinition($this->serviceId)) {
if ($returnResult) {
return false;
}

$this->fail(
$containerBuilder,
sprintf(
'The container builder has no service "%s"',
$this->serviceId
)
);
}

return true;
}

private function evaluateFactory(ContainerBuilder $containerBuilder, $returnResult)
{
/** @var Definition */
$definition = $containerBuilder->getDefinition($this->serviceId);

$factory = $this->getFactoryData($definition);

if (!is_array($factory)) {
if ($returnResult) {
return false;
}

$this->fail(
$containerBuilder,
sprintf(
'The container builder has service "%s" with not "%s" factory',
$this->serviceId,
$this->expectedFactoryClass
)
);
}

return true;
}

private function evaluateFactoryClass(ContainerBuilder $containerBuilder, $returnResult)
{
/** @var Definition */
$definition = $containerBuilder->getDefinition($this->serviceId);

$factory = $this->getFactoryData($definition);

list($factoryDefinition, $factoryMethod) = $factory;

if ($factoryDefinition instanceof Reference) {
$factoryClass = (string)$factoryDefinition;
} elseif (is_string($factoryDefinition)) {
$factoryClass = $factoryDefinition;
} else {
if ($returnResult) {
return false;
}

$this->fail(
$containerBuilder,
sprintf(
'The container builder has service "%s" with not service "%s" factory',
$this->serviceId,
$this->expectedFactoryClass
)
);
}

$constraint = new IsEqual($this->expectedFactoryClass);
if (!$constraint->evaluate($factoryClass, '', true)) {
if ($returnResult) {
return false;
}

$this->fail(
$containerBuilder,
sprintf(
'The container builder has service "%s" with not service class "%s" factory',
$this->serviceId,
$this->expectedFactoryClass
)
);
}

if ($this->expectedFactoryMethod) {
$constraint = new IsEqual($this->expectedFactoryMethod);
if (!$constraint->evaluate($factoryMethod, '', true)) {
if ($returnResult) {
return false;
}

$this->fail(
$containerBuilder,
sprintf(
'The container builder has service "%s" with not service class method "%s::%s" factory',
$this->serviceId,
$this->expectedFactoryClass,
$this->expectedFactoryMethod
)
);
}
}

return true;
}

private function getFactoryData(Definition $definition)
{
if (self::isLegacySymfonyDI()) {
$factoryService = $definition->getFactoryService();
$factoryMethod = $definition->getFactoryMethod();
$factoryClass = $definition->getFactoryClass();
if (!$factoryService && !$factoryClass) {
return null;
}

return array( $factoryClass ? $factoryClass : $factoryService, $factoryMethod );
} else {
$factory = $definition->getFactory();
if (is_array($factory)) {
return $factory;
}

if (is_string($factory) && false !== strpos($factory, ':')) {
return preg_split('/:/', $factory, 2);
}

return $factory;
}
}


public static function isLegacySymfonyDI()
{
return !method_exists(Definition::class, 'getFactory') &&
method_exists(Definition::class, 'getFactoryService');
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ These are the available semantic assertions for each of the test cases shown abo
<dd>Assert that the <code>ContainerBuilder</code> for this test has an alias.</dd>
<dt><code>assertContainerBuilderHasAlias($aliasId, $expectedServiceId)</code></dt>
<dd>Assert that the <code>ContainerBuilder</code> for this test has an alias and that it is an alias for the given service id.</dd>
<dt><code>assertContainerBuilderHasCreatedByFactoryService($serviceId)</code></dt>
<dd>Assert that the <code>ContainerBuilder</code> for this test has an service with factory.</dd>
<dt><code>assertContainerBuilderHasCreatedByFactoryService($serviceId,$factoryService, $factoryMethod)</code></dt>
<dd>Assert that the <code>ContainerBuilder</code> for this test has an service with factory for given factory service and method.</dd>
<dt><code>assertContainerBuilderHasParameter($parameterName)</code></dt>
<dd>Assert that the <code>ContainerBuilder</code> for this test has a parameter.</dd>
<dt><code>assertContainerBuilderHasParameter($parameterName, $expectedParameterValue)</code></dt>
Expand Down
30 changes: 30 additions & 0 deletions Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\ContainerBuilderHasFactoryConstraint;

class MatthiasDependencyInjectionTestExtension implements ExtensionInterface
{
Expand All @@ -16,6 +19,17 @@ public function load(array $config, ContainerBuilder $container)
$loader = new XmlFileLoader($container, new FileLocator(__DIR__));
$loader->load('services.xml');

// load factory services definitions
if (ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI()) {
$loader->load('services-factory-legacy.xml');
} else {
$loader->load('services-factory.xml');

// Load old syntax for services in YML files
$ymlLoader = new YamlFileLoader($container, new FileLocator(__DIR__));
$ymlLoader->load('services-factory-old-syntax.yml');
}

// set a parameter manually
$container->setParameter('manual_parameter', 'parameter value');

Expand All @@ -31,6 +45,22 @@ public function load(array $config, ContainerBuilder $container)

// add an alias to an existing service
$container->setAlias('manual_alias', 'service_id');

// add an factory service
$container->register('manual_factory_service', 'stdClass');

if (ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI()) {
$container
->register('manual_created_by_factory_service', 'stdClass')
->setFactoryService(new Reference('manual_factory_service'))
->setFactoryMethod('factoryMethod');
;
} else {
$container
->register('manual_created_by_factory_service', 'stdClass')
->setFactory('manual_factory_service:factoryMethod')
;
}
}

public function getAlias()
Expand Down
12 changes: 12 additions & 0 deletions Tests/Fixtures/services-factory-legacy.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="factory_service">
</service>

<service id="created_by_factory_service" factory-service="factory_service" factory-method="factoryMethod">
</service>
</services>
</container>
3 changes: 3 additions & 0 deletions Tests/Fixtures/services-factory-old-syntax.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
services:
created_with_factory_with_old_syntax:
factory: ['@factory_service', 'factoryMethod']
13 changes: 13 additions & 0 deletions Tests/Fixtures/services-factory.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="factory_service">
</service>

<service id="created_by_factory_service">
<factory service="factory_service" method="factoryMethod" />
</service>
</services>
</container>
1 change: 1 addition & 0 deletions Tests/Fixtures/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@

<service id="synthetic_service" synthetic="true">
</service>

</services>
</container>
20 changes: 20 additions & 0 deletions Tests/PhpUnit/AbstractExtensionTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Matthias\DependencyInjectionTests\Test\DependencyInjection;

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\ContainerBuilderHasFactoryConstraint;
use Matthias\SymfonyDependencyInjectionTest\Tests\Fixtures\MatthiasDependencyInjectionTestExtension;
use PHPUnit\Framework\ExpectationFailedException;

Expand All @@ -28,6 +29,9 @@ public function if_load_is_successful_it_does_not_fail()
// defined in services.xml
$this->assertContainerBuilderHasSyntheticService('synthetic_service');

// defined in services-factory.xml
$this->assertContainerBuilderHasCreatedByFactoryService('created_by_factory_service', 'factory_service', 'factoryMethod');

// manually defined parameter
$this->assertContainerBuilderHasParameter('manual_parameter', 'parameter value');
// Just check parameter exists, value will not be checked.
Expand All @@ -49,8 +53,24 @@ public function if_load_is_successful_it_does_not_fail()
// check for existence of manually created arguments, not checking values.
$this->assertContainerBuilderHasServiceDefinitionWithArgument('manual_service_id', 0);
$this->assertContainerBuilderHasServiceDefinitionWithArgument('manual_service_id', 1);

// manually created factory service
$this->assertContainerBuilderHasCreatedByFactoryService(
'manual_created_by_factory_service',
'manual_factory_service',
'factoryMethod'
);

if (!ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI()) {
$this->assertContainerBuilderHasCreatedByFactoryService(
'created_with_factory_with_old_syntax',
'factory_service',
'factoryMethod'
);
}
}


/**
* @test
*/
Expand Down
Loading