Created
October 1, 2013 07:44
-
-
Save JJK801/6775090 to your computer and use it in GitHub Desktop.
Listener test example for M6Web Article
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
<?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