12
12
namespace Symfony \Component \Security \Csrf \Tests \TokenStorage ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \HttpFoundation \Session \Session ;
16
+ use Symfony \Component \HttpFoundation \Session \Storage \MockArraySessionStorage ;
15
17
use Symfony \Component \Security \Csrf \TokenStorage \SessionTokenStorage ;
16
18
17
19
/**
@@ -22,7 +24,7 @@ class SessionTokenStorageTest extends TestCase
22
24
const SESSION_NAMESPACE = 'foobar ' ;
23
25
24
26
/**
25
- * @var \PHPUnit_Framework_MockObject_MockObject
27
+ * @var Session
26
28
*/
27
29
private $ session ;
28
30
@@ -33,118 +35,53 @@ class SessionTokenStorageTest extends TestCase
33
35
34
36
protected function setUp ()
35
37
{
36
- $ this ->session = $ this ->getMockBuilder ('Symfony\Component\HttpFoundation\Session\SessionInterface ' )
37
- ->disableOriginalConstructor ()
38
- ->getMock ();
38
+ $ this ->session = new Session (new MockArraySessionStorage ());
39
39
$ this ->storage = new SessionTokenStorage ($ this ->session , self ::SESSION_NAMESPACE );
40
40
}
41
41
42
- public function testStoreTokenInClosedSession ()
42
+ public function testStoreTokenInNotStartedSessionStartsTheSession ()
43
43
{
44
- $ this ->session ->expects ($ this ->any ())
45
- ->method ('isStarted ' )
46
- ->will ($ this ->returnValue (false ));
47
-
48
- $ this ->session ->expects ($ this ->once ())
49
- ->method ('start ' );
50
-
51
- $ this ->session ->expects ($ this ->once ())
52
- ->method ('set ' )
53
- ->with (self ::SESSION_NAMESPACE .'/token_id ' , 'TOKEN ' );
54
-
55
44
$ this ->storage ->setToken ('token_id ' , 'TOKEN ' );
45
+
46
+ $ this ->assertTrue ($ this ->session ->isStarted ());
56
47
}
57
48
58
49
public function testStoreTokenInActiveSession ()
59
50
{
60
- $ this ->session ->expects ($ this ->any ())
61
- ->method ('isStarted ' )
62
- ->will ($ this ->returnValue (true ));
63
-
64
- $ this ->session ->expects ($ this ->never ())
65
- ->method ('start ' );
66
-
67
- $ this ->session ->expects ($ this ->once ())
68
- ->method ('set ' )
69
- ->with (self ::SESSION_NAMESPACE .'/token_id ' , 'TOKEN ' );
70
-
51
+ $ this ->session ->start ();
71
52
$ this ->storage ->setToken ('token_id ' , 'TOKEN ' );
53
+
54
+ $ this ->assertSame ('TOKEN ' , $ this ->session ->get (self ::SESSION_NAMESPACE .'/token_id ' ));
72
55
}
73
56
74
57
public function testCheckTokenInClosedSession ()
75
58
{
76
- $ this ->session ->expects ($ this ->any ())
77
- ->method ('isStarted ' )
78
- ->will ($ this ->returnValue (false ));
79
-
80
- $ this ->session ->expects ($ this ->once ())
81
- ->method ('start ' );
59
+ $ this ->session ->set (self ::SESSION_NAMESPACE .'/token_id ' , 'RESULT ' );
82
60
83
- $ this ->session ->expects ($ this ->once ())
84
- ->method ('has ' )
85
- ->with (self ::SESSION_NAMESPACE .'/token_id ' )
86
- ->will ($ this ->returnValue ('RESULT ' ));
87
-
88
- $ this ->assertSame ('RESULT ' , $ this ->storage ->hasToken ('token_id ' ));
61
+ $ this ->assertTrue ($ this ->storage ->hasToken ('token_id ' ));
62
+ $ this ->assertTrue ($ this ->session ->isStarted ());
89
63
}
90
64
91
65
public function testCheckTokenInActiveSession ()
92
66
{
93
- $ this ->session ->expects ($ this ->any ())
94
- ->method ('isStarted ' )
95
- ->will ($ this ->returnValue (true ));
96
-
97
- $ this ->session ->expects ($ this ->never ())
98
- ->method ('start ' );
67
+ $ this ->session ->start ();
68
+ $ this ->session ->set (self ::SESSION_NAMESPACE .'/token_id ' , 'RESULT ' );
99
69
100
- $ this ->session ->expects ($ this ->once ())
101
- ->method ('has ' )
102
- ->with (self ::SESSION_NAMESPACE .'/token_id ' )
103
- ->will ($ this ->returnValue ('RESULT ' ));
104
-
105
- $ this ->assertSame ('RESULT ' , $ this ->storage ->hasToken ('token_id ' ));
70
+ $ this ->assertTrue ($ this ->storage ->hasToken ('token_id ' ));
106
71
}
107
72
108
73
public function testGetExistingTokenFromClosedSession ()
109
74
{
110
- $ this ->session ->expects ($ this ->any ())
111
- ->method ('isStarted ' )
112
- ->will ($ this ->returnValue (false ));
113
-
114
- $ this ->session ->expects ($ this ->once ())
115
- ->method ('start ' );
116
-
117
- $ this ->session ->expects ($ this ->once ())
118
- ->method ('has ' )
119
- ->with (self ::SESSION_NAMESPACE .'/token_id ' )
120
- ->will ($ this ->returnValue (true ));
121
-
122
- $ this ->session ->expects ($ this ->once ())
123
- ->method ('get ' )
124
- ->with (self ::SESSION_NAMESPACE .'/token_id ' )
125
- ->will ($ this ->returnValue ('RESULT ' ));
75
+ $ this ->session ->set (self ::SESSION_NAMESPACE .'/token_id ' , 'RESULT ' );
126
76
127
77
$ this ->assertSame ('RESULT ' , $ this ->storage ->getToken ('token_id ' ));
78
+ $ this ->assertTrue ($ this ->session ->isStarted ());
128
79
}
129
80
130
81
public function testGetExistingTokenFromActiveSession ()
131
82
{
132
- $ this ->session ->expects ($ this ->any ())
133
- ->method ('isStarted ' )
134
- ->will ($ this ->returnValue (true ));
135
-
136
- $ this ->session ->expects ($ this ->never ())
137
- ->method ('start ' );
138
-
139
- $ this ->session ->expects ($ this ->once ())
140
- ->method ('has ' )
141
- ->with (self ::SESSION_NAMESPACE .'/token_id ' )
142
- ->will ($ this ->returnValue (true ));
143
-
144
- $ this ->session ->expects ($ this ->once ())
145
- ->method ('get ' )
146
- ->with (self ::SESSION_NAMESPACE .'/token_id ' )
147
- ->will ($ this ->returnValue ('RESULT ' ));
83
+ $ this ->session ->start ();
84
+ $ this ->session ->set (self ::SESSION_NAMESPACE .'/token_id ' , 'RESULT ' );
148
85
149
86
$ this ->assertSame ('RESULT ' , $ this ->storage ->getToken ('token_id ' ));
150
87
}
@@ -154,18 +91,6 @@ public function testGetExistingTokenFromActiveSession()
154
91
*/
155
92
public function testGetNonExistingTokenFromClosedSession ()
156
93
{
157
- $ this ->session ->expects ($ this ->any ())
158
- ->method ('isStarted ' )
159
- ->will ($ this ->returnValue (false ));
160
-
161
- $ this ->session ->expects ($ this ->once ())
162
- ->method ('start ' );
163
-
164
- $ this ->session ->expects ($ this ->once ())
165
- ->method ('has ' )
166
- ->with (self ::SESSION_NAMESPACE .'/token_id ' )
167
- ->will ($ this ->returnValue (false ));
168
-
169
94
$ this ->storage ->getToken ('token_id ' );
170
95
}
171
96
@@ -174,85 +99,33 @@ public function testGetNonExistingTokenFromClosedSession()
174
99
*/
175
100
public function testGetNonExistingTokenFromActiveSession ()
176
101
{
177
- $ this ->session ->expects ($ this ->any ())
178
- ->method ('isStarted ' )
179
- ->will ($ this ->returnValue (true ));
180
-
181
- $ this ->session ->expects ($ this ->never ())
182
- ->method ('start ' );
183
-
184
- $ this ->session ->expects ($ this ->once ())
185
- ->method ('has ' )
186
- ->with (self ::SESSION_NAMESPACE .'/token_id ' )
187
- ->will ($ this ->returnValue (false ));
188
-
102
+ $ this ->session ->start ();
189
103
$ this ->storage ->getToken ('token_id ' );
190
104
}
191
105
192
106
public function testRemoveNonExistingTokenFromClosedSession ()
193
107
{
194
- $ this ->session ->expects ($ this ->any ())
195
- ->method ('isStarted ' )
196
- ->will ($ this ->returnValue (false ));
197
-
198
- $ this ->session ->expects ($ this ->once ())
199
- ->method ('start ' );
200
-
201
- $ this ->session ->expects ($ this ->once ())
202
- ->method ('remove ' )
203
- ->with (self ::SESSION_NAMESPACE .'/token_id ' )
204
- ->will ($ this ->returnValue (null ));
205
-
206
108
$ this ->assertNull ($ this ->storage ->removeToken ('token_id ' ));
207
109
}
208
110
209
111
public function testRemoveNonExistingTokenFromActiveSession ()
210
112
{
211
- $ this ->session ->expects ($ this ->any ())
212
- ->method ('isStarted ' )
213
- ->will ($ this ->returnValue (true ));
214
-
215
- $ this ->session ->expects ($ this ->never ())
216
- ->method ('start ' );
217
-
218
- $ this ->session ->expects ($ this ->once ())
219
- ->method ('remove ' )
220
- ->with (self ::SESSION_NAMESPACE .'/token_id ' )
221
- ->will ($ this ->returnValue (null ));
113
+ $ this ->session ->start ();
222
114
223
115
$ this ->assertNull ($ this ->storage ->removeToken ('token_id ' ));
224
116
}
225
117
226
118
public function testRemoveExistingTokenFromClosedSession ()
227
119
{
228
- $ this ->session ->expects ($ this ->any ())
229
- ->method ('isStarted ' )
230
- ->will ($ this ->returnValue (false ));
231
-
232
- $ this ->session ->expects ($ this ->once ())
233
- ->method ('start ' );
234
-
235
- $ this ->session ->expects ($ this ->once ())
236
- ->method ('remove ' )
237
- ->with (self ::SESSION_NAMESPACE .'/token_id ' )
238
- ->will ($ this ->returnValue ('TOKEN ' ));
120
+ $ this ->session ->set (self ::SESSION_NAMESPACE .'/token_id ' , 'TOKEN ' );
239
121
240
122
$ this ->assertSame ('TOKEN ' , $ this ->storage ->removeToken ('token_id ' ));
241
123
}
242
124
243
125
public function testRemoveExistingTokenFromActiveSession ()
244
126
{
245
- $ this ->session ->expects ($ this ->any ())
246
- ->method ('isStarted ' )
247
- ->will ($ this ->returnValue (true ));
248
-
249
- $ this ->session ->expects ($ this ->never ())
250
- ->method ('start ' );
251
-
252
- $ this ->session ->expects ($ this ->once ())
253
- ->method ('remove ' )
254
- ->with (self ::SESSION_NAMESPACE .'/token_id ' )
255
- ->will ($ this ->returnValue ('TOKEN ' ));
127
+ $ this ->session ->start ();
128
+ $ this ->session ->set (self ::SESSION_NAMESPACE .'/token_id ' , 'TOKEN ' );
256
129
257
130
$ this ->assertSame ('TOKEN ' , $ this ->storage ->removeToken ('token_id ' ));
258
131
}
0 commit comments