• Categories
    • PHP
    • phpMyAdmin
    • PHPMailer
    • FFMpeg
    • PHPEXcel
    • PHPDoc
    • PHPUnit
    • CakePHP
    • CakePHP 2.0
    • Cake PHP 2.1
    • CakePHP Model
    • Facebook PHP SDK
    • composer-php
    • PHP 7
    • PHP GD
    All Categories
  • About

How to Mock the Request Class in Laravel?

laravelmockingphpunit


PHP Snippet 1:

app()->bind(\Illuminate\Http\Request::class, function () {
    $mock = \Mockery::mock(\Illuminate\Http\Request::class)->makePartial();
    $mock->shouldReceive('all')->andReturn(['includes' => ['some_val','another_val']]);

    return $mock;
});

PHP Snippet 2:

public function testPostRequest() {
    $response = $this->post(
        '/my-custom-route',
        ['includes' => ['some_val','another_val']]
    );
}

Related Snippets

array_key_exists(): The first argument should be either a string or an integer

Laravel 5 session not persisting after user is logged in

Guzzle Not Sending Grant Type to Laravel Passport

How should I get started using PHPUnit as a testing framework for a bunch of functions and classes I have already made?

PHPUnit (Phar) Fatal Error Only When Test Fails

Laravel Displaying image from database

Laravel 5 issue with wherePivot

Laravel Carbon throws error The format separator does not match

How to create tests w/Doctrine entities without persisting them (how to set id)

Store multiple fields in JSON column (Nova CMS)

enroll_table three field fetch to payment form to create payment field in laravel 5.5

find in set in laravel ? example

PHPUnit 7: Failed asserting that exception of type InvalidArgumentException is thrown

Get sum of arrays inside array

Laravel Model save() & update() Not Saving

About Contact Privacy policy Terms and conditions