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 ->createMock (ContainerBuilder::class);
35
+ $ storageDefinition = $ this ->createMock (Definition::class);
36
+ $ parameterBag = $ this ->createMock (ParameterBag::class);
37
+
38
+ $ className = 'stdClassUnresolved ' . random_bytes (5 );
39
+ $ resolvedClassName = 'stdClass ' ;
40
+
41
+ $ container
42
+ ->expects ($ this ->once ())
43
+ ->method ('findDefinition ' )
44
+ ->with ('fos_oauth_server.storage ' )
45
+ ->willReturn ($ storageDefinition )
46
+ ;
47
+
48
+ $ storageDefinition
49
+ ->expects ($ this ->once ())
50
+ ->method ('getClass ' )
51
+ ->with ()
52
+ ->willReturn ($ className )
53
+ ;
54
+
55
+ $ container
56
+ ->expects ($ this ->once ())
57
+ ->method ('getParameterBag ' )
58
+ ->with ()
59
+ ->willReturn ($ parameterBag )
60
+ ;
61
+
62
+ $ parameterBag
63
+ ->expects ($ this ->once ())
64
+ ->method ('resolveValue ' )
65
+ ->with ($ className )
66
+ ->willReturn ($ resolvedClassName )
67
+ ;
68
+
69
+ $ this ->assertNull ($ this ->instance ->process ($ container ));
70
+ }
71
+
72
+ public function testProcessWillFailIfUriIsEmpty ()
73
+ {
74
+ $ container = $ this ->createMock (ContainerBuilder::class);
75
+ $ storageDefinition = $ this ->createMock (Definition::class);
76
+ $ parameterBag = $ this ->createMock (ParameterBag::class);
77
+
78
+ $ storageInstance = $ this ->createMock (GrantExtensionDispatcherInterface::class);
79
+
80
+ $ className = 'stdClassUnresolved ' . random_bytes (5 );
81
+ $ resolvedClassName = get_class ($ storageInstance );
82
+
83
+ $ container
84
+ ->expects ($ this ->once ())
85
+ ->method ('findDefinition ' )
86
+ ->with ('fos_oauth_server.storage ' )
87
+ ->willReturn ($ storageDefinition )
88
+ ;
89
+
90
+ $ storageDefinition
91
+ ->expects ($ this ->once ())
92
+ ->method ('getClass ' )
93
+ ->with ()
94
+ ->willReturn ($ className )
95
+ ;
96
+
97
+ $ container
98
+ ->expects ($ this ->once ())
99
+ ->method ('getParameterBag ' )
100
+ ->with ()
101
+ ->willReturn ($ parameterBag )
102
+ ;
103
+
104
+ $ parameterBag
105
+ ->expects ($ this ->once ())
106
+ ->method ('resolveValue ' )
107
+ ->with ($ className )
108
+ ->willReturn ($ resolvedClassName )
109
+ ;
110
+
111
+ $ data = [
112
+ 'service.id.1 ' => [
113
+ [
114
+ 'uri ' => 'uri11 ' ,
115
+ ],
116
+ [
117
+ 'uri ' => '' ,
118
+ ],
119
+ ],
120
+ ];
121
+
122
+ $ container
123
+ ->expects ($ this ->once ())
124
+ ->method ('findTaggedServiceIds ' )
125
+ ->with ('fos_oauth_server.grant_extension ' )
126
+ ->willReturn ($ data )
127
+ ;
128
+
129
+ $ idx = 0 ;
130
+ foreach ($ data as $ id => $ tags ) {
131
+ foreach ($ tags as $ tag ) {
132
+ if (empty ($ tag ['uri ' ])) {
133
+ $ exceptionMessage = sprintf ('Service "%s" must define the "uri" attribute on "fos_oauth_server.grant_extension" tags. ' , $ id );
134
+ $ this ->expectExceptionMessage ($ exceptionMessage );
135
+ break ;
136
+ }
137
+
138
+ $ storageDefinition
139
+ ->expects ($ this ->at (++$ idx ))
140
+ ->method ('addMethodCall ' )
141
+ ->with (
142
+ 'setGrantExtension ' ,
143
+ [
144
+ $ tag ['uri ' ],
145
+ new Reference ($ id )
146
+ ]
147
+ )
148
+ ;
149
+ }
150
+ }
151
+
152
+ $ this ->expectException (InvalidArgumentException::class);
153
+
154
+ $ this ->assertNull ($ this ->instance ->process ($ container ));
155
+ }
156
+
157
+ public function testProcess ()
158
+ {
159
+ $ container = $ this ->createMock (ContainerBuilder::class);
160
+ $ storageDefinition = $ this ->createMock (Definition::class);
161
+ $ parameterBag = $ this ->createMock (ParameterBag::class);
162
+
163
+ $ storageInstance = $ this ->createMock (GrantExtensionDispatcherInterface::class);
164
+
165
+ $ className = 'stdClassUnresolved ' . random_bytes (5 );
166
+ $ resolvedClassName = get_class ($ storageInstance );
167
+
168
+ $ container
169
+ ->expects ($ this ->once ())
170
+ ->method ('findDefinition ' )
171
+ ->with ('fos_oauth_server.storage ' )
172
+ ->willReturn ($ storageDefinition )
173
+ ;
174
+
175
+ $ storageDefinition
176
+ ->expects ($ this ->once ())
177
+ ->method ('getClass ' )
178
+ ->with ()
179
+ ->willReturn ($ className )
180
+ ;
181
+
182
+ $ container
183
+ ->expects ($ this ->once ())
184
+ ->method ('getParameterBag ' )
185
+ ->with ()
186
+ ->willReturn ($ parameterBag )
187
+ ;
188
+
189
+ $ parameterBag
190
+ ->expects ($ this ->once ())
191
+ ->method ('resolveValue ' )
192
+ ->with ($ className )
193
+ ->willReturn ($ resolvedClassName )
194
+ ;
195
+
196
+ $ data = [
197
+ 'service.id.1 ' => [
198
+ [
199
+ 'uri ' => 'uri11 ' ,
200
+ ],
201
+ [
202
+ 'uri ' => 'uri12 ' ,
203
+ ],
204
+ ],
205
+ 'service.id.2 ' => [
206
+ [
207
+ 'uri ' => 'uri21 ' ,
208
+ ],
209
+ [
210
+ 'uri ' => 'uri22 ' ,
211
+ ],
212
+ ],
213
+ ];
214
+
215
+ $ container
216
+ ->expects ($ this ->once ())
217
+ ->method ('findTaggedServiceIds ' )
218
+ ->with ('fos_oauth_server.grant_extension ' )
219
+ ->willReturn ($ data )
220
+ ;
221
+
222
+ $ idx = 0 ;
223
+ foreach ($ data as $ id => $ tags ) {
224
+ foreach ($ tags as $ tag ) {
225
+ $ storageDefinition
226
+ ->expects ($ this ->at (++$ idx ))
227
+ ->method ('addMethodCall ' )
228
+ ->with (
229
+ 'setGrantExtension ' ,
230
+ [
231
+ $ tag ['uri ' ],
232
+ new Reference ($ id )
233
+ ]
234
+ )
235
+ ;
236
+ }
237
+ }
238
+
239
+ $ this ->assertNull ($ this ->instance ->process ($ container ));
240
+ }
241
+ }
0 commit comments