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)
49
49
$ post ->setSummary ($ this ->getRandomPostSummary ());
50
50
$ post ->setSlug ($ this ->container ->get ('slugger ' )->slugify ($ post ->getTitle ()));
51
51
$ 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
52
55
// "References" are the way to share objects between fixtures defined
53
56
// in different files. This reference has been added in the UserFixtures
54
57
// 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 ());
57
59
58
60
// for aesthetic reasons, the first blog post always has 2 tags
59
61
foreach ($ this ->getRandomTags ($ i > 0 ? mt_rand (0 , 3 ) : 2 ) as $ tag ) {
@@ -93,6 +95,14 @@ public function getDependencies()
93
95
];
94
96
}
95
97
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
+
96
106
private function getRandomTags ($ numTags = 0 )
97
107
{
98
108
$ tags = [];
Original file line number Diff line number Diff line change @@ -53,6 +53,16 @@ public function load(ObjectManager $manager)
53
53
// See https://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html#sharing-objects-between-fixtures
54
54
$ this ->addReference ('jane-admin ' , $ janeAdmin );
55
55
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
+
56
66
$ johnUser = new User ();
57
67
$ johnUser ->setFullName ('John Doe ' );
58
68
$ johnUser ->setUsername ('john_user ' );
Original file line number Diff line number Diff line change @@ -38,8 +38,9 @@ public function findLatest($page = 1)
38
38
{
39
39
$ query = $ this ->getEntityManager ()
40
40
->createQuery ('
41
- SELECT p, t
41
+ SELECT p, a, t
42
42
FROM AppBundle:Post p
43
+ JOIN p.author a
43
44
LEFT JOIN p.tags t
44
45
WHERE p.publishedAt <= :now
45
46
ORDER BY p.publishedAt DESC
Original file line number Diff line number Diff line change @@ -67,9 +67,9 @@ public function testAdminBackendHomePage()
67
67
$ crawler = $ client ->request ('GET ' , '/en/admin/post/ ' );
68
68
$ this ->assertSame (Response::HTTP_OK , $ client ->getResponse ()->getStatusCode ());
69
69
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 () ,
73
73
'The backend homepage displays all the available posts. '
74
74
);
75
75
}
You can’t perform that action at this time.
0 commit comments