Unit test a file upload form in Yii

I’m trying to learn unit testing by building a small web app that has the user upload a file and then return a modified version the file.

I’ve found several ways to ‘start’ my test when it comes to the controller. I can do it this way:


$app = Yii::app();




$siteController = new SiteController('site/index');


However this fails when it comes to rendering the view (it can’t find the index view, despite my efforts to set viewPath).

Currently writing my test like this instead:

$_POST['UploadForm'] = array(

	'addonFile' => '',


$_POST['yt0'] = 'Upload';

$_FILES['UploadForm'] = array(

	'name' => array(

		'addonFile' => 'addon-163-latest.xpi'


	'type' => array(

		'addonFile' => 'application/x-xpinstall'


	'tmp_name' => array(

		'addonFile' => 'C:\Users\LarryLaffer\Desktop\Webprod\wamp\tmp\addon-163-latest.xpi'


	'error' => array(

		'addonFile' => 0


	'size' => array(

		'addonFile' => 47350



$_SERVER['REQUEST_URI'] = 'site/index';

$app = Yii::app();

$returnedControllerSet = $app->createController('');

$siteController = $returnedControllerSet[0];


All the $_POST and $_FILES stuff is trying to emulate a file being uploaded. The file it points to actually exists.

And this fails miserably with:

PHPUnit_Framework_Error_Warning : E_WARNING: Cannot modify header information - headers already sent by (output started at C:\Users\LarryLaffer\AppData\Local\Temp\phpunit_xippy_LarryLaffer.php:459)

I feel I’m grasping at straws here — could I perhaps be in error when it comes to the basics, as in, assuming that it should be possible to unit test a file upload?

Has anyone done it?

My googling resulted in very lame solutions that doesn’t seem to work specifically for Yii.

Instead of Calling the controller, why not just unit test the model?

I’ve done the same as you but in the model itself, and got no errors.