Here is two things to consider:
tmpfile() is deleted automatically when the resource is closed, according to PHP docs:The file is automatically removed when closed (for example, by calling fclose(), or when there are no remaining references to the file handle returned by tmpfile()), or when the script ends.
imagepng($image, $to, ...) closes the resource automatically when $to is a resource, according to PHP docs:The path or an open stream resource (which is automatically being closed after this function returns) to save the file to. If not set or NULL, the raw image stream will be outputted directly.
When you try to do something like this:
UploadedFile::fake()->image('avatar.jpg');
phpunit just stop with an error segmentation fault, I think is because the resource is closed by imagepng() causing the temporary file beign deleted, as soon as generateImage(...) attempts to return the closed resource it fails.
Here is the FileFactory::generateImage() function for reference:
protected function generateImage($width, $height)
{
return tap(tmpfile(), function ($temp) use ($width, $height) {
imagepng(imagecreatetruecolor($width, $height), $temp);
});
}
I'm not sure if this behavior occurs only on my system (macOS running Laravel Valet), but I'm pretty sure that it only fails when calling imagepng() because the resource ($temp) is closed right after.
If this is confirmed as a bug I can make the fix and a pull request.
@sahibalejandro yes please open a PR.
I have the same error while trying to test file upload using Storage::fake() will be happy of a PR can be opened for this.
I will open the PR in the next few hours. 馃暫馃徑
A similar thing happens in Windows 8.1. When I try to do UploadedFile::fake()->image('avatar.jpg'), phpunit crashes.
Most helpful comment
I will open the PR in the next few hours. 馃暫馃徑