-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathShortcodeCompilerPassTest.php
More file actions
134 lines (113 loc) · 4.33 KB
/
ShortcodeCompilerPassTest.php
File metadata and controls
134 lines (113 loc) · 4.33 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
127
128
129
130
131
132
133
134
<?php
namespace Webfactory\ShortcodeBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
use Webfactory\ShortcodeBundle\Controller\GuideController;
use Webfactory\ShortcodeBundle\DependencyInjection\Compiler\ShortcodeCompilerPass;
final class ShortcodeCompilerPassTest extends TestCase
{
/**
* System under test.
*
* @var ShortcodeCompilerPass
*/
private $compilerPass;
/** @var ContainerBuilder|MockObject */
private $containerBuilder;
protected function setUp(): void
{
$this->compilerPass = new ShortcodeCompilerPass();
$this->containerBuilder = $this->getMockBuilder(ContainerBuilder::class)
->disableOriginalConstructor()
->getMock();
}
#[Test]
public function tagged_services_are_added_as_handlers_to_handler_container(): void
{
$this->containerBuilder->expects($this->once())
->method('findTaggedServiceIds')
->willReturn([
'service_id1' => [
['shortcode' => 'shortcode1'],
],
'service_id2' => [
['shortcode' => 'shortcode2'],
],
]);
$mockedShortcodeHandlerContainer = $this->createMock(Definition::class);
$mockedShortcodeHandlerContainer->expects($this->exactly(2))
->method('addMethodCall')
->with('add', $this->callback(function (array $argument) {
static $count = 0;
++$count;
return 'shortcode'.$count === $argument[0] && $argument[1] instanceof Reference;
}));
$this->containerBuilder->expects($this->once())
->method('findDefinition')
->with(HandlerContainer::class)
->willReturn($mockedShortcodeHandlerContainer);
$this->compilerPass->process($this->containerBuilder);
}
#[Test]
public function no_tagged_services_do_no_harm(): void
{
$this->containerBuilder->expects($this->once())
->method('findTaggedServiceIds')
->willReturn([]);
$this->compilerPass->process($this->containerBuilder);
}
#[Test]
public function shortcode_guide_service_gets_configured_if_set(): void
{
$this->containerBuilder->expects($this->once())
->method('findTaggedServiceIds')
->willReturn([
'service_id1' => [
['shortcode' => 'shortcode1'],
],
'service_id2' => [
['shortcode' => 'shortcode2'],
],
]);
$this->containerBuilder->expects($this->once())
->method('findDefinition')
->with(HandlerContainer::class)
->willReturn($this->createMock(Definition::class));
$this->containerBuilder->expects($this->once())
->method('has')
->with(GuideController::class)
->willReturn(true);
$mockedShortcodeGuideServiceDefinition = $this->createMock(Definition::class);
$mockedShortcodeGuideServiceDefinition->expects($this->once())
->method('setArgument')
->with(
0,
[
['shortcode' => 'shortcode1'],
['shortcode' => 'shortcode2'],
]
);
$this->containerBuilder->expects($this->once())
->method('getDefinition')
->with(GuideController::class)
->willReturn($mockedShortcodeGuideServiceDefinition);
$this->compilerPass->process($this->containerBuilder);
}
#[Test]
public function missing_shortcode_guide_service_does_no_harm(): void
{
$this->containerBuilder->expects($this->once())
->method('findTaggedServiceIds')
->willReturn([]);
$this->containerBuilder->expects($this->once())
->method('has')
->with(GuideController::class)
->willReturn(false);
$this->compilerPass->process($this->containerBuilder);
}
}