Skip to content

Instantly share code, notes, and snippets.

@nasrulhazim
Last active April 14, 2020 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nasrulhazim/8be0b26c6784162e44a77299a55b4ec0 to your computer and use it in GitHub Desktop.
Save nasrulhazim/8be0b26c6784162e44a77299a55b4ec0 to your computer and use it in GitHub Desktop.
Learn Middleware Test
<?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