Thursday, February 11, 2010

Zend PHPUnit posting to controller

Sending data to the controller to check what you get back can be tricky. Especially if you have subforms or file uploads.

Here is how if you have subforms (in this case a select and a multiselect in separate subforms):
public function testPostMissingFilledFields() {
$this ->request ->setMethod('POST')
->setPost(array(
//Selected element with value '2'
'firstLayerSubform1' =>
array('secondLayerSubform1' =>
array('thirdLayerSubform1' =>
array('select' => array('2')))),
//Selected elements with values 1159 and 1161
'firstLayerSubform2' =>
array('secondLayerSubform2' =>
array('thirdLayerSubform2' =>
array('multiselect' => array('1159', '1161')))),
));


Useful functions to see what you submit look like are:
In Test file:
var_dump($this->request->getParams());
In controller:
var_dump($this->_request->getParams());

If you find a way to check for uploaded files please tell me. :)

No comments:

Post a Comment