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

Factory service test #85

wants to merge 17 commits into from

Conversation

silvioq
Copy link
Contributor

@silvioq silvioq commented Nov 19, 2017

Hi, I hope this pull request was useful for you. I apologize for my basic English

  # test example ...
  $this->assertContainerBuilderHasCreatedByFactoryService(
      'target.service', 'factory.service', 'factoryMethod');

@silvioq
Copy link
Contributor Author

silvioq commented Nov 19, 2017

Sadly, factories aren't compatible with Symfony 2.3 ... Do you plan remove BC for that version?


$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.

- 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() ) {
Copy link
Contributor Author

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

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

Copy link
Member

@stloyd stloyd left a 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 )
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);
}


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

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.

if (!is_array($factory)) {


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

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.

return null;

return array( $factoryClass ? $factoryClass : $factoryService, $factoryMethod );
} else {
Copy link
Member

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.

@Nyholm
Copy link
Member

Nyholm commented Dec 2, 2017

Could you also rebase on master?

@silvioq
Copy link
Contributor Author

silvioq commented Dec 2, 2017

@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());
Copy link
Contributor Author

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.

@silvioq
Copy link
Contributor Author

silvioq commented Feb 17, 2018

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)
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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants