Laravel 测试授权中间件

Laravel Testing auth middleware

在对我的应用程序进行功能测试时,我发现自己编写了几乎相同的测试来验证我的控制器是否需要身份验证。它通常看起来像这样:

public function a_guest_cannot_view_any_of_the_pages()
{
    $this->withExceptionHandling();

    $model = factory(Model::class)->create();

    $response = $this->get(route('models.show', [ 'id' => $model->id ]));
    $response->assertRedirect(route('login'));

    $response = $this->get(route('models.edit', [ 'id' => $model->id ]));
    $response->assertRedirect(route('login'));

   ...etc 
}

但是,我发现像这样对每个需要身份验证的控制器进行测试是不必要的麻烦。

使用auth中间件测试CRUD有什么技巧吗?我该如何改进?

解决方案 1 在控制器构造函数中定义将作用于所有函数的中间件

public function __construct()
{
    $this->middleware('auth');
}

解决方案 2 直接在路由上定义中间件

Route::get('admin/profile', function () {
    //
})->middleware('auth');

https://laravel.com/docs/5.7/middleware

您可以使用数据提供者:

在 tests/TestCase.php:

/**
* @dataProvide dataProvider
*/
public function testRedirectToAuth($routeName)
    {
    $this->withExceptionHandling();

    $model = factory(Model::class)->create();

    $response = $this->get(route($routeName, [ 'id' => $model->id ]));
    $response->assertRedirect(route('login'));
}

然后你就可以在所有的测试用例中调用它了:

public function dataProvider()
{
  return [
    'model.show',
    'model.edit',
    ...
  ];
}

您可以使用 ShowTrait,使用此特征时,您必须指定路线和型号名称。

<?php

class ModelTest extends Test
{
    use ShowTrait;

    protected $routebase = 'api.v1.models.';
    protected $model = Model::class;
}

abstract class Test extends TestCase
{
    use RefreshDatabase, InteractsWithDatabase, UseAuthentication;

    protected $routebase = 'api.v1.';
    protected $model;

    /**
     * @test
     */
    public function is_valid_model()
    {
        $this->assertTrue(class_exists($this->model));
    }
}

trait ShowTrait {

    public function test_show_as_authenticated_user()
    {
        $record = factory($this->model);

        $this->assertShow($record)
    }


    protected function assertShow($record)
    {
        $route = route($this->routebase . "show", ['id' => $record->id]);

        // Get response
        $response = $this->get($route);
        $response->assertRedirect(route('login'));
    }
}