summaryrefslogtreecommitdiff
path: root/tests/Unit/Middleware/SessionHandlerTest.php
blob: 3611b5c07312dc5a0e343ae5c8e48051c527d5a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php

namespace Engelsystem\Test\Unit\Middleware;

use Engelsystem\Middleware\SessionHandler;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;

class SessionHandlerTest extends TestCase
{
    /**
     * @covers \Engelsystem\Middleware\SessionHandler::__construct
     * @covers \Engelsystem\Middleware\SessionHandler::process
     */
    public function testProcess()
    {
        /** @var NativeSessionStorage|MockObject $sessionStorage */
        $sessionStorage = $this->createMock(NativeSessionStorage::class);
        /** @var ServerRequestInterface|MockObject $request */
        $request = $this->getMockForAbstractClass(ServerRequestInterface::class);
        /** @var RequestHandlerInterface|MockObject $handler */
        $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
        /** @var ResponseInterface|MockObject $response */
        $response = $this->getMockForAbstractClass(ResponseInterface::class);

        $handler->expects($this->exactly(2))
            ->method('handle')
            ->with($request)
            ->willReturn($response);

        $request->expects($this->exactly(2))
            ->method('getCookieParams')
            ->willReturnOnConsecutiveCalls([], ['SESSION' => 'BlaFoo']);

        $request->expects($this->exactly(2))
            ->method('getAttribute')
            ->with('route-request-path')
            ->willReturn('/foo');

        $sessionStorage->expects($this->exactly(2))
            ->method('getName')
            ->willReturn('SESSION');

        /** @var SessionHandler|MockObject $middleware */
        $middleware = $this->getMockBuilder(SessionHandler::class)
            ->setConstructorArgs([$sessionStorage, ['/foo']])
            ->setMethods(['destroyNative'])
            ->getMock();

        $middleware->expects($this->once())
            ->method('destroyNative')
            ->willReturn(true);

        $middleware->process($request, $handler);
        $middleware->process($request, $handler);
    }
}