1
+ <?php
2
+
3
+ namespace FOS \OAuthServerBundle \Tests \DependencyInjection \Compiler ;
4
+
5
+ use FOS \OAuthServerBundle \DependencyInjection \Compiler \GrantExtensionsCompilerPass ;
6
+ use FOS \OAuthServerBundle \Storage \GrantExtensionDispatcherInterface ;
7
+ use Symfony \Component \DependencyInjection \ContainerBuilder ;
8
+ use Symfony \Component \DependencyInjection \Definition ;
9
+ use Symfony \Component \DependencyInjection \Exception \InvalidArgumentException ;
10
+ use Symfony \Component \DependencyInjection \ParameterBag \ParameterBag ;
11
+ use Symfony \Component \DependencyInjection \Reference ;
12
+
13
+ /**
14
+ * Class GrantExtensionsCompilerPassTest
15
+ * @package FOS\OAuthServerBundle\Tests\DependencyInjection\Compiler
16
+ * @author Nikola Petkanski <[email protected] >
17
+ */
18
+ class GrantExtensionsCompilerPassTest extends \PHPUnit_Framework_TestCase
19
+ {
20
+ /**
21
+ * @var GrantExtensionsCompilerPass
22
+ */
23
+ protected $ instance ;
24
+
25
+ public function setUp ()
26
+ {
27
+ $ this ->instance = new GrantExtensionsCompilerPass ();
28
+
29
+ parent ::setUp ();
30
+ }
31
+
32
+ public function testProcessWillNotDoAnythingIfTheStorageDoesNotImplementOurInterface ()
33
+ {
34
+ $ container = $ this ->getMockBuilder (ContainerBuilder::class)
35
+ ->disableOriginalConstructor ()
36
+ ->setMethods ([
37
+ 'findDefinition ' ,
38
+ 'getParameterBag ' ,
39
+
40
+ ])
41
+ ->getMock ()
42
+ ;
43
+ $ storageDefinition = $ this ->getMockBuilder (Definition::class)
44
+ ->disableOriginalConstructor ()
45
+ ->getMock ()
46
+ ;
47
+ $ parameterBag = $ this ->getMockBuilder (ParameterBag::class)
48
+ ->disableOriginalConstructor ()
49
+ ->getMock ()
50
+ ;
51
+
52
+ $ className = 'stdClassUnresolved ' . random_bytes (5 );
53
+ $ resolvedClassName = 'stdClass ' ;
54
+
55
+ $ container
56
+ ->expects ($ this ->once ())
57
+ ->method ('findDefinition ' )
58
+ ->with ('fos_oauth_server.storage ' )
59
+ ->willReturn ($ storageDefinition )
60
+ ;
61
+
62
+ $ storageDefinition
63
+ ->expects ($ this ->once ())
64
+ ->method ('getClass ' )
65
+ ->with ()
66
+ ->willReturn ($ className )
67
+ ;
68
+
69
+ $ container
70
+ ->expects ($ this ->once ())
71
+ ->method ('getParameterBag ' )
72
+ ->with ()
73
+ ->willReturn ($ parameterBag )
74
+ ;
75
+
76
+ $ parameterBag
77
+ ->expects ($ this ->once ())
78
+ ->method ('resolveValue ' )
79
+ ->with ($ className )
80
+ ->willReturn ($ resolvedClassName )
81
+ ;
82
+
83
+ $ this ->assertNull ($ this ->instance ->process ($ container ));
84
+ }
85
+
86
+ public function testProcessWillFailIfUriIsEmpty ()
87
+ {
88
+ $ container = $ this ->getMockBuilder (ContainerBuilder::class)
89
+ ->disableOriginalConstructor ()
90
+ ->setMethods ([
91
+ 'findDefinition ' ,
92
+ 'getParameterBag ' ,
93
+ 'findTaggedServiceIds ' ,
94
+ ])
95
+ ->getMock ()
96
+ ;
97
+ $ storageDefinition = $ this ->getMockBuilder (Definition::class)
98
+ ->disableOriginalConstructor ()
99
+ ->getMock ()
100
+ ;
101
+ $ parameterBag = $ this ->getMockBuilder (ParameterBag::class)
102
+ ->disableOriginalConstructor ()
103
+ ->getMock ()
104
+ ;
105
+
106
+ $ storageInstance = $ this ->getMockBuilder (GrantExtensionDispatcherInterface::class)
107
+ ->disableOriginalConstructor ()
108
+ ->getMock ()
109
+ ;
110
+
111
+ $ className = 'stdClassUnresolved ' . random_bytes (5 );
112
+ $ resolvedClassName = get_class ($ storageInstance );
113
+
114
+ $ container
115
+ ->expects ($ this ->once ())
116
+ ->method ('findDefinition ' )
117
+ ->with ('fos_oauth_server.storage ' )
118
+ ->willReturn ($ storageDefinition )
119
+ ;
120
+
121
+ $ storageDefinition
122
+ ->expects ($ this ->once ())
123
+ ->method ('getClass ' )
124
+ ->with ()
125
+ ->willReturn ($ className )
126
+ ;
127
+
128
+ $ container
129
+ ->expects ($ this ->once ())
130
+ ->method ('getParameterBag ' )
131
+ ->with ()
132
+ ->willReturn ($ parameterBag )
133
+ ;
134
+
135
+ $ parameterBag
136
+ ->expects ($ this ->once ())
137
+ ->method ('resolveValue ' )
138
+ ->with ($ className )
139
+ ->willReturn ($ resolvedClassName )
140
+ ;
141
+
142
+ $ data = [
143
+ 'service.id.1 ' => [
144
+ [
145
+ 'uri ' => 'uri11 ' ,
146
+ ],
147
+ [
148
+ 'uri ' => '' ,
149
+ ],
150
+ ],
151
+ ];
152
+
153
+ $ container
154
+ ->expects ($ this ->once ())
155
+ ->method ('findTaggedServiceIds ' )
156
+ ->with ('fos_oauth_server.grant_extension ' )
157
+ ->willReturn ($ data )
158
+ ;
159
+
160
+ $ exceptionMessage = 'Service "%s" must define the "uri" attribute on "fos_oauth_server.grant_extension" tags. ' ;
161
+
162
+ $ idx = 0 ;
163
+ foreach ($ data as $ id => $ tags ) {
164
+ foreach ($ tags as $ tag ) {
165
+ if (empty ($ tag ['uri ' ])) {
166
+ $ exceptionMessage = sprintf ($ exceptionMessage , $ id );
167
+ break ;
168
+ }
169
+
170
+ $ storageDefinition
171
+ ->expects ($ this ->at (++$ idx ))
172
+ ->method ('addMethodCall ' )
173
+ ->with (
174
+ 'setGrantExtension ' ,
175
+ [
176
+ $ tag ['uri ' ],
177
+ new Reference ($ id )
178
+ ]
179
+ )
180
+ ;
181
+ }
182
+ }
183
+
184
+ $ this ->setExpectedException (InvalidArgumentException::class, $ exceptionMessage );
185
+
186
+ $ this ->assertNull ($ this ->instance ->process ($ container ));
187
+ }
188
+
189
+ public function testProcess ()
190
+ {
191
+ $ container = $ this ->getMockBuilder (ContainerBuilder::class)
192
+ ->disableOriginalConstructor ()
193
+ ->setMethods ([
194
+ 'findDefinition ' ,
195
+ 'getParameterBag ' ,
196
+ 'findTaggedServiceIds ' ,
197
+ ])
198
+ ->getMock ()
199
+ ;
200
+ $ storageDefinition = $ this ->getMockBuilder (Definition::class)
201
+ ->disableOriginalConstructor ()
202
+ ->getMock ()
203
+ ;
204
+ $ parameterBag = $ this ->getMockBuilder (ParameterBag::class)
205
+ ->disableOriginalConstructor ()
206
+ ->getMock ()
207
+ ;
208
+
209
+ $ storageInstance = $ this ->getMockBuilder (GrantExtensionDispatcherInterface::class)
210
+ ->disableOriginalConstructor ()
211
+ ->getMock ()
212
+ ;
213
+
214
+ $ className = 'stdClassUnresolved ' . random_bytes (5 );
215
+ $ resolvedClassName = get_class ($ storageInstance );
216
+
217
+ $ container
218
+ ->expects ($ this ->once ())
219
+ ->method ('findDefinition ' )
220
+ ->with ('fos_oauth_server.storage ' )
221
+ ->willReturn ($ storageDefinition )
222
+ ;
223
+
224
+ $ storageDefinition
225
+ ->expects ($ this ->once ())
226
+ ->method ('getClass ' )
227
+ ->with ()
228
+ ->willReturn ($ className )
229
+ ;
230
+
231
+ $ container
232
+ ->expects ($ this ->once ())
233
+ ->method ('getParameterBag ' )
234
+ ->with ()
235
+ ->willReturn ($ parameterBag )
236
+ ;
237
+
238
+ $ parameterBag
239
+ ->expects ($ this ->once ())
240
+ ->method ('resolveValue ' )
241
+ ->with ($ className )
242
+ ->willReturn ($ resolvedClassName )
243
+ ;
244
+
245
+ $ data = [
246
+ 'service.id.1 ' => [
247
+ [
248
+ 'uri ' => 'uri11 ' ,
249
+ ],
250
+ [
251
+ 'uri ' => 'uri12 ' ,
252
+ ],
253
+ ],
254
+ 'service.id.2 ' => [
255
+ [
256
+ 'uri ' => 'uri21 ' ,
257
+ ],
258
+ [
259
+ 'uri ' => 'uri22 ' ,
260
+ ],
261
+ ],
262
+ ];
263
+
264
+ $ container
265
+ ->expects ($ this ->once ())
266
+ ->method ('findTaggedServiceIds ' )
267
+ ->with ('fos_oauth_server.grant_extension ' )
268
+ ->willReturn ($ data )
269
+ ;
270
+
271
+ $ idx = 0 ;
272
+ foreach ($ data as $ id => $ tags ) {
273
+ foreach ($ tags as $ tag ) {
274
+ $ storageDefinition
275
+ ->expects ($ this ->at (++$ idx ))
276
+ ->method ('addMethodCall ' )
277
+ ->with (
278
+ 'setGrantExtension ' ,
279
+ [
280
+ $ tag ['uri ' ],
281
+ new Reference ($ id )
282
+ ]
283
+ )
284
+ ;
285
+ }
286
+ }
287
+
288
+ $ this ->assertNull ($ this ->instance ->process ($ container ));
289
+ }
290
+ }
0 commit comments