Fixture 对象引用在功能测试中不正确

Fixture object reference incorrect in functional test

功能测试 class 依赖于在夹具中创建的对象引用。但是,引用的 ID 与实体管理器返回的对象的 ID 属性 不同。下面是演示此问题的测试。

备注:

  1. 错误与使用$this->setReference(...)时相同 使用 public const ...$this->addReference(...).
  2. 测试中使用的对象引用似乎是下一个 非营利实体的可用 ID。
  3. 测试 class 是在更一般的测试 class 中观察到错误后创建的。
  4. 无论之前是否加载fixture,错误都是一样的 运行 测试 class.
  5. 该应用程序使用 Symfony 5.1.2 并更新了所有依赖项。

测试class:

namespace App\Tests\Controller;

use Liip\TestFixturesBundle\Test\FixturesTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ReferenceTest extends WebTestCase
{

    use FixturesTrait;

    public function setup(): void {
        $this->client = $this->createClient();

        $this->fixtures = $this->loadFixtures([
                    'App\DataFixtures\Test\OptionsFixture',
                    'App\DataFixtures\Test\NonprofitFixture',
                    'App\DataFixtures\Test\OpportunityFixture',
                    'App\DataFixtures\Test\UserFixture',
                ])
                ->getReferenceRepository();
        $this->client->followRedirects();

        $kernel = self::bootKernel();

        $this->entityManager = $kernel->getContainer()
                ->get('doctrine')
                ->getManager('test');
    }

    public function testNonprofitReference() {
        $npo = $this->entityManager->getRepository(\App\Entity\Nonprofit::class)
                ->findOneBy(['orgname' => 'Marmot Fund']);
        $nId = $npo->getId();
        $id = $this->fixtures->getReference('npo')->getId();

        $this->assertEquals($nId, $id, 'Reference incorrect');
    }   
}

测试结果:

Reference incorrect
Failed asserting that 4 matches expected 1.

NonprofitFixture(其他固定装置可能不相关):

namespace App\DataFixtures\Test;

use App\Entity\Nonprofit;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Bundle\FixturesBundle\ORMFixtureInterface;

class NonprofitFixture extends AbstractFixture implements OrderedFixtureInterface, ORMFixtureInterface
{

    public const NPO_REFERENCE = 'npo';

    public function load(ObjectManager $manager) {
        $npo = new Nonprofit();
        $npo->setOrgname('Marmot Fund');
        $npo->setEin('123456789');
        $npo->setActive(false);
//        $this->setReference('npo', $npo);
        $this->addReference(self::NPO_REFERENCE, $npo);

        $npo1 = new Nonprofit();
        $npo1->setOrgname('Turkey Fund');
        $npo1->setEin('321654978');
        $npo1->setActive(true);
        $npo1->setWebsite('http://turkeysRUs.bogus.info');

        $npo3 = new Nonprofit();
        $npo3->setOrgname('Talk Trash Fund');
        $npo3->setEin('978654321');
        $npo3->setActive(true);
        $npo3->setWebsite('http://ttrash.bogus.info');

        $staff = $this->getReference(UserFixture::STAFF_REFERENCE);
        $npo->setStaff($staff);
        
        $opp = $this->getReference(OpportunityFixture::OPP_REFERENCE);
        $opp1 = $this->getReference(OpportunityFixture::OPP1_REFERENCE);
        $npo1->addOpportunity($opp);
        $npo3->addOpportunity($opp1);

        $manager->persist($npo);
        $manager->persist($npo1);
        $manager->persist($npo3);

        $manager->flush();
    }

    public function getOrder() {
        return 5; // the order in which fixtures will be loaded
    }

}

framework.yaml 摘录:

liip_test_fixtures:
    keep_database_and_schema: true
    cache_db:
        sqlite: liip_test_fixtures.services_database_backup.sqlite

dama_doctrine_test_bundle.yaml:

dama_doctrine_test:
    enable_static_connection: true
    enable_static_meta_data_cache: true
    enable_static_query_cache: true

csv 导出自 app.db:

"id","orgName"
"1","Marmot Fund"
"2","Turkey Fund"
"3","Talk Trash Fund"

答案是,引用在功能测试中没有位置。它们的使用实际上是单击链接或执行其他操作的快捷方式。更好的测试是使用爬虫模仿动作。