laminas / laminas-diactoros

PSR HTTP Message implementations
https://docs.laminas.dev/laminas-diactoros/
BSD 3-Clause "New" or "Revised" License
487 stars 63 forks source link

Multiple use of the UploadedFile object in PHPUnit test cases with the same file results in errors - File is consumed #107

Closed triopsi closed 2 years ago

triopsi commented 2 years ago

Bug Report

Q A
Version(s) 2.2.2

Summary

Hi,

I use the UploadedFile object to make my PHPUnit test cases more realistic for my application. (See example below) After an update of the laminas-diactoros framework, I noticed that many PHPUnit test cases no longer work. This is because the UploadedFile object deletes the original file after use. See commit link: https://github.com/laminas/laminas-diactoros/commit/197f195ef75dd8db528a459154ba917c0fd644b9#commitcomment-77930965

If this is not a bug, is there an alternative to creating an UploadedFile object without the object deleting the file?

Danger! This only applies if multiple PHPUnit test cases that want to use the same file to test a function.

Example

public function testAddAuthorizedPostDataOKActiveTrue(): void {
        $data = array(
            'active' => true,
            'uploadFiles' => array(new UploadedFile( TESTS . '/TestFiles/Home-Network.pdf', 0, UPLOAD_ERR_OK, 'Home-Network.pdf', 'application/pdf')),
        );
        $this->post( 'documents/add', $data );
        $this->assertFlashMessage( 'The document could be saved successfully', 'flash' );
}

public function testAddAuthorizedPostDataOKActiveFalse(): void {
        $data = array(
            'active' => false,
            'uploadFiles' => array(new UploadedFile( TESTS . '/TestFiles/Home-Network.pdf', 0, UPLOAD_ERR_OK, 'Home-Network.pdf', 'application/pdf')),
        );
        $this->post( 'documents/add', $data );
        $this->assertFlashMessage( 'The document could be saved successfully', 'flash' );
}
Ocramius commented 2 years ago

This:

new UploadedFile( TESTS . '/TestFiles/Home-Network.pdf'

Should be:

copy(TESTS . '/TestFiles/Home-Network.pdf'`, $someTempLocation);
new UploadedFile( TESTS . '/TestFiles/Home-Network.pdf', ...
triopsi commented 2 years ago

Thanks @Ocramius Your solution is ok and I can work with it :) Thanks