1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fesor\RequestObject\Tests\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Fesor\RequestObject\DependeyInjection\CollectMappersCompilePass; |
6
|
|
|
use Fesor\RequestObject\DependeyInjection\RequestObjectExtension; |
7
|
|
|
use Fesor\RequestObject\Tests\Fixtures\ExampleRequestMapper; |
8
|
|
|
use Fesor\RequestObject\Tests\Fixtures\ExampleRequestMapperV2; |
9
|
|
|
use Fesor\RequestObject\Tests\Fixtures\ExampleRequestObject; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
12
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
13
|
|
|
|
14
|
|
|
class CollectMappersCompilePassTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var CollectMappersCompilePass |
18
|
|
|
*/ |
19
|
|
|
private $compilePass; |
20
|
|
|
private $containerMock; |
21
|
|
|
private $definitionMock; |
22
|
|
|
|
23
|
|
|
protected function setUp() |
24
|
|
|
{ |
25
|
|
|
$this->containerMock = $this->prophesize(ContainerBuilder::class); |
26
|
|
|
$this->definitionMock = $this->prophesize(Definition::class); |
27
|
|
|
|
28
|
|
|
$this->containerMock |
29
|
|
|
->getDefinition(RequestObjectExtension::REQUEST_MAPPER_ID) |
30
|
|
|
->willReturn($this->definitionMock->reveal()); |
31
|
|
|
|
32
|
|
|
$this->compilePass = new CollectMappersCompilePass(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testRegisterBindings() |
36
|
|
|
{ |
37
|
|
|
$this->stubTaggedServices([ |
38
|
|
|
'test.mapper' => [ExampleRequestMapper::class, ['priority' => 10]], |
39
|
|
|
'test.mapper.v2' => ExampleRequestMapperV2::class, |
40
|
|
|
]); |
41
|
|
|
|
42
|
|
|
$this->expectBindings([ |
43
|
|
|
ExampleRequestObject::class => [ |
44
|
|
|
['test.mapper.v2', 'exampleMapper'], |
45
|
|
|
['test.mapper', 'exampleMapper'], |
46
|
|
|
], |
47
|
|
|
]); |
48
|
|
|
|
49
|
|
|
$this->compilePass->process($this->containerMock->reveal()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function stubTaggedServices($services) |
53
|
|
|
{ |
54
|
|
|
$definitions = []; |
55
|
|
|
$tags = []; |
56
|
|
|
|
57
|
|
|
foreach ($services as $id => $service) { |
58
|
|
|
if (!is_array($service)) $service = [$service, []]; |
59
|
|
|
list($className, $tagAttributes) = $service; |
60
|
|
|
|
61
|
|
|
$tags[$id] = $tagAttributes; |
62
|
|
|
$definition = new Definition($className); |
63
|
|
|
$definition->addTag(RequestObjectExtension::MAPPER_TAG, $tagAttributes); |
64
|
|
|
$definitions[$id] = $definition; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->containerMock |
68
|
|
|
->findTaggedServiceIds(RequestObjectExtension::MAPPER_TAG) |
69
|
|
|
->willReturn($tags) |
70
|
|
|
->shouldBeCalled(); |
71
|
|
|
|
72
|
|
|
foreach ($definitions as $id => $definition) { |
73
|
|
|
$this->containerMock |
74
|
|
|
->getDefinition($id) |
75
|
|
|
->willReturn($definition); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function expectBindings(array $bindings) |
80
|
|
|
{ |
81
|
|
|
$this->definitionMock->addMethodCall('registerBindings', [$bindings])->shouldBeCalled(); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|