Generating code coverage report in Clover XML format ... PHP Fatal error: Cannot declare class ..., because the name is already in use in

Generating code coverage report in Clover XML format ... PHP Fatal error: Cannot declare class ..., because the name is already in use in

我想为 CodeCov 获取 coverage.xml。 PHP8.0.2 PHP9.5.2单元 Xdebug 3.0.2

我的class。很简单,练习一下代码覆盖率 src/Car.php

<?php
/**
* This class is for car.
*/

class Car
{
 public function GetColor()
 {
   return 'Blue';
 }
}

测试文件。 tests/CarTest.php

<?php
include_once __DIR__ .'/../src/Car.php';
use PHPUnit\Framework\TestCase;
/**
 * @coversDefaultClass Car
 */
class CarTest extends TestCase
{
  /**
  * @covers Car::GetColor
  */
  public function testCarGoodColor(): void
  {
    $car = new Car();
    $this->assertSame('Blue', $car->GetColor());
  }
}

当我进入

./vendor/bin/phpunit tests/ --coverage-clover ./coverage.xml

我收到这条消息:

PHPUnit 9.5.2 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.0.2 with Xdebug 3.0.2
Configuration: /Applications/CI-CD/phpunit.xml

.                                                                   1 / 1 (100%)

Time: 00:00.187, Memory: 8.00 MB

OK (1 test, 1 assertion)

Generating code coverage report in Clover XML format ... PHP Fatal error:  Cannot declare 
class Car, because the name is already in use in /Applications/CI-CD/src/Car.php on line 8

Fatal error: Cannot declare class Car, because the name is already in use in /Applications/CI-CD/src/Car.php on line 8

如何使用 classes 解决这个问题?

非常感谢LazyOne

问题出在

include_once __DIR__ .'/

../src/Car.php';

我用自动加载修复了它。 此外,我在 index.php 中有 src/ 并将其添加到 ignore 以获取代码覆盖率。