From 0ad830b157e30c550fa031805b7301d96a2c7bdc Mon Sep 17 00:00:00 2001 From: Jan Rosier Date: Mon, 9 Dec 2024 21:03:33 +0100 Subject: [PATCH] Update the data fixtures --- src/DataFixtures/AppFixtures.php | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/DataFixtures/AppFixtures.php b/src/DataFixtures/AppFixtures.php index 1543b42a9..d6f522aa0 100644 --- a/src/DataFixtures/AppFixtures.php +++ b/src/DataFixtures/AppFixtures.php @@ -48,6 +48,7 @@ private function loadUsers(ObjectManager $manager): void $user->setRoles($roles); $manager->persist($user); + $this->addReference($username, $user); } @@ -60,6 +61,7 @@ private function loadTags(ObjectManager $manager): void $tag = new Tag($name); $manager->persist($tag); + $this->addReference('tag-'.$name, $tag); } @@ -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')); @@ -138,10 +137,6 @@ 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(), @@ -149,7 +144,7 @@ private function getPostData(): array $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(), ]; } @@ -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 + ); } }