Skip to content
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

Factory service test #85

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
178 changes: 178 additions & 0 deletions PhpUnit/ContainerBuilderHasFactoryConstraint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?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;

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 )
Copy link
Member

Choose a reason for hiding this comment

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

Please remove all spaces between sprintf() & strings inside:

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

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 = $definition->getFactory();

if( !is_array( $factory ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

You could check if the Definition::getFactoryClass() method exists to implement a pre Symfony 2.7 compatible solution.

Copy link
Member

Choose a reason for hiding this comment

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

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 = $definition->getFactory();

list( $factoryDefinition, $factoryMethod ) = $factory;
Copy link
Member

Choose a reason for hiding this comment

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

Please remove all spaces in function calls. Same for those in if/else etc.


if( $factoryDefinition instanceof Reference ) {
$factoryClass = (string)$factoryDefinition;
} else if( 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;
}

}
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
10 changes: 10 additions & 0 deletions Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Matthias\SymfonyDependencyInjectionTest\Tests\Fixtures;

use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
Expand Down Expand Up @@ -31,6 +32,15 @@ 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', new Definition() );

$container
->register( 'manual_created_by_factory_service', new Definition() )
->setFactory( [new Reference('manual_factory_service'), 'factoryMethod'] )
;
}

public function getAlias()
Expand Down
7 changes: 7 additions & 0 deletions Tests/Fixtures/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,12 @@

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

<service id="factory_service">
</service>

<service id="created_by_factory_service">
<factory class="@factory_service" method="factoryMethod" />
</service>
</services>
</container>
9 changes: 9 additions & 0 deletions Tests/PhpUnit/AbstractExtensionTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function if_load_is_successful_it_does_not_fail()
// defined in services.xml
$this->assertContainerBuilderHasSyntheticService('synthetic_service');

// defined in services.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,6 +52,12 @@ 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' );
}

/**
Expand Down
126 changes: 126 additions & 0 deletions Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace Matthias\SymfonyDependencyInjectionTest\Tests\PhpUnit;

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\ContainerBuilderHasFactoryConstraint;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class ContainerBuilderHasFactoryConstraintTest extends TestCase
{
/**
* @test
* @dataProvider containerBuilderProvider
*/
public function match(ContainerBuilder $containerBuilder, $serviceId, $expectedFactoryClass, $expectedFactoryMethod, $shouldMatch)
{
$constraint = new ContainerBuilderHasFactoryConstraint($serviceId, $expectedFactoryClass, $expectedFactoryMethod);

$this->assertSame($shouldMatch, $constraint->evaluate($containerBuilder, null, true));
}

public function containerBuilderProvider()
{
$rightServiceId = 'some_service_id';
$wrongServiceId = 'other_service_id';
$factoryClass = 'factory_class_service';
$invalidFactoryClass = 'invalid_class_service';
$factoryMethod = 'someMethod';
$invalidFactoryMethod = 'invalidMethod';

$emptyContainerBuilder = new ContainerBuilder();

$builderWithFactory = new ContainerBuilder();
$factoryReference = new Reference($factoryClass);
$builderWithFactory->register( $rightServiceId)
->setFactory([$factoryReference,$factoryMethod]);
$builderWithFactory->register( $wrongServiceId );

return [
array($builderWithFactory, $rightServiceId, $factoryClass, $factoryMethod, true ), ];

return array(
// the container does not have the service
array($emptyContainerBuilder, $rightServiceId, null, null, false),

// the container has service created by factory
array($builderWithFactory, $rightServiceId, null, null, true ),

// the container has service, but they has not factory
array($builderWithFactory, $wrongServiceId, null, null, false ),

// the container has service created by factory, but factory is invalid
array($builderWithFactory, $rightServiceId, $invalidFactoryClass, $factoryMethod, false ),

// the container has service created by factory, but factory method is invalid
array($builderWithFactory, $rightServiceId, $factoryClass, $invalidFactoryMethod, false ),

// the container has service created by factory, and whole arguments are valid
array($builderWithFactory, $rightServiceId, $factoryClass, $factoryMethod, true ),

// the container has service created by factory, and whole arguments are valid
array($builderWithFactory, $wrongServiceId, $factoryClass, $factoryMethod, false ),

);
}

/**
* @test
*/
public function it_has_a_string_representation()
{
$serviceId = 'service_id';
$factoryClass = 'SomeFactoryClass';
$factoryMethod = 'someMethod';
$constraint = new ContainerBuilderHasFactoryConstraint($serviceId, $factoryClass, $factoryMethod);
$this->assertSame(
'"service_id" has factory "@SomeFactoryClass:someMethod"',
$constraint->toString()
);
}

/**
* @test
*/
public function it_expects_a_string_for_service_id()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('string');

new ContainerBuilderHasFactoryConstraint(new \stdClass(), 'service_id');
}

/**
* @test
*/
public function it_expects_a_string_for_factory_class()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('string');

new ContainerBuilderHasFactoryConstraint('service_id', new \stdClass(), '');
}

/**
* @test
*/
public function it_expects_a_string_for_factory_method()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('string');

new ContainerBuilderHasFactoryConstraint('service_id', 'FactoryClass', new \stdClass());
}

/**
* @test
*/
public function it_expects_factory_class_be_informed()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('inform');

new ContainerBuilderHasFactoryConstraint('service_id', null, 'factoryMethod');
}
}