-
Notifications
You must be signed in to change notification settings - Fork 48
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
base: master
Are you sure you want to change the base?
Changes from 14 commits
312de09
7aacdbb
e49fdd3
384d88f
4c7905e
16b4ece
323330e
a84f713
c138ec9
268b680
9802ce7
6d228af
569d9af
eb5bfec
1c71370
3fb9b74
5e84b5b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
<?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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No point for |
||
$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, 'getFactoryService'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,13 @@ | |
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; | ||
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; | ||
use Symfony\Component\Config\FileLocator; | ||
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\ContainerBuilderHasFactoryConstraint; | ||
|
||
class MatthiasDependencyInjectionTestExtension implements ExtensionInterface | ||
{ | ||
|
@@ -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'); | ||
|
||
|
@@ -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', new Definition()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Nyholm I'm sorry again! The bug is here ... must be
|
||
|
||
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() | ||
|
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> |
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'] |
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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,5 +26,6 @@ | |
|
||
<service id="synthetic_service" synthetic="true"> | ||
</service> | ||
|
||
</services> | ||
</container> |
There was a problem hiding this comment.
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