0

I'm using api-platform and I write unittest with fixtures (https://api-platform.com/docs/distribution/testing/#testing-the-api). When I run test for specific class I got the same fixtures data but when I run all test I got some data random.

Here are my fixtures:

# one file
App\Entity\User:
  user_{1..11}:
    nickname: '<username()>'
    avatar: '@image_user_<current()>'
    firstname: '<firstname()>'
    lastname: '<lastname()>'

  user_{12..13}:
    nickname: '<username()>'
    avatar: null
    firstname: '<firstname()>'
    lastname: '<lastname()>'

# other file
App\Entity\Project:
  project_{1..7}:
    name: '<company()>'
    author: '@user_<numberBetween(3, 13)>'
    main_image: '@image_project_<current()>'
    score: '<randomFloat(null, 0, 5)>'
    created_at: <dateTime('2020-08-11 09:24')>

Nelmio_alice config looks like:

> bin/api debug:config nelmio_alice

Current configuration for extension with alias "nelmio_alice"
=============================================================

nelmio_alice:
  functions_blacklist:
    - current
    - shuffle
    - date
    - time
    - file
    - md5
    - sha1
  locale: en_US
  seed: 1
  loading_limit: 5
  max_unique_values_retry: 150

When I run test for one class every time they pass (all data are the same):

bin/api-test tests/Entity/UserTest
bin/api-test tests/Entity/ProjectTest

But when I want to run all tests I got random data for user

bin/api-test

When I clear cache I got random data for project too but with next run projects test pass, users not

bin/api cache:clear --env=test
bin/api-test
// some projects and users test fail
bin/api-test
// projects tests pass, users not
  • bin/api is an alias for docker-compose exec php bin/console
  • bin/api-test is an alias for docker-compose exec php bin/phpunit
d0niek
  • 173
  • 11
  • What have you tried to debug the problem? Looks like your tests manipulate the database? – Nico Haase Aug 11 '20 at 08:24
  • yep. I use `use Hautelook\AliceBundle\PhpUnit\RefreshDatabaseTrait;` which load fixtures every test – d0niek Aug 11 '20 at 08:57
  • 1
    Why not load them **once** instead? https://packagist.org/packages/dama/doctrine-test-bundle would help you in this case, as this wraps running a test into a database transaction which is rolled back after each test – Nico Haase Aug 11 '20 at 09:10
  • It helps, I load fixtures once so performance go up too. But I think a problem is still. – d0niek Aug 11 '20 at 09:30

0 Answers0