Skip to content

Instantly share code, notes, and snippets.

@JJK801
Created October 1, 2013 07:44

Revisions

  1. JJK801 created this gist Oct 1, 2013.
    64 changes: 64 additions & 0 deletions listener_example_test.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    <?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;
    }
    }