File tree Expand file tree Collapse file tree 5 files changed +27
-6
lines changed
tests/AppBundle/Controller/Admin Expand file tree Collapse file tree 5 files changed +27
-6
lines changed Original file line number Diff line number Diff 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 = [];
Original file line number Diff line number Diff 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 ('tom_admin@symfony.com ' );
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 ' );
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments