From 08bb442ec8b4e9c86104ed25c5c65e3c693e4371 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Thu, 21 Feb 2019 08:20:52 +0800 Subject: [PATCH] Test middleware using testbench. Signed-off-by: Mior Muhammad Zaki --- src/Http/Middleware/StoreMessageBag.php | 27 +++---------------- .../Http/Middleware/StoreMessageBagTest.php | 18 ++++--------- 2 files changed, 9 insertions(+), 36 deletions(-) rename tests/{Unit => Feature}/Http/Middleware/StoreMessageBagTest.php (60%) diff --git a/src/Http/Middleware/StoreMessageBag.php b/src/Http/Middleware/StoreMessageBag.php index 5445e72..8a55605 100644 --- a/src/Http/Middleware/StoreMessageBag.php +++ b/src/Http/Middleware/StoreMessageBag.php @@ -3,27 +3,10 @@ namespace Orchestra\Messages\Http\Middleware; use Closure; -use Illuminate\Contracts\Foundation\Application; +use Orchestra\Support\Facades\Messages; class StoreMessageBag { - /** - * The application implementation. - * - * @var \Illuminate\Contracts\Foundation\Application - */ - protected $app; - - /** - * Create a new middleware instance. - * - * @param \Illuminate\Contracts\Foundation\Application $app - */ - public function __construct(Application $app) - { - $this->app = $app; - } - /** * Handle an incoming request. * @@ -34,10 +17,8 @@ public function __construct(Application $app) */ public function handle($request, Closure $next) { - $response = $next($request); - - $this->app->make('orchestra.messages')->save(); - - return $response; + return \tap($next($request), function () { + Messages::save(); + }); } } diff --git a/tests/Unit/Http/Middleware/StoreMessageBagTest.php b/tests/Feature/Http/Middleware/StoreMessageBagTest.php similarity index 60% rename from tests/Unit/Http/Middleware/StoreMessageBagTest.php rename to tests/Feature/Http/Middleware/StoreMessageBagTest.php index 1c9bac9..b72f2db 100644 --- a/tests/Unit/Http/Middleware/StoreMessageBagTest.php +++ b/tests/Feature/Http/Middleware/StoreMessageBagTest.php @@ -1,35 +1,27 @@ shouldReceive('make')->once()->with('orchestra.messages')->andReturn($messages); + $this->app->instance('orchestra.messages', $messages); $messages->shouldReceive('save')->once()->andReturnNull(); - $stub = new StoreMessageBag($app); + $stub = new StoreMessageBag(); $this->assertEquals('foo', $stub->handle($request, function ($request) { return 'foo';