-
Notifications
You must be signed in to change notification settings - Fork 48
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
base: master
Are you sure you want to change the base?
Conversation
Sadly, factories aren't compatible with Symfony 2.3 ... Do you plan remove BC for that version? |
|
||
$factory = $definition->getFactory(); | ||
|
||
if( !is_array( $factory ) ) { |
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.
You could check if the Definition::getFactoryClass()
method exists to implement a pre Symfony 2.7 compatible solution.
- Check for <2.6 symfony/dependency-injection version - Test old yml syntax format
@@ -175,4 +176,30 @@ private function evaluateFactoryClass(ContainerBuilder $containerBuilder, $retur | |||
return true; | |||
} | |||
|
|||
private function getFactoryData( Definition $definition ) | |||
{ | |||
if( self::isLegacySymfonyDI() ) { |
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.
This is a dirty, but working solution ... I checked with DI 2.3, 3.0 and 3.3 and they working ... a better approach may be an abstract class with "getFactoryData" as abstract method, but this commit resolves <= 2.6 versions and variated factory syntax in yml.
A test case not cover is a service with "setFactoryClass" definition ... This is pending.
|
||
static public function isLegacySymfonyDI() | ||
{ | ||
return method_exists( new Definition(), 'getFactoryService' ); |
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.
no need to create an instance here, just using the class name works too
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.
CS must match the Symfony CS.
|
||
public function toString() | ||
{ | ||
if( null === $this->expectedFactoryClass ) |
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.
Please remove all spaces between sprintf()
& strings inside:
if (null === $this->expectedFactoryClass) {
return sprintf('"%s" has factory', $this->serviceId);
}
|
||
$factory = $this->getFactoryData($definition); | ||
|
||
if( !is_array( $factory ) ) { |
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.
if (!is_array($factory)) {
|
||
$factory = $this->getFactoryData( $definition ); | ||
|
||
list( $factoryDefinition, $factoryMethod ) = $factory; |
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.
Please remove all spaces in function calls. Same for those in if/else
etc.
return null; | ||
|
||
return array( $factoryClass ? $factoryClass : $factoryService, $factoryMethod ); | ||
} else { |
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.
No point for else
as you do return
in if
.
Could you also rebase on master? |
- Check for <2.6 symfony/dependency-injection version - Test old yml syntax format
@Nyholm I'm sorry ... I'm doing some bad. How to I must rebase my PR? |
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
@Nyholm I'm sorry again! The bug is here ... must be $container->register('manual_factory_service', 'stdClass');
I will fix and I will push again later.
Hi @stloyd In December 2017 I reviewed the Symfony CS unmatches. I forgot something ? |
* @param $expectedFactoryClass | ||
* @param $expectedFactoryMethod | ||
*/ | ||
protected function assertContainerBuilderHasCreatedByFactoryService($serviceId, $expectedFactoryClass = null, $expectedFactoryMethod) |
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
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 comment
The reason will be displayed to describe this comment to others. Learn more.
same here
Hi, I hope this pull request was useful for you. I apologize for my basic English