• 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 Factory not calling callback 'afterCreating'

PHP using str_starts_with for array to exclude same as with wildcard

JSON Render Issue in Date Object Laravel and PHP 7.4

laravel sanctum Route [login] not defined

laravel and phpunit: could not find driver (SQL: PRAGMA foreign_keys = ON;)

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

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

Laravel - Browser displays message again when I press back button

How to reset Laravel AuthManager/guards in between API calls in tests?

How can I pass the list to the component variable in Laravel?

PHPUnit (Phar) Fatal Error Only When Test Fails

api response laravel doesn't show

How do I upload a laravel project on cPanel shared hosting?

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

Unable to create lockable file - Laravel 8 & PHP 8

About Contact Privacy policy Terms and conditions