Instantiate Request for Testing

Posted: 2017-02-28 01:26:54

When I need to make a request for a test in which the Class is expecting a \Illuminate\Http\Request I can do this for a file based or non file based request.

    /**
     * @Then I should be able to upload an image file
     */
    public function iShouldBeAbleToUploadAnImageFile()
    {
        $request = new \Illuminate\Http\Request();
        $file = new \Symfony\Component\HttpFoundation\FileBag();
        $path = base_path('tests/fixtures/example_profile.jpg');
        $originalName = 'example_profile.jpg';
        $upload = new \Illuminate\Http\UploadedFile($path, $originalName, null, null, null, true);
        $file->set('profile_image', $upload);
        $request->files = $file;

        $this->repo = new \App\Repositories\ProfileRepository();
        $results = $this->repo->uploadUserProfileImage($request);
        PHPUnit::assertTrue($results, "Repo did not return true");
        PHPUnit::assertTrue(File::exists(public_path('storage/' . $this->user->id . '/example_profile.jpg')), "File Not found");
    }

Or just Mocked

        $fixture = File::get(base_path('tests/fixtures/slack_incoming_run_report.json'));
        $fixture = json_decode($fixture, true);

        //$request = new \Illuminate\Http\Request([], [], $fixture,[], [], [], false);
        $request = Mockery::mock(\Illuminate\Http\Request::class);
        $request->shouldReceive('all')->andReturn($fixture);

        /** @var \App\PlannerSlackReport $report */
        $report = new \App\PlannerSlackReport();
        $report->handle($request);