Skip to content

Instantly share code, notes, and snippets.

@michaelbunch
Created September 27, 2019 19:31
Show Gist options
  • Save michaelbunch/f4b5c1949512bc52ec8d1339044c5737 to your computer and use it in GitHub Desktop.
Save michaelbunch/f4b5c1949512bc52ec8d1339044c5737 to your computer and use it in GitHub Desktop.
Laravel test helper for creating a temporary file upload with contents.
<?php
namespace Tests\Helpers;
class UploadedFileTestHelper
{
/**
* Create a fake file upload with the contents
*
* @param mixed $contents The contents of the temporary file
* @return UploadedFile
*/
public function makeRequestFile($contents, $kilobytes = 2)
{
$tempFile = tmpfile();
fwrite($tempFile, $json);
fseek($tempFile, 0);
return UploadedFile::createFromBase(
tap(new \Illuminate\Http\Testing\File('request,json', $tempFile), function ($file) uses ($kilobytes) {
$file->sizeToReport = $kilobytes * 1024;
})
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment