Symfony2功能测试+学说固定装置:在学说固定装置中刷新后,实体的更改将无法保存

时间:2015-06-03 07:59:24

标签: php symfony doctrine-orm functional-testing fixtures

我在Symfony 2.6中为我的功能测试执行fixtures时遇到了问题。在夹具中,我有一个创建基本实体并将其刷新到数据库的功能。然后我从其他函数中调用此函数,将新创建的实体更改为更复杂的东西(比如设置与其他实体的关系),然后再次刷新。

但是,当我检查数据库时,第二次刷新不会对实体进行任何更改。

我已经尝试过调试,我看到的是UnitOfWork在第二次刷新之前不包含任何计划插入/更新的内容,尽管我执行了函数set ....()。

你知道我做错了什么吗?

以下是我的装置的摘录:

class LoadTestApplications extends AbstractFixture implements OrderedFixtureInterface
{
    const APPLICATION_ID_NOT_SUBMITTED_VALID = 37;

    public function load(ObjectManager $manager)
    {
        //other lines ommitted

        $this->loadNotSubmittedValidApplication($manager);

        //other lines ommited
    }

    private function loadNotSubmittedValidApplication(ObjectManager $manager)
    {
        $application = $this->createAndGetFilledApplication($manager, self::APPLICATION_ID_NOT_SUBMITTED_VALID,
            'property-property-empty', 'GROUP_INSTALLER', 'application-application-not-submitted-valid',
            'person-default', 'person-default', 'person-default', 'person-default');

        /** @var Installer $installer */
        $installer = $this->getReference('installer-installer1');

        /** @var InstallerContractor $installerContractor */
        $installerContractor = $this->getReference('installer-installer-contractor1');

        $application->setInstaller($installer);
        $installer->addApplication($application);

        $application->setInstallerContractor($installerContractor);
        $installerContractor->addApplication($application);

        $manager->persist($installer);
        $manager->persist($installerContractor);
        $manager->persist($application);
        $manager->flush(); // this saves no changes
    }

    private function createAndGetFilledApplication(ObjectManager $manager, $applicationId, $propertyReference,
       $acg, $referenceName, $contactReference, $improverReference, $billPayerReference, $propertyOwnerReference)
    {
        $application = new Application();
        $application->setId($applicationId);

        // lots of other set...() functions


        // that's for forcing our own ID on the entity, so we can force only
        // some of the entities to be loaded in the fixture and the id of
        // the entity stays the same no matter how many other entities have
        // been loaded before. This is crucial in our functional tests, so
        // we can go to e.g. http://..../application/15/show
        /** @var ClassMetadata $metadata */
        $metadata = $manager->getClassMetadata(get_class($application));
        $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);

        $manager->flush(); //this saves all changes from this function

        $this->addReference($referenceName, $application);

        return $application;
    }
}

感谢任何帮助,提前谢谢!

1 个答案:

答案 0 :(得分:0)

好的,这次是我傻了。

我们最近将Application - InstallerContractor关系更改为多对多关系(我们之前使用的是一对多),因此不使用

setInstallerContractor()

我必须使用

addInstallerContractor()

感谢DerStoffel的帮助,我们一定会尝试https://github.com/liip/LiipFunctionalTestBundle