[PHP-DEV] Unable to unit test code containing *_uploaded_file()

2008-04-23 Thread 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
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

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP-DEV] Re: Unable to unit test code containing *_uploaded_file()

2008-05-06 Thread Piotr Czachur
2008/4/23 Piotr Czachur [EMAIL PROTECTED]:
 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.


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!

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php