Skip to content

Instantly share code, notes, and snippets.

@JJK801
Created October 1, 2013 07:44
Show Gist options
  • Save JJK801/6775090 to your computer and use it in GitHub Desktop.
Save JJK801/6775090 to your computer and use it in GitHub Desktop.
Listener test example for M6Web Article
<?php
namespace M6\Bundle\MyBundle\Tests\Units\Listener;
use M6\Bundle\MyBundle\Listener;
use Symfony\Component\HttpFoundation\Request;
class MyEventSubscriber
{
public function testOnRequest()
{
$request = $this->getRequestMock(‘/tested/path’);
$event = $this->getGetResponseEventMock($request);
$helper = $this->getMyHelperMock(array(
‘pathInfo’ => ‘/tested/path’,
));
$subscriber = new Listener\MyEventSubscriber($helper);
$subscriber->onRequest($event);
$this->mock($helper)->call(‘doSomething’)->once();
}
protected function getRequestMock($path)
{
$request = new \mock\Symfony\Component\HttpFoundation\Request();
$request->getMockController()->getPathInfo = function () use ($path) {
return $path;
};
return $request;
}
protected function getGetResponseEventMock(Request $request)
{
$this->mockGenerator->orphanize('__construct');
$event = new \mock\Symfony\Component\HttpKernel\Event\GetResponseEvent();
$event->getMockController()->getRequest = function () use ($request) {
return $request;
};
return $event;
}
protected function getMyHelperMock($data = array())
{
$this->mockGenerator->orphanize('__construct');
$self = $this;
$helper = new \mock\M6\Bundle\MyBundle\Helper\MyHelper();
$helper->getMockController()->doSomething = function ($pathInfo) use ($self, $data) {
if (isset($data[‘pathInfo’])) { // If pathInfo key exists, data is tested
$self
->assert
->string($pathInfo)
->isIdenticalTo($data[‘pathInfo’]);
}
};
return $helper;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment