在 Symfony4 中测试服务:找不到类

syl*_*ain 2 php phpunit symfony4

我想使用 phpunit 桥在 Symfony 4 中测试我的服务,但是当我启动测试时,我得到:

Error: Class 'App\Service\CompanyManager' not found
Run Code Online (Sandbox Code Playgroud)

我的服务位于 src/Service/CompanyManager.php

测试/Service/CompanyManagerTest.php

namespace App\Tests\Service;

use App\Service\CompanyManager;
use PHPUnit\Framework\TestCase;
use App\Entity\Company;

class CompanyManagerTest extends TestCase
{
    public function testGetCompany()
    {
        $companyManager = new CompanyManager();
        $company = $companyManager->getCompany(2);
        $this->assertInstanceOf(Company::class,$company);
        $company = $companyManager->getCompany(1000);
        $this->assertNull($company);
    }
}
Run Code Online (Sandbox Code Playgroud)

在config/services_test.yaml中,有这样的语句:

# If you need to access services in a test, create an alias
# and then fetch that alias from the container. As a convention,
# aliases are prefixed with test. For example:
#
# test.App\Service\MyService: '@App\Service\MyService'
Run Code Online (Sandbox Code Playgroud)

所以我尝试添加:

test.App\Service\CompanyManager: '@App\Service\CompanyManager'
Run Code Online (Sandbox Code Playgroud)

但我仍然收到错误:

$ ./vendor/bin/simple-phpunit tests
PHPUnit 5.7.27 by Sebastian Bergmann and contributors.

Testing tests
E                                                                   1 / 1 
(100%)

Time: 364 ms, Memory: 4.00MB

There was 1 error:

1) App\Tests\Service\CompanyManagerTest::testGetCompany
Error: Class 'App\Service\CompanyManager' not found

C:\...\web\vp20\tests\Service\CompanyManagerTest.php:22
Run Code Online (Sandbox Code Playgroud)

第 22 行是:

$companyManager = new CompanyManager();
Run Code Online (Sandbox Code Playgroud)

任何想法 ?

PS:听起来有人有同样的问题:PHPUnit 错误:找不到类

Dan*_*l P 6

我刚刚遇到这个问题。phpunit.xml.dist不知道为什么,但我的项目根目录中没有. 所有示例都没有显示这一点,但如果您添加bootstrap=然后包含自动加载的。它应该开始找到您的课程。

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.0/phpunit.xsd"
     bootstrap="vendor/autoload.php"
>
Run Code Online (Sandbox Code Playgroud)