Laravel路由上的PHPunit测试始终返回404

时间:2020-07-21 23:13:12

标签: php laravel testing phpunit laravel-7

我刚刚开始在我的laravel应用程序上编写PHPUnit路由测试,并且可以通过浏览器和Postman正常工作,而不是通过PHPunit。

示例:在此测试中

public function test_getAll(){
    $this->withoutExceptionHandling(); // If i comment this line I get the 404 and not the error shown below

    $response = $this->get('/api/users');
    $response->assertStatus(401);
}

我得到:

PHPUnit 8.5.3 by Sebastian Bergmann and contributors.

.E                                                                  2 / 2 (100%)

Time: 1.89 seconds, Memory: 8.00 MB

There was 1 error:

1) Tests\Feature\Users\UserRoute_SuperAdminTest::test_getAll
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: GET http://localhost/cms/api/users

E:\www\projects\cms-php\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithExceptionHandling.php:126
E:\www\projects\cms-php\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:415
E:\www\projects\cms-php\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:113
E:\www\projects\cms-php\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php:468
E:\www\projects\cms-php\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php:258
E:\www\projects\cms-php\tests\Feature\Users\UsersRoute-SuperAdmin_Test.php:45

奇怪的是:如果我将URL更改为:

$response = $this->get('http://anythingatallinhere/api/users');

我得到了我应该收到的401响应。

更多上下文信息可以解决问题。

我的环境APP_URL是APP_URL=http://localhost/cms,并且以这种方式动态注册路由:我有一个CoreServiceProvider,其启动过程如下:

public function boot()
    {
       [...]
        $moduleController = app()->make(ModuleController::class);
        $moduleController->registerCoreRoutes();
       [...]
    }

在ModuleController上:

function registerCoreRoutes(){
        foreach($this->allValidated as $moduleAlias => $registeredModule){
            $modName = ucfirst(str_replace('core/', '', strtolower($moduleAlias)));
            $namespace = 'App\Api\Core' . '\\' . $modName . '\Controllers';

            try {
                foreach ($registeredModule['routerFiles'] as $key => $routerInfo){
                    $routePath = app_path('Api/Core/' . $modName . '/Routes/' . $routerInfo['fileName'] . '.php');
                    $prefix = 'api/' . $routerInfo['path'];

                    $this->pathToModule[$routerInfo['path']] = $moduleAlias;

                    Route::prefix($prefix)
                        ->namespace($namespace)
                        ->group($routePath);
                }
                $this->allRegistered[$moduleAlias] = $registeredModule;
            } catch (\Throwable $th) {
                var_dump('Core module route registration failed');
                $this->allRegistered = [];
                throw $th;
            }
        }
    }

如果我使用php artisan route:list,它们也都已正确注册。

1 个答案:

答案 0 :(得分:1)

我最近也遇到了类似的问题,但是在运行以下命令后它可以工作:

    id      action  timestamp
1   A123    B       2
2   A123    C       3
3   A123    D       4
11  A341    B       2
12  A341    C       3
13  A341    D       4
15  A341    B       6
16  A341    C       7
17  A341    D       8

特别是如果您更改了刚刚更改的.env文件中的APP_URL。

您还可以尝试在phpunit.xml文件中将php artisan config:clear php artisan config:cache 设置为processIsolation

true

编辑: 如果以上方法均无效,则还可以创建另一个<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="bootstrap/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="true" stopOnFailure="false"> 并将.env.testing设置为APP_URL。 PHPUnit将使用该文件中的变量。无论您的应用程序的实际URL是什么,它都有效