Last active
January 18, 2018 05:45
-
-
Save duan-li/1edbe23e1332fbb15df37aa99a8b1cf3 to your computer and use it in GitHub Desktop.
Correct way to test laravel middleware
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 App\Http\Middleware; | |
use Closure; | |
class Sample | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
return $next($request); | |
} | |
} |
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 Middleware; | |
use App\Http\Middleware\Sample; | |
use Mockery; | |
class SampleMiTest extends \TestCase | |
{ | |
public function test_sample_middleware() | |
{ | |
$response = $this->faker->md5; | |
$request = Mockery::mock(\App\Http\Requests\Request::class); | |
$request->shouldReceive('test') | |
->once() | |
->andReturn($response); | |
$middleware = new Sample(); | |
$result = $middleware->handle($request, function ($r) { | |
return $r->test(); | |
}); | |
$this->assertSame($response, $result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment