PHPUnit test with getBaseURL()

This may just be a configuration thing with PhpUnit (I’m relatively new using it), but I’m having testing fail because the getBaseURL() isn’t returning a correct URL within a test.

For example, I have a class and function:




	public static function GetImagePath($strFile) {

		return Yii::app()->getBaseUrl() . "/images/" . $strFile;

	}

Within the “real” site, this works, I’ll get www.example.com/images/test1.jpg

However, when I test this in PHPUnit:


class ImageTest extends CDbTestCase

{


	public function testGetImagePath() {


		$strName = Images::GetImagePath('test1.jpg');

		$this->assertEquals("www.example.com/images/test1.jpg",$strName);




	}

}

This will fail because the URL being generated is:

www.example.com/private/var/folders/0l/znklvw_x7fb7phtyhdp9q24w0000gn/T/images/test1.jpg

(I’ve manually defined $_SERVER[‘SERVER_NAME’] in my phpunit.xml to even get that.)

Any suggestions?

(note these URLs have the H T T P prefix on them but this forum isn’t letting me post with that because it think’s I’m spamming links.)

buddy change your


Yii::app()->getBaseUrl() 

with


Yii::app()->request->baseUrl

That wouldn’t make any sense as Yii::app()->getBaseUrl() is a shortcut to request->baseUrl.

I’m struggling with the same problem when setting up unit testing. Thanks for the suggestion anyway to set the servername.

I have the same problem with this and other Yii::app() -calls. Since I am new in testing with PHPUnit I am also looking for an answer. I’ll write what I found till now - correct if I am wrong

Yii::app() -calls are static (::). They bring data from outside the method - and you can’t trust on which data is delivered. While testing a method you don’t want to have such disturbance and so you mock (or stub ?) this external calls.

On PHPUnit.de you can read:

I hope not but I fear: that makes testing impossible. Or am I wrong (I would be very happy to be)?