Olle Härstedt
Olle Härstedt

Reputation: 4020

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?

Upvotes: 2

Views: 556

Answers (2)

user2513149
user2513149

Reputation: 880

If you need a controller for multiple tests you can set it once in the setUp method.

public function setUp()
{
    parent::setUp();
    Yii::app()->controller = new CController('test');
}

And then you can use it in your tests:

Yii::app()->controller->createUrl(...)

Upvotes: 0

szako
szako

Reputation: 1301

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.

Upvotes: 2

Related Questions