engelsystem/tests/Unit/Http/SessionServiceProviderTest.php

192 lines
6.5 KiB
PHP
Raw Normal View History

<?php
namespace Engelsystem\Test\Unit\Http;
2018-09-15 17:24:59 +02:00
use Engelsystem\Config\Config;
use Engelsystem\Http\Request;
use Engelsystem\Http\SessionHandlers\DatabaseHandler;
use Engelsystem\Http\SessionServiceProvider;
use Engelsystem\Test\Unit\ServiceProviderTest;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\HttpFoundation\Session\Session;
2018-09-03 16:33:13 +02:00
use Symfony\Component\HttpFoundation\Session\SessionInterface;
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()
* @covers \Engelsystem\Http\SessionServiceProvider::register()
*/
public function testRegister()
{
$app = $this->getApp(['make', 'instance', 'bind', 'get']);
$sessionStorage = $this->getMockForAbstractClass(StorageInterface::class);
$sessionStorage2 = $this->getMockForAbstractClass(StorageInterface::class);
$databaseHandler = $this->getMockBuilder(DatabaseHandler::class)
2018-09-15 17:24:59 +02:00
->disableOriginalConstructor()
->getMock();
$session = $this->getSessionMock();
$request = $this->getRequestMock();
/** @var SessionServiceProvider|MockObject $serviceProvider */
$serviceProvider = $this->getMockBuilder(SessionServiceProvider::class)
->setConstructorArgs([$app])
->onlyMethods(['isCli'])
->getMock();
2018-09-15 17:24:59 +02:00
/** @var Config|MockObject $config */
$config = $this->createMock(Config::class);
$serviceProvider->expects($this->exactly(3))
->method('isCli')
2018-09-15 17:24:59 +02:00
->willReturnOnConsecutiveCalls(true, false, false);
2018-09-15 17:24:59 +02:00
$app->expects($this->exactly(7))
->method('make')
->withConsecutive(
[MockArraySessionStorage::class],
[Session::class],
2018-09-15 17:24:59 +02:00
[
NativeSessionStorage::class,
['options' => ['cookie_httponly' => true, 'name' => 'session'], 'handler' => null],
2018-09-15 17:24:59 +02:00
],
[Session::class],
[DatabaseHandler::class],
2018-09-15 17:24:59 +02:00
[
NativeSessionStorage::class,
['options' => ['cookie_httponly' => true, 'name' => 'foobar'], 'handler' => $databaseHandler],
2018-09-15 17:24:59 +02:00
],
[Session::class]
)
->willReturnOnConsecutiveCalls(
$sessionStorage,
$session,
$sessionStorage2,
2018-09-15 17:24:59 +02:00
$session,
$databaseHandler,
2018-09-15 17:24:59 +02:00
$sessionStorage2,
$session
);
$app->expects($this->atLeastOnce())
->method('instance')
->withConsecutive(
['session.storage', $sessionStorage],
2018-08-26 12:23:47 +02:00
[Session::class, $session],
['session', $session]
);
$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
);
$config->expects($this->exactly(2))
->method('get')
->with('session')
->willReturnOnConsecutiveCalls(
['driver' => 'native', 'name' => 'session'],
['driver' => 'pdo', 'name' => 'foobar']
);
2018-09-03 16:33:13 +02:00
$app->expects($this->atLeastOnce())
->method('bind')
->withConsecutive(
[StorageInterface::class, 'session.storage'],
[SessionInterface::class, Session::class]
);
$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());
$this->setExpects($session, 'start', null, null, $this->atLeastOnce());
$serviceProvider->register();
$serviceProvider->register();
2018-09-15 17:24:59 +02:00
$serviceProvider->register();
}
/**
* @covers \Engelsystem\Http\SessionServiceProvider::isCli()
*/
public function testIsCli()
{
$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))
->method('instance')
->withConsecutive(
['session.storage', $sessionStorage],
2018-08-26 12:23:47 +02:00
[Session::class, $session],
['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]
);
$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);
$this->setExpects($session, 'start');
$serviceProvider = new SessionServiceProvider($app);
$serviceProvider->register();
}
/**
* @return MockObject
*/
private function getSessionMock()
{
2017-11-12 15:38:12 +01:00
$sessionStorage = $this->getMockForAbstractClass(StorageInterface::class);
return $this->getMockBuilder(Session::class)
2017-11-12 15:38:12 +01:00
->setConstructorArgs([$sessionStorage])
->onlyMethods(['start', 'has', 'set'])
->getMock();
}
/**
* @return MockObject
*/
private function getRequestMock()
{
return $this->getMockBuilder(Request::class)
->onlyMethods(['setSession'])
->getMock();
}
}