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:
Yii::import('application.controllers.SiteController'); $app = Yii::app(); $app->controllerPath=dirname(__FILE__).DIRECTORY_SEPARATOR.'../../controllers'; $app->layoutPath=dirname(__FILE__).DIRECTORY_SEPARATOR.'../../views/layouts'; $app->viewPath=dirname(__FILE__).DIRECTORY_SEPARATOR.'..\..\views'; $siteController = new SiteController('site/index'); $siteController->run('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; $siteController->run('index');
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.