Skip to content

Instantly share code, notes, and snippets.

@devdrops
Last active June 15, 2016 23:12
Show Gist options
  • Select an option

  • Save devdrops/03d5a27c6afbf24c848042314bf6ed26 to your computer and use it in GitHub Desktop.

Select an option

Save devdrops/03d5a27c6afbf24c848042314bf6ed26 to your computer and use it in GitHub Desktop.
Symfony 2.7, PHPUnit and a Listener with RedirectResponse
<?php
namespace My\Awesome\Namespace;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class HomeRedirect
{
/**
* @var bool
*/
private $redirectSwitch;
/**
* @var string
*/
private $redirectUrl;
/**
* @var string
*/
const URI_MATCH = '/';
/**
* @param bool $redirectSwitch Provided from parameters.yml. Defines when the redirect should be performed or not.
* @param string $redirectUrl Provided from parameters.yml. Defines the URL to RedirectResponse.
*/
public function __construct($redirectSwitch, $redirectUrl)
{
$this->redirectSwitch = $redirectSwitch;
$this->redirectUrl = $redirectUrl;
}
/**
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
*/
public function verify(GetResponseEvent $event)
{
$request = $event->getRequest();
if ($this->redirectSwitch == true && $request->getRequestUri() === self::URI_MATCH) {
$event->setResponse(new RedirectResponse($this->redirectUrl));
}
}
}
<?php
namespace My\Awesome\Test\Namespace;
use My\Awesome\Namespace\HomeRedirect;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
class HomeRedirectTest extends \PHPUnit_Framework_TestCase
{
/**
* The HomeRedirect listener will provide a RedirectResponse on this scenario,
* so this test expects a HTTP Status Code 302 (Found).
*/
public function testWithRedirectAndRequestToHome()
{
$subject = new HomeRedirect(true, 'http://thecodinglove.com/');
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addListener('kernel.request', [$subject, 'verify']);
$request = Request::create('/', 'GET');
$httpKernelInterface = $this->getMock('\Symfony\Component\HttpKernel\HttpKernelInterface');
$event = new GetResponseEvent($httpKernelInterface, $request, HttpKernelInterface::MASTER_REQUEST);
$resultEvent = $eventDispatcher->dispatch('kernel.request', $event);
$response = $resultEvent->getResponse();
$this->assertEquals(302, $response->getStatusCode());
}
/**
* The HomeRedirect listener should not provide any redirects here, so this
* test expects NULL as the Response.
*/
public function testWithRedirectAndRequestToSearch()
{
$subject = new HomeRedirect(true, 'http://thecodinglove.com/');
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addListener('kernel.request', [$subject, 'verify']);
$request = Request::create('/awesome-uri', 'GET');
$httpKernelInterface = $this->getMock('\Symfony\Component\HttpKernel\HttpKernelInterface');
$event = new GetResponseEvent($httpKernelInterface, $request, HttpKernelInterface::MASTER_REQUEST);
$resultEvent = $eventDispatcher->dispatch('kernel.request', $event);
$this->assertEquals(null, $resultEvent->getResponse());
}
/**
* The HomeRedirect listener should not provide any redirects here, so this
* test expects NULL as the Response.
*/
public function testWithoutRedirectAndRequestToHome()
{
$subject = new HomeRedirect(false, 'http://thecodinglove.com/');
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addListener('kernel.request', [$subject, 'verify']);
$request = Request::create('/', 'GET');
$httpKernelInterface = $this->getMock('\Symfony\Component\HttpKernel\HttpKernelInterface');
$event = new GetResponseEvent($httpKernelInterface, $request, HttpKernelInterface::MASTER_REQUEST);
$resultEvent = $eventDispatcher->dispatch('kernel.request', $event);
$this->assertEquals(null, $resultEvent->getResponse());
}
/**
* The HomeRedirect listener should not provide any redirects here, so this
* test expects NULL as the Response.
*/
public function testWithoutRedirectAndRequestToSearch()
{
$subject = new HomeRedirect(false, 'http://thecodinglove.com/');
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addListener('kernel.request', [$subject, 'verify']);
$request = Request::create('/awesome-uri', 'GET');
$httpKernelInterface = $this->getMock('\Symfony\Component\HttpKernel\HttpKernelInterface');
$event = new GetResponseEvent($httpKernelInterface, $request, HttpKernelInterface::MASTER_REQUEST);
$resultEvent = $eventDispatcher->dispatch('kernel.request', $event);
$this->assertEquals(null, $resultEvent->getResponse());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment