• 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

Eloquent insert id with sequence next value

Target class controller does not exist - Laravel 8

How to pass data to all views in Laravel 5?

Autoloading classes in PHPUnit using Composer and autoload.php

Limit login attempts in Laravel 5.7

Laravel Model save() & update() Not Saving

Issue with laravel eloquent model property

Laravel views are showing from cache on one server but works fine on other server

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

Laravel query builder binding parameters more than once

IlluminateDatabaseQueryException: SQLSTATE[HY000]: General error: 1 no such table - In clone project

Laravel Carbon throws error The format separator does not match

Laravel - Implicit route model binding with soft deleted data

Unit (real unit test) of test laravel relationship

Sync Records With Default Values in Pivot Tables, Call to undefined method syncWithPivotDefaults()

About Contact Privacy policy Terms and conditions