Skip to content

Commit

Permalink
bug #1546 Update the data fixtures (rosier)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the main branch.

Discussion
----------

Update the data fixtures

The class parameter is now mandatory for references.

Fixes exception when running `bin/console doctrine:fixtures:load`:
```
[ArgumentCountError]
  Too few arguments to function Doctrine\Common\DataFixtures\AbstractFixture::getReference(), 1 passed in /workspace/project/src/DataFi
  xtures/AppFixtures.php on line 143 and exactly 2 expected
```

Related to: doctrine/data-fixtures#464

Commits
-------

0ad830b Update the data fixtures
  • Loading branch information
javiereguiluz committed Dec 10, 2024
2 parents b42567d + 0ad830b commit 381f0fd
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions src/DataFixtures/AppFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ private function loadUsers(ObjectManager $manager): void
$user->setRoles($roles);

$manager->persist($user);

$this->addReference($username, $user);
}

Expand All @@ -60,6 +61,7 @@ private function loadTags(ObjectManager $manager): void
$tag = new Tag($name);

$manager->persist($tag);

$this->addReference('tag-'.$name, $tag);
}

Expand All @@ -79,11 +81,8 @@ private function loadPosts(ObjectManager $manager): void
$post->addTag(...$tags);

foreach (range(1, 5) as $i) {
/** @var User $commentAuthor */
$commentAuthor = $this->getReference('john_user');

$comment = new Comment();
$comment->setAuthor($commentAuthor);
$comment->setAuthor($this->getReference('john_user', User::class));
$comment->setContent($this->getRandomText(random_int(255, 512)));
$comment->setPublishedAt(new \DateTimeImmutable('now + '.$i.'seconds'));

Expand Down Expand Up @@ -138,18 +137,14 @@ private function getPostData(): array

foreach ($this->getPhrases() as $i => $title) {
// $postData = [$title, $slug, $summary, $content, $publishedAt, $author, $tags, $comments];

/** @var User $user */
$user = $this->getReference(['jane_admin', 'tom_admin'][0 === $i ? 0 : random_int(0, 1)]);

$posts[] = [
$title,
$this->slugger->slug($title)->lower(),
$this->getRandomText(),
$this->getPostContent(),
(new \DateTimeImmutable('now - '.$i.'days'))->setTime(random_int(8, 17), random_int(7, 49), random_int(0, 59)),
// Ensure that the first post is written by Jane Doe to simplify tests
$user,
$this->getReference(['jane_admin', 'tom_admin'][0 === $i ? 0 : random_int(0, 1)], User::class),
$this->getRandomTags(),
];
}
Expand Down Expand Up @@ -260,11 +255,9 @@ private function getRandomTags(): array
shuffle($tagNames);
$selectedTags = \array_slice($tagNames, 0, random_int(2, 4));

return array_map(function ($tagName) {
/** @var Tag $tag */
$tag = $this->getReference('tag-'.$tagName);

return $tag;
}, $selectedTags);
return array_map(
fn ($tagName) => $this->getReference('tag-'.$tagName, Tag::class),
$selectedTags
);
}
}

0 comments on commit 381f0fd

Please sign in to comment.