1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Symfony package. |
5
|
|
|
* |
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace App\DataFixtures\ORM; |
13
|
|
|
|
14
|
|
|
use App\DataFixtures\FixturesTrait; |
15
|
|
|
use App\Entity\Comment; |
16
|
|
|
use App\Entity\Post; |
17
|
|
|
use App\Entity\User; |
18
|
|
|
use App\Utils\Slugger; |
19
|
|
|
use Doctrine\Common\DataFixtures\AbstractFixture; |
20
|
|
|
use Doctrine\Common\DataFixtures\DependentFixtureInterface; |
21
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Defines the sample blog posts to load in the database before running the unit |
25
|
|
|
* and functional tests. Execute this command to load the data. |
26
|
|
|
* |
27
|
|
|
* $ php bin/console doctrine:fixtures:load |
28
|
|
|
* |
29
|
|
|
* See https://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html |
30
|
|
|
* |
31
|
|
|
* @author Ryan Weaver <[email protected]> |
32
|
|
|
* @author Javier Eguiluz <[email protected]> |
33
|
|
|
* @author Yonel Ceruto <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class PostFixtures extends AbstractFixture implements DependentFixtureInterface |
36
|
|
|
{ |
37
|
|
|
use FixturesTrait; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function load(ObjectManager $manager): void |
43
|
|
|
{ |
44
|
|
|
foreach ($this->getRandomPostTitles() as $i => $title) { |
45
|
|
|
$post = new Post(); |
46
|
|
|
|
47
|
|
|
$post->setTitle($title); |
48
|
|
|
$post->setSummary($this->getRandomPostSummary()); |
49
|
|
|
$post->setSlug(Slugger::slugify($post->getTitle())); |
50
|
|
|
$post->setContent($this->getPostContent()); |
51
|
|
|
$post->setPublishedAt(new \DateTime('now - '.$i.'days')); |
52
|
|
|
|
53
|
|
|
// Ensure that the first post is written by Jane Doe to simplify tests |
54
|
|
|
// "References" are the way to share objects between fixtures defined |
55
|
|
|
// in different files. This reference has been added in the UserFixtures |
56
|
|
|
// file and it contains an instance of the User entity. |
57
|
|
|
$post->setAuthor(0 === $i ? $this->getReference('jane-admin') : $this->getRandomUser()); |
58
|
|
|
|
59
|
|
|
// for aesthetic reasons, the first blog post always has 2 tags |
60
|
|
|
foreach ($this->getRandomTags($i > 0 ? mt_rand(0, 3) : 2) as $tag) { |
61
|
|
|
$post->addTag($tag); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
foreach (range(1, 5) as $j) { |
65
|
|
|
$comment = new Comment(); |
66
|
|
|
|
67
|
|
|
$comment->setAuthor($this->getReference('john-user')); |
68
|
|
|
$comment->setPublishedAt(new \DateTime('now + '.($i + $j).'seconds')); |
69
|
|
|
$comment->setContent($this->getRandomCommentContent()); |
70
|
|
|
|
71
|
|
|
$post->addComment($comment); |
72
|
|
|
|
73
|
|
|
$manager->persist($comment); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$manager->persist($post); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$manager->flush(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Instead of defining the exact order in which the fixtures files must be loaded, |
84
|
|
|
* this method defines which other fixtures this file depends on. Then, Doctrine |
85
|
|
|
* will figure out the best order to fit all the dependencies. |
86
|
|
|
*/ |
87
|
|
|
public function getDependencies(): array |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
|
|
TagFixtures::class, |
91
|
|
|
UserFixtures::class, |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function getRandomUser(): User |
96
|
|
|
{ |
97
|
|
|
$admins = ['jane-admin', 'tom-admin']; |
98
|
|
|
$index = array_rand($admins); |
99
|
|
|
|
100
|
|
|
return $this->getReference($admins[$index]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function getRandomTags(int $numTags = 0): array |
104
|
|
|
{ |
105
|
|
|
$tags = []; |
106
|
|
|
|
107
|
|
|
if (0 === $numTags) { |
108
|
|
|
return $tags; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$indexes = (array) array_rand($this->getTagNames(), $numTags); |
112
|
|
|
foreach ($indexes as $index) { |
113
|
|
|
$tags[] = $this->getReference('tag-'.$index); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $tags; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|