2018-12-28 03:28:33 +01:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-12-28 03:28:33 +01:00
|
|
|
namespace Engelsystem\Test\Unit\Middleware;
|
|
|
|
|
|
|
|
use Engelsystem\Middleware\SessionHandler;
|
2019-04-24 10:45:00 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-12-28 03:28:33 +01:00
|
|
|
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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testProcess(): void
|
2018-12-28 03:28:33 +01:00
|
|
|
{
|
|
|
|
/** @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')
|
2023-07-10 17:59:49 +02:00
|
|
|
->with('route-api-accessible')
|
|
|
|
->willReturnOnConsecutiveCalls(true, false);
|
2023-01-28 00:41:29 +01:00
|
|
|
|
|
|
|
$sessionStorage->expects($this->once())
|
2018-12-28 03:28:33 +01:00
|
|
|
->method('getName')
|
|
|
|
->willReturn('SESSION');
|
|
|
|
|
|
|
|
/** @var SessionHandler|MockObject $middleware */
|
|
|
|
$middleware = $this->getMockBuilder(SessionHandler::class)
|
|
|
|
->setConstructorArgs([$sessionStorage, ['/foo']])
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['destroyNative'])
|
2018-12-28 03:28:33 +01:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$middleware->expects($this->once())
|
|
|
|
->method('destroyNative')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$middleware->process($request, $handler);
|
|
|
|
$middleware->process($request, $handler);
|
|
|
|
}
|
|
|
|
}
|