This is where the PHP Testing Framework (PHPT) comes in. It is installed with PEAR and allows you to feed HTTP requests, I/O operations, file uploads, etc. to a PHP script by writing a .phpt
file and executing it with run-phpt
or pear run-tests <file.phpt>
. You can also run them with PHPUnit using PhptTestCase
and PhptTestSuite
.
To simulate a file upload you need to use the --POST_RAW--
section with the same data the browser sends. This is the example from the PHP QAT site:
Important: Unfortunately, the example on their site fails without any explanation.
is_uploaded_file.phpt
--TEST--
is_uploaded_file() function
--CREDITS--
Dave Kelsey <d_kelsey@uk.ibm.com>
--SKIPIF--
<?php if (php_sapi_name()=='cli') die('skip'); ?>
--POST_RAW--
Content-type: multipart/form-data, boundary=AaB03x
--AaB03x
content-disposition: form-data; name="field1"
Joe Blow
--AaB03x
content-disposition: form-data; name="pics"; filename="file1.txt"
Content-Type: text/plain
abcdef123456789
--AaB03x--
--FILE--
<?php
// uploaded file
var_dump(is_uploaded_file($_FILES['pics']['tmp_name']));
// not an uploaded file
var_dump(is_uploaded_file($_FILES['pics']['name']));
// not an uploaded file
var_dump(is_uploaded_file('random_filename.txt'));
// not an uploaded file
var_dump(is_uploaded_file('__FILE__'));
// Error cases
var_dump(is_uploaded_file());
var_dump(is_uploaded_file('a', 'b'));
?>
--EXPECTF--
bool(true)
bool(false)
bool(false)
bool(false)
Warning: is_uploaded_file() expects exactly 1 parameter, 0 given in %s on line %d
NULL
Warning: is_uploaded_file() expects exactly 1 parameter, 2 given in %s on line %d
NULL
Running
pear run-tests --cgi=PHPCGI is_uploaded_file.phpt
Manuel Pichler wrote up an example in a blog post, but it fails as well.