How do I unit test the zend action controller?

I need to quickly create good software in php and use the zend framework. I am trying to do this in the TDD way because his people, more experienced than me, have told me that this is the best way to build quickly while keeping your code manageable.

So I got the book in phpunit and went well and really after the initial hassle, it starts to speed up, and the code is still good. I like how my objects can be tested individually.

However, there is one serious problem when testing the zend action controller. The zend_test package provides the ability to test it. But this seems to check the entire application at once. It seems that I can’t stub or mock beautifully any repository or introduce any other dependency. Therefore, I could not verify them as widely as I could do with the rest of the project, and this shows.

I was looking for a solution to this problem. But all I could find on the net was the way zend_test did it.

I would like to know your opinion on this matter. Maybe I'm just trying to do something, or maybe there is a better way to develop a unit test for zend action controllers.

+4
source share
2 answers

In Zend 1, the controller is a normal class. You can create an instance of it, you can call methods on it (for example, replacing your default repository with the 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 enter a request and send it in order to get a response.

Personally, for controllers, I prefer to write functional tests rather than unit tests (with Zend_Test ). This is slower, you probably need a sqlite database in memory and so on. But you will know if your application really works: you can unit test each class, but if the factory that forces your objects erroneously, you will continue to get the green PHPUnit panel with the broken application.

+1
source

Rob Allen has written a very good article on testing Zend Controller actions using PHPUnit. This uses Zend_Test_PHPUnit_ControllerTestCase , which downloads the entire application and validates the response.

PHPUnit and functional testing in general are not suitable for testing controllers. Conversely, controllers are not suitable for unit testing. By this, I mean that the concept of unit testing does not make sense with the concept of a controller level, and controllers are usually built in such a way that it is difficult for them to perform a unit test.

The best alternative I can offer is to use Selenium . This checks the response from the controller (so it really checks the view in most cases). Besides the Selenium tests, you should also be unit testing your models and the rest of your library. It's like bulletproof, as you can really get into your controller layer.

+1
source

All Articles