我正在尝试配置 symfony 以使用 sqlite 进行测试。
composer.json(需要开发)
"doctrine/doctrine-fixtures-bundle": "^3.1",
"liip/functional-test-bundle": "~2.0@alpha",
config/packages/test/doctrine.yaml:
doctrine:
dbal:
driver: 'pdo_sqlite'
url: 'sqlite:///%kernel.project_dir%/var/test.db3'
然后我做了一个这样的测试
class SimplestTest extends WebTestCase
{
private $fixtures;
public function setUp()
{
$this->fixtures = $this->loadFixtures([
MyFixtures::class
])->getReferenceRepository();
}
public function testToSeeIfItWorks()
{
$this->assertTrue(true);
}
}
MyFixtures 类扩展了 AbstractFixture 并加载了一些简单的对象:
class MyFixtures extends AbstractFixture
{
public function load(ObjectManager $manager)
{
$user1 = new User();
$user1->setRoles(['ROLE_USER']);
$manager->persist($user1);
$manager->flush();
$myFeed = new Feed();
$myFeed->setName('Feed 1');
$myFeed->setUrl('http://someurl');
$myFeed->setUser($user1);
$manager->persist($myFeed);
$manager->flush();
}
}
当我运行测试时,我得到:
InvalidArgumentException: "App\Tests\DataFixtures\ORM\MyFixtures" is not a registered fixture
不知道为什么。有帮助吗?
谢谢
最佳答案
我想我知道问题出在哪里了。如果涉及到夹具应从中扩展的类,则库的文档中似乎存在错误。在 the documentation在使用 Doctrine\Common\DataFixtures\AbstractFixture
的状态下,但它使夹具不会自动加载。
正如您在 DoctrineFixturesBundle documentation 中所读到的那样, bundle 将通过 Autowiring 到任何实现 ORMFixtureInterface
的类来添加一个 doctrine.fixture.orm
标签,比如 Doctrine\Bundle\FixturesBundle\Fixture
.从实现该接口(interface)的类加载夹具由 FixturesCompilerPass
完成。
如果您将 fixture 的基类更改为 Doctrine\Bundle\FixturesBundle\Fixture
它应该可以工作。
更新:
最重要的是,fixture 类必须位于 App\DataFixtures
命名空间中。
那是因为该命名空间被配置为 Autowiring 和自动配置。如果您想将夹具文件保留在 App\Tests\DataFixtures\
下,请将适当的配置添加到 config/services.yaml
文件:
services:
App\Tests\DataFixtures\:
resource: '../tests/DataFixtures'
关于php - 使用 sqlite 运行 phpunit 时,Symfony 出现固定装置问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55553856/