简体   繁体   中英

Routing tests in ZF2 / ZF2 equivalent of Zend_Test_PHPUnit_Controller_TestCase?

Thus far I've been testing my ZF2 controllers as follows:

namespace Application\Controller;

use Application\Controller\IndexController;
use Zend\Http\Request;
use Zend\Http\Response;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;
use PHPUnit_Framework_TestCase;

class IndexControllerTest extends PHPUnit_Framework_TestCase
{
    public function testIndexActionCanBeAccessed()
    {
        $this->routeMatch->setParam('action', 'index');

        $result   = $this->controller->dispatch($this->request);
        $response = $this->controller->getResponse();

        $this->assertEquals(200, $response->getStatusCode());
        $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
    }

    protected function setUp()
    {
        \Zend\Mvc\Application::init(include 'config/application.config.php');

        $this->controller = new IndexController();
        $this->request    = new Request();
        $this->routeMatch = new RouteMatch(array('controller' => 'index'));
        $this->event      = new MvcEvent();
        $this->event->setRouteMatch($this->routeMatch);
        $this->controller->setEvent($this->event);
    }

    protected $controller = null;
    protected $event = null;
    protected $request = null;
    protected $response = null;
    protected $routeMatch = null;
}

This allows me to test that the ViewModel is having the correct data (if any) assigned to it before the view is rendered. This serves that purpose just fine, but what it doesn't do is test that my routing is working correctly like the ZF1 Zend_Test_PHPUnit_Controller_TestCase tests would.

In those, I'd kick off the test by running $this->dispatch('/some/relative/url') and only get positive test results if the routes were set up correctly. With these ZF2 tests, I'm specifically telling it which route to use, which doesn't necessarily mean that a real request will be routed correctly.

How do I test that my routing is working correctly in ZF2?

I'm late to the party, but it still could be useful for newcomers. The solution nowadays would be to inherit from \\Zend\\Test\\PHPUnit\\Controller\\AbstractControllerTestCase , so the usage would be very similar to ZF1:

class IndexControllerTest extends \Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase
{
    public function setUp()
    {
        $this->setApplicationConfig(
                include __DIR__ . '/../../../../../config/application.config.php'
        );
        parent::setUp();
    }

    public function testIndexActionCanBeAccessed()
    {
        $this->dispatch('/');

        $this->assertResponseStatusCode(200);
        $this->assertModuleName('application');
        $this->assertControllerName('application\controller\index');
        $this->assertControllerClass('IndexController');
        $this->assertMatchedRouteName('home');

        $this->assertQuery('html > head');
    }
}

Note: This uses \\Zend\\Test\\PHPUnit\\Controller\\AbstractHttpControllerTestCase which includes assertQuery($path) as well as other web related methods.

EDIT: ZF2 has been updated since I self-answered this. PowerKiki's answer is better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM