Snippets

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

Navigation

Snippets by user Benjamin Meynell Snippets by user Benjamin Meynell

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 

download images with sfWebBrowserPlugin

Download an image via sfWebBrowser and save it as-is or turn it into a thumbnail.

$b = new sfWebBrowser();
$b->get('http://foo/images/bar.gif');
 
// create thumbnail
$thumb = new sfThumbnail(150, 150);
$thumb->loadData($b->getResponseText(), $b->getResponseHeader('Content-Type'));
$thumb->save('/home/foo/bar.gif');
 
// just store the file - don't make thumbnail
file_put_contents('/home/foo/bar.gif', $b->getResponseText());
 
by Benjamin Meynell on 2007-09-15, tagged sfthumbnailplugin  sfwebbrowserplugin