Skip to content

Commit

Permalink
feature #478 Add more posts authors (voronkovich, javiereguiluz)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
javiereguiluz committed Jun 21, 2017
2 parents d05d2ee + c891805 commit 465dfd7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/AppBundle/DataFixtures/ORM/PostFixtures.php
Expand Up @@ -49,11 +49,13 @@ public function load(ObjectManager $manager)
$post->setSummary($this->getRandomPostSummary());
$post->setSlug($this->container->get('slugger')->slugify($post->getTitle()));
$post->setContent($this->getPostContent());
$post->setPublishedAt(new \DateTime('now - '.$i.'days'));

// Ensure that the first post is written by Jane Doe to simplify tests
// "References" are the way to share objects between fixtures defined
// in different files. This reference has been added in the UserFixtures
// file and it contains an instance of the User entity.
$post->setAuthor($this->getReference('jane-admin'));
$post->setPublishedAt(new \DateTime('now - '.$i.'days'));
$post->setAuthor(0 === $i ? $this->getReference('jane-admin') : $this->getRandomUser());

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

private function getRandomUser()
{
$admins = ['jane-admin', 'tom-admin'];
$index = array_rand($admins);

return $this->getReference($admins[$index]);
}

private function getRandomTags($numTags = 0)
{
$tags = [];
Expand Down
10 changes: 10 additions & 0 deletions src/AppBundle/DataFixtures/ORM/UserFixtures.php
Expand Up @@ -53,6 +53,16 @@ public function load(ObjectManager $manager)
// See https://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html#sharing-objects-between-fixtures
$this->addReference('jane-admin', $janeAdmin);

$tomAdmin = new User();
$tomAdmin->setFullName('Tom Doe');
$tomAdmin->setUsername('tom_admin');
$tomAdmin->setEmail('tom_admin@symfony.com');
$tomAdmin->setRoles(['ROLE_ADMIN']);
$encodedPassword = $passwordEncoder->encodePassword($tomAdmin, 'kitten');
$tomAdmin->setPassword($encodedPassword);
$manager->persist($tomAdmin);
$this->addReference('tom-admin', $tomAdmin);

$johnUser = new User();
$johnUser->setFullName('John Doe');
$johnUser->setUsername('john_user');
Expand Down
3 changes: 2 additions & 1 deletion src/AppBundle/Repository/PostRepository.php
Expand Up @@ -38,8 +38,9 @@ public function findLatest($page = 1)
{
$query = $this->getEntityManager()
->createQuery('
SELECT p, t
SELECT p, a, t
FROM AppBundle:Post p
JOIN p.author a
LEFT JOIN p.tags t
WHERE p.publishedAt <= :now
ORDER BY p.publishedAt DESC
Expand Down
6 changes: 3 additions & 3 deletions tests/AppBundle/Controller/Admin/BlogControllerTest.php
Expand Up @@ -67,9 +67,9 @@ public function testAdminBackendHomePage()
$crawler = $client->request('GET', '/en/admin/post/');
$this->assertSame(Response::HTTP_OK, $client->getResponse()->getStatusCode());

$this->assertCount(
30,
$crawler->filter('body#admin_post_index #main tbody tr'),
$this->assertGreaterThanOrEqual(
1,
$crawler->filter('body#admin_post_index #main tbody tr')->count(),
'The backend homepage displays all the available posts.'
);
}
Expand Down
Binary file modified var/data/blog.sqlite
Binary file not shown.

0 comments on commit 465dfd7

Please sign in to comment.