Skip to content

Commit 381f0fd

Browse files
committed
bug #1546 Update the data fixtures (rosier)
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
2 parents b42567d + 0ad830b commit 381f0fd

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

src/DataFixtures/AppFixtures.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ private function loadUsers(ObjectManager $manager): void
4848
$user->setRoles($roles);
4949

5050
$manager->persist($user);
51+
5152
$this->addReference($username, $user);
5253
}
5354

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

6263
$manager->persist($tag);
64+
6365
$this->addReference('tag-'.$name, $tag);
6466
}
6567

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

8183
foreach (range(1, 5) as $i) {
82-
/** @var User $commentAuthor */
83-
$commentAuthor = $this->getReference('john_user');
84-
8584
$comment = new Comment();
86-
$comment->setAuthor($commentAuthor);
85+
$comment->setAuthor($this->getReference('john_user', User::class));
8786
$comment->setContent($this->getRandomText(random_int(255, 512)));
8887
$comment->setPublishedAt(new \DateTimeImmutable('now + '.$i.'seconds'));
8988

@@ -138,18 +137,14 @@ private function getPostData(): array
138137

139138
foreach ($this->getPhrases() as $i => $title) {
140139
// $postData = [$title, $slug, $summary, $content, $publishedAt, $author, $tags, $comments];
141-
142-
/** @var User $user */
143-
$user = $this->getReference(['jane_admin', 'tom_admin'][0 === $i ? 0 : random_int(0, 1)]);
144-
145140
$posts[] = [
146141
$title,
147142
$this->slugger->slug($title)->lower(),
148143
$this->getRandomText(),
149144
$this->getPostContent(),
150145
(new \DateTimeImmutable('now - '.$i.'days'))->setTime(random_int(8, 17), random_int(7, 49), random_int(0, 59)),
151146
// Ensure that the first post is written by Jane Doe to simplify tests
152-
$user,
147+
$this->getReference(['jane_admin', 'tom_admin'][0 === $i ? 0 : random_int(0, 1)], User::class),
153148
$this->getRandomTags(),
154149
];
155150
}
@@ -260,11 +255,9 @@ private function getRandomTags(): array
260255
shuffle($tagNames);
261256
$selectedTags = \array_slice($tagNames, 0, random_int(2, 4));
262257

263-
return array_map(function ($tagName) {
264-
/** @var Tag $tag */
265-
$tag = $this->getReference('tag-'.$tagName);
266-
267-
return $tag;
268-
}, $selectedTags);
258+
return array_map(
259+
fn ($tagName) => $this->getReference('tag-'.$tagName, Tag::class),
260+
$selectedTags
261+
);
269262
}
270263
}

0 commit comments

Comments
 (0)