Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FrameworkBundle] added session listener for test environment
- Loading branch information
1 parent
b1448ec
commit d1cd442
Showing
3 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
src/Symfony/Bundle/FrameworkBundle/HttpFoundation/SessionListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/Symfony/Bundle/FrameworkBundle/Tests/HttpFoundation/SessionListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |