2017-11-01 14:47:09 +01:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-11-01 14:47:09 +01:00
|
|
|
namespace Engelsystem\Test\Unit\Http;
|
|
|
|
|
2018-09-15 17:24:59 +02:00
|
|
|
use Engelsystem\Config\Config;
|
2017-11-01 14:47:09 +01:00
|
|
|
use Engelsystem\Http\Request;
|
2018-09-16 14:08:09 +02:00
|
|
|
use Engelsystem\Http\SessionHandlers\DatabaseHandler;
|
2017-11-01 14:47:09 +01:00
|
|
|
use Engelsystem\Http\SessionServiceProvider;
|
|
|
|
use Engelsystem\Test\Unit\ServiceProviderTest;
|
2019-04-24 10:45:00 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-11-01 14:47:09 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Session\Session;
|
2018-09-03 16:33:13 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
2017-11-01 14:47:09 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
|
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface as StorageInterface;
|
|
|
|
|
|
|
|
class SessionServiceProviderTest extends ServiceProviderTest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\SessionServiceProvider::getSessionStorage()
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Http\SessionServiceProvider::register()
|
2017-11-01 14:47:09 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testRegister(): void
|
2017-11-01 14:47:09 +01:00
|
|
|
{
|
|
|
|
$app = $this->getApp(['make', 'instance', 'bind', 'get']);
|
|
|
|
|
|
|
|
$sessionStorage = $this->getMockForAbstractClass(StorageInterface::class);
|
|
|
|
$sessionStorage2 = $this->getMockForAbstractClass(StorageInterface::class);
|
2018-09-16 14:08:09 +02:00
|
|
|
$databaseHandler = $this->getMockBuilder(DatabaseHandler::class)
|
2018-09-15 17:24:59 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-11-01 14:47:09 +01:00
|
|
|
|
|
|
|
$session = $this->getSessionMock();
|
|
|
|
$request = $this->getRequestMock();
|
|
|
|
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var SessionServiceProvider|MockObject $serviceProvider */
|
2017-11-01 14:47:09 +01:00
|
|
|
$serviceProvider = $this->getMockBuilder(SessionServiceProvider::class)
|
|
|
|
->setConstructorArgs([$app])
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['isCli'])
|
2017-11-01 14:47:09 +01:00
|
|
|
->getMock();
|
2018-09-15 17:24:59 +02:00
|
|
|
|
|
|
|
/** @var Config|MockObject $config */
|
2022-10-21 19:49:18 +02:00
|
|
|
$config = new Config([
|
|
|
|
'session' => ['driver' => 'native', 'name' => 'session', 'lifetime' => 2],
|
|
|
|
]);
|
2018-09-15 17:24:59 +02:00
|
|
|
|
|
|
|
$serviceProvider->expects($this->exactly(3))
|
2017-11-01 14:47:09 +01:00
|
|
|
->method('isCli')
|
2018-09-15 17:24:59 +02:00
|
|
|
->willReturnOnConsecutiveCalls(true, false, false);
|
2017-11-01 14:47:09 +01:00
|
|
|
|
2018-09-15 17:24:59 +02:00
|
|
|
$app->expects($this->exactly(7))
|
2017-11-01 14:47:09 +01:00
|
|
|
->method('make')
|
|
|
|
->withConsecutive(
|
|
|
|
[MockArraySessionStorage::class],
|
|
|
|
[Session::class],
|
2018-09-15 17:24:59 +02:00
|
|
|
[
|
|
|
|
NativeSessionStorage::class,
|
2022-10-21 18:16:49 +02:00
|
|
|
[
|
2022-10-21 19:49:18 +02:00
|
|
|
// 2 days
|
2022-10-21 18:16:49 +02:00
|
|
|
'options' => ['cookie_httponly' => true, 'name' => 'session', 'cookie_lifetime' => 172800],
|
|
|
|
'handler' => null
|
|
|
|
],
|
2018-09-15 17:24:59 +02:00
|
|
|
],
|
|
|
|
[Session::class],
|
2018-09-16 14:08:09 +02:00
|
|
|
[DatabaseHandler::class],
|
2018-09-15 17:24:59 +02:00
|
|
|
[
|
|
|
|
NativeSessionStorage::class,
|
2022-10-21 18:16:49 +02:00
|
|
|
[
|
2022-10-21 19:49:18 +02:00
|
|
|
// 5 days
|
2022-10-21 18:16:49 +02:00
|
|
|
'options' => ['cookie_httponly' => true, 'name' => 'foobar', 'cookie_lifetime' => 432000],
|
|
|
|
'handler' => $databaseHandler
|
|
|
|
],
|
2018-09-15 17:24:59 +02:00
|
|
|
],
|
2017-11-01 14:47:09 +01:00
|
|
|
[Session::class]
|
|
|
|
)
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
$sessionStorage,
|
|
|
|
$session,
|
|
|
|
$sessionStorage2,
|
2018-09-15 17:24:59 +02:00
|
|
|
$session,
|
2018-09-16 14:08:09 +02:00
|
|
|
$databaseHandler,
|
2018-09-15 17:24:59 +02:00
|
|
|
$sessionStorage2,
|
2017-11-01 14:47:09 +01:00
|
|
|
$session
|
|
|
|
);
|
|
|
|
$app->expects($this->atLeastOnce())
|
|
|
|
->method('instance')
|
|
|
|
->withConsecutive(
|
|
|
|
['session.storage', $sessionStorage],
|
2018-08-26 12:23:47 +02:00
|
|
|
[Session::class, $session],
|
2017-11-01 14:47:09 +01:00
|
|
|
['session', $session]
|
|
|
|
);
|
|
|
|
|
2018-09-16 14:08:09 +02:00
|
|
|
$app->expects($this->exactly(5))
|
2018-09-15 17:24:59 +02:00
|
|
|
->method('get')
|
|
|
|
->withConsecutive(
|
|
|
|
['request'],
|
|
|
|
['config'],
|
|
|
|
['request'],
|
|
|
|
['config'],
|
|
|
|
['request']
|
|
|
|
)
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
$request,
|
|
|
|
$config,
|
|
|
|
$request,
|
|
|
|
$config,
|
|
|
|
$request
|
|
|
|
);
|
|
|
|
|
2018-09-03 16:33:13 +02:00
|
|
|
$app->expects($this->atLeastOnce())
|
|
|
|
->method('bind')
|
|
|
|
->withConsecutive(
|
|
|
|
[StorageInterface::class, 'session.storage'],
|
|
|
|
[SessionInterface::class, Session::class]
|
|
|
|
);
|
|
|
|
|
2017-11-01 14:47:09 +01:00
|
|
|
$this->setExpects($request, 'setSession', [$session], null, $this->atLeastOnce());
|
2018-09-03 16:33:13 +02:00
|
|
|
$this->setExpects($session, 'has', ['_token'], false, $this->atLeastOnce());
|
|
|
|
$this->setExpects($session, 'set', ['_token'], null, $this->atLeastOnce());
|
2017-11-01 14:47:09 +01:00
|
|
|
$this->setExpects($session, 'start', null, null, $this->atLeastOnce());
|
|
|
|
|
|
|
|
$serviceProvider->register();
|
2022-10-21 19:49:18 +02:00
|
|
|
$serviceProvider->register(); // native handler
|
|
|
|
$config->set('session', ['driver' => 'pdo', 'name' => 'foobar', 'lifetime' => 5]);
|
|
|
|
$serviceProvider->register(); // pdo handler
|
2017-11-01 14:47:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\SessionServiceProvider::isCli()
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testIsCli(): void
|
2017-11-01 14:47:09 +01:00
|
|
|
{
|
|
|
|
$app = $this->getApp(['make', 'instance', 'bind', 'get']);
|
|
|
|
|
|
|
|
$sessionStorage = $this->getMockForAbstractClass(StorageInterface::class);
|
|
|
|
|
|
|
|
$session = $this->getSessionMock();
|
|
|
|
$request = $this->getRequestMock();
|
|
|
|
|
|
|
|
$app->expects($this->exactly(2))
|
|
|
|
->method('make')
|
|
|
|
->withConsecutive(
|
|
|
|
[MockArraySessionStorage::class],
|
|
|
|
[Session::class]
|
|
|
|
)
|
|
|
|
->willReturnOnConsecutiveCalls(
|
|
|
|
$sessionStorage,
|
|
|
|
$session
|
|
|
|
);
|
2018-08-26 12:23:47 +02:00
|
|
|
$app->expects($this->exactly(3))
|
2017-11-01 14:47:09 +01:00
|
|
|
->method('instance')
|
|
|
|
->withConsecutive(
|
|
|
|
['session.storage', $sessionStorage],
|
2018-08-26 12:23:47 +02:00
|
|
|
[Session::class, $session],
|
2017-11-01 14:47:09 +01:00
|
|
|
['session', $session]
|
|
|
|
);
|
2018-09-03 16:33:13 +02:00
|
|
|
$app->expects($this->atLeastOnce())
|
|
|
|
->method('bind')
|
|
|
|
->withConsecutive(
|
|
|
|
[StorageInterface::class, 'session.storage'],
|
|
|
|
[SessionInterface::class, Session::class]
|
|
|
|
);
|
2017-11-01 14:47:09 +01:00
|
|
|
|
|
|
|
$this->setExpects($app, 'get', ['request'], $request);
|
|
|
|
$this->setExpects($request, 'setSession', [$session]);
|
2018-09-03 16:33:13 +02:00
|
|
|
$this->setExpects($session, 'has', ['_token'], true);
|
2017-11-01 14:47:09 +01:00
|
|
|
$this->setExpects($session, 'start');
|
|
|
|
|
|
|
|
$serviceProvider = new SessionServiceProvider($app);
|
|
|
|
$serviceProvider->register();
|
|
|
|
}
|
|
|
|
|
2022-12-14 19:15:20 +01:00
|
|
|
private function getSessionMock(): MockObject
|
2017-11-01 14:47:09 +01:00
|
|
|
{
|
2017-11-12 15:38:12 +01:00
|
|
|
$sessionStorage = $this->getMockForAbstractClass(StorageInterface::class);
|
2017-11-01 14:47:09 +01:00
|
|
|
return $this->getMockBuilder(Session::class)
|
2017-11-12 15:38:12 +01:00
|
|
|
->setConstructorArgs([$sessionStorage])
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['start', 'has', 'set'])
|
2017-11-01 14:47:09 +01:00
|
|
|
->getMock();
|
|
|
|
}
|
|
|
|
|
2022-12-14 19:15:20 +01:00
|
|
|
private function getRequestMock(): MockObject
|
2017-11-01 14:47:09 +01:00
|
|
|
{
|
|
|
|
return $this->getMockBuilder(Request::class)
|
2019-11-06 12:29:58 +01:00
|
|
|
->onlyMethods(['setSession'])
|
2017-11-01 14:47:09 +01:00
|
|
|
->getMock();
|
|
|
|
}
|
|
|
|
}
|