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!
Update
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?