• 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

Laravel 8: Array to string conversion while calling route:list

Elastic Beanstalk with Laravel Envoy

Laravel Nova, route not found

Losing session data after POST from third party website

Laravel + phpunit + github actions = Failed asserting that '1' is identical to 1

Access relation of pivot table in Laravel

"There is no active transaction" when refreshing database in Laravel 8.0 test

Display Link Title Instead of URL in XSL

PHPUNIT Test - Expected Status 200 But Received 500

Image upload not working through ajax Laravel

Target class controller does not exist - Laravel 8

Laravel/ PHP: Order By Alphabetical with numbers in order [duplicate]

Clients authentication and user authentication with laravel

Laravel PackageManifest.php: Undefined index: name

Laravel SQL query midnight time not showing

About Contact Privacy policy Terms and conditions