1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright 2011 Johannes M. Schmitt <[email protected]> |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace JMS\SerializerBundle\Tests\DependencyInjection; |
20
|
|
|
|
21
|
|
|
use JMS\Serializer\Exception\RuntimeException; |
22
|
|
|
use JMS\SerializerBundle\DependencyInjection\Compiler\CustomHandlersPass; |
23
|
|
|
use JMS\SerializerBundle\DependencyInjection\JMSSerializerExtension; |
24
|
|
|
use PHPUnit\Framework\TestCase; |
25
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
26
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
27
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
28
|
|
|
|
29
|
|
|
class CustomHandlerPassTest extends TestCase |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @param array $configs |
33
|
|
|
* @return ContainerBuilder |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
private function getContainer(array $configs = array()) |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$loader = new JMSSerializerExtension(); |
38
|
|
|
$container = new ContainerBuilder(); |
39
|
|
|
|
40
|
|
|
$container->setParameter('kernel.debug', true); |
41
|
|
|
$container->setParameter('kernel.cache_dir', sys_get_temp_dir() . '/serializer'); |
42
|
|
|
$container->setParameter('kernel.bundles', array()); |
43
|
|
|
$container->setParameter('kernel.bundles', array()); |
44
|
|
|
|
45
|
|
|
$loader->load(['jms_serializer' => $configs], $container); |
46
|
|
|
|
47
|
|
|
// remove other subscribers |
48
|
|
|
foreach (array_keys($container->findTaggedServiceIds('jms_serializer.handler')) as $id) { |
49
|
|
|
$container->removeDefinition($id); |
50
|
|
|
} |
51
|
|
|
foreach (array_keys($container->findTaggedServiceIds('jms_serializer.subscribing_handler')) as $id) { |
52
|
|
|
$container->removeDefinition($id); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $container; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testHandler() |
59
|
|
|
{ |
60
|
|
|
$container = $this->getContainer(); |
61
|
|
|
|
62
|
|
|
$def = new Definition('Foo'); |
63
|
|
|
$def->addTag('jms_serializer.handler', [ |
64
|
|
|
'type' => 'DateTime', |
65
|
|
|
'format' => 'json', |
66
|
|
|
]); |
67
|
|
|
$container->setDefinition('my_service', $def); |
68
|
|
|
|
69
|
|
|
$pass = new CustomHandlersPass(); |
70
|
|
|
$pass->process($container); |
71
|
|
|
|
72
|
|
|
$args = $container->getDefinition('jms_serializer.handler_registry')->getArguments(); |
73
|
|
|
|
74
|
|
|
$this->assertSame([ |
75
|
|
|
2 => ['DateTime' => ['json' => ['my_service', 'deserializeDateTimeFromjson']]], |
76
|
|
|
1 => ['DateTime' => ['json' => ['my_service', 'serializeDateTimeTojson']]] |
77
|
|
|
], $args[1]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testHandlerCanBePrivate() |
81
|
|
|
{ |
82
|
|
|
$container = $this->getContainer(); |
83
|
|
|
|
84
|
|
|
$def = new Definition('Foo'); |
85
|
|
|
$def->setPublic(false); |
86
|
|
|
$def->addTag('jms_serializer.handler', [ |
87
|
|
|
'type' => 'DateTime', |
88
|
|
|
'format' => 'json', |
89
|
|
|
]); |
90
|
|
|
$container->setDefinition('my_service', $def); |
91
|
|
|
|
92
|
|
|
$pass = new CustomHandlersPass(); |
93
|
|
|
$pass->process($container); |
94
|
|
|
|
95
|
|
|
$args = $container->getDefinition('jms_serializer.handler_registry')->getArguments(); |
96
|
|
|
|
97
|
|
|
$this->assertEquals([ |
98
|
|
|
2 => ['DateTime' => ['json' => [new Reference('my_service'), 'deserializeDateTimeFromjson']]], |
99
|
|
|
1 => ['DateTime' => ['json' => [new Reference('my_service'), 'serializeDateTimeTojson']]] |
100
|
|
|
], $args[1]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testHandlerDirection() |
104
|
|
|
{ |
105
|
|
|
$container = $this->getContainer(); |
106
|
|
|
|
107
|
|
|
$def = new Definition('Foo'); |
108
|
|
|
$def->addTag('jms_serializer.handler', [ |
109
|
|
|
'type' => 'DateTime', |
110
|
|
|
'format' => 'json', |
111
|
|
|
'direction' => 'SERIALIZATION', |
112
|
|
|
]); |
113
|
|
|
$container->setDefinition('my_service', $def); |
114
|
|
|
|
115
|
|
|
$pass = new CustomHandlersPass(); |
116
|
|
|
$pass->process($container); |
117
|
|
|
|
118
|
|
|
$args = $container->getDefinition('jms_serializer.handler_registry')->getArguments(); |
119
|
|
|
|
120
|
|
|
$this->assertSame([ |
121
|
|
|
1 => ['DateTime' => ['json' => ['my_service', 'serializeDateTimeTojson']]] |
122
|
|
|
], $args[1]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @expectedException RuntimeException |
127
|
|
|
* @expectedExceptionMessage The direction "bar" of tag "jms_serializer.handler" of service "my_service" does not exist |
128
|
|
|
*/ |
129
|
|
|
public function testHandlerIncorrectDirection() |
130
|
|
|
{ |
131
|
|
|
$container = $this->getContainer(); |
132
|
|
|
|
133
|
|
|
$def = new Definition('Foo'); |
134
|
|
|
$def->addTag('jms_serializer.handler', [ |
135
|
|
|
'type' => 'DateTime', |
136
|
|
|
'format' => 'json', |
137
|
|
|
'direction' => 'bar', |
138
|
|
|
]); |
139
|
|
|
$container->setDefinition('my_service', $def); |
140
|
|
|
|
141
|
|
|
$pass = new CustomHandlersPass(); |
142
|
|
|
$pass->process($container); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @expectedException RuntimeException |
147
|
|
|
* @expectedExceptionMessage Each tag named "jms_serializer.handler" of service "my_service" must have at least two attributes: "type" and "format" |
148
|
|
|
*/ |
149
|
|
View Code Duplication |
public function testHandlerMustHaveTypeAndFormat() |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$container = $this->getContainer(); |
152
|
|
|
|
153
|
|
|
$def = new Definition('Foo'); |
154
|
|
|
$def->addTag('jms_serializer.handler'); |
155
|
|
|
$container->setDefinition('my_service', $def); |
156
|
|
|
|
157
|
|
|
$pass = new CustomHandlersPass(); |
158
|
|
|
$pass->process($container); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
View Code Duplication |
public function testSubscribingHandler() |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
$container = $this->getContainer(); |
164
|
|
|
|
165
|
|
|
$def = new Definition('JMS\SerializerBundle\Tests\DependencyInjection\Fixture\SubscribingHandler'); |
166
|
|
|
$def->addTag('jms_serializer.subscribing_handler'); |
167
|
|
|
$container->setDefinition('my_service', $def); |
168
|
|
|
|
169
|
|
|
$pass = new CustomHandlersPass(); |
170
|
|
|
$pass->process($container); |
171
|
|
|
|
172
|
|
|
$args = $container->getDefinition('jms_serializer.handler_registry')->getArguments(); |
173
|
|
|
|
174
|
|
|
$this->assertSame([ |
175
|
|
|
1 => ['DateTime' => ['json' => ['my_service', 'onDateTime']]] |
176
|
|
|
], $args[1]); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
View Code Duplication |
public function testSubscribingHandlerCanBePrivate() |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
$container = $this->getContainer(); |
182
|
|
|
|
183
|
|
|
$def = new Definition('JMS\SerializerBundle\Tests\DependencyInjection\Fixture\SubscribingHandler'); |
184
|
|
|
$def->addTag('jms_serializer.subscribing_handler'); |
185
|
|
|
$def->setPublic(false); |
186
|
|
|
$container->setDefinition('my_service', $def); |
187
|
|
|
|
188
|
|
|
$pass = new CustomHandlersPass(); |
189
|
|
|
$pass->process($container); |
190
|
|
|
|
191
|
|
|
$args = $container->getDefinition('jms_serializer.handler_registry')->getArguments(); |
192
|
|
|
|
193
|
|
|
$this->assertEquals([ |
194
|
|
|
1 => ['DateTime' => ['json' => [new Reference('my_service'), 'onDateTime']]] |
195
|
|
|
], $args[1]); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @expectedException RuntimeException |
200
|
|
|
* @expectedExceptionMessage The service "my_service" must implement the SubscribingHandlerInterface |
201
|
|
|
*/ |
202
|
|
View Code Duplication |
public function testSubscribingHandlerInterface() |
|
|
|
|
203
|
|
|
{ |
204
|
|
|
$container = $this->getContainer(); |
205
|
|
|
|
206
|
|
|
$def = new Definition('JMS\SerializerBundle\Tests\DependencyInjection\Fixture\SimpleObject'); |
207
|
|
|
$def->addTag('jms_serializer.subscribing_handler'); |
208
|
|
|
$container->setDefinition('my_service', $def); |
209
|
|
|
|
210
|
|
|
$pass = new CustomHandlersPass(); |
211
|
|
|
$pass->process($container); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.