Posterous theme by Cory Watilo

PHP Unit Testing Zend_Framework Controllers With ExtJS

I have an application that uses Zend_Framework for the Controller and Model, and uses ExtJS to handle the view. All of the user interface handling is managed on the client. Therefore my controllers return only JSON data to communicate with the stores

In setting up my PHPUnit test environment I found I had a couple of hurdles to overcome. First was the usual issues with getting the Zend Framework parts to work by setting up the bootstrap.php file correctly. The other issues had to do with retrieving the JSON data to compare for the results.

If you are setting up PHPUnit testing against a Controller that returns JSON you are going to need to:

1) Make sure your post format is correct. Remember, ExtJS sends JSON encoded form data in the form submittal.

$this->request->setPost(

array(
"data"=>Zend_Json::encode(array(

"field1"=>value1
"field2"=>value2

))

)

)

This assumes you use "data" as the parameter to pass the form data.

2) You need to capture the results and decode them

$results = $this->getFrontController()->getResponse()->getBody();

$resultsArray = Zend_Json::decode($results);

// now do your assertions, such as checking the success flag