laravel -无法运行phpunit - TestCase::setUp()必须与PHPUnit兼容

bis0qfac  于 2023-03-28  发布在  PHP
关注(0)|答案(1)|浏览(222)

我尝试运行phpunit,得到这个错误:

PHP Fatal error:  Declaration of Illuminate\Foundation\Testing\TestCase::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp(): void in /Users/xxx/dev/xxx/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php on line 65

所以我找到了解决方案:将:void添加到测试的setUp()tearDown方法中。
所以我修改了laravel里面的文件:vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php

public function setUp():void
{
  parent::setUp();
}

public function tearDown():void
{
  parent::tearDown();
}

它工作,但问题是,我正在修改一个文件,这是在vendor目录,它可能会被覆盖的 composer 命令。
如何在不修改vendor目录中的文件的情况下应用此解决方案?
我的composer.json文件

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=7.2",
        "aloha/twilio": "^4.0",
        "aws/aws-sdk-php": "^3.154",
        "bensampo/laravel-enum": "^1.11",
        "crockett/csv-seeder": "^1.1",
        "cviebrock/eloquent-sluggable": "^4.5",
        "fideloper/proxy": "^4.0",
        "guzzlehttp/guzzle": "^6.3",
        "iugu/iugu": "^1.0",
        "kreait/firebase-php": "^4.35",
        "laravel/framework": "5.6.*",
        "laravel/tinker": "^1.0",
        "league/flysystem-aws-s3-v3": "^1.0",
        "maatwebsite/excel": "^3.1",
        "malhal/laravel-geographical": "^1.0",
        "phpseclib/phpseclib": "2.0",
        "simplesoftwareio/simple-qrcode": "^2.0",
        "tightenco/ziggy": "^0.6.7",
        "tymon/jwt-auth": "1.0.0-rc.2",
        "zizaco/entrust": "^1.9"
    },
    "require-dev": {
        "filp/whoops": "^2.0",
        "fzaninotto/faker": "^1.4",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^2.0",
        "phpunit/phpunit": "*"
    },
    "autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "extra": {
        "laravel": {
            "dont-discover": [
            ]
        }
    },
    "scripts": {
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate"
        ],
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true,
        "platform": {
            "php": "7.2"
        },
        "allow-plugins": {
            "kylekatarnls/update-helper": true
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}
gmxoilav

gmxoilav1#

你使用的是PHPUnit 8,它与Laravel 5.6不兼容,你必须使用PHPUnit 7,而不是8+。为什么?

PHPUnit TestCase.php
/**
 * This method is called before each test.
 */
protected function setUp(): void
{
}

正如你所看到的,它定义了: void,但是Laravel 5.6没有:

/**
 * Setup the test environment.
 *
 * @return void
 */
protected function setUp()
{

因为这些文件是vendor文件,你不能编辑它们,但要安装每个文件的正确版本。你的修复方法是将你的composer.json"phpunit/phpunit": "*"更改为"phpunit/phpunit": "^7.0",就像官方的Laravel 5.6 composer.json一样:https://github.com/laravel/laravel/blob/0d5c1c81ff6faafbd8cc995aa3d0cd1b155df4c3/composer.json#L18。
您可以在这里看到,PHPUnit 7与Laravel 5.6 TestCase.php文件相比具有有效的定义:

/**
 * This method is called before each test.
 */
protected function setUp()/* The :void return type declaration that should be here would cause a BC issue */
{
}

/**
 * This method is called after each test.
 */
protected function tearDown()/* The :void return type declaration that should be here would cause a BC issue */
{
}

相关问题