-
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?
Changes from 3 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,178 @@ | ||
<?php | ||
|
||
namespace Matthias\SymfonyDependencyInjectionTest\PhpUnit; | ||
|
||
use PHPUnit\Framework\Constraint\Constraint; | ||
use PHPUnit\Framework\Constraint\IsEqual; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
class ContainerBuilderHasFactoryConstraint extends Constraint | ||
{ | ||
private $serviceId; | ||
private $expectedFactoryClass; | ||
private $expectedFactoryMethod; | ||
|
||
public function __construct($serviceId, $expectedFactoryClass = null, $expectedFactoryMethod = null) | ||
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 ) | ||
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. Please remove all spaces between
|
||
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 ) ) { | ||
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. You could check if the 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.
|
||
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; | ||
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. Please remove all spaces in function calls. Same for those in |
||
|
||
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; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
namespace Matthias\SymfonyDependencyInjectionTest\Tests\PhpUnit; | ||
|
||
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\ContainerBuilderHasFactoryConstraint; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
class ContainerBuilderHasFactoryConstraintTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* @dataProvider containerBuilderProvider | ||
*/ | ||
public function match(ContainerBuilder $containerBuilder, $serviceId, $expectedFactoryClass, $expectedFactoryMethod, $shouldMatch) | ||
{ | ||
$constraint = new ContainerBuilderHasFactoryConstraint($serviceId, $expectedFactoryClass, $expectedFactoryMethod); | ||
|
||
$this->assertSame($shouldMatch, $constraint->evaluate($containerBuilder, null, true)); | ||
} | ||
|
||
public function containerBuilderProvider() | ||
{ | ||
$rightServiceId = 'some_service_id'; | ||
$wrongServiceId = 'other_service_id'; | ||
$factoryClass = 'factory_class_service'; | ||
$invalidFactoryClass = 'invalid_class_service'; | ||
$factoryMethod = 'someMethod'; | ||
$invalidFactoryMethod = 'invalidMethod'; | ||
|
||
$emptyContainerBuilder = new ContainerBuilder(); | ||
|
||
$builderWithFactory = new ContainerBuilder(); | ||
$factoryReference = new Reference($factoryClass); | ||
$builderWithFactory->register( $rightServiceId) | ||
->setFactory([$factoryReference,$factoryMethod]); | ||
$builderWithFactory->register( $wrongServiceId ); | ||
|
||
return [ | ||
array($builderWithFactory, $rightServiceId, $factoryClass, $factoryMethod, true ), ]; | ||
|
||
return array( | ||
// the container does not have the service | ||
array($emptyContainerBuilder, $rightServiceId, null, null, false), | ||
|
||
// the container has service created by factory | ||
array($builderWithFactory, $rightServiceId, null, null, true ), | ||
|
||
// the container has service, but they has not factory | ||
array($builderWithFactory, $wrongServiceId, null, null, false ), | ||
|
||
// the container has service created by factory, but factory is invalid | ||
array($builderWithFactory, $rightServiceId, $invalidFactoryClass, $factoryMethod, false ), | ||
|
||
// the container has service created by factory, but factory method is invalid | ||
array($builderWithFactory, $rightServiceId, $factoryClass, $invalidFactoryMethod, false ), | ||
|
||
// the container has service created by factory, and whole arguments are valid | ||
array($builderWithFactory, $rightServiceId, $factoryClass, $factoryMethod, true ), | ||
|
||
// the container has service created by factory, and whole arguments are valid | ||
array($builderWithFactory, $wrongServiceId, $factoryClass, $factoryMethod, false ), | ||
|
||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_has_a_string_representation() | ||
{ | ||
$serviceId = 'service_id'; | ||
$factoryClass = 'SomeFactoryClass'; | ||
$factoryMethod = 'someMethod'; | ||
$constraint = new ContainerBuilderHasFactoryConstraint($serviceId, $factoryClass, $factoryMethod); | ||
$this->assertSame( | ||
'"service_id" has factory "@SomeFactoryClass:someMethod"', | ||
$constraint->toString() | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_expects_a_string_for_service_id() | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('string'); | ||
|
||
new ContainerBuilderHasFactoryConstraint(new \stdClass(), 'service_id'); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_expects_a_string_for_factory_class() | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('string'); | ||
|
||
new ContainerBuilderHasFactoryConstraint('service_id', new \stdClass(), ''); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_expects_a_string_for_factory_method() | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('string'); | ||
|
||
new ContainerBuilderHasFactoryConstraint('service_id', 'FactoryClass', new \stdClass()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_expects_factory_class_be_informed() | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('inform'); | ||
|
||
new ContainerBuilderHasFactoryConstraint('service_id', null, 'factoryMethod'); | ||
} | ||
} |
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