Skip to content

Commit a8d95dd

Browse files
xabbuhfabpot
authored andcommitted
do not mock the session in token storage tests
1 parent 2ed66b2 commit a8d95dd

File tree

1 file changed

+25
-152
lines changed

1 file changed

+25
-152
lines changed

src/Symfony/Component/Security/Csrf/Tests/TokenStorage/SessionTokenStorageTest.php

Lines changed: 25 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Security\Csrf\Tests\TokenStorage;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Session\Session;
16+
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
1517
use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;
1618

1719
/**
@@ -22,7 +24,7 @@ class SessionTokenStorageTest extends TestCase
2224
const SESSION_NAMESPACE = 'foobar';
2325

2426
/**
25-
* @var \PHPUnit_Framework_MockObject_MockObject
27+
* @var Session
2628
*/
2729
private $session;
2830

@@ -33,118 +35,53 @@ class SessionTokenStorageTest extends TestCase
3335

3436
protected function setUp()
3537
{
36-
$this->session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface')
37-
->disableOriginalConstructor()
38-
->getMock();
38+
$this->session = new Session(new MockArraySessionStorage());
3939
$this->storage = new SessionTokenStorage($this->session, self::SESSION_NAMESPACE);
4040
}
4141

42-
public function testStoreTokenInClosedSession()
42+
public function testStoreTokenInNotStartedSessionStartsTheSession()
4343
{
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-
5544
$this->storage->setToken('token_id', 'TOKEN');
45+
46+
$this->assertTrue($this->session->isStarted());
5647
}
5748

5849
public function testStoreTokenInActiveSession()
5950
{
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();
7152
$this->storage->setToken('token_id', 'TOKEN');
53+
54+
$this->assertSame('TOKEN', $this->session->get(self::SESSION_NAMESPACE.'/token_id'));
7255
}
7356

7457
public function testCheckTokenInClosedSession()
7558
{
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');
8260

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());
8963
}
9064

9165
public function testCheckTokenInActiveSession()
9266
{
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');
9969

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'));
10671
}
10772

10873
public function testGetExistingTokenFromClosedSession()
10974
{
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');
12676

12777
$this->assertSame('RESULT', $this->storage->getToken('token_id'));
78+
$this->assertTrue($this->session->isStarted());
12879
}
12980

13081
public function testGetExistingTokenFromActiveSession()
13182
{
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');
14885

14986
$this->assertSame('RESULT', $this->storage->getToken('token_id'));
15087
}
@@ -154,18 +91,6 @@ public function testGetExistingTokenFromActiveSession()
15491
*/
15592
public function testGetNonExistingTokenFromClosedSession()
15693
{
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-
16994
$this->storage->getToken('token_id');
17095
}
17196

@@ -174,85 +99,33 @@ public function testGetNonExistingTokenFromClosedSession()
17499
*/
175100
public function testGetNonExistingTokenFromActiveSession()
176101
{
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();
189103
$this->storage->getToken('token_id');
190104
}
191105

192106
public function testRemoveNonExistingTokenFromClosedSession()
193107
{
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-
206108
$this->assertNull($this->storage->removeToken('token_id'));
207109
}
208110

209111
public function testRemoveNonExistingTokenFromActiveSession()
210112
{
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();
222114

223115
$this->assertNull($this->storage->removeToken('token_id'));
224116
}
225117

226118
public function testRemoveExistingTokenFromClosedSession()
227119
{
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');
239121

240122
$this->assertSame('TOKEN', $this->storage->removeToken('token_id'));
241123
}
242124

243125
public function testRemoveExistingTokenFromActiveSession()
244126
{
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');
256129

257130
$this->assertSame('TOKEN', $this->storage->removeToken('token_id'));
258131
}

0 commit comments

Comments
 (0)