Hello!
I use PHPUnit for unit testing of my application (but this issue is
independent of PHPUnit). Tests are run from command line so it's not
way (that I can imagine) to simulate file upload, because app code
uses is_uploaded_file()
to check if file was really uploaded.
In my opionion some functions for simulation of file upload in command
line script would be very handy for unit testing.
What I mean is adding a function or a set of functions for setting
file upload, for example:
do_file_upload( upload_params, upload data).
This function would be for use only in testing code, so there would be
no need to change existing application code to add unit tests, and
what's more important it would not leak to security risk.
Regards,
Piotr Czachur
Hello!
I use PHPUnit for unit testing of my application (but this issue is
independent of PHPUnit). Tests are run from command line so it's not
way (that I can imagine) to simulate file upload, because app code
usesis_uploaded_file()
to check if file was really uploaded.
In my opionion some functions for simulation of file upload in command
line script would be very handy for unit testing.What I mean is adding a function or a set of functions for setting
file upload, for example:
do_file_upload( upload_params, upload data).This function would be for use only in testing code, so there would be
no need to change existing application code to add unit tests, and
what's more important it would not leak to security risk.
you can write custom extension for such purposes.
or you can try to separate logical part of your code, from the part,
which interacts with file-uploading and unit-test the logical part
--
Alexey Zakhlestin
http://blog.milkfarmsoft.com/
I use PHPUnit for unit testing of my application (but this issue is
independent of PHPUnit). Tests are run from command line so it's not
way (that I can imagine) to simulate file upload, because app code
usesis_uploaded_file()
to check if file was really uploaded.
In my opionion some functions for simulation of file upload in command
line script would be very handy for unit testing.What I mean is adding a function or a set of functions for setting
file upload, for example:
do_file_upload( upload_params, upload data).This function would be for use only in testing code, so there would be
no need to change existing application code to add unit tests, and
what's more important it would not leak to security risk.
See runtests and the respective phpt tests in the php releases or in
cvs (php-src module). It is possible to emulate file upload using the
CGI sapi (I'm not sure if we have tests using this feature). It is
even possible to emulate almost all http behavior (from a server point
of view) with it.
However, as far as I remember phpunit does not "fork" php and uses the
current instance and that will not allow you to use the phpt method.
If it is not the case, then it should be relatively easy to port the
phpt system to phpunit (or use phpt :).
It is not the ideal solution if you use phpunit, but it may already
give you something to test or use.
Cheers,
Pierre Joye schrieb:
However, as far as I remember phpunit does not "fork" php and uses the
current instance and that will not allow you to use the phpt method.
If it is not the case, then it should be relatively easy to port the
phpt system to phpunit (or use phpt :).
PHPUnit_Extensions_PhptTestCase should support this.
--
Sebastian Bergmann http://sebastian-bergmann.de/
GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69
-----Original Message-----
From: Piotr Czachur [mailto:zimnyx@gmail.com]
Sent: 23 April 2008 12:36
To: internals@lists.php.net
Subject: [PHP-DEV] Unable to unit test code containing
*_uploaded_file()Hello!
I use PHPUnit for unit testing of my application (but this
issue is independent of PHPUnit). Tests are run from command
line so it's not way (that I can imagine) to simulate file
upload, because app code usesis_uploaded_file()
to check if
file was really uploaded.
In my opionion some functions for simulation of file upload
in command line script would be very handy for unit testing.What I mean is adding a function or a set of functions for
setting file upload, for example:
do_file_upload( upload_params, upload data).This function would be for use only in testing code, so there
would be no need to change existing application code to add
unit tests, and what's more important it would not leak to
security risk.Regards,
Piotr Czachur
As of 1.0.1, SimpleTest's WebTester can upload files, I believe.
Jared
I think - you can write wrapper for file
class file{
public function is_uploaded(){
return SingletonContextObject::isForTestingFileIsUpload() ||
is_uploaded_file($this->filepath);
}
}
Piotr Czachur пишет:
Hello!
I use PHPUnit for unit testing of my application (but this issue is
independent of PHPUnit). Tests are run from command line so it's not
way (that I can imagine) to simulate file upload, because app code
usesis_uploaded_file()
to check if file was really uploaded.
In my opionion some functions for simulation of file upload in command
line script would be very handy for unit testing.What I mean is adding a function or a set of functions for setting
file upload, for example:
do_file_upload( upload_params, upload data).This function would be for use only in testing code, so there would be
no need to change existing application code to add unit tests, and
what's more important it would not leak to security risk.Regards,
Piotr Czachur
2008/4/23 Piotr Czachur zimnyx@gmail.com:
Hello!
I use PHPUnit for unit testing of my application (but this issue is
independent of PHPUnit). Tests are run from command line so it's not
way (that I can imagine) to simulate file upload, because app code
usesis_uploaded_file()
to check if file was really uploaded.
In my opionion some functions for simulation of file upload in command
line script would be very handy for unit testing.
Thank you guys.
I finally managed to create test case using PHPUnit and phpt.
Sebastaian Bergmann has made a patch in PHPUnit trunk changeset 2863
that allows to run phpt using CGI sapi, which I'm using in this test
case, so it should be included in next release.
Here is very simple example, two files upload.phpt and UploadTest.php
===========
UploadTest.php
<?php
require_once 'PHPUnit/TextUI/TestRunner.php';
require_once 'PHPUnit/Util/Skeleton.php';
require_once 'PHPUnit/Util/Filesystem.php';
require_once 'PHPUnit/Extensions/PhptTestCase.php';
$test = new PHPUnit_Extensions_PhptTestCase('upload.phpt', array('cgi'
=> '/usr/bin/php5-cgi'));
$siute = new PHPUnit_Framework_TestSuite('Upload testing');
$siute->addTest($test);
$result = PHPUnit_TextUI_TestRunner::run($siute);
?>
=========
upload.phpt
--TEST--
Multipart Form POST Data
--HEADERS--
return <<<END
CONTENT_TYPE=multipart/form-data;
boundary=---------------------------240723202011929
CONTENT_LENGTH=862
END;
--ENV--
return <<<END
CONTENT_TYPE=multipart/form-data;
boundary=---------------------------240723202011929
CONTENT_LENGTH=862
END;
--POST_RAW--
-----------------------------240723202011929
Content-Disposition: form-data; name="file"; filename="fileNameee"
Content-Type: application/octet-stream
This is text in the file
-----------------------------240723202011929--
--FILE--
<?php
error_reporting(0);
print_r($_FILES);
echo (is_uploaded_file($_FILES['file']['tmp_name'])) ? 'uploaded' :
'not uploaded';
?>
--EXPECTF--
Array
(
[file] => Array
(
[name] => fileNameee
[type] => application/octet-stream
[tmp_name] => %s
[error] => 0
[size] => 24
)
)
uploaded
==============
Test run in terminal
$ php UploadTest.php
PHPUnit 3.2.15 by Sebastian Bergmann.
.
Time: 0 seconds
OK (1 test)
Hurraa!