|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Webfactory\ShortcodeBundle\Tests\DependencyInjection\Compiler; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
9
|
|
|
use Thunder\Shortcode\HandlerContainer\HandlerContainer; |
|
10
|
|
|
use Webfactory\ShortcodeBundle\Controller\GuideController; |
|
11
|
|
|
use Webfactory\ShortcodeBundle\DependencyInjection\Compiler\ShortcodeCompilerPass; |
|
12
|
|
|
|
|
13
|
|
|
final class ShortcodeCompilerPassTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* System under test. |
|
17
|
|
|
* |
|
18
|
|
|
* @var ShortcodeCompilerPass |
|
19
|
|
|
*/ |
|
20
|
|
|
private $compilerPass; |
|
21
|
|
|
|
|
22
|
|
|
/** @var ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject */ |
|
23
|
|
|
private $containerBuilder; |
|
24
|
|
|
|
|
25
|
|
|
protected function setUp(): void |
|
26
|
|
|
{ |
|
27
|
|
|
$this->compilerPass = new ShortcodeCompilerPass(); |
|
28
|
|
|
$this->containerBuilder = $this->getMockBuilder(ContainerBuilder::class) |
|
|
|
|
|
|
29
|
|
|
->disableOriginalConstructor() |
|
30
|
|
|
->getMock(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** @test */ |
|
34
|
|
|
public function tagged_services_are_added_as_handlers_to_handler_container(): void |
|
35
|
|
|
{ |
|
36
|
|
|
$this->containerBuilder->expects($this->once()) |
|
|
|
|
|
|
37
|
|
|
->method('findTaggedServiceIds') |
|
38
|
|
|
->willReturn([ |
|
39
|
|
|
'service_id1' => [ |
|
40
|
|
|
['shortcode' => 'shortcode1'], |
|
41
|
|
|
], |
|
42
|
|
|
'service_id2' => [ |
|
43
|
|
|
['shortcode' => 'shortcode2'], |
|
44
|
|
|
], |
|
45
|
|
|
]); |
|
46
|
|
|
|
|
47
|
|
|
$mockedShortcodeHandlerContainer = $this->createMock(Definition::class); |
|
48
|
|
|
$mockedShortcodeHandlerContainer->expects($this->at(0)) |
|
49
|
|
|
->method('addMethodCall') |
|
50
|
|
|
->with('add', $this->callback(function (array $argument) { |
|
51
|
|
|
return 'shortcode1' === $argument[0] |
|
52
|
|
|
&& $argument[1] instanceof Reference; |
|
53
|
|
|
})); |
|
54
|
|
|
$mockedShortcodeHandlerContainer->expects($this->at(1)) |
|
55
|
|
|
->method('addMethodCall') |
|
56
|
|
|
->with('add', $this->callback(function ($argument) { |
|
57
|
|
|
return 'shortcode2' === $argument[0] |
|
58
|
|
|
&& $argument[1] instanceof Reference; |
|
59
|
|
|
})); |
|
60
|
|
|
|
|
61
|
|
|
$this->containerBuilder->expects($this->once()) |
|
62
|
|
|
->method('findDefinition') |
|
63
|
|
|
->with(HandlerContainer::class) |
|
64
|
|
|
->willReturn($mockedShortcodeHandlerContainer); |
|
65
|
|
|
|
|
66
|
|
|
$this->compilerPass->process($this->containerBuilder); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** @test */ |
|
70
|
|
|
public function no_tagged_services_do_no_harm(): void |
|
71
|
|
|
{ |
|
72
|
|
|
$this->containerBuilder->expects($this->once()) |
|
73
|
|
|
->method('findTaggedServiceIds') |
|
74
|
|
|
->willReturn([]); |
|
75
|
|
|
|
|
76
|
|
|
$this->compilerPass->process($this->containerBuilder); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** @test */ |
|
80
|
|
|
public function shortcode_guide_service_gets_configured_if_set(): void |
|
81
|
|
|
{ |
|
82
|
|
|
$this->containerBuilder->expects($this->once()) |
|
83
|
|
|
->method('findTaggedServiceIds') |
|
84
|
|
|
->willReturn([ |
|
85
|
|
|
'service_id1' => [ |
|
86
|
|
|
['shortcode' => 'shortcode1'], |
|
87
|
|
|
], |
|
88
|
|
|
'service_id2' => [ |
|
89
|
|
|
['shortcode' => 'shortcode2'], |
|
90
|
|
|
], |
|
91
|
|
|
]); |
|
92
|
|
|
|
|
93
|
|
|
$this->containerBuilder->expects($this->once()) |
|
94
|
|
|
->method('findDefinition') |
|
95
|
|
|
->with(HandlerContainer::class) |
|
96
|
|
|
->willReturn($this->createMock(Definition::class)); |
|
97
|
|
|
|
|
98
|
|
|
$this->containerBuilder->expects($this->once()) |
|
99
|
|
|
->method('has') |
|
100
|
|
|
->with(GuideController::class) |
|
101
|
|
|
->willReturn(true); |
|
102
|
|
|
|
|
103
|
|
|
$mockedShortcodeGuideServiceDefinition = $this->createMock(Definition::class); |
|
104
|
|
|
$mockedShortcodeGuideServiceDefinition->expects($this->once()) |
|
105
|
|
|
->method('setArgument') |
|
106
|
|
|
->with( |
|
107
|
|
|
0, |
|
108
|
|
|
[ |
|
109
|
|
|
['shortcode' => 'shortcode1'], |
|
110
|
|
|
['shortcode' => 'shortcode2'], |
|
111
|
|
|
] |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
$this->containerBuilder->expects($this->once()) |
|
115
|
|
|
->method('getDefinition') |
|
116
|
|
|
->with(GuideController::class) |
|
117
|
|
|
->willReturn($mockedShortcodeGuideServiceDefinition); |
|
118
|
|
|
|
|
119
|
|
|
$this->compilerPass->process($this->containerBuilder); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** @test */ |
|
123
|
|
|
public function missing_shortcode_guide_service_does_no_harm(): void |
|
124
|
|
|
{ |
|
125
|
|
|
$this->containerBuilder->expects($this->once()) |
|
126
|
|
|
->method('findTaggedServiceIds') |
|
127
|
|
|
->willReturn([]); |
|
128
|
|
|
|
|
129
|
|
|
$this->containerBuilder->expects($this->once()) |
|
130
|
|
|
->method('has') |
|
131
|
|
|
->with(GuideController::class) |
|
132
|
|
|
->willReturn(false); |
|
133
|
|
|
|
|
134
|
|
|
$this->compilerPass->process($this->containerBuilder); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..