10000 [Security] refactored tests · symfony/symfony@3387612 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3387612

Browse files
committed
[Security] refactored tests
1 parent 2040770 commit 3387612

File tree

1 file changed

+31
-49
lines changed

1 file changed

+31
-49
lines changed

src/Symfony/Component/Security/Http/Tests/Authentication/DefaultAuthenticationSuccessHandlerTest.php

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,28 @@
1212
namespace Symfony\Component\Security\Http\Tests\Authentication;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpFoundation\Response;
1617
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
1718

1819
class DefaultAuthenticationSuccessHandlerTest extends TestCase
1920
{
2021
private $httpUtils = null;
21-
22-
private $request = null;
23-
2422
private $token = null;
2523

2624
protected function setUp()
2725
{
2826
$this->httpUtils = $this->getMockBuilder('Symfony\Component\Security\Http\HttpUtils')->getMock();
29-
$this->request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
30-
$this->request->headers = $this->getMockBuilder('Symfony\Component\HttpFoundation\HeaderBag')->getMock();
3127
$this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
3228
}
3329

3430
public function testRequestIsRedirected()
3531
{
36-
$response = $this->expectRedirectResponse('/');
32+
$request = Request::create('/');
33+
$response = $this->expectRedirectResponse($request, '/');
3734

3835
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array());
39-
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
36+
$result = $handler->onAuthenticationSuccess($request, $this->token);
4037

4138
$this->assertSame($response, $result);
4239
}
@@ -48,40 +45,34 @@ public function testDefaultTargetPathCanBeForced()
4845
'default_target_path' => '/dashboard',
4946
);
5047

51-
$response = $this->expectRedirectResponse('/dashboard');
48+
$request = Request::create('/');
49+
$response = $this->expectRedirectResponse($request, '/dashboard');
5250

5351
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options);
54-
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
52+
$result = $handler->onAuthenticationSuccess($request, $this->token);
5553

5654
$this->assertSame($response, $result);
5755
}
5856

5957
public function testTargetPathIsPassedWithRequest()
6058
{
61-
$this->request->expects($this->once())
62-
->method('get')->with('_target_path')
63-
->will($this->returnValue('/dashboard'));
64-
65-
$response = $this->expectRedirectResponse('/dashboard');
59+
$request = Request::create('/?_target_path=/dashboard');
60+
$response = $this->expectRedirectResponse($request, '/dashboard');
6661

6762
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array());
68-
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
63+
$result = $handler->onAuthenticationSuccess($request, $this->token);
6964

7065
$this->assertSame($response, $result);
7166
}
7267

7368
public function testTargetPathParameterIsCustomised()
7469
{
7570
$options = array('target_path_parameter' => '_my_target_path');
76-
77-
$this->request->expects($this->once())
78-
->method('get')->with('_my_target_path')
79-
->will($this->returnValue('/dashboard'));
80-
81-
$response = $this->expectRedirectResponse('/dashboard');
71+
$request = Request::create('/?_my_target_path=/dashboard');
72+
$response = $this->expectRedirectResponse($request, '/dashboard');
8273

8374
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options);
84-
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
75+
$result = $handler->onAuthenticationSuccess($request, $this->token);
8576

8677
$this->assertSame($response, $result);
8778
}
@@ -95,74 +86,65 @@ public function testTargetPathIsTakenFromTheSession()
9586
$session->expects($this->once())
9687
->method('remove')->with('_security.admin.target_path');
9788

98-
$this->request->expects($this->any())
99-
->method('getSession')
100-
->will($this->returnValue($session));
101-
102-
$response = $this->expectRedirectResponse('/admin/dashboard');
89+
$request = Request::create('/?_my_target_path=/dashboard');
90+
$request->setSession($session);
91+
$response = $this->expectRedirectResponse($request, '/admin/dashboard');
10392

10493
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array());
10594
$handler->setProviderKey('admin');
10695

107-
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
96+
$result = $handler->onAuthenticationSuccess($request, $this->token);
10897

10998
$this->assertSame($response, $result);
11099
}
111100

112101
public function testTargetPathIsPassedAsReferer()
113102
{
114103
$options = array('use_referer' => true);
115-
116-
$this->request->headers->expects($this->once())
117-
->method('get')->with('Referer')
118-
->will($this->returnValue('/dashboard'));
119-
120-
$response = $this->expectRedirectResponse('/dashboard');
104+
$request = Request::create('/');
105+
$request->headers->set('Referer', '/dashboard');
106+
$response = $this->expectRedirectResponse($request, '/dashboard');
121107

122108
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options);
123-
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
109+
$result = $handler->onAuthenticationSuccess($request, $this->token);
124110

125111
$this->assertSame($response, $result);
126112
}
127113

128114
public function testRefererHasToBeDifferentThatLoginUrl()
129115
{
130116
$options = array('use_referer' => true);
131-
132-
$this->request->headers->expects($this->any())
133-
->method('get')->with('Referer')
134-
->will($this->returnValue('/login'));
135-
117+
$request = Request::create('/');
118+
$request->headers->set('Referer', '/login');
136119
$this->httpUtils->expects($this->once())
137-
->method('generateUri')->with($this->request, '/login')
120+
->method('generateUri')->with($request, '/login')
138121
->will($this->returnValue('/login'));
139122

140-
$response = $this->expectRedirectResponse('/');
123+
$response = $this->expectRedirectResponse($request, '/');
141124

142125
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, $options);
143-
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
126+
$result = $handler->onAuthenticationSuccess($request, $this->token);
144127

145128
$this->assertSame($response, $result);
146129
}
147130

148131
public function testRefererTargetPathIsIgnoredByDefault()
149132
{
150-
$this->request->headers->expects($this->never())->method('get');
151-
152-
$response = $this->expectRedirectResponse('/');
133+
$request = Request::create('/');
134+
$response = $this->expectRedirectResponse($request, '/');
153135

154136
$handler = new DefaultAuthenticationSuccessHandler($this->httpUtils, array());
155-
$result = $handler->onAuthenticationSuccess($this->request, $this->token);
137+
$result = $handler->onAuthenticationSuccess($request, $this->token);
156138

157139
$this->assertSame($response, $result);
158140
}
159141

160-
private function expectRedirectResponse($path)
142+
private function expectRedirectResponse(Request $request, $path)
161143
{
162144
$response = new Response();
163145
$this->httpUtils->expects($this->once())
164146
->method('createRedirectResponse')
165-
->with($this->request, $path)
147+
->with($request, $path)
166148
->will($this->returnValue($response));
167149

168150
return $response;

0 commit comments

Comments
 (0)
0