From 312de0905d3a3404498c65fa7f05623410885c8c Mon Sep 17 00:00:00 2001 From: Silvio Date: Sat, 18 Nov 2017 21:43:13 -0300 Subject: [PATCH 01/14] Factory test --- PhpUnit/AbstractContainerBuilderTestCase.php | 15 ++ .../ContainerBuilderHasFactoryConstraint.php | 178 ++++++++++++++++++ README.md | 4 + ...tthiasDependencyInjectionTestExtension.php | 10 + Tests/Fixtures/services.xml | 7 + .../PhpUnit/AbstractExtensionTestCaseTest.php | 9 + ...ntainerBuilderHasFactoryConstraintTest.php | 126 +++++++++++++ 7 files changed, 349 insertions(+) create mode 100644 PhpUnit/ContainerBuilderHasFactoryConstraint.php create mode 100644 Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php diff --git a/PhpUnit/AbstractContainerBuilderTestCase.php b/PhpUnit/AbstractContainerBuilderTestCase.php index e1cc475..4eff3bd 100644 --- a/PhpUnit/AbstractContainerBuilderTestCase.php +++ b/PhpUnit/AbstractContainerBuilderTestCase.php @@ -129,6 +129,21 @@ protected function assertContainerBuilderHasAlias($aliasId, $expectedServiceId = ); } + /** + * Assert that the ContainerBuilder for this test has an service wich is created by other service + * + * @param $serivceId + * @param $expectedFactoryClass + * @param $expectedFactoryMethod + */ + protected function assertContainerBuilderHasCreatedByFactoryService($serviceId, $expectedFactoryClass = null, $expectedFactoryMethod) + { + 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. * diff --git a/PhpUnit/ContainerBuilderHasFactoryConstraint.php b/PhpUnit/ContainerBuilderHasFactoryConstraint.php new file mode 100644 index 0000000..879d262 --- /dev/null +++ b/PhpUnit/ContainerBuilderHasFactoryConstraint.php @@ -0,0 +1,178 @@ +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 = $definition->getFactory(); + + 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; + + 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; + } + +} diff --git a/README.md b/README.md index b0865ae..0b5e114 100644 --- a/README.md +++ b/README.md @@ -301,6 +301,10 @@ These are the available semantic assertions for each of the test cases shown abo
Assert that the ContainerBuilder for this test has an alias.
assertContainerBuilderHasAlias($aliasId, $expectedServiceId)
Assert that the ContainerBuilder for this test has an alias and that it is an alias for the given service id.
+
assertContainerBuilderHasCreatedByFactoryService($serviceId)
+
Assert that the ContainerBuilder for this test has an service with factory.
+
assertContainerBuilderHasCreatedByFactoryService($serviceId,$factoryService, $factoryMethod)
+
Assert that the ContainerBuilder for this test has an service with factory for given factory service and method.
assertContainerBuilderHasParameter($parameterName)
Assert that the ContainerBuilder for this test has a parameter.
assertContainerBuilderHasParameter($parameterName, $expectedParameterValue)
diff --git a/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php b/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php index 14da294..4b79c69 100644 --- a/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php +++ b/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php @@ -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; @@ -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() diff --git a/Tests/Fixtures/services.xml b/Tests/Fixtures/services.xml index 9abc74b..4133658 100644 --- a/Tests/Fixtures/services.xml +++ b/Tests/Fixtures/services.xml @@ -26,5 +26,12 @@ + + + + + + + diff --git a/Tests/PhpUnit/AbstractExtensionTestCaseTest.php b/Tests/PhpUnit/AbstractExtensionTestCaseTest.php index b39aa61..9369252 100644 --- a/Tests/PhpUnit/AbstractExtensionTestCaseTest.php +++ b/Tests/PhpUnit/AbstractExtensionTestCaseTest.php @@ -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. @@ -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' ); } /** diff --git a/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php b/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php new file mode 100644 index 0000000..0143b97 --- /dev/null +++ b/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php @@ -0,0 +1,126 @@ +assertSame($shouldMatch, $constraint->evaluate($containerBuilder, null, true)); + } + + public function containerBuilderProvider() + { + $rightServiceId = 'some_service_id'; + $wrongServiceId = 'other_service_id'; + $factoryClass = 'SomeFactoryClass'; + $invalidFactoryClass = 'InvalidFactoryClass'; + $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'); + } +} From 7aacdbb56c98b0181aeea7c1e8295e58c75acd85 Mon Sep 17 00:00:00 2001 From: Silvio Date: Sat, 18 Nov 2017 22:00:38 -0300 Subject: [PATCH 02/14] Fix typo --- PhpUnit/AbstractContainerBuilderTestCase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PhpUnit/AbstractContainerBuilderTestCase.php b/PhpUnit/AbstractContainerBuilderTestCase.php index 4eff3bd..f9355bb 100644 --- a/PhpUnit/AbstractContainerBuilderTestCase.php +++ b/PhpUnit/AbstractContainerBuilderTestCase.php @@ -130,7 +130,7 @@ protected function assertContainerBuilderHasAlias($aliasId, $expectedServiceId = } /** - * Assert that the ContainerBuilder for this test has an service wich is created by other service + * Assert that the ContainerBuilder for this test has a service which is created by other service * * @param $serivceId * @param $expectedFactoryClass From e49fdd3332ad589ceaf0b2d1dae427ccc28a5cb3 Mon Sep 17 00:00:00 2001 From: Silvio Date: Sat, 18 Nov 2017 22:25:53 -0300 Subject: [PATCH 03/14] BC for SYMFONY_DI_VERSION=2.7|2.8|3.2 --- Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php b/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php index 0143b97..2429407 100644 --- a/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php +++ b/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php @@ -24,8 +24,8 @@ public function containerBuilderProvider() { $rightServiceId = 'some_service_id'; $wrongServiceId = 'other_service_id'; - $factoryClass = 'SomeFactoryClass'; - $invalidFactoryClass = 'InvalidFactoryClass'; + $factoryClass = 'factory_class_service'; + $invalidFactoryClass = 'invalid_class_service'; $factoryMethod = 'someMethod'; $invalidFactoryMethod = 'invalidMethod'; From 384d88fb8538a8447c3c2cfa41b9ca0ae3882e11 Mon Sep 17 00:00:00 2001 From: Silvio Date: Mon, 20 Nov 2017 19:31:58 -0300 Subject: [PATCH 04/14] Backward compatibility - Check for <2.6 symfony/dependency-injection version - Test old yml syntax format --- .../ContainerBuilderHasFactoryConstraint.php | 31 ++++++++++++++++-- ...tthiasDependencyInjectionTestExtension.php | 32 +++++++++++++++---- Tests/Fixtures/services-factory-legacy.xml | 12 +++++++ .../Fixtures/services-factory-old-syntax.yml | 3 ++ Tests/Fixtures/services-factory.xml | 13 ++++++++ Tests/Fixtures/services.xml | 6 ---- .../PhpUnit/AbstractExtensionTestCaseTest.php | 13 ++++++-- ...ntainerBuilderHasFactoryConstraintTest.php | 15 ++++++--- 8 files changed, 104 insertions(+), 21 deletions(-) create mode 100644 Tests/Fixtures/services-factory-legacy.xml create mode 100644 Tests/Fixtures/services-factory-old-syntax.yml create mode 100644 Tests/Fixtures/services-factory.xml diff --git a/PhpUnit/ContainerBuilderHasFactoryConstraint.php b/PhpUnit/ContainerBuilderHasFactoryConstraint.php index 879d262..84a8697 100644 --- a/PhpUnit/ContainerBuilderHasFactoryConstraint.php +++ b/PhpUnit/ContainerBuilderHasFactoryConstraint.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\Constraint\IsEqual; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\DependencyInjection\Definition; class ContainerBuilderHasFactoryConstraint extends Constraint { @@ -93,7 +94,7 @@ private function evaluateFactory(ContainerBuilder $containerBuilder, $returnResu /** @var Definition */ $definition = $containerBuilder->getDefinition($this->serviceId); - $factory = $definition->getFactory(); + $factory = $this->getFactoryData($definition); if( !is_array( $factory ) ) { if ($returnResult) { @@ -117,7 +118,7 @@ private function evaluateFactoryClass(ContainerBuilder $containerBuilder, $retur /** @var Definition */ $definition = $containerBuilder->getDefinition($this->serviceId); - $factory = $definition->getFactory(); + $factory = $this->getFactoryData( $definition ); list( $factoryDefinition, $factoryMethod ) = $factory; @@ -175,4 +176,30 @@ private function evaluateFactoryClass(ContainerBuilder $containerBuilder, $retur 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; + } + } + + + static public function isLegacySymfonyDI() + { + return method_exists( new Definition(), 'getFactoryService' ); + } } diff --git a/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php b/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php index 4b79c69..08cc343 100644 --- a/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php +++ b/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php @@ -7,7 +7,9 @@ use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; +use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use Symfony\Component\Config\FileLocator; +use Matthias\SymfonyDependencyInjectionTest\PhpUnit\ContainerBuilderHasFactoryConstraint; class MatthiasDependencyInjectionTestExtension implements ExtensionInterface { @@ -17,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'); @@ -34,13 +47,20 @@ public function load(array $config, ContainerBuilder $container) $container->setAlias('manual_alias', 'service_id'); // add an factory service - $container - ->register( 'manual_factory_service', new Definition() ); + $container->register( 'manual_factory_service', new Definition() ); - $container - ->register( 'manual_created_by_factory_service', new Definition() ) - ->setFactory( [new Reference('manual_factory_service'), 'factoryMethod'] ) - ; + if( ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI() ) { + $container + ->register( 'manual_created_by_factory_service', new Definition() ) + ->setFactoryService(new Reference('manual_factory_service')) + ->setFactoryMethod('factoryMethod'); + ; + } else { + $container + ->register( 'manual_created_by_factory_service', new Definition() ) + ->setFactory( 'manual_factory_service:factoryMethod' ) + ; + } } public function getAlias() diff --git a/Tests/Fixtures/services-factory-legacy.xml b/Tests/Fixtures/services-factory-legacy.xml new file mode 100644 index 0000000..736cb9e --- /dev/null +++ b/Tests/Fixtures/services-factory-legacy.xml @@ -0,0 +1,12 @@ + + + + + + + + + + diff --git a/Tests/Fixtures/services-factory-old-syntax.yml b/Tests/Fixtures/services-factory-old-syntax.yml new file mode 100644 index 0000000..7544846 --- /dev/null +++ b/Tests/Fixtures/services-factory-old-syntax.yml @@ -0,0 +1,3 @@ +services: + created_with_factory_with_old_syntax: + factory: ['@factory_service', 'factoryMethod'] diff --git a/Tests/Fixtures/services-factory.xml b/Tests/Fixtures/services-factory.xml new file mode 100644 index 0000000..5037246 --- /dev/null +++ b/Tests/Fixtures/services-factory.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + diff --git a/Tests/Fixtures/services.xml b/Tests/Fixtures/services.xml index 4133658..84a5ce4 100644 --- a/Tests/Fixtures/services.xml +++ b/Tests/Fixtures/services.xml @@ -27,11 +27,5 @@ - - - - - - diff --git a/Tests/PhpUnit/AbstractExtensionTestCaseTest.php b/Tests/PhpUnit/AbstractExtensionTestCaseTest.php index 9369252..fd2db65 100644 --- a/Tests/PhpUnit/AbstractExtensionTestCaseTest.php +++ b/Tests/PhpUnit/AbstractExtensionTestCaseTest.php @@ -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; @@ -28,8 +29,8 @@ 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'); + // defined in services-factory.xml + $this->assertContainerBuilderHasCreatedByFactoryService('created_by_factory_service', 'factory_service', 'factoryMethod'); // manually defined parameter $this->assertContainerBuilderHasParameter('manual_parameter', 'parameter value'); @@ -58,8 +59,16 @@ public function if_load_is_successful_it_does_not_fail() 'manual_created_by_factory_service', 'manual_factory_service', 'factoryMethod' ); + + if( !ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI() ) { + $this->assertContainerBuilderHasCreatedByFactoryService( + 'created_with_factory_with_old_syntax', + 'factory_service', + 'factoryMethod' ); + } } + /** * @test */ diff --git a/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php b/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php index 2429407..f9828e4 100644 --- a/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php +++ b/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php @@ -33,12 +33,17 @@ public function containerBuilderProvider() $builderWithFactory = new ContainerBuilder(); $factoryReference = new Reference($factoryClass); - $builderWithFactory->register( $rightServiceId) - ->setFactory([$factoryReference,$factoryMethod]); - $builderWithFactory->register( $wrongServiceId ); - return [ - array($builderWithFactory, $rightServiceId, $factoryClass, $factoryMethod, true ), ]; + if( ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI() ) { + $builderWithFactory->register( $rightServiceId) + ->setFactoryService($factoryReference) + ->setFactoryMethod($factoryMethod); + } else { + $builderWithFactory->register( $rightServiceId) + ->setFactory([$factoryReference,$factoryMethod]); + } + + $builderWithFactory->register( $wrongServiceId ); return array( // the container does not have the service From 4c7905ec9a4f835623f0da8d000ec90b75a7ba10 Mon Sep 17 00:00:00 2001 From: Silvio Date: Tue, 21 Nov 2017 09:14:47 -0300 Subject: [PATCH 05/14] Optimize, don't create an instance --- PhpUnit/ContainerBuilderHasFactoryConstraint.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PhpUnit/ContainerBuilderHasFactoryConstraint.php b/PhpUnit/ContainerBuilderHasFactoryConstraint.php index 84a8697..17d7dcf 100644 --- a/PhpUnit/ContainerBuilderHasFactoryConstraint.php +++ b/PhpUnit/ContainerBuilderHasFactoryConstraint.php @@ -200,6 +200,6 @@ private function getFactoryData( Definition $definition ) static public function isLegacySymfonyDI() { - return method_exists( new Definition(), 'getFactoryService' ); + return method_exists( Definition::class, 'getFactoryService' ); } } From 16b4ecea8ce017d0cfc6a2e09f795e920c1fa756 Mon Sep 17 00:00:00 2001 From: Silvio Date: Sat, 2 Dec 2017 10:37:03 -0300 Subject: [PATCH 06/14] Fix a lot of CS breaks --- .../ContainerBuilderHasFactoryConstraint.php | 59 +++++++++++-------- ...tthiasDependencyInjectionTestExtension.php | 14 ++--- .../PhpUnit/AbstractExtensionTestCaseTest.php | 8 ++- ...ntainerBuilderHasFactoryConstraintTest.php | 8 +-- 4 files changed, 50 insertions(+), 39 deletions(-) diff --git a/PhpUnit/ContainerBuilderHasFactoryConstraint.php b/PhpUnit/ContainerBuilderHasFactoryConstraint.php index 17d7dcf..3fe1982 100644 --- a/PhpUnit/ContainerBuilderHasFactoryConstraint.php +++ b/PhpUnit/ContainerBuilderHasFactoryConstraint.php @@ -26,11 +26,11 @@ public function __construct($serviceId, $expectedFactoryClass = null, $expectedF throw new \InvalidArgumentException('The $expectedFactoryClass argument should be a string'); } - if( null !== $expectedFactoryMethod && null === $expectedFactoryClass ) { + if (null !== $expectedFactoryMethod && null === $expectedFactoryClass) { throw new \InvalidArgumentException('When argument $expectedFactoryMethod is set, must inform $expectedFactoryClass'); } - if( null !== $expectedFactoryMethod && !is_string($expectedFactoryMethod ) ) { + if (null !== $expectedFactoryMethod && !is_string($expectedFactoryMethod)) { throw new \InvalidArgumentException('The $expectedFactoryMethod argument should be a string'); } @@ -41,10 +41,11 @@ public function __construct($serviceId, $expectedFactoryClass = null, $expectedF public function toString() { - if( null === $this->expectedFactoryClass ) - return sprintf( '"%s" has factory', $this->serviceId ); + if (null === $this->expectedFactoryClass) { + return sprintf('"%s" has factory', $this->serviceId); + } - return sprintf( '"%s" has factory "@%s:%s"', $this->serviceId, $this->expectedFactoryClass, $this->expectedFactoryMethod ); + return sprintf('"%s" has factory "@%s:%s"', $this->serviceId, $this->expectedFactoryClass, $this->expectedFactoryMethod); } public function evaluate($other, $description = '', $returnResult = false) @@ -96,7 +97,7 @@ private function evaluateFactory(ContainerBuilder $containerBuilder, $returnResu $factory = $this->getFactoryData($definition); - if( !is_array( $factory ) ) { + if (!is_array($factory)) { if ($returnResult) { return false; } @@ -105,7 +106,8 @@ private function evaluateFactory(ContainerBuilder $containerBuilder, $returnResu $containerBuilder, sprintf( 'The container builder has service "%s" with not "%s" factory', - $this->serviceId, $this->expectedFactoryClass + $this->serviceId, + $this->expectedFactoryClass ) ); } @@ -118,13 +120,13 @@ private function evaluateFactoryClass(ContainerBuilder $containerBuilder, $retur /** @var Definition */ $definition = $containerBuilder->getDefinition($this->serviceId); - $factory = $this->getFactoryData( $definition ); + $factory = $this->getFactoryData($definition); - list( $factoryDefinition, $factoryMethod ) = $factory; + list($factoryDefinition, $factoryMethod) = $factory; - if( $factoryDefinition instanceof Reference ) { + if ($factoryDefinition instanceof Reference) { $factoryClass = (string)$factoryDefinition; - } else if( is_string( $factoryDefinition ) ) { + } elseif (is_string($factoryDefinition)) { $factoryClass = $factoryDefinition; } else { if ($returnResult) { @@ -135,13 +137,14 @@ private function evaluateFactoryClass(ContainerBuilder $containerBuilder, $retur $containerBuilder, sprintf( 'The container builder has service "%s" with not service "%s" factory', - $this->serviceId, $this->expectedFactoryClass + $this->serviceId, + $this->expectedFactoryClass ) ); } $constraint = new IsEqual($this->expectedFactoryClass); - if( !$constraint->evaluate( $factoryClass, '', true ) ) { + if (!$constraint->evaluate($factoryClass, '', true)) { if ($returnResult) { return false; } @@ -150,14 +153,15 @@ private function evaluateFactoryClass(ContainerBuilder $containerBuilder, $retur $containerBuilder, sprintf( 'The container builder has service "%s" with not service class "%s" factory', - $this->serviceId, $this->expectedFactoryClass + $this->serviceId, + $this->expectedFactoryClass ) ); } - if( $this->expectedFactoryMethod ) { + if ($this->expectedFactoryMethod) { $constraint = new IsEqual($this->expectedFactoryMethod); - if( !$constraint->evaluate( $factoryMethod, '', true ) ) { + if (!$constraint->evaluate($factoryMethod, '', true)) { if ($returnResult) { return false; } @@ -166,7 +170,8 @@ private function evaluateFactoryClass(ContainerBuilder $containerBuilder, $retur $containerBuilder, sprintf( 'The container builder has service "%s" with not service class method "%s::%s" factory', - $this->serviceId, $this->expectedFactoryClass, + $this->serviceId, + $this->expectedFactoryClass, $this->expectedFactoryMethod ) ); @@ -176,30 +181,34 @@ private function evaluateFactoryClass(ContainerBuilder $containerBuilder, $retur return true; } - private function getFactoryData( Definition $definition ) + private function getFactoryData(Definition $definition) { - if( self::isLegacySymfonyDI() ) { + if (self::isLegacySymfonyDI()) { $factoryService = $definition->getFactoryService(); $factoryMethod = $definition->getFactoryMethod(); $factoryClass = $definition->getFactoryClass(); - if( !$factoryService && !$factoryClass ) + if (!$factoryService && !$factoryClass) { return null; + } return array( $factoryClass ? $factoryClass : $factoryService, $factoryMethod ); } else { $factory = $definition->getFactory(); - if( is_array( $factory ) ) return $factory; + if (is_array($factory)) { + return $factory; + } - if( is_string( $factory ) && false !== strpos( $factory, ':' ) ) - return preg_split( '/:/', $factory, 2 ); + if (is_string($factory) && false !== strpos($factory, ':')) { + return preg_split('/:/', $factory, 2); + } return $factory; } } - static public function isLegacySymfonyDI() + public static function isLegacySymfonyDI() { - return method_exists( Definition::class, 'getFactoryService' ); + return method_exists(Definition::class, 'getFactoryService'); } } diff --git a/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php b/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php index 08cc343..5ca99d4 100644 --- a/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php +++ b/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php @@ -20,7 +20,7 @@ public function load(array $config, ContainerBuilder $container) $loader->load('services.xml'); // load factory services definitions - if( ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI() ) { + if (ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI()) { $loader->load('services-factory-legacy.xml'); } else { $loader->load('services-factory.xml'); @@ -47,18 +47,18 @@ public function load(array $config, ContainerBuilder $container) $container->setAlias('manual_alias', 'service_id'); // add an factory service - $container->register( 'manual_factory_service', new Definition() ); + $container->register('manual_factory_service', new Definition()); - if( ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI() ) { + if (ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI()) { $container - ->register( 'manual_created_by_factory_service', new Definition() ) + ->register('manual_created_by_factory_service', new Definition()) ->setFactoryService(new Reference('manual_factory_service')) ->setFactoryMethod('factoryMethod'); - ; + ; } else { $container - ->register( 'manual_created_by_factory_service', new Definition() ) - ->setFactory( 'manual_factory_service:factoryMethod' ) + ->register('manual_created_by_factory_service', new Definition()) + ->setFactory('manual_factory_service:factoryMethod') ; } } diff --git a/Tests/PhpUnit/AbstractExtensionTestCaseTest.php b/Tests/PhpUnit/AbstractExtensionTestCaseTest.php index fd2db65..75a29c5 100644 --- a/Tests/PhpUnit/AbstractExtensionTestCaseTest.php +++ b/Tests/PhpUnit/AbstractExtensionTestCaseTest.php @@ -58,13 +58,15 @@ public function if_load_is_successful_it_does_not_fail() $this->assertContainerBuilderHasCreatedByFactoryService( 'manual_created_by_factory_service', 'manual_factory_service', - 'factoryMethod' ); + 'factoryMethod' + ); - if( !ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI() ) { + if (!ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI()) { $this->assertContainerBuilderHasCreatedByFactoryService( 'created_with_factory_with_old_syntax', 'factory_service', - 'factoryMethod' ); + 'factoryMethod' + ); } } diff --git a/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php b/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php index f9828e4..6f7b0b9 100644 --- a/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php +++ b/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php @@ -34,16 +34,16 @@ public function containerBuilderProvider() $builderWithFactory = new ContainerBuilder(); $factoryReference = new Reference($factoryClass); - if( ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI() ) { - $builderWithFactory->register( $rightServiceId) + if (ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI()) { + $builderWithFactory->register($rightServiceId) ->setFactoryService($factoryReference) ->setFactoryMethod($factoryMethod); } else { - $builderWithFactory->register( $rightServiceId) + $builderWithFactory->register($rightServiceId) ->setFactory([$factoryReference,$factoryMethod]); } - $builderWithFactory->register( $wrongServiceId ); + $builderWithFactory->register($wrongServiceId); return array( // the container does not have the service From a84f7132a503394306b99ed15ffb4ed8720cb3ff Mon Sep 17 00:00:00 2001 From: Silvio Date: Sat, 18 Nov 2017 21:43:13 -0300 Subject: [PATCH 07/14] Factory test --- PhpUnit/AbstractContainerBuilderTestCase.php | 15 ++ .../ContainerBuilderHasFactoryConstraint.php | 178 ++++++++++++++++++ README.md | 4 + ...tthiasDependencyInjectionTestExtension.php | 10 + Tests/Fixtures/services.xml | 7 + .../PhpUnit/AbstractExtensionTestCaseTest.php | 9 + ...ntainerBuilderHasFactoryConstraintTest.php | 126 +++++++++++++ 7 files changed, 349 insertions(+) create mode 100644 PhpUnit/ContainerBuilderHasFactoryConstraint.php create mode 100644 Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php diff --git a/PhpUnit/AbstractContainerBuilderTestCase.php b/PhpUnit/AbstractContainerBuilderTestCase.php index e1cc475..4eff3bd 100644 --- a/PhpUnit/AbstractContainerBuilderTestCase.php +++ b/PhpUnit/AbstractContainerBuilderTestCase.php @@ -129,6 +129,21 @@ protected function assertContainerBuilderHasAlias($aliasId, $expectedServiceId = ); } + /** + * Assert that the ContainerBuilder for this test has an service wich is created by other service + * + * @param $serivceId + * @param $expectedFactoryClass + * @param $expectedFactoryMethod + */ + protected function assertContainerBuilderHasCreatedByFactoryService($serviceId, $expectedFactoryClass = null, $expectedFactoryMethod) + { + 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. * diff --git a/PhpUnit/ContainerBuilderHasFactoryConstraint.php b/PhpUnit/ContainerBuilderHasFactoryConstraint.php new file mode 100644 index 0000000..879d262 --- /dev/null +++ b/PhpUnit/ContainerBuilderHasFactoryConstraint.php @@ -0,0 +1,178 @@ +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 = $definition->getFactory(); + + 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; + + 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; + } + +} diff --git a/README.md b/README.md index c089a5f..78d0a4e 100644 --- a/README.md +++ b/README.md @@ -301,6 +301,10 @@ These are the available semantic assertions for each of the test cases shown abo
Assert that the ContainerBuilder for this test has an alias.
assertContainerBuilderHasAlias($aliasId, $expectedServiceId)
Assert that the ContainerBuilder for this test has an alias and that it is an alias for the given service id.
+
assertContainerBuilderHasCreatedByFactoryService($serviceId)
+
Assert that the ContainerBuilder for this test has an service with factory.
+
assertContainerBuilderHasCreatedByFactoryService($serviceId,$factoryService, $factoryMethod)
+
Assert that the ContainerBuilder for this test has an service with factory for given factory service and method.
assertContainerBuilderHasParameter($parameterName)
Assert that the ContainerBuilder for this test has a parameter.
assertContainerBuilderHasParameter($parameterName, $expectedParameterValue)
diff --git a/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php b/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php index 14da294..4b79c69 100644 --- a/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php +++ b/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php @@ -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; @@ -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() diff --git a/Tests/Fixtures/services.xml b/Tests/Fixtures/services.xml index 9abc74b..4133658 100644 --- a/Tests/Fixtures/services.xml +++ b/Tests/Fixtures/services.xml @@ -26,5 +26,12 @@ + + + + + + + diff --git a/Tests/PhpUnit/AbstractExtensionTestCaseTest.php b/Tests/PhpUnit/AbstractExtensionTestCaseTest.php index b39aa61..9369252 100644 --- a/Tests/PhpUnit/AbstractExtensionTestCaseTest.php +++ b/Tests/PhpUnit/AbstractExtensionTestCaseTest.php @@ -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. @@ -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' ); } /** diff --git a/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php b/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php new file mode 100644 index 0000000..0143b97 --- /dev/null +++ b/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php @@ -0,0 +1,126 @@ +assertSame($shouldMatch, $constraint->evaluate($containerBuilder, null, true)); + } + + public function containerBuilderProvider() + { + $rightServiceId = 'some_service_id'; + $wrongServiceId = 'other_service_id'; + $factoryClass = 'SomeFactoryClass'; + $invalidFactoryClass = 'InvalidFactoryClass'; + $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'); + } +} From c138ec9a3897ab996ec84c86b69f3849c6b56db0 Mon Sep 17 00:00:00 2001 From: Silvio Date: Sat, 18 Nov 2017 22:00:38 -0300 Subject: [PATCH 08/14] Fix typo --- PhpUnit/AbstractContainerBuilderTestCase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PhpUnit/AbstractContainerBuilderTestCase.php b/PhpUnit/AbstractContainerBuilderTestCase.php index 4eff3bd..f9355bb 100644 --- a/PhpUnit/AbstractContainerBuilderTestCase.php +++ b/PhpUnit/AbstractContainerBuilderTestCase.php @@ -130,7 +130,7 @@ protected function assertContainerBuilderHasAlias($aliasId, $expectedServiceId = } /** - * Assert that the ContainerBuilder for this test has an service wich is created by other service + * Assert that the ContainerBuilder for this test has a service which is created by other service * * @param $serivceId * @param $expectedFactoryClass From 268b680f9ec0a4dce0c27572a9087319caf49c31 Mon Sep 17 00:00:00 2001 From: Silvio Date: Sat, 18 Nov 2017 22:25:53 -0300 Subject: [PATCH 09/14] BC for SYMFONY_DI_VERSION=2.7|2.8|3.2 --- Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php b/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php index 0143b97..2429407 100644 --- a/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php +++ b/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php @@ -24,8 +24,8 @@ public function containerBuilderProvider() { $rightServiceId = 'some_service_id'; $wrongServiceId = 'other_service_id'; - $factoryClass = 'SomeFactoryClass'; - $invalidFactoryClass = 'InvalidFactoryClass'; + $factoryClass = 'factory_class_service'; + $invalidFactoryClass = 'invalid_class_service'; $factoryMethod = 'someMethod'; $invalidFactoryMethod = 'invalidMethod'; From 9802ce73a8164fd224c23d4997ca3b3f00933fe9 Mon Sep 17 00:00:00 2001 From: Silvio Date: Mon, 20 Nov 2017 19:31:58 -0300 Subject: [PATCH 10/14] Backward compatibility - Check for <2.6 symfony/dependency-injection version - Test old yml syntax format --- .../ContainerBuilderHasFactoryConstraint.php | 31 ++++++++++++++++-- ...tthiasDependencyInjectionTestExtension.php | 32 +++++++++++++++---- Tests/Fixtures/services-factory-legacy.xml | 12 +++++++ .../Fixtures/services-factory-old-syntax.yml | 3 ++ Tests/Fixtures/services-factory.xml | 13 ++++++++ Tests/Fixtures/services.xml | 6 ---- .../PhpUnit/AbstractExtensionTestCaseTest.php | 13 ++++++-- ...ntainerBuilderHasFactoryConstraintTest.php | 15 ++++++--- 8 files changed, 104 insertions(+), 21 deletions(-) create mode 100644 Tests/Fixtures/services-factory-legacy.xml create mode 100644 Tests/Fixtures/services-factory-old-syntax.yml create mode 100644 Tests/Fixtures/services-factory.xml diff --git a/PhpUnit/ContainerBuilderHasFactoryConstraint.php b/PhpUnit/ContainerBuilderHasFactoryConstraint.php index 879d262..84a8697 100644 --- a/PhpUnit/ContainerBuilderHasFactoryConstraint.php +++ b/PhpUnit/ContainerBuilderHasFactoryConstraint.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\Constraint\IsEqual; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\DependencyInjection\Definition; class ContainerBuilderHasFactoryConstraint extends Constraint { @@ -93,7 +94,7 @@ private function evaluateFactory(ContainerBuilder $containerBuilder, $returnResu /** @var Definition */ $definition = $containerBuilder->getDefinition($this->serviceId); - $factory = $definition->getFactory(); + $factory = $this->getFactoryData($definition); if( !is_array( $factory ) ) { if ($returnResult) { @@ -117,7 +118,7 @@ private function evaluateFactoryClass(ContainerBuilder $containerBuilder, $retur /** @var Definition */ $definition = $containerBuilder->getDefinition($this->serviceId); - $factory = $definition->getFactory(); + $factory = $this->getFactoryData( $definition ); list( $factoryDefinition, $factoryMethod ) = $factory; @@ -175,4 +176,30 @@ private function evaluateFactoryClass(ContainerBuilder $containerBuilder, $retur 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; + } + } + + + static public function isLegacySymfonyDI() + { + return method_exists( new Definition(), 'getFactoryService' ); + } } diff --git a/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php b/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php index 4b79c69..08cc343 100644 --- a/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php +++ b/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php @@ -7,7 +7,9 @@ use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; +use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; use Symfony\Component\Config\FileLocator; +use Matthias\SymfonyDependencyInjectionTest\PhpUnit\ContainerBuilderHasFactoryConstraint; class MatthiasDependencyInjectionTestExtension implements ExtensionInterface { @@ -17,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'); @@ -34,13 +47,20 @@ public function load(array $config, ContainerBuilder $container) $container->setAlias('manual_alias', 'service_id'); // add an factory service - $container - ->register( 'manual_factory_service', new Definition() ); + $container->register( 'manual_factory_service', new Definition() ); - $container - ->register( 'manual_created_by_factory_service', new Definition() ) - ->setFactory( [new Reference('manual_factory_service'), 'factoryMethod'] ) - ; + if( ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI() ) { + $container + ->register( 'manual_created_by_factory_service', new Definition() ) + ->setFactoryService(new Reference('manual_factory_service')) + ->setFactoryMethod('factoryMethod'); + ; + } else { + $container + ->register( 'manual_created_by_factory_service', new Definition() ) + ->setFactory( 'manual_factory_service:factoryMethod' ) + ; + } } public function getAlias() diff --git a/Tests/Fixtures/services-factory-legacy.xml b/Tests/Fixtures/services-factory-legacy.xml new file mode 100644 index 0000000..736cb9e --- /dev/null +++ b/Tests/Fixtures/services-factory-legacy.xml @@ -0,0 +1,12 @@ + + + + + + + + + + diff --git a/Tests/Fixtures/services-factory-old-syntax.yml b/Tests/Fixtures/services-factory-old-syntax.yml new file mode 100644 index 0000000..7544846 --- /dev/null +++ b/Tests/Fixtures/services-factory-old-syntax.yml @@ -0,0 +1,3 @@ +services: + created_with_factory_with_old_syntax: + factory: ['@factory_service', 'factoryMethod'] diff --git a/Tests/Fixtures/services-factory.xml b/Tests/Fixtures/services-factory.xml new file mode 100644 index 0000000..5037246 --- /dev/null +++ b/Tests/Fixtures/services-factory.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + diff --git a/Tests/Fixtures/services.xml b/Tests/Fixtures/services.xml index 4133658..84a5ce4 100644 --- a/Tests/Fixtures/services.xml +++ b/Tests/Fixtures/services.xml @@ -27,11 +27,5 @@ - - - - - - diff --git a/Tests/PhpUnit/AbstractExtensionTestCaseTest.php b/Tests/PhpUnit/AbstractExtensionTestCaseTest.php index 9369252..fd2db65 100644 --- a/Tests/PhpUnit/AbstractExtensionTestCaseTest.php +++ b/Tests/PhpUnit/AbstractExtensionTestCaseTest.php @@ -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; @@ -28,8 +29,8 @@ 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'); + // defined in services-factory.xml + $this->assertContainerBuilderHasCreatedByFactoryService('created_by_factory_service', 'factory_service', 'factoryMethod'); // manually defined parameter $this->assertContainerBuilderHasParameter('manual_parameter', 'parameter value'); @@ -58,8 +59,16 @@ public function if_load_is_successful_it_does_not_fail() 'manual_created_by_factory_service', 'manual_factory_service', 'factoryMethod' ); + + if( !ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI() ) { + $this->assertContainerBuilderHasCreatedByFactoryService( + 'created_with_factory_with_old_syntax', + 'factory_service', + 'factoryMethod' ); + } } + /** * @test */ diff --git a/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php b/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php index 2429407..f9828e4 100644 --- a/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php +++ b/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php @@ -33,12 +33,17 @@ public function containerBuilderProvider() $builderWithFactory = new ContainerBuilder(); $factoryReference = new Reference($factoryClass); - $builderWithFactory->register( $rightServiceId) - ->setFactory([$factoryReference,$factoryMethod]); - $builderWithFactory->register( $wrongServiceId ); - return [ - array($builderWithFactory, $rightServiceId, $factoryClass, $factoryMethod, true ), ]; + if( ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI() ) { + $builderWithFactory->register( $rightServiceId) + ->setFactoryService($factoryReference) + ->setFactoryMethod($factoryMethod); + } else { + $builderWithFactory->register( $rightServiceId) + ->setFactory([$factoryReference,$factoryMethod]); + } + + $builderWithFactory->register( $wrongServiceId ); return array( // the container does not have the service From 6d228af0e4c47942fdaa356ead3150246ff8db2b Mon Sep 17 00:00:00 2001 From: Silvio Date: Tue, 21 Nov 2017 09:14:47 -0300 Subject: [PATCH 11/14] Optimize, don't create an instance --- PhpUnit/ContainerBuilderHasFactoryConstraint.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PhpUnit/ContainerBuilderHasFactoryConstraint.php b/PhpUnit/ContainerBuilderHasFactoryConstraint.php index 84a8697..17d7dcf 100644 --- a/PhpUnit/ContainerBuilderHasFactoryConstraint.php +++ b/PhpUnit/ContainerBuilderHasFactoryConstraint.php @@ -200,6 +200,6 @@ private function getFactoryData( Definition $definition ) static public function isLegacySymfonyDI() { - return method_exists( new Definition(), 'getFactoryService' ); + return method_exists( Definition::class, 'getFactoryService' ); } } From 569d9af691d135cba374cadda1ecd25b83abcabc Mon Sep 17 00:00:00 2001 From: Silvio Date: Sat, 2 Dec 2017 10:37:03 -0300 Subject: [PATCH 12/14] Fix a lot of CS breaks --- .../ContainerBuilderHasFactoryConstraint.php | 59 +++++++++++-------- ...tthiasDependencyInjectionTestExtension.php | 14 ++--- .../PhpUnit/AbstractExtensionTestCaseTest.php | 8 ++- ...ntainerBuilderHasFactoryConstraintTest.php | 8 +-- 4 files changed, 50 insertions(+), 39 deletions(-) diff --git a/PhpUnit/ContainerBuilderHasFactoryConstraint.php b/PhpUnit/ContainerBuilderHasFactoryConstraint.php index 17d7dcf..3fe1982 100644 --- a/PhpUnit/ContainerBuilderHasFactoryConstraint.php +++ b/PhpUnit/ContainerBuilderHasFactoryConstraint.php @@ -26,11 +26,11 @@ public function __construct($serviceId, $expectedFactoryClass = null, $expectedF throw new \InvalidArgumentException('The $expectedFactoryClass argument should be a string'); } - if( null !== $expectedFactoryMethod && null === $expectedFactoryClass ) { + if (null !== $expectedFactoryMethod && null === $expectedFactoryClass) { throw new \InvalidArgumentException('When argument $expectedFactoryMethod is set, must inform $expectedFactoryClass'); } - if( null !== $expectedFactoryMethod && !is_string($expectedFactoryMethod ) ) { + if (null !== $expectedFactoryMethod && !is_string($expectedFactoryMethod)) { throw new \InvalidArgumentException('The $expectedFactoryMethod argument should be a string'); } @@ -41,10 +41,11 @@ public function __construct($serviceId, $expectedFactoryClass = null, $expectedF public function toString() { - if( null === $this->expectedFactoryClass ) - return sprintf( '"%s" has factory', $this->serviceId ); + if (null === $this->expectedFactoryClass) { + return sprintf('"%s" has factory', $this->serviceId); + } - return sprintf( '"%s" has factory "@%s:%s"', $this->serviceId, $this->expectedFactoryClass, $this->expectedFactoryMethod ); + return sprintf('"%s" has factory "@%s:%s"', $this->serviceId, $this->expectedFactoryClass, $this->expectedFactoryMethod); } public function evaluate($other, $description = '', $returnResult = false) @@ -96,7 +97,7 @@ private function evaluateFactory(ContainerBuilder $containerBuilder, $returnResu $factory = $this->getFactoryData($definition); - if( !is_array( $factory ) ) { + if (!is_array($factory)) { if ($returnResult) { return false; } @@ -105,7 +106,8 @@ private function evaluateFactory(ContainerBuilder $containerBuilder, $returnResu $containerBuilder, sprintf( 'The container builder has service "%s" with not "%s" factory', - $this->serviceId, $this->expectedFactoryClass + $this->serviceId, + $this->expectedFactoryClass ) ); } @@ -118,13 +120,13 @@ private function evaluateFactoryClass(ContainerBuilder $containerBuilder, $retur /** @var Definition */ $definition = $containerBuilder->getDefinition($this->serviceId); - $factory = $this->getFactoryData( $definition ); + $factory = $this->getFactoryData($definition); - list( $factoryDefinition, $factoryMethod ) = $factory; + list($factoryDefinition, $factoryMethod) = $factory; - if( $factoryDefinition instanceof Reference ) { + if ($factoryDefinition instanceof Reference) { $factoryClass = (string)$factoryDefinition; - } else if( is_string( $factoryDefinition ) ) { + } elseif (is_string($factoryDefinition)) { $factoryClass = $factoryDefinition; } else { if ($returnResult) { @@ -135,13 +137,14 @@ private function evaluateFactoryClass(ContainerBuilder $containerBuilder, $retur $containerBuilder, sprintf( 'The container builder has service "%s" with not service "%s" factory', - $this->serviceId, $this->expectedFactoryClass + $this->serviceId, + $this->expectedFactoryClass ) ); } $constraint = new IsEqual($this->expectedFactoryClass); - if( !$constraint->evaluate( $factoryClass, '', true ) ) { + if (!$constraint->evaluate($factoryClass, '', true)) { if ($returnResult) { return false; } @@ -150,14 +153,15 @@ private function evaluateFactoryClass(ContainerBuilder $containerBuilder, $retur $containerBuilder, sprintf( 'The container builder has service "%s" with not service class "%s" factory', - $this->serviceId, $this->expectedFactoryClass + $this->serviceId, + $this->expectedFactoryClass ) ); } - if( $this->expectedFactoryMethod ) { + if ($this->expectedFactoryMethod) { $constraint = new IsEqual($this->expectedFactoryMethod); - if( !$constraint->evaluate( $factoryMethod, '', true ) ) { + if (!$constraint->evaluate($factoryMethod, '', true)) { if ($returnResult) { return false; } @@ -166,7 +170,8 @@ private function evaluateFactoryClass(ContainerBuilder $containerBuilder, $retur $containerBuilder, sprintf( 'The container builder has service "%s" with not service class method "%s::%s" factory', - $this->serviceId, $this->expectedFactoryClass, + $this->serviceId, + $this->expectedFactoryClass, $this->expectedFactoryMethod ) ); @@ -176,30 +181,34 @@ private function evaluateFactoryClass(ContainerBuilder $containerBuilder, $retur return true; } - private function getFactoryData( Definition $definition ) + private function getFactoryData(Definition $definition) { - if( self::isLegacySymfonyDI() ) { + if (self::isLegacySymfonyDI()) { $factoryService = $definition->getFactoryService(); $factoryMethod = $definition->getFactoryMethod(); $factoryClass = $definition->getFactoryClass(); - if( !$factoryService && !$factoryClass ) + if (!$factoryService && !$factoryClass) { return null; + } return array( $factoryClass ? $factoryClass : $factoryService, $factoryMethod ); } else { $factory = $definition->getFactory(); - if( is_array( $factory ) ) return $factory; + if (is_array($factory)) { + return $factory; + } - if( is_string( $factory ) && false !== strpos( $factory, ':' ) ) - return preg_split( '/:/', $factory, 2 ); + if (is_string($factory) && false !== strpos($factory, ':')) { + return preg_split('/:/', $factory, 2); + } return $factory; } } - static public function isLegacySymfonyDI() + public static function isLegacySymfonyDI() { - return method_exists( Definition::class, 'getFactoryService' ); + return method_exists(Definition::class, 'getFactoryService'); } } diff --git a/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php b/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php index 08cc343..5ca99d4 100644 --- a/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php +++ b/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php @@ -20,7 +20,7 @@ public function load(array $config, ContainerBuilder $container) $loader->load('services.xml'); // load factory services definitions - if( ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI() ) { + if (ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI()) { $loader->load('services-factory-legacy.xml'); } else { $loader->load('services-factory.xml'); @@ -47,18 +47,18 @@ public function load(array $config, ContainerBuilder $container) $container->setAlias('manual_alias', 'service_id'); // add an factory service - $container->register( 'manual_factory_service', new Definition() ); + $container->register('manual_factory_service', new Definition()); - if( ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI() ) { + if (ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI()) { $container - ->register( 'manual_created_by_factory_service', new Definition() ) + ->register('manual_created_by_factory_service', new Definition()) ->setFactoryService(new Reference('manual_factory_service')) ->setFactoryMethod('factoryMethod'); - ; + ; } else { $container - ->register( 'manual_created_by_factory_service', new Definition() ) - ->setFactory( 'manual_factory_service:factoryMethod' ) + ->register('manual_created_by_factory_service', new Definition()) + ->setFactory('manual_factory_service:factoryMethod') ; } } diff --git a/Tests/PhpUnit/AbstractExtensionTestCaseTest.php b/Tests/PhpUnit/AbstractExtensionTestCaseTest.php index fd2db65..75a29c5 100644 --- a/Tests/PhpUnit/AbstractExtensionTestCaseTest.php +++ b/Tests/PhpUnit/AbstractExtensionTestCaseTest.php @@ -58,13 +58,15 @@ public function if_load_is_successful_it_does_not_fail() $this->assertContainerBuilderHasCreatedByFactoryService( 'manual_created_by_factory_service', 'manual_factory_service', - 'factoryMethod' ); + 'factoryMethod' + ); - if( !ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI() ) { + if (!ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI()) { $this->assertContainerBuilderHasCreatedByFactoryService( 'created_with_factory_with_old_syntax', 'factory_service', - 'factoryMethod' ); + 'factoryMethod' + ); } } diff --git a/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php b/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php index f9828e4..6f7b0b9 100644 --- a/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php +++ b/Tests/PhpUnit/ContainerBuilderHasFactoryConstraintTest.php @@ -34,16 +34,16 @@ public function containerBuilderProvider() $builderWithFactory = new ContainerBuilder(); $factoryReference = new Reference($factoryClass); - if( ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI() ) { - $builderWithFactory->register( $rightServiceId) + if (ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI()) { + $builderWithFactory->register($rightServiceId) ->setFactoryService($factoryReference) ->setFactoryMethod($factoryMethod); } else { - $builderWithFactory->register( $rightServiceId) + $builderWithFactory->register($rightServiceId) ->setFactory([$factoryReference,$factoryMethod]); } - $builderWithFactory->register( $wrongServiceId ); + $builderWithFactory->register($wrongServiceId); return array( // the container does not have the service From 1c7137096d999d8e59b451f96d7a8bb82f4d04e8 Mon Sep 17 00:00:00 2001 From: Silvio Date: Sat, 2 Dec 2017 11:47:47 -0300 Subject: [PATCH 13/14] Changes legacy factory service interface to avoid deprecation messages --- PhpUnit/ContainerBuilderHasFactoryConstraint.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/PhpUnit/ContainerBuilderHasFactoryConstraint.php b/PhpUnit/ContainerBuilderHasFactoryConstraint.php index 3fe1982..3432da3 100644 --- a/PhpUnit/ContainerBuilderHasFactoryConstraint.php +++ b/PhpUnit/ContainerBuilderHasFactoryConstraint.php @@ -209,6 +209,7 @@ private function getFactoryData(Definition $definition) public static function isLegacySymfonyDI() { - return method_exists(Definition::class, 'getFactoryService'); + return !method_exists(Definition::class, 'getFactory') && + method_exists(Definition::class, 'getFactoryService'); } } From 3fb9b74bbbe6496e2a4d1db3fa90c735ee3cecd4 Mon Sep 17 00:00:00 2001 From: Silvio Date: Sat, 2 Dec 2017 11:49:38 -0300 Subject: [PATCH 14/14] Fix service creation --- Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php b/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php index 5ca99d4..f7cbb34 100644 --- a/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php +++ b/Tests/Fixtures/MatthiasDependencyInjectionTestExtension.php @@ -47,17 +47,17 @@ public function load(array $config, ContainerBuilder $container) $container->setAlias('manual_alias', 'service_id'); // add an factory service - $container->register('manual_factory_service', new Definition()); + $container->register('manual_factory_service', 'stdClass'); if (ContainerBuilderHasFactoryConstraint::isLegacySymfonyDI()) { $container - ->register('manual_created_by_factory_service', new Definition()) + ->register('manual_created_by_factory_service', 'stdClass') ->setFactoryService(new Reference('manual_factory_service')) ->setFactoryMethod('factoryMethod'); ; } else { $container - ->register('manual_created_by_factory_service', new Definition()) + ->register('manual_created_by_factory_service', 'stdClass') ->setFactory('manual_factory_service:factoryMethod') ; }