Olle Härstedt Olle Härstedt -4 years ago 90
PHP Question

Set Yii controller dynamically in unit-test

I'm running some unit-tests using PHPUnit, and hack my application pretty hard in those tests (no other way, old code-base). Some parts of the code-base use

Yii::app()->getController()->createUrl(...);


but in this case, there is no controller, so the test fails. Is there a way to add a dummy controller dynamically in my test? Something like

Yii::app()->setController($dummyController);


Or do I have to initiate some kind of fake routing event?

Answer Source

You can simply use:

$ctrl = new CController('whatever you need for the id')

and use its methods. Be careful, construct method sets id only. You didn't provide too much code, so this is a general idea. Look inside createUrl() method and check if it should work.

I used this technique to render pages (and use their contents) under console enviroment.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download