I'm using Lumen 5.5, and I wrote simple app that upload files.
I wrote test like this (following this tutorial)
<?php
class UploadImageTest extends TestCase
{
Use DatabaseMigrations;
public function testUploadingImageSuccessfully()
{
$this->json('POST', '/images', [
'image' => UploadedFile::fake()->image('image.jpg')
])->assertResponseOk()
}
}
problem is that in my controller, $request->file('image')
returns null.
<?php
use Illuminate\Http\Request;
class UploadController extends Controller
{
public function upload(Request $request)
{
if ($request->file('image')) { // always return null
return "File is uploaded!";
}
return "File is not uploaded!";
}
}
I checked other questions (like this one) and tried given solutions with no luck!
I came across this question while searching for the answer to the same problem and wasn't sure it was related, so I posed one relevant to my use case. (Here)
The solution is simple: UploadedFile::fake() doesn't work with JSON, as it fakes a file upload with an XmlHttpRequest (as far as I can tell). You must therefore change your test from this:
public function testUploadingImageSuccessfully()
{
$this->json('POST', '/images', [
'image' => UploadedFile::fake()->image('image.jpg')
])->assertResponseOk()
}
to this:
public function testUploadingImageSuccessfully()
{
$this->call('POST', '/images', [
'image' => UploadedFile::fake()->image('image.jpg')
])->assertResponseOk()
}
Hope it helps!