Added RequestServiceProvider and SessionServiceProvider
parent
e727b367cc
commit
ad948bdd32
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Engelsystem\Http;
|
||||||
|
|
||||||
|
use Engelsystem\Container\ServiceProvider;
|
||||||
|
|
||||||
|
class RequestServiceProvider extends ServiceProvider
|
||||||
|
{
|
||||||
|
public function register()
|
||||||
|
{
|
||||||
|
$request = $this->app->call([Request::class, 'createFromGlobals']);
|
||||||
|
$this->app->instance('request', $request);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Engelsystem\Http;
|
||||||
|
|
||||||
|
use Engelsystem\Container\ServiceProvider;
|
||||||
|
use Symfony\Component\HttpFoundation\Session\Session;
|
||||||
|
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||||
|
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
|
||||||
|
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
|
||||||
|
|
||||||
|
class SessionServiceProvider extends ServiceProvider
|
||||||
|
{
|
||||||
|
public function register()
|
||||||
|
{
|
||||||
|
$sessionStorage = $this->getSessionStorage();
|
||||||
|
$this->app->instance('session.storage', $sessionStorage);
|
||||||
|
$this->app->bind(SessionStorageInterface::class, 'session.storage');
|
||||||
|
|
||||||
|
$session = $this->app->make(Session::class);
|
||||||
|
$this->app->instance('session', $session);
|
||||||
|
|
||||||
|
/** @var Request $request */
|
||||||
|
$request = $this->app->get('request');
|
||||||
|
$request->setSession($session);
|
||||||
|
|
||||||
|
$session->start();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the session storage
|
||||||
|
*
|
||||||
|
* @return SessionStorageInterface
|
||||||
|
*/
|
||||||
|
protected function getSessionStorage()
|
||||||
|
{
|
||||||
|
if ($this->isCli()) {
|
||||||
|
return $this->app->make(MockArraySessionStorage::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->app->make(NativeSessionStorage::class, ['options' => ['cookie_httponly' => true]]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if is called from cli
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function isCli()
|
||||||
|
{
|
||||||
|
return PHP_SAPI == 'cli';
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Engelsystem\Test\Unit\Http;
|
||||||
|
|
||||||
|
use Engelsystem\Http\Request;
|
||||||
|
use Engelsystem\Http\RequestServiceProvider;
|
||||||
|
use Engelsystem\Test\Unit\ServiceProviderTest;
|
||||||
|
use PHPUnit_Framework_MockObject_MockObject as MockObject;
|
||||||
|
|
||||||
|
class RequestServiceProviderTest extends ServiceProviderTest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @covers \Engelsystem\Http\RequestServiceProvider::register()
|
||||||
|
*/
|
||||||
|
public function testRegister()
|
||||||
|
{
|
||||||
|
/** @var MockObject|Request $request */
|
||||||
|
$request = $this->getMockBuilder(Request::class)
|
||||||
|
->getMock();
|
||||||
|
|
||||||
|
$app = $this->getApp(['call', 'instance']);
|
||||||
|
|
||||||
|
$this->setExpects($app, 'call', [[Request::class, 'createFromGlobals']], $request);
|
||||||
|
$this->setExpects($app, 'instance', ['request', $request]);
|
||||||
|
|
||||||
|
$serviceProvider = new RequestServiceProvider($app);
|
||||||
|
$serviceProvider->register();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,126 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Engelsystem\Test\Unit\Http;
|
||||||
|
|
||||||
|
use Engelsystem\Http\Request;
|
||||||
|
use Engelsystem\Http\SessionServiceProvider;
|
||||||
|
use Engelsystem\Test\Unit\ServiceProviderTest;
|
||||||
|
use PHPUnit_Framework_MockObject_MockObject as MockObject;
|
||||||
|
use Symfony\Component\HttpFoundation\Session\Session;
|
||||||
|
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::register()
|
||||||
|
* @covers \Engelsystem\Http\SessionServiceProvider::getSessionStorage()
|
||||||
|
*/
|
||||||
|
public function testRegister()
|
||||||
|
{
|
||||||
|
$app = $this->getApp(['make', 'instance', 'bind', 'get']);
|
||||||
|
|
||||||
|
$sessionStorage = $this->getMockForAbstractClass(StorageInterface::class);
|
||||||
|
$sessionStorage2 = $this->getMockForAbstractClass(StorageInterface::class);
|
||||||
|
|
||||||
|
$session = $this->getSessionMock();
|
||||||
|
$request = $this->getRequestMock();
|
||||||
|
|
||||||
|
/** @var MockObject|SessionServiceProvider $serviceProvider */
|
||||||
|
$serviceProvider = $this->getMockBuilder(SessionServiceProvider::class)
|
||||||
|
->setConstructorArgs([$app])
|
||||||
|
->setMethods(['isCli'])
|
||||||
|
->getMock();
|
||||||
|
$serviceProvider->expects($this->exactly(2))
|
||||||
|
->method('isCli')
|
||||||
|
->willReturnOnConsecutiveCalls(true, false);
|
||||||
|
|
||||||
|
$app->expects($this->exactly(4))
|
||||||
|
->method('make')
|
||||||
|
->withConsecutive(
|
||||||
|
[MockArraySessionStorage::class],
|
||||||
|
[Session::class],
|
||||||
|
[NativeSessionStorage::class, ['options' => ['cookie_httponly' => true]]],
|
||||||
|
[Session::class]
|
||||||
|
)
|
||||||
|
->willReturnOnConsecutiveCalls(
|
||||||
|
$sessionStorage,
|
||||||
|
$session,
|
||||||
|
$sessionStorage2,
|
||||||
|
$session
|
||||||
|
);
|
||||||
|
$app->expects($this->atLeastOnce())
|
||||||
|
->method('instance')
|
||||||
|
->withConsecutive(
|
||||||
|
['session.storage', $sessionStorage],
|
||||||
|
['session', $session]
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->setExpects($app, 'bind', [StorageInterface::class, 'session.storage'], null, $this->atLeastOnce());
|
||||||
|
$this->setExpects($app, 'get', ['request'], $request, $this->atLeastOnce());
|
||||||
|
$this->setExpects($request, 'setSession', [$session], null, $this->atLeastOnce());
|
||||||
|
$this->setExpects($session, 'start', null, null, $this->atLeastOnce());
|
||||||
|
|
||||||
|
$serviceProvider->register();
|
||||||
|
$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
|
||||||
|
);
|
||||||
|
$app->expects($this->exactly(2))
|
||||||
|
->method('instance')
|
||||||
|
->withConsecutive(
|
||||||
|
['session.storage', $sessionStorage],
|
||||||
|
['session', $session]
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->setExpects($app, 'bind', [StorageInterface::class, 'session.storage']);
|
||||||
|
$this->setExpects($app, 'get', ['request'], $request);
|
||||||
|
$this->setExpects($request, 'setSession', [$session]);
|
||||||
|
$this->setExpects($session, 'start');
|
||||||
|
|
||||||
|
$serviceProvider = new SessionServiceProvider($app);
|
||||||
|
$serviceProvider->register();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return MockObject
|
||||||
|
*/
|
||||||
|
private function getSessionMock()
|
||||||
|
{
|
||||||
|
return $this->getMockBuilder(Session::class)
|
||||||
|
->setMethods(['start'])
|
||||||
|
->getMock();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return MockObject
|
||||||
|
*/
|
||||||
|
private function getRequestMock()
|
||||||
|
{
|
||||||
|
return $this->getMockBuilder(Request::class)
|
||||||
|
->setMethods(['setSession'])
|
||||||
|
->getMock();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue