Skip to content

Commit

Permalink
[FrameworkBundle] added session listener for test environment
Browse files Browse the repository at this point in the history
  • Loading branch information
avalanche123 authored and fabpot committed Jan 30, 2011
1 parent b1448ec commit d1cd442
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 0 deletions.
@@ -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;
}
}
6 changes: 6 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/test.xml
Expand Up @@ -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>
Expand All @@ -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();
}
}

0 comments on commit d1cd442

Please sign in to comment.