Last active
April 14, 2020 00:31
-
-
Save nasrulhazim/8be0b26c6784162e44a77299a55b4ec0 to your computer and use it in GitHub Desktop.
Learn Middleware Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Feature; | |
use App\Http\Middleware\LearnUnitTestMiddleware; | |
use Tests\TestCase; | |
use Illuminate\Http\Request; | |
use Illuminate\Http\Response; | |
use Mockery; | |
class LearnMiddlewareTest extends TestCase | |
{ | |
/** @test */ | |
public function the_learn_middleware_works() | |
{ | |
// Create mock response | |
$response = Mockery::Mock(Response::class); | |
// Create request on the fly | |
$request = Request::create('/', 'GET'); | |
// Assert that the current request no input yet | |
$this->assertEmpty($request->input()); | |
// Create a new middleware object and call `handle()` method. | |
// Pass the `$request`, together with `$response` | |
// What it does basically to pass the request to middleware | |
// to process the request. | |
(new LearnUnitTestMiddleware()) | |
->handle( | |
$request, | |
function () use ($response) { | |
return $response; | |
} | |
); | |
// After it's being handle by middleware, | |
// we want to check either the $request->input() | |
// is injected with new parameters. | |
// So we simply assert not empty | |
$this->assertNotEmpty($request->input()); | |
// And explicit check the $request->input() contained | |
// ['learn' => true], which injected in the middleware. | |
$this->assertContains(['learn' => true], $request->input()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment