1

I have a set of fixtures (here is a simplification):

My\Entity\User:
  user_{1..10}:
    name: <firstName()>

My\Entity\Item:
  item_{1..10}:
    user: '@user_$current'
    data: <numberBetween(111111111, 999999999)>

I want to fetch Item with ID 4 inside my phpunit functional test.

I can't be sure that autoincrement ID is started from 1. It's not 1 after TRUNCATE. So this is incorrect:

$item4 = $this->em->getRepository(Item::class)->find(4);

How can I get a reference to item_4?

Dmitry
  • 7,457
  • 12
  • 57
  • 83
  • I don´t know an elegant solution, so what´s about remapping the keys? – J. Knabenschuh Mar 22 '19 at 14:08
  • Can you add the part of your code that loads the fixtures? The Loader that loads the fixture file will return an array with all the entities. From this array you can get the entity instead of fetching it via the entity manager. If you want to make sure, you can find it, you can use this entity's id to pass to `find()` – dbrumann Mar 22 '19 at 15:39

1 Answers1

4

You can get the entities generated from the fixtures file directly from the loader:

$loader = new Nelmio\Alice\Loader\NativeLoader();
$objectSet = $loader->loadFile(__DIR__.'/fixtures.yml');

The $objectSet should contain all your entities by their alias, so you can get a specific item and directly work with it or fetch it again using your repository

$fixtureItem4 = $objectSet['item_4'];
$persistedItem4 = $this->em->getRepository(Item::class)->find($fixtureItem4->getId());
dbrumann
  • 16,803
  • 2
  • 42
  • 58