Yii control unit without selenium

Suppose I have an action:

function actionShowItem($id) { $item = Item::model()->findByPk($id); $this->render("showitem",array('model' => $id)); } 

What is a simple unit test for this action, which will check the text in the view view. Its easy to use in a zend framework without using selenium. We could create fake GET and POST in zend too. But I did not find the same examples in Yii. Please suggest.

+7
source share
1 answer

The Yii PHP framework is very good in many aspects, but very disappointed that it does not support internal support for any simulated testing of controller output. It only has selenium based web browser methods. I came to Yii from ZendF, and zend has some good testing systems, including xpath based statements. Therefore, I had to understand the code stream and encode it in my /Controller.php components. This could be done without changing the framework of the kernel, which, in my opinion, is the charm of Yii.

Each client code has components /Controller.php, which is a common base class for all controllers in Yii. And the render is a CController method, which means that I could override it and capture the view output for use by unit test code.

You will need the runmode parameter (in config / main.php) to determine if you are test or production. In production, the output is just an echo, until we can drive away anything in testrun (just ruins the unit test report). In the test code, you get the output in $ render_output, on which you can check the bypass by xpath or strpos crawlers. This hack is not the best, but it works great.

 function render($view,$data=null,$return=false) { $out = parent::render($view,$data,true); if(isset(Yii::app()->params['runmode']) && Yii::app()->params['runmode'] == 'test') { global $render_output; return $render_output = $out; } if($return) return $out; else echo $out; } 
+4
source

All Articles