Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Testing] UploadedFile::fake()->image() produce a 'Test code or tested code did not (only) close its own output buffers' #46852

Closed
ghost opened this issue Apr 22, 2023 · 3 comments

Comments

@ghost
Copy link

ghost commented Apr 22, 2023

Laravel Version

10.8.0

PHP Version

8.2

Database Driver & Version

No response

Description

Using UploadedFile::fake()->image('image.jpg'). Instead of producing a fake image it cuts the test and produce Test code or tested code did not (only) close its own output buffers.

This is a snippet code of my test.

test('author can create a new book', function () {
    $cover = UploadedFile::fake()->image('image.jpg');

    $response = actingAs($this->user)->post(
        uri: route('books.store') ,
        data:[
            'book_title' => 'Book Title 1',
            'synopsis' => 'Book Test Sample Synopsis',
            'tags' => $this->tags->random(5),
            'genres' => $this->genres->random(3),
            'cover_photo' => $cover,
        ]
    );

    $response->assertValid();
    assertDatabaseHas(Book::class,[
        'book_title' => 'Book Title 1',
        'synopsis' => 'Book Test Sample Synopsis',
    ]);
});

Test Result :

   WARN  Tests\Feature\Author\AuthorBookTest
  ! author can create a new book → Test code or tested code did not (only) close its own output buffers                                                       1.30s

  Tests:    1 risky (0 assertions)
  Duration: 1.82s

Im using PestPHP but I also try it in PHPUnit. It's still the same. But using UploadedFile::fake()->create() works.

Steps To Reproduce

Just how you normally create a new test.

  1. php artisan make:test Author/AuthorBookTest
  2. Type the test.
  3. php artisan test --filter=AuthorBookTest
@cosmastech
Copy link
Contributor

Did you try creating a fresh repo and testing this? I just tried and could not reproduce. I'm not sure if it's something within your controller that is causing the issue or if it is actually UploadedFile::fake() that is causing the problem.

public function testBasic()
{
    $cover = UploadedFile::fake()->image('image.jpg');

    $this->get('/')->assertOk();
}

This test is not indicated as risky.

Are you running via Docker? What platform/architecture are you on? What version of PHPUnit are you running? Could you upload a repo with the minimal amount of data to produce the issue so we can triage this problem?

@cosmastech
Copy link
Contributor

cosmastech commented Apr 23, 2023

I submitted a PR to phpunit since the risky message doesn't indicate whether or not output buffers were closed. That would help diagnose the exact problem.

@ghost
Copy link
Author

ghost commented Apr 23, 2023

Thank you for the response @cosmastech. When I created a fresh repo and tried to test it again, it did not indicate as risky but produce an error undefined method imagecreatetruecolor() and after further research I found out that I need to uncomment the gd extension in php.ini file and it solves the problem.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant