Skip to content

Commit e1cfcab

Browse files
committed
Improve fixture generation
1 parent e8a803a commit e1cfcab

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/AppBundle/DataFixtures/ORM/PostFixtures.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ public function load(ObjectManager $manager)
5454
$post->setAuthor($this->getReference('jane-admin'));
5555
$post->setPublishedAt(new \DateTime('now - '.$i.'days'));
5656

57-
$this->addRandomTags($post);
57+
// for aesthetic reasons, the first blog post always has 2 tags
58+
foreach ($this->getRandomTags($i > 0 ? mt_rand(0, 3) : 2) as $tag) {
59+
$post->addTag($tag);
60+
}
5861

5962
foreach (range(1, 5) as $j) {
6063
$comment = new Comment();
@@ -89,18 +92,20 @@ public function getDependencies()
8992
];
9093
}
9194

92-
private function addRandomTags(Post $post)
95+
private function getRandomTags($numTags = 0)
9396
{
94-
if (0 === $count = mt_rand(0, 3)) {
95-
return;
97+
$tags = [];
98+
99+
if (0 === $numTags) {
100+
return $tags;
96101
}
97102

98-
$indexes = (array) array_rand(TagFixtures::$names, $count);
103+
$indexes = (array) array_rand(TagFixtures::$names, $numTags);
99104
foreach ($indexes as $index) {
100-
/** @var Tag $tag */
101-
$tag = $this->getReference('tag-'.$index);
102-
$post->addTag($tag);
105+
$tags[] = $this->getReference('tag-'.$index);
103106
}
107+
108+
return $tags;
104109
}
105110

106111
private function getPostContent()
@@ -197,8 +202,13 @@ private function getRandomPostSummary($maxLength = 255)
197202

198203
$numPhrases = mt_rand(6, 12);
199204
shuffle($phrases);
205+
$phrases = array_slice($phrases, 0, $numPhrases - 1);
206+
207+
while (strlen($summary = implode('. ', $phrases).'.') > $maxLength) {
208+
array_pop($phrases);
209+
}
200210

201-
return substr(implode(' ', array_slice($phrases, 0, $numPhrases - 1)), 0, $maxLength);
211+
return $summary;
202212
}
203213

204214
private function getRandomCommentContent()

var/data/blog.sqlite

0 Bytes
Binary file not shown.

var/data/blog_test.sqlite

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)