17
17
class AccessListenerTest extends TestCase
18
18
{
19
19
/**
20
+ * @expectedException \InvalidArgumentException
21
+ */
22
+ public function testBadConstructorSignature ()
23
+ {
24
+ $ tokenStorage = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' )->getMock ();
25
+
26
+ new AccessListener ($ tokenStorage , null , null , null );
27
+ }
28
+
29
+ /**
30
+ * @deprecated
31
+ * @group legacy
32
+ * @expectedDeprecation Signature "Symfony\Component\Security\Http\Firewall\AccessListener::__construct()" has changed since Symfony 4.2, now it accepts TokenStorageInterface, AccessMapInterface, AuthorizationCheckerInterface.
33
+ */
34
+ public function testOldConstructorSignature ()
35
+ {
36
+ $ tokenStorage = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' )->getMock ();
37
+ $ accessMap = $ this ->getMockBuilder ('Symfony\Component\Security\Http\AccessMapInterface ' )->getMock ();
38
+ $ accessDecisionManager = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface ' )->getMock ();
39
+ $ authManager = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface ' )->getMock ();
40
+
41
+ new AccessListener ($ tokenStorage , $ accessDecisionManager , $ accessMap , $ authManager );
42
+ }
43
+
44
+ /**
45
+ * @deprecated
46
+ * @group legacy
47
+ * @expectedDeprecation Signature "Symfony\Component\Security\Http\Firewall\AccessListener::__construct()" has changed since Symfony 4.2, now it accepts TokenStorageInterface, AccessMapInterface, AuthorizationCheckerInterface.
20
48
* @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
21
49
*/
22
- public function testHandleWhenTheAccessDecisionManagerDecidesToRefuseAccess ()
50
+ public function testOldHandleWhenTheAccessDecisionManagerDecidesToRefuseAccess ()
23
51
{
24
52
$ request = $ this ->getMockBuilder ('Symfony\Component\HttpFoundation\Request ' )->disableOriginalConstructor ()->disableOriginalClone ()->getMock ();
25
53
@@ -70,7 +98,56 @@ public function testHandleWhenTheAccessDecisionManagerDecidesToRefuseAccess()
70
98
$ listener ->handle ($ event );
71
99
}
72
100
73
- public function testHandleWhenTheTokenIsNotAuthenticated ()
101
+ /**
102
+ * @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
103
+ */
104
+ public function testHandleWhenTheAccessDecisionManagerDecidesToRefuseAccess ()
105
+ {
106
+ $ tokenStorage = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' )->getMock ();
107
+ $ accessMap = $ this ->getMockBuilder ('Symfony\Component\Security\Http\AccessMapInterface ' )->getMock ();
108
+ $ authorizationChecker = $ this ->getMockBuilder ('\Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface ' )->getMock ();
109
+ $ request = $ this ->getMockBuilder ('Symfony\Component\HttpFoundation\Request ' )->disableOriginalConstructor ()->disableOriginalClone ()->getMock ();
110
+ $ token = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\TokenInterface ' )->getMock ();
111
+ $ event = $ this ->getMockBuilder ('Symfony\Component\HttpKernel\Event\GetResponseEvent ' )->disableOriginalConstructor ()->getMock ();
112
+ $ attributes = ['foo ' => 'bar ' ];
113
+
114
+ $ tokenStorage
115
+ ->expects ($ this ->any ())
116
+ ->method ('getToken ' )
117
+ ->will ($ this ->returnValue ($ token ))
118
+ ;
119
+
120
+ $ accessMap
121
+ ->expects ($ this ->any ())
122
+ ->method ('getPatterns ' )
123
+ ->with ($ this ->equalTo ($ request ))
124
+ ->will ($ this ->returnValue ([$ attributes , null ]))
125
+ ;
126
+
127
+ $ authorizationChecker
128
+ ->expects ($ this ->once ())
129
+ ->method ('isGranted ' )
130
+ ->with ($ this ->equalTo ($ attributes ), $ this ->equalTo ($ request ))
131
+ ->will ($ this ->returnValue (false ))
132
+ ;
133
+
134
+ $ listener = new AccessListener ($ tokenStorage , $ accessMap , $ authorizationChecker );
135
+
136
+ $ event
137
+ ->expects ($ this ->any ())
138
+ ->method ('getRequest ' )
139
+ ->will ($ this ->returnValue ($ request ))
140
+ ;
141
+
142
+ $ listener ->handle ($ event );
143
+ }
144
+
145
+ /**
146
+ * @deprecated
147
+ * @group legacy
148
+ * @expectedDeprecation Signature "Symfony\Component\Security\Http\Firewall\AccessListener::__construct()" has changed since Symfony 4.2, now it accepts TokenStorageInterface, AccessMapInterface, AuthorizationCheckerInterface.
149
+ */
150
+ public function testOldHandleWhenTheTokenIsNotAuthenticated ()
74
151
{
75
152
$ request = $ this ->getMockBuilder ('Symfony\Component\HttpFoundation\Request ' )->disableOriginalConstructor ()->disableOriginalClone ()->getMock ();
76
153
@@ -141,7 +218,12 @@ public function testHandleWhenTheTokenIsNotAuthenticated()
141
218
$ listener ->handle ($ event );
142
219
}
143
220
144
- public function testHandleWhenThereIsNoAccessMapEntryMatchingTheRequest ()
221
+ /**
222
+ * @deprecated
223
+ * @group legacy
224
+ * @expectedDeprecation Signature "Symfony\Component\Security\Http\Firewall\AccessListener::__construct()" has changed since Symfony 4.2, now it accepts TokenStorageInterface, AccessMapInterface, AuthorizationCheckerInterface.
225
+ */
226
+ public function testOldHandleWhenThereIsNoAccessMapEntryMatchingTheRequest ()
145
227
{
146
228
$ request = $ this ->getMockBuilder ('Symfony\Component\HttpFoundation\Request ' )->disableOriginalConstructor ()->disableOriginalClone ()->getMock ();
147
229
@@ -183,10 +265,51 @@ public function testHandleWhenThereIsNoAccessMapEntryMatchingTheRequest()
183
265
$ listener ->handle ($ event );
184
266
}
185
267
268
+ public function testHandleWhenThereIsNoAccessMapEntryMatchingTheRequest ()
269
+ {
270
+ $ tokenStorage = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' )->getMock ();
271
+ $ authorizationChecker = $ this ->getMockBuilder ('\Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface ' )->getMock ();
272
+ $ event = $ this ->getMockBuilder ('Symfony\Component\HttpKernel\Event\GetResponseEvent ' )->disableOriginalConstructor ()->getMock ();
273
+ $ token = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\TokenInterface ' )->getMock ();
274
+ $ request = $ this ->getMockBuilder ('Symfony\Component\HttpFoundation\Request ' )->disableOriginalConstructor ()->disableOriginalClone ()->getMock ();
275
+ $ accessMap = $ this ->getMockBuilder ('Symfony\Component\Security\Http\AccessMapInterface ' )->getMock ();
276
+
277
+ $ tokenStorage
278
+ ->expects ($ this ->any ())
279
+ ->method ('getToken ' )
280
+ ->will ($ this ->returnValue ($ token ))
281
+ ;
282
+
283
+ $ event
284
+ ->expects ($ this ->any ())
285
+ ->method ('getRequest ' )
286
+ ->will ($ this ->returnValue ($ request ))
287
+ ;
288
+
289
+ $ accessMap
290
+ ->expects ($ this ->any ())
291
+ ->method ('getPatterns ' )
292
+ ->with ($ this ->equalTo ($ request ))
293
+ ->will ($ this ->returnValue ([null , null ]))
294
+ ;
295
+
296
+ $ authorizationChecker
297
+ ->expects ($ this ->never ())
298
+ ->method ('isGranted ' )
299
+ ;
300
+
301
+ $ listener = new AccessListener ($ tokenStorage , $ accessMap , $ authorizationChecker );
302
+
303
+ $ listener ->handle ($ event );
304
+ }
305
+
186
306
/**
307
+ * @deprecated
308
+ * @group legacy
309
+ * @expectedDeprecation Signature "Symfony\Component\Security\Http\Firewall\AccessListener::__construct()" has changed since Symfony 4.2, now it accepts TokenStorageInterface, AccessMapInterface, AuthorizationCheckerInterface.
187
310
* @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException
188
311
*/
189
- public function testHandleWhenTheSecurityTokenStorageHasNoToken ()
312
+ public function testOldHandleWhenTheSecurityTokenStorageHasNoToken ()
190
313
{
191
314
$ tokenStorage = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' )->getMock ();
192
315
$ tokenStorage
@@ -195,14 +318,50 @@ public function testHandleWhenTheSecurityTokenStorageHasNoToken()
195
318
->will ($ this ->returnValue (null ))
196
319
;
197
320
321
+ $ request = $ this ->getMockBuilder ('Symfony\Component\HttpFoundation\Request ' )->disableOriginalConstructor ()->disableOriginalClone ()->getMock ();
322
+
323
+ $ accessMap = $ this ->getMockBuilder ('Symfony\Component\Security\Http\AccessMapInterface ' )->getMock ();
324
+ $ accessMap
325
+ ->expects ($ this ->any ())
326
+ ->method ('getPatterns ' )
327
+ ->with ($ this ->equalTo ($ request ))
328
+ ->will ($ this ->returnValue ([['foo ' => 'bar ' ], null ]))
329
+ ;
330
+
198
331
$ listener = new AccessListener (
199
332
$ tokenStorage ,
200
333
$ this ->getMockBuilder ('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface ' )->getMock (),
201
- $ this -> getMockBuilder ( ' Symfony\Component\Security\Http\AccessMapInterface ' )-> getMock () ,
334
+ $ accessMap ,
202
335
$ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface ' )->getMock ()
203
336
);
204
337
205
338
$ event = $ this ->getMockBuilder ('Symfony\Component\HttpKernel\Event\GetResponseEvent ' )->disableOriginalConstructor ()->getMock ();
339
+ $ event
340
+ ->expects ($ this ->any ())
341
+ ->method ('getRequest ' )
342
+ ->will ($ this ->returnValue ($ request ))
343
+ ;
344
+
345
+ $ listener ->handle ($ event );
346
+ }
347
+
348
+ /**
349
+ * @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException
350
+ */
351
+ public function testHandleWhenTheSecurityTokenStorageHasNoToken ()
352
+ {
353
+ $ tokenStorage = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' )->getMock ();
354
+ $ accessMap = $ this ->getMockBuilder ('Symfony\Component\Security\Http\AccessMapInterface ' )->getMock ();
355
+ $ authorizationChecker = $ this ->getMockBuilder ('\Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface ' )->getMock ();
356
+ $ event = $ this ->getMockBuilder ('Symfony\Component\HttpKernel\Event\GetResponseEvent ' )->disableOriginalConstructor ()->getMock ();
357
+
358
+ $ tokenStorage
359
+ ->expects ($ this ->any ())
360
+ ->method ('getToken ' )
361
+ ->will ($ this ->returnValue (null ))
362
+ ;
363
+
364
+ $ listener = new AccessListener ($ tokenStorage , $ accessMap , $ authorizationChecker );
206
365
207
366
$ listener ->handle ($ event );
208
367
}
0 commit comments