Fixture object reference incorrect in functional test

A functional test class relies on an object reference created in a fixture. The reference’s id, however, is not identical to the object’s id property as returned by the entity manager. Below is a test that demonstrates this problem.


  1. The error is the same when using $this->setReference(...) as when
    using the public const ... and $this->addReference(...).
  2. The object reference used in the test appears to be the next
    available id for nonprofit entities.
  3. The test class was created after the error was observed in a more general test class.
  4. The error is the same whether or not the fixtures are loaded before
    running the test class.
  5. The application uses Symfony 5.1.2 with all dependencies updated.

Test class:

namespace AppTestsController;

use LiipTestFixturesBundleTestFixturesTrait;
use SymfonyBundleFrameworkBundleTestWebTestCase;

class ReferenceTest extends WebTestCase

    use FixturesTrait;

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

        $this->fixtures = $this->loadFixtures([

        $kernel = self::bootKernel();

        $this->entityManager = $kernel->getContainer()

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

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

Test result:

Reference incorrect
Failed asserting that 4 matches expected 1.

NonprofitFixture (other fixtures may not be relevant):

namespace AppDataFixturesTest;

use AppEntityNonprofit;
use DoctrineCommonDataFixturesAbstractFixture;
use DoctrineCommonDataFixturesOrderedFixtureInterface;
use DoctrineCommonPersistenceObjectManager;
use DoctrineBundleFixturesBundleORMFixtureInterface;

class NonprofitFixture extends AbstractFixture implements OrderedFixtureInterface, ORMFixtureInterface

    public const NPO_REFERENCE = 'npo';

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

        $npo1 = new Nonprofit();
        $npo1->setOrgname('Turkey Fund');

        $npo3 = new Nonprofit();
        $npo3->setOrgname('Talk Trash Fund');

        $staff = $this->getReference(UserFixture::STAFF_REFERENCE);
        $opp = $this->getReference(OpportunityFixture::OPP_REFERENCE);
        $opp1 = $this->getReference(OpportunityFixture::OPP1_REFERENCE);



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


framework.yaml excerpt:

    keep_database_and_schema: true
        sqlite: liip_test_fixtures.services_database_backup.sqlite


    enable_static_connection: true
    enable_static_meta_data_cache: true
    enable_static_query_cache: true

csv export from app.db:

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

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *