Testing file upload in lumen 5.5

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!

Testing file upload in lumen 5.5, 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� Once installed, the lumen new command will create a fresh Lumen installation in the directory you specify. For instance, lumen new blog will create a directory named blog containing a fresh Lumen installation with all of Lumen's dependencies already installed. This method of installation is much faster than installing via Composer: lumen new blog

Please note that passing file argument in the second argument of call() method does not work. Since the second argument is the payload data.

If you pass it as the data, when you use the command on the backend like this:

if($request->hasFile('my_file')) {
    // Do some logic here
}

$request->hasFile() will always return false.

You need to pass fake file upload in the 5th argument to make this works.

Here is the method signature of call

call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)

Testing File Uploads with Laravel, Furthermore, we will test file uploads using laravel. Table of Contents. Create a new View; Routes; Application Logic. Migration file; Upload Model� Lumen is built with testing in mind. In fact, support for testing with PHPUnit is included out of the box, and a phpunit.xml file is already setup for your application. The framework also ships with convenient helper methods allowing you to expressively test your application's JSON responses.

this is working for me. just simple. this can submit file upload.

1.condition without authentication

5*1000 is file size (in KB). so i made a test with 5 MB file.

use Faker\Factory as Faker;
use Illuminate\Http\UploadedFile;

class SampleTest extends TestCase
{
    public function testItCanCreateUser()
    {
        $faker = Faker::create();
        $files = [
            'file' => UploadedFile::fake()->create('file.jpg', 5*1000)
        ];
        $response = $this->call('POST', '/chunk', [], [], $files);
        $this->assertEquals(200, $response->getStatusCode());
    }
}

2.condition with authentication (logged in user)

use Faker\Factory as Faker;
use Illuminate\Http\UploadedFile;

class SampleTest extends TestCase
{
    public function testItCanUpdateProfileUser()
    {
        $faker = Faker::create();
        $files = [
            'file' => UploadedFile::fake()->create('file.jpg', 5*1000)
        ];

        $headers = [
            'Accept' => 'application/json',
            'Authorization' => 'your-jwt-token'
        ];
        $servers = [];
        foreach ($headers as $k => $header) {
            $servers["HTTP_" . $k] = $header;
        }

        $response = $this->call('POST', '/chunk', [], [], $files, $servers);
        $this->assertEquals(200, $response->getStatusCode());
    }
}

you need to add HTTP_ on every request header. i'm not sure why. but it will work.

File upload fails in HTTP tests � Issue #835 � laravel/lumen , Lumen Version: 5.7.1 PHP Version: 7.1 Database Driver & Version: PostgresQL 10.4 Description: HTTP file upload fails in tests like� Testing File Uploads. The Illuminate\Http\UploadedFile class provides a fake method which may be used to generate dummy files or images for testing. This, combined with the Storage facade's fake method greatly simplifies the testing of file uploads. For example, you may combine these two features to easily test an avatar upload form:

Lumen 5.5 JSON API File Upload test breaks with UploadedFile , I am building a file upload JSON API with Lumen and trying to write phpunit API tests. The problem I am having though is that as soon as I try to simulate a file� "laravel/lumen-framework": "5.5.*" Updating The Bootstrap File. In Laravel 5.5, the service container implements the PSR-11 interface, causing conflicts with the get method of the router when attempting to use the service container as a router proxy.

Testing - Lumen, Lumen. ☰. 7.x. Master � 7.x � 6.x � 5.8 � 5.7 � 5.6 � 5.5 � 5.4 � 5.3 � 5.2 � 5.1 An ExampleTest.php file is provided in the tests directory. After installing a new Lumen application, simply run phpunit on the command line to If you are making POST , PUT , or PATCH requests you may pass an array of input data with the request. The Lumen logging facilities provide a simple layer on top of the powerful Monolog library. By default, Lumen is configured to create a single log file for your application which is stored in the storage/logs directory. You may write information to the logs using the Log facade:

Testing File Uploads With Laravel, Laravel now includes a new system for testing file uploads through two new fake methods, one on the UploadFile class and another on the� The testing environment variables may be configured in the phpunit.xml file, but make sure to clear your configuration cache using the config:clear Artisan command before running your tests! In addition, you may create a .env.testing file in the root of your project.