gpt4 book ai didi

php - 在 Symfony4 : class not found 中测试服务

转载 作者:行者123 更新时间:2023-12-02 17:10:30 26 4
gpt4 key购买 nike

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

Error: Class 'App\Service\CompanyManager' not found

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

测试/服务/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);
}
}

在 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'

所以我尝试添加:

test.App\Service\CompanyManager: '@App\Service\CompanyManager'

但我仍然得到错误:

$ ./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

第 22 行是:

$companyManager = new CompanyManager();

有什么想法吗?

PS:听起来有人在那里有同样的问题:PHPUnit Error: Class not found

最佳答案

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

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
>

关于php - 在 Symfony4 : class not found 中测试服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49571043/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com