在链配置的命名空间 App\Entity [Symfony 5.3][PHPUnit 8.5] 中找不到类 Mock_*
Posted
技术标签:
【中文标题】在链配置的命名空间 App\\Entity [Symfony 5.3][PHPUnit 8.5] 中找不到类 Mock_*【英文标题】:The class Mock_* was not found in the chain configured namespaces App\Entity [Symfony 5.3][PHPUnit 8.5]在链配置的命名空间 App\Entity [Symfony 5.3][PHPUnit 8.5] 中找不到类 Mock_* 【发布时间】:2021-12-27 21:54:30 【问题描述】:我正在尝试为我的 Symfony 5.3 项目编写 phpUnit 测试,其中一些存储库与其他存储库一起模拟。
$ bin/console -v
Symfony 5.3.10 (env: dev, debug: true)
$ bin/phpunit -V
PHPUnit 8.5.19 by Sebastian Bergmann and contributors.
Doctrine\Persistence\Mapping\MappingException: The class 'Mock_SType_b1b7aee4' was not found in the chain configured namespaces App\Entity
/var/www/cir/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/MappingException.php:23
/var/www/cir/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/Driver/MappingDriverChain.php:91
/var/www/cir/vendor/doctrine/doctrine-bundle/Mapping/MappingDriver.php:45
/var/www/cir/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:156
/var/www/cir/vendor/doctrine/doctrine-bundle/Mapping/ClassMetadataFactory.php:19
/var/www/cir/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:382
/var/www/cir/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:85
/var/www/cir/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:251
/var/www/cir/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:293
/var/www/cir/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1789
/var/www/cir/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1764
/var/www/cir/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:629
/var/www/cir/src/Component/User/UserManager.php:1059
/var/www/cir/src/Component/User/UserManager.php:383
/var/www/cir/tests/Component/User/Manager/Creation/ATCPTest.php:146
我创建了一个 KernelTestCase,其中:
$sTypeRepository = $this->createStub(STypeRepository::class);
所以我尝试使用实际的存储库:
$sTypeRepository = static::getContainer()->get('doctrine')->getRepository(SType::class);
我得到错误:
TypeError: Argument 2 passed to App\Component\User\UserManager::__construct() must be an instance of App\Repository\STypeRepository, instance of Doctrine\ORM\EntityRepository given, called in /var/www/cir/tests/Component/User/Manager/Creation/ATCPTest.php on line 122
/var/www/cir/src/Component/User/UserManager.php:214
/var/www/cir/tests/Component/User/Manager/Creation/ATCPTest.php:122
其他模拟类的其他测试运行良好,如下所示:
$this->SUserRoleRepository = $this->createStub(SUserRoleRepository::class);
那么,为什么我会分别为某些存储库获得 MappingException
或 TypeError
而不是其他存储库?也许可以帮助我更好地理解错误?谢谢!
【问题讨论】:
【参考方案1】:问题是我的 SType 实体没有准确的 @ORM\Entity(...) 注释。我有:
/**
* SType.
*
* @ORM\Table(name="s_type", indexes=@ORM\Index(name="idx_s_type_1", columns="type_group"), @ORM\Index(name="idx_s_type_2", columns="code"))
* @ORM\Entity
*/
class SType
需要repositoryClass关系:
/**
* SType.
*
* @ORM\Table(name="s_type", indexes=@ORM\Index(name="idx_s_type_1", columns="type_group"), @ORM\Index(name="idx_s_type_2", columns="code"))
* @ORM\Entity(repositoryClass="App\Repository\STypeRepository")
*/
class SType
【讨论】:
以上是关于在链配置的命名空间 App\Entity [Symfony 5.3][PHPUnit 8.5] 中找不到类 Mock_*的主要内容,如果未能解决你的问题,请参考以下文章