Skip to content

Commit

Permalink
Update the data fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
rosier authored and javiereguiluz committed Dec 10, 2024
1 parent 7169fb5 commit 0ad830b
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 0ad830b

Please sign in to comment.