Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

Unit testing file uploads

A good amount of people have expressed interest in unit testing file uploads. sfTestBrowser does not currently support this but we can use sfWebBrowser instead, as follows (uploadTest.php, to be placed in your unit tests directory):

require_once(dirname(__FILE__).'/../bootstrap/unit.php');
require_once(dirname(__FILE__).'/../../plugins/sfWebBrowserPlugin/lib/sfWebBrowser.class.php');
require_once(dirname(__FILE__).'/../../plugins/sfWebBrowserPlugin/lib/sfCurlAdapter.class.php');
require_once($sf_symfony_lib_dir.'/config/sfConfig.class.php');
require_once($sf_symfony_lib_dir.'/util/sfToolkit.class.php');
sfConfig::set('sf_data_dir', dirname(__FILE__).'/../../data');
$file = dirname(__FILE__).'/../fixtures/photos/maine01.jpg';
$invalid_file = dirname(__FILE__).'/../fixtures/photos/maine01.foo';
 
$t = new lime_test(2, new lime_output_color());
 
$b = new sfWebBrowser();
$b->post('http://mysite/upload', array(
  'file' => $file
));
$t->like($b->getResponseText(), '/has been uploaded/', 'file uploads successfully');
$b->post('/upload', array(
  'file' => $invalid_file
));
$t->like($b->getResponseText(), '/we only allow images/', 'unsupported mime types are not uploaded');
 

N.B. You must have curl installed for this to work.

by Benjamin Meynell on 2007-09-15, tagged test  upload 
You need to create an account or log in to post a comment or rate this snippet.