I'm just starting to look into unit testing Zend 2 applications, so forgive me if this is a stupid question.
Let's say I have a controller which updates a model with an array of data, and returns some JSON:
public function testAction($data){
$model = new \my
amespace\model();
$model->updateFromArray($data);
return new JsonModel(array(
'success' => true,
));
}
In this instance, pretty much all of the actual work is done inside the model, in the updateFromArray() method. All the controller does is call this method. When writing unit tests for this controller, I basically have a couple of tests which test that the 'testAction' can be accessed through a particular URL, and that the action returns an instance of JsonModel.
Now I am aware that I need to also unit test the model's 'updateFromArray' method a lot, since it contains all sorts of validation rules etc.
My question is, do I apply these tests to the controller (i.e. write a test that gets to the 'testAction' and send it lots of different arrays of data) or do I directly test the model by just submitting lots of different arrays directly to the updateFromArray method? Or both?
I hope this makes sense!
Just to clarify, should I test like this:
//send lots of different requests to the controller and see if it works
class myControllerTest extends \PHPUnit_Framework_TestCase{
//some code...//
public function testWhatHappensWhenSubmitX(){
$this->request->getPost()->set('somevariable','x');
$result = $this->controller->dispatch($this->request);
$response = $this->controller->getResponse();
$this->assertSomething();
}
public function testWhatHappensWhenSubmitY(){
$this->request->getPost()->set('somevariable','y');
$result = $this->controller->dispatch($this->request);
$response = $this->controller->getResponse();
$this->assertSomething();
}
public function testWhatHappensWhenSubmitZ(){
$this->request->getPost()->set('somevariable','z');
$result = $this->controller->dispatch($this->request);
$response = $this->controller->getResponse();
$this->assertSomething();
}
}
Or this:
//have lots of tests against the model
class myModelTest extends \PHPUnit_Framework_TestCase{
public function setUp(){
$this->model = new \my
amespace\model()
}
public function testWhatHappensWhenSubmitX(){
$this->$model->doSomething('x');
$this->assertSomething();
}
public function testWhatHappensWhenSubmitY(){
$this->$model->doSomething('y');
$this->assertSomething();
}
public function testWhatHappensWhenSubmitZ(){
$this->$model->doSomething('z');
$this->assertSomething();
}
}
Or, should I do both?
We do both, but to different extents.
Your first style, we only use for a select few system-critical features, as a sort of "smoke-test" to make sure everything runs together. It's important to note that these are not considered true unit tests, but more "functional" tests as the result includes the interaction of your controller with any other classes it uses/interacts with.
Your second style is what is most common when we test our controllers.
You should check out the mocking framework, "Phake" http://phake.digitalsandwich.com/docs/html/
This will help you isolate your controller's functionality and test it properly.
Your controller tests should only test the controller itself, nothing else. The methods inside the model will be tested by another class that tests only that model.
You should also read about Mocking Objects, it will be useful.