简体   繁体   中英

setId is ignored for referenced DataFixtures Symfony4 / Doctrine

I am using DataFixtures to populate my test db with data for unittests.

For entities, which are not using other entities I can set the primary key with setId. For entities, which are used by other entities I can set it, but it is ignored.

Eg I am setting my users like this:

    /** @var Gender $genderW */
    $genderM = $this->getReference(GenderFixtures::TEST_GENDER_W);

    $date = new DateTime('now');
    /** @var User $user */
    $user = new User();
    $user
        ->setId(9)
        ->setFirstName('Hermione')
        ->setLastName('Granger')
        ->setEmail('test9@example.com')
        ->setGender($genderM)
        ->setPassword('odsf3_!45sr-f')
        ->setCreated($date);

    $manager->persist($user);

    $manager->flush();

    $this->addReference(self::TEST_USER_REFERENCE_9, $user);

As you see I set the Id for Hermione, but if I do a var_dump in my tests I see that the user Id gets incremented by my number of users for each test I am running. (9, 18, 27....)

The result is that in my tests I have to fetch the users by the unique email address, which is possible, but very annoying:

    $userRepo = $this->em->getRepository(User::class);
    $this->user = $userRepo->findOneBy(['email' => 'test9@example.com']);
    var_dump($this->user->getId());

Is there a possibility to change this, so I can get my users by Id?

config:

"require": {
    "php": "7.2.*",
    "ext-ctype": "*",
    "ext-iconv": "*",
    "cache/predis-adapter": "^1.0",
    "doctrine/doctrine-bundle": "^1.6.10",
    "doctrine/doctrine-migrations-bundle": "^1.3",
    "doctrine/orm": "^2.5.11",
    "eightpoints/guzzle-bundle": "~7.3.1",
    "friendsofsymfony/rest-bundle": "^2.3",
    "guzzlehttp/guzzle": "^6.3",
    "jms/serializer-bundle": "^2.4",
    "predis/predis": "^1.1",
    "sensio/framework-extra-bundle": "^5.2",
    "snc/redis-bundle": "3.x-dev",
    "symfony/apache-pack": "^1.0",
    "symfony/console": "^4.1",
    "symfony/flex": "^1.0",
    "symfony/framework-bundle": "^4.1",
    "symfony/lts": "^4@dev",
    "symfony/monolog-bundle": "^3.1",
    "symfony/orm-pack": "^1.0",
    "symfony/polyfill-apcu": "^1.5",
    "symfony/security-bundle": "^4.0",
    "symfony/swiftmailer-bundle": "^3.2",
    "symfony/twig-bundle": "^4.1",
    "symfony/validator": "^4.1",
    "symfony/yaml": "^4.1"
},
"require-dev": {
    "diablomedia/phpunit-pretty-printer": "2.0.*",
    "doctrine/doctrine-fixtures-bundle": "^3.0",
    "friendsofphp/php-cs-fixer": "*",
    "phpmd/phpmd": "^2.6",
    "sensiolabs/security-checker": "^4.1",
    "squizlabs/php_codesniffer": "*",
    "symfony/dotenv": "^4.1",
    "symfony/maker-bundle": "^1.5",
    "symfony/phpunit-bridge": "^4.1",
    "symfony/var-dumper": "^4.1"
},

The correct way would be to create a setUp and tearDown Method in each class/for your Testsuite. With these methods you can create/truncate the testdata in your database each time it is run.

According to the Phpunit documentation for Databasetesting the setup will clean up before the test is run

PHPUnit will execute a TRUNCATE against all the tables you specified to reset their status to empty.

With only a truncate, not resetting the autoincrement values.

To clean your database keys you should use the tearDown for the test to reset the keys

ALTER TABLE tablename AUTO_INCREMENT = 1

This will reset the autoincrement values after each test.


To achieve this only with symfony you could create a command which will do the following things:

  1. Reset the testdatabase ./app/console doctrine:fixtures:load --purge-with-truncate found this command on SO
  2. Create fixtures
  3. Execute your testrun

You will have 1 command to run your tests only using symfony.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM