You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony7+Liip测试组件:数据库架构未创建、Fixture未加载问题

Symfony 7测试中Liip Fixtures加载失败及数据库架构生成问题

环境信息

  • Symfony 7.*
  • liip/functional-test-bundle 4.13.0
  • liip/test-fixtures-bundle 3.0.0
  • PHPUnit 9.6.22
  • dama/doctrine-test-bundle(引发冲突的第三方包)

配置与代码示例

doctrine.yaml(测试环境配置)

when@test:
    doctrine:
        dbal:
            driver: pdo_sqlite
            user: "root"
            dbname: "name_test"
            password: "*********"
            path: "%kernel.cache_dir%/name_test.db"
            charset: UTF8
            url: null

SceneTrait

...
use Liip\FunctionalTestBundle\Test\WebTestCase;
...

/**
 * @extends WebTestCase<SceneTrait>
 */
trait SceneTrait
{
    use FixtureTrait {
        setUp as fixtureSetUp;
        tearDown as fixtureTearDown;
    }

    public function setUp(): void
    {
        $this->getRestClient();
        $this->fixtureSetUp();
    }
...
}

FixtureTrait

/**
 * @extends KernelTestCase<FixtureTrait>
 */
trait FixtureTrait
{
...
    public function setUp(): void
    {
        foreach ($_ENV as $key => $value) {
            putenv($key.'='.$value);
        }

        parent::setUp();

        /** @var ContainerInterface $container */
        $container = static::getContainer();

        /** @var DatabaseToolCollection $databaseToolCollection */
        $databaseToolCollection = $container->get(DatabaseToolCollection::class);
        $this->databaseTool = $databaseToolCollection->get();

        /** @var Registry $doctrine */
        $doctrine = $container->get('doctrine');
        /** @var EntityManager $entityManager */
        $entityManager = $doctrine->getManager();
        $this->entityManager = $entityManager;

        $this->getRpFixture = $this->databaseTool->loadFixtures($this->_getDependencies())->getReferenceRepository();
    }
...
}

测试类示例

use Liip\FunctionalTestBundle\Test\WebTestCase;

final class AffaireControllerTest extends WebTestCase
{
    use SceneTrait;
...
}

调试到的核心方法(ORMSqliteDatabaseTool::loadFixtures)

public function loadFixtures(array $classNames = [], bool $append = false): AbstractExecutor
{
    $referenceRepository = new ProxyReferenceRepository($this->om);

    /** @var Configuration $config */
    $config = $this->om->getConfiguration();

    $cacheDriver = $config->getMetadataCache();

    if ($cacheDriver) {
        $cacheDriver->clear();
    }

    if (false === $append && false === $this->getKeepDatabaseAndSchemaParameter()) {
        // TODO: handle case when using persistent connections. Fail loudly?
        $schemaTool = new SchemaTool($this->om);
        $schemaTool->dropDatabase();
        if (!empty($this->getMetadatas())) {
            $schemaTool->createSchema($this->getMetadatas());
        }
    }

    $event = new FixtureEvent();
    $this->eventDispatcher->dispatch($event, LiipTestFixturesEvents::POST_FIXTURE_SETUP);

    $executor = $this->getExecutor($this->getPurger());
    $executor->setReferenceRepository($referenceRepository);
    if (false === $append) {
        $executor->purge();
    }

    $loader = $this->fixturesLoaderFactory->getFixtureLoader($classNames);
    $executor->execute($loader->getFixtures(), true);

    return $executor;
}

问题现象

  • 调试确认上述loadFixtures()方法被执行,且$schemaTool->schemaManager->_conn->params数据库参数、$this->getMetadatas()实体架构参数、$classnames Fixture类名均正确。
  • 初始状态:数据库已创建,但架构未生成,Fixture未加载。
  • 手动在test/bootstrap.php中添加以下代码后,架构生成成功,但Fixture仍未加载:
$appKernel = new Kernel('test', false);
$appKernel->boot();

$application = new Application($appKernel);
$application->setCatchExceptions(false);
$application->setAutoExit(false);

$application->run(new ArrayInput([
   'command' => 'doctrine:database:drop',
   '--force' => '1',
]));

$application->run(new ArrayInput([
   'command' => 'doctrine:database:create',
]));

$application->run(new ArrayInput([
   'command' => 'doctrine:schema:create',
]));

$appKernel->shutdown();
  • 移除dama/doctrine-test-bundle后,架构生成和Fixture加载均正常;但使用该包或Hautelook Alice Bundle的RefreshDatabaseTrait时,数据库架构又变为空。

疑问

  1. 为什么loadFixtures()中的$schemaTool->createSchema()逻辑未自动生效?
  2. 如何在保留数据库自动重置功能的前提下,让架构生成和Fixture加载正常工作?

内容的提问来源于stack exchange,提问作者Dev Th

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 14:39:57