assertAuthenticated 在本地通过但在 Bitbucket 管道中失败

assertAuthenticated passes locally but fails in Bitbucket Pipeline

我正在使用 Laravel/Breeze 使用的内置身份验证构建一个 Laravel 8 应用程序。我已经使用 assertAuthenticated() 编写了几个测试,这些测试在 运行 本地测试时通过,但是当在 Bitbucket 管道中 运行 时,对于调用该 function/require 身份验证的任何测试返回以下内容:

The user is not authenticated
Failed asserting that false is true.

我正在使用基于会话的身份验证,并在下面包含了 bitbucket.yml 文件、kernel.php 和 auth.php 文件。

Bitbucket-pipelines.yml

image: php:8.0.2-fpm

definitions:
  services:
    mysql:
      image: mysql
      environment:
        MYSQL_DATABASE: "test"
        MYSQL_USER: "root"
        MYSQL_PASSWORD: "root"
        MYSQL_ROOT_PASSWORD: "root"

pipelines:
  default:
    - step:
        name: Test
        services:
          - mysql
        script:
          - apt-get update && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev
          - docker-php-ext-install pdo_mysql
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - docker-php-ext-configure gd --with-freetype --with-jpeg
          - docker-php-ext-install -j$(nproc) gd
          - apt-get update && apt-get install -y unzip libzip-dev
          - docker-php-ext-install zip
          - docker-php-ext-enable zip
          - composer require monolog/monolog
          - composer require laravel/breeze --dev
          - composer install
          - composer update
          - cp .env.testing .env
          - php artisan migrate:fresh
          - php artisan db:seed
          - php artisan config:clear
          - php artisan config:cache
          - php artisan cache:clear
          - php artisan key:generate
          - php artisan test --testsuite=Feature #--filter EmailVerificationTest
        caches:
          - composer

app\Http\Kernel.php(包括在内,因为我必须更改 API 组以解决身份验证不起作用的问题)

protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
            'throttle:api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];

config\auth.php

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

    ],

原来问题出在 cache/config 的清除上。

从 .yml 文件中删除以下三行解决了问题并允许所有测试通过。

- php artisan config:clear
- php artisan config:cache
- php artisan cache:clear

感谢 https://forum.gitlab.com/t/laravel-pipeline-tests-with-posts-fail-with-419/50358 提供此解决方案。