1
+ <?php
2
+
3
+ namespace Symfony \Tests \Component \Security \Http \Firewall ;
4
+
5
+ use Symfony \Component \HttpKernel \HttpKernelInterface ;
6
+ use Symfony \Component \HttpKernel \Events ;
7
+ use Symfony \Component \HttpFoundation \Response ;
8
+ use Symfony \Component \Security \Core \Exception \AuthenticationException ;
9
+ use Symfony \Component \Security \Http \Firewall \ContextListener ;
10
+ use Symfony \Component \HttpFoundation \Request ;
11
+ use Symfony \Component \HttpFoundation \Session ;
12
+
13
+ class ContextListenerTest extends \PHPUnit_Framework_TestCase
14
+ {
15
+ // test that if the session has a token, it's set on the context
16
+ public function testOnCoreRequestRestoresToken ()
17
+ {
18
+ list ($ listener , $ context , $ provider , $ contextKey , $ logger , $ dispatcher ) = $ this ->getListener ();
19
+
20
+ list ($ request , $ session ) = $ this ->getRequest ();
21
+
22
+ // create a real token, mocking this and then serializing it did not work
23
+ $ token = new \Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ('foo ' , 'bar ' , 'baz ' );
24
+ $ request
25
+ ->expects ($ this ->once ())
26
+ ->method ('hasPreviousSession ' )
27
+ ->will ($ this ->returnValue (true ));
28
+ $ session
29
+ ->expects ($ this ->once ())
30
+ ->method ('get ' )
31
+ ->with ('_security_ ' .$ contextKey )
32
+ ->will ($ this ->returnValue (serialize ($ token )));
33
+
34
+ $ event = $ this ->getGetResponseEvent ();
35
+ $ event
36
+ ->expects ($ this ->once ())
37
+ ->method ('getRequest ' )
38
+ ->will ($ this ->returnValue ($ request ))
39
+ ;
40
+
41
+ // the end goal is that the token is set on the context
42
+ $ context
43
+ ->expects ($ this ->once ())
44
+ ->method ('setToken ' )
45
+ ->with ($ token )
46
+ ;
47
+
48
+ // since a logger is injected, debug should be called at least once
49
+ $ logger
50
+ ->expects ($ this ->atLeastOnce ())
51
+ ->method ('debug ' )
52
+ ;
53
+
54
+ $ listener ->handle ($ event );
55
+ }
56
+
57
+ protected function getGetResponseEvent ()
58
+ {
59
+ return $ this ->getMock ('Symfony\Component\HttpKernel\Event\GetResponseEvent ' , array (), array (), '' , false );
60
+ }
61
+
62
+ protected function getFilterResponseEvent ()
63
+ {
64
+ return $ this ->getMock ('Symfony\Component\HttpKernel\Event\FilterResponseEvent ' , array (), array (), '' , false );
65
+ }
66
+
67
+ protected function getListener ($ withLogger = true )
68
+ {
69
+ $ logger = $ withLogger ? $ this ->getLogger () : null ;
70
+
71
+ $ listener = new ContextListener (
72
+ $ context = $ this ->getContext (),
73
+ array ($ provider = $ this ->getUserProvider ()),
74
+ $ key = 'context_key ' ,
75
+ $ logger ,
76
+ $ dispatcher = $ this ->getDispatcher ()
77
+ );
78
+
79
+ return array ($ listener , $ context , $ provider , $ key , $ logger , $ dispatcher );
80
+ }
81
+
82
+ protected function getLogger ()
83
+ {
84
+ return $ this ->getMock ('Symfony\Component\HttpKernel\Log\LoggerInterface ' );
85
+ }
86
+
87
+ protected function getManager ()
88
+ {
89
+ return $ this ->getMock ('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface ' );
90
+ }
91
+
92
+ protected function getUserProvider ()
93
+ {
94
+ return $ this ->getMock ('Symfony\Component\Security\Core\User\UserProviderInterface ' );
95
+ }
96
+
97
+ protected function getContext ()
98
+ {
99
+ return $ this ->getMockBuilder ('Symfony\Component\Security\Core\SecurityContext ' )
100
+ ->disableOriginalConstructor ()
101
+ ->getMock ();
102
+ }
103
+
104
+ protected function getDispatcher ()
105
+ {
106
+ return $ this ->getMock ('Symfony\Component\EventDispatcher\EventDispatcherInterface ' );
107
+ }
108
+
109
+ protected function getRequest ()
110
+ {
111
+ $ request = $ this ->getMock ('Symfony\Component\HttpFoundation\Request ' );
112
+ $ session = $ this ->getMockBuilder ('Symfony\Component\HttpFoundation\Session ' )
113
+ ->disableOriginalConstructor ()
114
+ ->getMock ();
115
+
116
+ $ request ->expects ($ this ->any ())
117
+ ->method ('getSession ' )
118
+ ->will ($ this ->returnValue ($ session ));
119
+
120
+ return array ($ request , $ session );
121
+ }
122
+ }
0 commit comments