Skip to content

Commit 465dfd7

Browse files
committed
feature #478 Add more posts authors (voronkovich, javiereguiluz)
This PR was merged into the master branch. Discussion ---------- Add more posts authors For now we have only one admin: Jane Doe. Latest posts with more than one author look more naturally. Commits ------- c891805 Update fixtures eafb89e Updated the user fixtures c366c05 Join posts authors when quering latests posts 72bddba Add more posts authors
2 parents d05d2ee + c891805 commit 465dfd7

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

src/AppBundle/DataFixtures/ORM/PostFixtures.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ 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+
$post->setPublishedAt(new \DateTime('now - '.$i.'days'));
53+
54+
// Ensure that the first post is written by Jane Doe to simplify tests
5255
// "References" are the way to share objects between fixtures defined
5356
// in different files. This reference has been added in the UserFixtures
5457
// file and it contains an instance of the User entity.
55-
$post->setAuthor($this->getReference('jane-admin'));
56-
$post->setPublishedAt(new \DateTime('now - '.$i.'days'));
58+
$post->setAuthor(0 === $i ? $this->getReference('jane-admin') : $this->getRandomUser());
5759

5860
// for aesthetic reasons, the first blog post always has 2 tags
5961
foreach ($this->getRandomTags($i > 0 ? mt_rand(0, 3) : 2) as $tag) {
@@ -93,6 +95,14 @@ public function getDependencies()
9395
];
9496
}
9597

98+
private function getRandomUser()
99+
{
100+
$admins = ['jane-admin', 'tom-admin'];
101+
$index = array_rand($admins);
102+
103+
return $this->getReference($admins[$index]);
104+
}
105+
96106
private function getRandomTags($numTags = 0)
97107
{
98108
$tags = [];

src/AppBundle/DataFixtures/ORM/UserFixtures.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ 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 Doe');
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+
5666
$johnUser = new User();
5767
$johnUser->setFullName('John Doe');
5868
$johnUser->setUsername('john_user');

src/AppBundle/Repository/PostRepository.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ public function findLatest($page = 1)
3838
{
3939
$query = $this->getEntityManager()
4040
->createQuery('
41-
SELECT p, t
41+
SELECT p, a, t
4242
FROM AppBundle:Post p
43+
JOIN p.author a
4344
LEFT JOIN p.tags t
4445
WHERE p.publishedAt <= :now
4546
ORDER BY p.publishedAt DESC

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)