1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2017 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* @license GPL-3.0 |
8
|
|
|
* @author Gerrit Addiks <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Addiks; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Addiks\SymfonyGenerics\DependencyInjection\Compiler\DecoratorTemplateCompilerPass; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
17
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
|
20
|
|
|
final class BarTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @test |
25
|
|
|
*/ |
26
|
|
|
public function shouldApplyDecoratorTemplate() |
27
|
|
|
{ |
28
|
|
|
/** @var Reference $oldReference */ |
29
|
|
|
$oldReference = $this->createMock(Reference::class); |
30
|
|
|
$oldReference->method('__toString')->willReturn('some-string-representation'); |
|
|
|
|
31
|
|
|
$oldReference->method('getInvalidBehavior')->willReturn(1); |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** @var Definition $decoratorDefinition */ |
34
|
|
|
$decoratorDefinition = $this->createMock(Definition::class); |
35
|
|
|
$decoratorDefinition->method('getTags')->willReturn([ |
|
|
|
|
36
|
|
|
'some.tag.name' => 'foo' |
37
|
|
|
]); |
38
|
|
|
$decoratorDefinition->expects($this->once())->method('setTags')->with([ |
|
|
|
|
39
|
|
|
]); |
40
|
|
|
$decoratorDefinition->method('getArguments')->willReturn([ |
|
|
|
|
41
|
|
|
'foo', |
42
|
|
|
['bar', 123, '%some_param%'], |
43
|
|
|
['abc' => ['def' => $oldReference]], |
44
|
|
|
new Reference('some-decorator-service-id.inner', 1) |
45
|
|
|
]); |
46
|
|
|
$decoratorDefinition->expects($this->once())->method('setArguments')->with([ |
|
|
|
|
47
|
|
|
'foo', |
48
|
|
|
['bar', 123, 'some-resolved'], |
49
|
|
|
['abc' => ['def' => new Reference('some-string-representation', 1)]], |
50
|
|
|
new Reference('some-decorated-service-id.some.tag.name.some-decorator-service-id.inner', 1) |
51
|
|
|
]); |
52
|
|
|
$decoratorDefinition->expects($this->once())->method('setDecoratedService')->with( |
|
|
|
|
53
|
|
|
$this->equalTo('some-decorated-service-id') |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
/** @var ContainerBuilder $container */ |
57
|
|
|
$container = $this->createMock(ContainerBuilder::class); |
58
|
|
|
$container->expects($this->once())->method('findTaggedServiceIds')->with( |
|
|
|
|
59
|
|
|
$this->equalTo("some.tag.name") |
60
|
|
|
)->willReturn([ |
61
|
|
|
'some-decorator-service-id' => [ |
62
|
|
|
['decorates' => 'some-decorated-service-id', 'some_param' => 'some-resolved'] |
63
|
|
|
] |
64
|
|
|
]); |
65
|
|
|
$container->expects($this->once())->method('getDefinition')->with( |
|
|
|
|
66
|
|
|
$this->equalTo('some-decorator-service-id') |
67
|
|
|
)->willReturn($decoratorDefinition); |
68
|
|
|
$container->expects($this->once())->method('setDefinition')->with( |
|
|
|
|
69
|
|
|
$this->equalTo("some-decorated-service-id.some.tag.name.some-decorator-service-id"), |
70
|
|
|
$this->equalTo($decoratorDefinition) |
71
|
|
|
); |
72
|
|
|
$container->expects($this->once())->method('removeDefinition')->with( |
|
|
|
|
73
|
|
|
$this->equalTo("some-decorator-service-id") |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$compilerPass = new DecoratorTemplateCompilerPass("some.tag.name"); |
77
|
|
|
$compilerPass->process($container); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @test |
82
|
|
|
*/ |
83
|
|
|
public function shouldExpectDecoratesKeyInTags() |
84
|
|
|
{ |
85
|
|
|
$this->expectException(InvalidArgumentException::class); |
86
|
|
|
|
87
|
|
|
/** @var ContainerBuilder $container */ |
88
|
|
|
$container = $this->createMock(ContainerBuilder::class); |
89
|
|
|
$container->expects($this->once())->method('findTaggedServiceIds')->with( |
|
|
|
|
90
|
|
|
$this->equalTo("some.tag.name") |
91
|
|
|
)->willReturn([ |
92
|
|
|
'some-decorator-service-id' => [ |
93
|
|
|
['some_param' => 'some-resolved'] |
94
|
|
|
] |
95
|
|
|
]); |
96
|
|
|
|
97
|
|
|
$compilerPass = new DecoratorTemplateCompilerPass("some.tag.name"); |
98
|
|
|
$compilerPass->process($container); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @test |
103
|
|
|
*/ |
104
|
|
|
public function shouldExpectTypeTagInDecoratorTags() |
105
|
|
|
{ |
106
|
|
|
$this->expectException(InvalidArgumentException::class); |
107
|
|
|
|
108
|
|
|
/** @var Definition $decoratorDefinition */ |
109
|
|
|
$decoratorDefinition = $this->createMock(Definition::class); |
110
|
|
|
$decoratorDefinition->method('getTags')->willReturn([ |
|
|
|
|
111
|
|
|
]); |
112
|
|
|
|
113
|
|
|
/** @var ContainerBuilder $container */ |
114
|
|
|
$container = $this->createMock(ContainerBuilder::class); |
115
|
|
|
$container->expects($this->once())->method('findTaggedServiceIds')->with( |
|
|
|
|
116
|
|
|
$this->equalTo("some.tag.name") |
117
|
|
|
)->willReturn([ |
118
|
|
|
'some-decorator-service-id' => [ |
119
|
|
|
['decorates' => 'some-decorated-service-id', 'some_param' => 'some-resolved'] |
120
|
|
|
] |
121
|
|
|
]); |
122
|
|
|
$container->expects($this->once())->method('getDefinition')->with( |
|
|
|
|
123
|
|
|
$this->equalTo('some-decorator-service-id') |
124
|
|
|
)->willReturn($decoratorDefinition); |
125
|
|
|
|
126
|
|
|
$compilerPass = new DecoratorTemplateCompilerPass("some.tag.name"); |
127
|
|
|
$compilerPass->process($container); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.