[FrameworkBundle] added session listener for test environment
This commit is contained in:
parent
b1448ecdc7
commit
d1cd442361
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Bundle\FrameworkBundle\HttpFoundation;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
|
||||
/**
|
||||
* SessionListener.
|
||||
*
|
||||
* Saves session in test environment.
|
||||
*
|
||||
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
|
||||
*/
|
||||
class SessionListener
|
||||
{
|
||||
/**
|
||||
* @var Request
|
||||
*/
|
||||
private $request;
|
||||
|
||||
/**
|
||||
* @var Boolean
|
||||
*/
|
||||
private $master = false;
|
||||
|
||||
/**
|
||||
* Assigns request and its type on 'core.request' event
|
||||
*
|
||||
* @param EventInterface $event
|
||||
*/
|
||||
public function handle(EventInterface $event)
|
||||
{
|
||||
$this->request = $event->get('request');
|
||||
$this->master = HttpKernelInterface::MASTER_REQUEST === $event->get('request_type');
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if session was initialized and saves if current request is master
|
||||
* Runs on 'core.response' in test environment
|
||||
*
|
||||
* @param EventInterface $event
|
||||
* @param Response $response
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function filter(EventInterface $event, Response $response)
|
||||
{
|
||||
if (isset($this->request) && $this->master && null !== $this->request->getSession()) {
|
||||
$this->request->getSession()->save();
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true is current request is master request
|
||||
*
|
||||
* @return Boolean
|
||||
*/
|
||||
public function isMaster()
|
||||
{
|
||||
return $this->master;
|
||||
}
|
||||
}
|
@ -9,6 +9,7 @@
|
||||
<parameter key="test.client.parameters" type="collection"></parameter>
|
||||
<parameter key="test.client.history.class">Symfony\Component\BrowserKit\History</parameter>
|
||||
<parameter key="test.client.cookiejar.class">Symfony\Component\BrowserKit\CookieJar</parameter>
|
||||
<parameter key="test.session.listener.class">Symfony\Bundle\FrameworkBundle\HttpFoundation\SessionListener</parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
@ -22,5 +23,10 @@
|
||||
<service id="test.client.history" class="%test.client.history.class%" scope="prototype" />
|
||||
|
||||
<service id="test.client.cookiejar" class="%test.client.cookiejar.class%" scope="prototype" />
|
||||
|
||||
<service id="test.session.listener" class="%test.session.listener.class%">
|
||||
<tag name="kernel.listener" event="core.request" method="handle" />
|
||||
<tag name="kernel.listener" event="core.response" method="filter" />
|
||||
</service>
|
||||
</services>
|
||||
</container>
|
||||
|
@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Bundle\FrameworkBundle\Tests\HttpFoundation;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\HttpFoundation\SessionListener;
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
|
||||
/**
|
||||
* SessionListenerTest.
|
||||
*
|
||||
* Tests SessionListener.
|
||||
*
|
||||
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
|
||||
*/
|
||||
class SessionListenerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
private $listener;
|
||||
private $session;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->listener = new SessionListener();
|
||||
$this->session = $this->getSession();
|
||||
}
|
||||
|
||||
public function testShouldSaveMasterRequestSession()
|
||||
{
|
||||
$this->handle(new Request());
|
||||
|
||||
$this->assertRequestIsMaster();
|
||||
|
||||
$this->sessionMustBeSaved();
|
||||
|
||||
$this->filterResponse();
|
||||
}
|
||||
|
||||
public function testShouldNotSaveSubRequestSession()
|
||||
{
|
||||
$this->handle(new Request(), HttpKernelInterface::SUB_REQUEST);
|
||||
|
||||
$this->assertRequestIsNotMaster();
|
||||
|
||||
$this->sessionMustNotBeSaved();
|
||||
|
||||
$this->filterResponse();
|
||||
}
|
||||
|
||||
private function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST)
|
||||
{
|
||||
$request->setSession($this->session);
|
||||
|
||||
$this->listener->handle(new Event($this, 'core.request', array(
|
||||
'request' => $request,
|
||||
'request_type' => $type
|
||||
)));
|
||||
}
|
||||
|
||||
private function filterResponse()
|
||||
{
|
||||
$response = new Response();
|
||||
|
||||
$this->assertSame($response, $this->listener->filter(new Event(
|
||||
$this, 'core.response'
|
||||
), $response));
|
||||
}
|
||||
|
||||
private function assertRequestIsMaster()
|
||||
{
|
||||
$this->assertTrue($this->listener->isMaster());
|
||||
}
|
||||
|
||||
private function assertRequestIsNotMaster()
|
||||
{
|
||||
$this->assertFalse($this->listener->isMaster());
|
||||
}
|
||||
|
||||
private function sessionMustNotBeSaved()
|
||||
{
|
||||
$this->session->expects($this->never())
|
||||
->method('save');
|
||||
}
|
||||
|
||||
private function sessionMustBeSaved()
|
||||
{
|
||||
$this->session->expects($this->once())
|
||||
->method('save');
|
||||
}
|
||||
|
||||
private function getSession()
|
||||
{
|
||||
return $this->getMockBuilder('Symfony\Component\HttpFoundation\Session')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user