# Unable to unit test code containing *_uploaded_file()

- Thread: https://externals.io/message/37198
- Original archive: https://news-web.php.net/php.internals/37198
- Started by Piotr Czachur on 2008-04-23T11:35:47+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #37198 — Piotr Czachur — 2008-04-23T11:35:47+00:00

URL: https://externals.io/message/37198#37198

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

---

## #37199 — Alexey Zakhlestin — 2008-04-23T11:54:32+00:00

URL: https://externals.io/message/37198#37199 · In reply to: #37198

> 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.

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/

---

## #37200 — Pierre Joye — 2008-04-23T12:11:06+00:00

URL: https://externals.io/message/37198#37200 · In reply to: #37198

>  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.

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
http://blog.thepimp.net | http://www.libgd.org

---

## #37203 — Sebastian Bergmann — 2008-04-23T13:52:05+00:00

URL: https://externals.io/message/37198#37203 · In reply to: #37200

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

---

## #37204 — Jared Williams — 2008-04-23T14:04:39+00:00

URL: https://externals.io/message/37198#37204 · In reply to: #37198

> -----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 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
> 

As of 1.0.1, SimpleTest's WebTester can upload files, I believe.

Jared

---

## #37210 — Maxim Antonov — 2008-04-24T15:11:32+00:00

URL: https://externals.io/message/37198#37210 · In reply to: #37198

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
> 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

---

## #37495 — Piotr Czachur — 2008-05-06T13:54:21+00:00

URL: https://externals.io/message/37198#37495 · In reply to: #37198

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
>  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!
