开发者

How do I unit test the zend action controller?

I need to rapidly build good software in php and 开发者_Python百科using the zend framework. I try to go at this in a TDD way because its people more experienced than me told me that was the best way to rapidly build while keeping your code manageable.

So I got the book on phpunit and went along nicely and indeed after the initial hassle it starts to speed up and the code is still nice. I kind off like how my objects can be tested individually.

There is however one major problem with testing the zend action controller. The zend_test package provides a way to test it. But that seems to test the entire application at once. I don't seem to be able to nicely stub or mock any repository's or inject any other dependency's . So i've not been able to test them as extensively as i could do with the rest of the project and it shows.

I've been looking to solve this problem. But all i could find on the net was the zend_test way of doing it.

I would like to know your opinion on this. Maybe i am just trying to over do things or maybe there is a nicer way to develop the unit test for the zend action controllers.


In Zend 1 a controller is a normal class. You can instantiate it, you can call methods on it (for example, replacing your default repository with a PHPUnit mock of your repository:

class MyController extends Zend_Controller_Action
{
   public functioni init()
   {
      $this->repository = new MyRepository();
   }

   public function setRepository($repository)
   {
      $this->repository = $repository;
   }

   public function saveAction()
   {
      $dataToWrite = manipulate in some way $this->getRequest()->getParams();
      $this->repository->update($dataToWrite, ...);
   }
}

But you must also inject a request, and dispatch it to get the response.

Personally for controllers I prefer to write functional tests rather than unit tests (with Zend_Test). It's slower, you will probably need an in-memory sqlite database, and so on. But you will know if your application really works: you can unit test every class, but if a factory that wires your objects is wrong, you will continue to get a green PHPUnit bar with a broken application.


Rob Allen wrote a very good article on testing Zend Controller Actions with PHPUnit. This uses Zend_Test_PHPUnit_ControllerTestCase, which bootstraps the whole application and tests the reponse.

PHPUnit and functional testing in general are not suited to testing controllers. Inversely, controllers are not suited to unit testing. By that I mean that the concept of unit testing doesn't make sense with concept of the controller layer, and controllers typically are constructed in a way that makes them inherently difficult to unit test.

The best alternative I can suggest is to use Selenium. This tests the response from the controller (so really tests the View in most cases). On top of the Selenium tests, you should also be unit testing your models and the rest of your library. That's about as bulletproof as you can really get in your controller layer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜