-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathContainerBuilderHasFactoryConstraintTest.php
More file actions
126 lines (101 loc) · 4.41 KB
/
ContainerBuilderHasFactoryConstraintTest.php
File metadata and controls
126 lines (101 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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');
}
}