如何在laravel 5.5中使用phpunit测试中间件?

时间:2018-03-27 10:10:09

标签: laravel testing phpunit tdd laravel-5.5

我如何测试我的中间件? 在这里,我测试管理员是否可以访问受中间件保护的路由,如果用户没有特权IP,则返回500,然后中间件在尝试访问/500页面时返回401(未授权)

我的测试:

use App\Http\Middleware\OnlyAdminIp;
use Illuminate\Http\Request;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class HttpTests extends TestCase
{
    use DatabaseTransactions;

   /** @test */
    public function if_a_500_page_returns_a_500_response_for_admin()
    {
        $request = Request::create(config('app.url') . '500', 'GET');
        $middleware = new OnlyAdminIp();
        $response = $middleware->handle($request, function () {});
        $this->assertEquals($response->getStatusCode(), 401);
    }
}

我的中间件:

namespace App\Http\Middleware;

use App\IpList;
use Closure;

class OnlyAdminIp
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $client_ip = $_SERVER["HTTP_CF_CONNECTING_IP"] ?? $request->ip(); // CDN(Cloudflare) provides the real client ip, a safeguard is used to prevent critical error if CDN is removed/changed.
        $ipList = IpList::all()
            ->pluck('ip')
            ->toArray();
        if (!in_array($client_ip, $ipList)) {
            abort(401);
        }

        return $next($request);
    }
}

为了更清晰 - 500路线(在web.php中)。

Route::group(['middleware' => 'admin.ip'], function () {


    Route::get('500', function () {
        abort(500);
    }); 

});

通过此设置,我得到Call to a member function getStatusCode() on null 提前谢谢!

1 个答案:

答案 0 :(得分:0)

我想出了一个有效的解决方案。

$request = Request::create(config('app.url') . '500', 'GET',[],[],[],['REMOTE_ADDR'=>'127.0.0.2']);
    $middleware = new OnlyAdminIp();
    $expectedStatusCode = 401;
    try {
      $middleware->handle($request, function () {});
    }catch(\Symfony\Component\HttpKernel\Exception\HttpException $e){
        $this->assertEquals(
            $expectedStatusCode,
            $e->getStatusCode(),
            sprintf("Expected an HTTP status of %d but got %d.", $expectedStatusCode, $e->getStatusCode())
        );
    }