Skip to content

Commit 4b1aa94

Browse files
committed
Tests for CompilerPass
1 parent a8069af commit 4b1aa94

3 files changed

Lines changed: 141 additions & 1 deletion

File tree

DependencyInjection/Compiler/ShortcodeCompilerPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function process(ContainerBuilder $container)
2727
if ($container->has('webfactory.shortcode.guide.controller')) {
2828
$allShortcodeTags = [];
2929
foreach ($shortcodeServices as $id => $shortcodeTags) {
30-
$allShortcodeTags += $shortcodeTags;
30+
$allShortcodeTags = array_merge($allShortcodeTags, $shortcodeTags);
3131
}
3232

3333
$container
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
namespace Webfactory\ShortcodeBundle\Tests\DependencyInjection\Compiler;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\DependencyInjection\Definition;
7+
use Symfony\Component\DependencyInjection\Reference;
8+
use Symfony\Component\DependencyInjection\TaggedContainerInterface;
9+
use Thunder\Shortcode\ShortcodeFacade;
10+
use Webfactory\ShortcodeBundle\DependencyInjection\Compiler\ShortcodeCompilerPass;
11+
12+
final class ShortcodeCompilerPassTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* System under test.
16+
*
17+
* @var ShortcodeCompilerPass
18+
*/
19+
private $compilerPass;
20+
21+
/** @var ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject */
22+
private $containerBuilder;
23+
24+
protected function setUp()
25+
{
26+
$this->compilerPass = new ShortcodeCompilerPass();
27+
$this->containerBuilder = $this->getMockBuilder(ContainerBuilder::class)
28+
->disableOriginalConstructor()
29+
->getMock();
30+
}
31+
32+
/** @test */
33+
public function tagged_services_are_added_as_handlers_to_facade()
34+
{
35+
$this->containerBuilder->expects($this->once())
36+
->method('findTaggedServiceIds')
37+
->willReturn([
38+
'service_id1' => [
39+
['shortcode' => 'shortcode1']
40+
],
41+
'service_id2' => [
42+
['shortcode' => 'shortcode2']
43+
]
44+
]);
45+
46+
$mockedShortcodeFacade = $this->getMock(Definition::class);
47+
$mockedShortcodeFacade->expects($this->at(0))
48+
->method('addMethodCall')
49+
->with('addHandler', $this->callback(function (array $argument) {
50+
return $argument[0] === 'shortcode1'
51+
&& $argument[1] instanceof Reference;
52+
}));
53+
$mockedShortcodeFacade->expects($this->at(1))
54+
->method('addMethodCall')
55+
->with('addHandler', $this->callback(function ($argument) {
56+
return $argument[0] === 'shortcode2'
57+
&& $argument[1] instanceof Reference;
58+
}));
59+
60+
$this->containerBuilder->expects($this->once())
61+
->method('findDefinition')
62+
->with('webfactory.shortcode.facade')
63+
->willReturn($mockedShortcodeFacade);
64+
65+
$this->compilerPass->process($this->containerBuilder);
66+
}
67+
68+
/** @test */
69+
public function no_tagged_services_do_no_harm()
70+
{
71+
$this->containerBuilder->expects($this->once())
72+
->method('findTaggedServiceIds')
73+
->willReturn([]);
74+
75+
$this->setExpectedException(null);
76+
77+
$this->compilerPass->process($this->containerBuilder);
78+
}
79+
80+
/** @test */
81+
public function shortcode_guide_service_gets_configured_if_set()
82+
{
83+
$this->containerBuilder->expects($this->once())
84+
->method('findTaggedServiceIds')
85+
->willReturn([
86+
'service_id1' => [
87+
['shortcode' => 'shortcode1']
88+
],
89+
'service_id2' => [
90+
['shortcode' => 'shortcode2']
91+
]
92+
]);
93+
94+
$this->containerBuilder->expects($this->once())
95+
->method('findDefinition')
96+
->with('webfactory.shortcode.facade')
97+
->willReturn($this->getMock(Definition::class));
98+
99+
$this->containerBuilder->expects($this->once())
100+
->method('has')
101+
->with('webfactory.shortcode.guide.controller')
102+
->willReturn(true);
103+
104+
$mockedShortcodeGuideServiceDefinition = $this->getMock(Definition::class);
105+
$mockedShortcodeGuideServiceDefinition->expects($this->once())
106+
->method('setArgument')
107+
->with(
108+
0,
109+
[
110+
['shortcode' => 'shortcode1'],
111+
['shortcode' => 'shortcode2']
112+
]
113+
);
114+
115+
$this->containerBuilder->expects($this->once())
116+
->method('getDefinition')
117+
->with('webfactory.shortcode.guide.controller')
118+
->willReturn($mockedShortcodeGuideServiceDefinition);
119+
120+
$this->compilerPass->process($this->containerBuilder);
121+
}
122+
123+
/** @test */
124+
public function missing_shortcode_guide_service_does_no_harm()
125+
{
126+
$this->containerBuilder->expects($this->once())
127+
->method('findTaggedServiceIds')
128+
->willReturn([]);
129+
130+
$this->containerBuilder->expects($this->once())
131+
->method('has')
132+
->with('webfactory.shortcode.guide.controller')
133+
->willReturn(false);
134+
135+
$this->setExpectedException(null);
136+
137+
$this->compilerPass->process($this->containerBuilder);
138+
}
139+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
"require-dev": {
1515
"phpunit/phpunit": "^4.8.36",
16+
"symfony/expression-language": "^3.4.11",
1617
"symfony/framework-bundle": "^3.4.11",
1718
"symfony/templating": "^3.4.11",
1819
"roave/security-advisories": "dev-master"

0 commit comments

Comments
 (0)