Jeśli jesteś właścicielem tej strony, możesz wyłączyć reklamę poniżej zmieniając pakiet na PRO lub VIP w panelu naszego hostingu już od 4zł!

How to unit test redirecting in Zend Framework 2

How to unit test redirecting in Zend Framework 2

I’ve been working with Zend Framework 2 last couple of weeks and so far I like it better than the legacy Zend Framework. Proper namespaces and separate bootstrap for each module are nice features. I am still learning all the new features so I will be documenting some helpful ideas.

One problem I had today was I needed to write a unit test for a controller action which under certain condition redirects to a different action using the redirect helper:

  1. $this->redirect->roToute('routeName', array('controller' => 'index', 'action' = 'index'));

Unit testing a controller action was not a problem, I figured that out a week or two ago. In order to write an assertion for redirection, you will need to create a new instance of ZendMvcRouterSimpleRouteStack and add routes you are testing for to it. Then attach the router to ZendMvcMvcEvent.

So include few classes from Zend library plus the controller you want to test:

  1. use MyModuleControllerIndexController,
  2.     ZendHttpRequest,
  3.     ZendHttpResponse,
  4.     ZendMvcMvcEvent,
  5.     ZendMvcRouterRouteMatch,
  6.     ZendMvcRouterSimpleRouteStack,
  7.     ZendMvcRouterHttpSegment,

Then put this inside your PHPUnit test case’s setUp method:

  1. public function setUp()
  2. {
  3.     $this->_controller = new IndexController;
  4.     $this->_request = new Request;
  5.     $this->_response = new Response;
  6.  
  7.     $this->_event = new MvcEvent();
  8.  
  9.     $routeStack = new SimpleRouteStack;
  10.     $route = new Segment('/mymodule/[:controller/[:action/]]');
  11.     $routeStack->addRoute('mymodule', $route);
  12.     $this->_event->setRouter($routeStack);
  13.  
  14.     $routeMatch = new RouteMatch(array('controller' => 'index', 'action' => 'index'));
  15.     $routeMatch->setMatchedRouteName('admin');
  16.     $this->_event->setRouteMatch($routeMatch);
  17.  
  18.     $this->_controller->setEvent($this->_event);
  19. }

After you have done all that, there are two ways how to assert a redirection has actually happened.

First, you can test for a 302 HTTP status:

  1. public function testIndexActionRedirectsByHttpStatus()
  2. {
  3.     $this->_controller->dispatch($this->_request, $this->_response);
  4.     $this->assertEquals(302, $this->_response->getStatusCode());
  5. }

Second, you can check for the Location header:

  1. public function testIndexActionRedirectsToCorrectUri()
  2. {
  3.     $this->_controller->dispatch($this->_request, $this->_response);
  4.     $this->assertEquals('/mymodule/mycontroller/myaction/', $this->_event->getResponse()->getHeaders()->get('Location')->getUri());
  5. }

Ideally use both assertions to be sure.

Source: http://blog.richardknop.com/2012/09/how-to-unit-test-redirecting-in-zend-framework-2/

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

Posted Wrzesień 27th, 2012 in Zend and PHP.

Leave a response: