Skip to content

Commit 77b2fe7

Browse files
committed
Don't rely on session service in tests
1 parent 20a6ce1 commit 77b2fe7

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

Tests/EventListener/SessionListenerTest.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,27 @@ class SessionListenerTest extends TestCase
4040
*/
4141
public function testSessionCookieOptions(array $phpSessionOptions, array $sessionOptions, array $expectedSessionOptions)
4242
{
43-
$session = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock();
44-
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
45-
$session->expects($this->exactly(1))->method('getId')->willReturn('123456');
46-
$session->expects($this->exactly(1))->method('getName')->willReturn('PHPSESSID');
47-
$session->expects($this->exactly(1))->method('save');
48-
$session->expects($this->exactly(1))->method('isStarted')->willReturn(true);
43+
$session = $this->createMock(Session::class);
44+
$session->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
45+
$session->method('getId')->willReturn('123456');
46+
$session->method('getName')->willReturn('PHPSESSID');
47+
$session->method('save');
48+
$session->method('isStarted')->willReturn(true);
4949

5050
if (isset($phpSessionOptions['samesite'])) {
5151
ini_set('session.cookie_samesite', $phpSessionOptions['samesite']);
5252
}
5353
session_set_cookie_params(0, $phpSessionOptions['path'] ?? null, $phpSessionOptions['domain'] ?? null, $phpSessionOptions['secure'] ?? null, $phpSessionOptions['httponly'] ?? null);
5454

55-
$container = new Container();
56-
$container->set('initialized_session', $session);
57-
58-
$listener = new SessionListener($container, false, $sessionOptions);
59-
$kernel = $this->getMockBuilder(HttpKernelInterface::class)->disableOriginalConstructor()->getMock();
55+
$listener = new SessionListener(new Container(), false, $sessionOptions);
56+
$kernel = $this->createMock(HttpKernelInterface::class);
6057

6158
$request = new Request();
6259
$listener->onKernelRequest(new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST));
6360

61+
$request->setSession($session);
6462
$response = new Response();
65-
$listener->onKernelResponse(new ResponseEvent($kernel, new Request(), HttpKernelInterface::MAIN_REQUEST, $response));
63+
$listener->onKernelResponse(new ResponseEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response));
6664

6765
$cookies = $response->headers->getCookies();
6866
$this->assertSame('PHPSESSID', $cookies[0]->getName());

0 commit comments

Comments
 (0)