Skip to content

Commit 72bddba

Browse files
committed
Add more posts authors
1 parent d90e0c1 commit 72bddba

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

src/AppBundle/DataFixtures/ORM/PostFixtures.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,15 @@ public function load(ObjectManager $manager)
4949
$post->setSummary($this->getRandomPostSummary());
5050
$post->setSlug($this->container->get('slugger')->slugify($post->getTitle()));
5151
$post->setContent($this->getPostContent());
52-
// "References" are the way to share objects between fixtures defined
53-
// in different files. This reference has been added in the UserFixtures
54-
// file and it contains an instance of the User entity.
55-
$post->setAuthor($this->getReference('jane-admin'));
52+
// This ensures that the first post is written by Jane Doe
53+
if (0 === $i) {
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($this->getReference('jane-admin'));
58+
} else {
59+
$post->setAuthor($this->getRandomUser());
60+
}
5661
$post->setPublishedAt(new \DateTime('now - '.$i.'days'));
5762

5863
// for aesthetic reasons, the first blog post always has 2 tags
@@ -93,6 +98,14 @@ public function getDependencies()
9398
];
9499
}
95100

101+
private function getRandomUser()
102+
{
103+
$admins = ['jane-admin', 'tom-admin', 'bob-admin'];
104+
$index = array_rand($admins);
105+
106+
return $this->getReference($admins[$index]);
107+
}
108+
96109
private function getRandomTags($numTags = 0)
97110
{
98111
$tags = [];

src/AppBundle/DataFixtures/ORM/UserFixtures.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ public function load(ObjectManager $manager)
5353
// See https://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html#sharing-objects-between-fixtures
5454
$this->addReference('jane-admin', $janeAdmin);
5555

56+
$tomAdmin = new User();
57+
$tomAdmin->setFullName('Tom Good');
58+
$tomAdmin->setUsername('tom_admin');
59+
$tomAdmin->setEmail('[email protected]');
60+
$tomAdmin->setRoles(['ROLE_ADMIN']);
61+
$encodedPassword = $passwordEncoder->encodePassword($tomAdmin, 'kitten');
62+
$tomAdmin->setPassword($encodedPassword);
63+
$manager->persist($tomAdmin);
64+
$this->addReference('tom-admin', $tomAdmin);
65+
66+
$bobAdmin = new User();
67+
$bobAdmin->setFullName('Bob Bad');
68+
$bobAdmin->setUsername('bob_admin');
69+
$bobAdmin->setEmail('[email protected]');
70+
$bobAdmin->setRoles(['ROLE_ADMIN']);
71+
$encodedPassword = $passwordEncoder->encodePassword($bobAdmin, 'kitten');
72+
$bobAdmin->setPassword($encodedPassword);
73+
$manager->persist($bobAdmin);
74+
$this->addReference('bob-admin', $bobAdmin);
75+
5676
$johnUser = new User();
5777
$johnUser->setFullName('John Doe');
5878
$johnUser->setUsername('john_user');

tests/AppBundle/Controller/Admin/BlogControllerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ public function testAdminBackendHomePage()
6767
$crawler = $client->request('GET', '/en/admin/post/');
6868
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());
6969

70-
$this->assertCount(
71-
30,
72-
$crawler->filter('body#admin_post_index #main tbody tr'),
70+
$this->assertGreaterThanOrEqual(
71+
1,
72+
$crawler->filter('body#admin_post_index #main tbody tr')->count(),
7373
'The backend homepage displays all the available posts.'
7474
);
7575
}

var/data/blog.sqlite

4 KB
Binary file not shown.

0 commit comments

Comments
 (0)