# Unit Testing File Upload Postprocessing

**URL:** <https://discourse.laminas.dev/t/unit-testing-file-upload-postprocessing/2411>\
**Category:** Components & MVC\
**Tags:** phpunit, laminas-mvc, laminas-test\
**Created:** [July 16, 2021, 9:44am UTC](https://discourse.laminas.dev/t/unit-testing-file-upload-postprocessing/2411 "2021-07-16T09:44:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![makxk](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/makxk/32/1266_2.png) [@makxk](https://discourse.laminas.dev/u/makxk)\
**Post date:** [July 16, 2021, 9:44am UTC](https://discourse.laminas.dev/t/unit-testing-file-upload-postprocessing/2411/1 "2021-07-16T09:44:32Z")

</div>

Hello everyone!

I’ve been trying my hands on a somewhat stateless file upload: User uploads a file, the controller processes it and returns a form with pre-filled out fields based on the values from the file. The uploaded file is not saved in any manner.  
Fortunately, I was successful 😛  
Now, I want to write unit tests for this function, and I am stuck as to how I would approach that.  
I’ve tried already to use setFiles() in the test case like so:

```auto
$request = $this->getRequest();
$request->setFiles(new Parameters(
                [
                    'fileUpload' => [
                        'name' => 'project.xml',
                        'type' => 'text/xml',
                        'tmp_name' => __DIR__.'/project.xml',
                        'error' => 0,
                        'size' => 128885
                    ]
                ]
            ));
        $this->dispatch('/thing/add', 'POST', ['importButton' => 'Import']);

```

But PHP considers this ‘an illegal file upload’ (which is understandable imho).  
How would I ‘legally’ upload a file in this scenario? Or is another approach such as overwriting the $form-\>isValid() (in the controller) via stub more viable?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![praem90](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/praem90/32/1731_2.png) [@praem90](https://discourse.laminas.dev/u/praem90)\
**Post date:** [June 18, 2024, 10:18am UTC](https://discourse.laminas.dev/t/unit-testing-file-upload-postprocessing/2411/2 "2024-06-18T10:18:32Z")

</div>

You can define the PHP’s global variable `$_FILES` like below

```auto
        $_FILES['files'] = [
            'name' => [
                'project.xml'
            ],
            'type' => [
                'text/xml'
            ],
            'tmp_name' => [
                __DIR__. '/project.xml'
            ],
            'error' => [
                0
            ],
            'size' => [
                128885
            ]
        ];

```

---

<div class="post-metadata">

**Author:** ![praem90](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/praem90/32/1731_2.png) [@praem90](https://discourse.laminas.dev/u/praem90)\
**Post date:** [June 18, 2024, 10:20am UTC](https://discourse.laminas.dev/t/unit-testing-file-upload-postprocessing/2411/3 "2024-06-18T10:20:05Z")

</div>

Hope this helps

> <https://stackoverflow.com/questions/1063540/file-upload-and-unit-testing-with-zend-framework/1064669#1064669>

---

<div class="post-metadata">

**Author:** ![makxk](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/makxk/32/1266_2.png) [@makxk](https://discourse.laminas.dev/u/makxk)\
**Post date:** [June 5, 2025, 9:17am UTC](https://discourse.laminas.dev/t/unit-testing-file-upload-postprocessing/2411/4 "2025-06-05T09:17:22Z")

</div>

I’ve really abandoned the notion since this post, but i’d like to highlight something the linked post talks about:

> Basically, you can manipulate the `$_FILES` superglobal directly in the `setUp()` of your unit tests. Next, use mock objects for any external sort of service, otherwise you’re not truly unit testing, but integration testing.

Haven’t tried that, but from a distance, this does look like a valid solution.

Thanks, praem.
