I need to rapidly build good software in php and 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.