Note that there are some explanatory texts on larger screens.

plurals
  1. PORouting tests in ZF2 / ZF2 equivalent of Zend_Test_PHPUnit_Controller_TestCase?
    text
    copied!<p>Thus far I've been testing my ZF2 controllers as follows:</p> <pre><code>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-&gt;routeMatch-&gt;setParam('action', 'index'); $result = $this-&gt;controller-&gt;dispatch($this-&gt;request); $response = $this-&gt;controller-&gt;getResponse(); $this-&gt;assertEquals(200, $response-&gt;getStatusCode()); $this-&gt;assertInstanceOf('Zend\View\Model\ViewModel', $result); } protected function setUp() { \Zend\Mvc\Application::init(include 'config/application.config.php'); $this-&gt;controller = new IndexController(); $this-&gt;request = new Request(); $this-&gt;routeMatch = new RouteMatch(array('controller' =&gt; 'index')); $this-&gt;event = new MvcEvent(); $this-&gt;event-&gt;setRouteMatch($this-&gt;routeMatch); $this-&gt;controller-&gt;setEvent($this-&gt;event); } protected $controller = null; protected $event = null; protected $request = null; protected $response = null; protected $routeMatch = null; } </code></pre> <p>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 <code>Zend_Test_PHPUnit_Controller_TestCase</code> tests would.</p> <p>In those, I'd kick off the test by running <code>$this-&gt;dispatch('/some/relative/url')</code> 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.</p> <p>How do I test that my routing is working correctly in ZF2?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload