11
11
12
12
namespace App \Tests \Controller \Admin ;
13
13
14
- use App \Entity \ Post ;
14
+ use App \Repository \ PostRepository ;
15
15
use Symfony \Bundle \FrameworkBundle \Test \WebTestCase ;
16
16
use Symfony \Component \HttpFoundation \Response ;
17
17
@@ -35,7 +35,7 @@ class BlogControllerTest extends WebTestCase
35
35
/**
36
36
* @dataProvider getUrlsForRegularUsers
37
37
*/
38
- public function testAccessDeniedForRegularUsers (string $ httpMethod , string $ url )
38
+ public function testAccessDeniedForRegularUsers (string $ httpMethod , string $ url ): void
39
39
{
40
40
$ client = static ::createClient ([], [
41
41
'PHP_AUTH_USER ' => 'john_user ' ,
@@ -47,22 +47,21 @@ public function testAccessDeniedForRegularUsers(string $httpMethod, string $url)
47
47
$ this ->assertResponseStatusCodeSame (Response::HTTP_FORBIDDEN );
48
48
}
49
49
50
- public function getUrlsForRegularUsers ()
50
+ public function getUrlsForRegularUsers (): ? \ Generator
51
51
{
52
52
yield ['GET ' , '/en/admin/post/ ' ];
53
53
yield ['GET ' , '/en/admin/post/1 ' ];
54
54
yield ['GET ' , '/en/admin/post/1/edit ' ];
55
55
yield ['POST ' , '/en/admin/post/1/delete ' ];
56
56
}
57
57
58
- public function testAdminBackendHomePage ()
58
+ public function testAdminBackendHomePage (): void
59
59
{
60
60
$ client = static ::createClient ([], [
61
61
'PHP_AUTH_USER ' => 'jane_admin ' ,
62
62
'PHP_AUTH_PW ' => 'kitten ' ,
63
63
]);
64
-
65
- $ crawler = $ client ->request ('GET ' , '/en/admin/post/ ' );
64
+ $ client ->request ('GET ' , '/en/admin/post/ ' );
66
65
67
66
$ this ->assertResponseIsSuccessful ();
68
67
$ this ->assertSelectorExists (
@@ -77,7 +76,7 @@ public function testAdminBackendHomePage()
77
76
* to the database are rolled back when this test completes. This means that
78
77
* all the application tests begin with the same database contents.
79
78
*/
80
- public function testAdminNewPost ()
79
+ public function testAdminNewPost (): void
81
80
{
82
81
$ postTitle = 'Blog Post Title ' .mt_rand ();
83
82
$ postSummary = $ this ->generateRandomString (255 );
@@ -87,25 +86,23 @@ public function testAdminNewPost()
87
86
'PHP_AUTH_USER ' => 'jane_admin ' ,
88
87
'PHP_AUTH_PW ' => 'kitten ' ,
89
88
]);
90
- $ crawler = $ client ->request ('GET ' , '/en/admin/post/new ' );
91
- $ form = $ crawler -> selectButton ('Create post ' )-> form ( [
89
+ $ client ->request ('GET ' , '/en/admin/post/new ' );
90
+ $ client -> submitForm ('Create post ' , [
92
91
'post[title] ' => $ postTitle ,
93
92
'post[summary] ' => $ postSummary ,
94
93
'post[content] ' => $ postContent ,
95
94
]);
96
- $ client ->submit ($ form );
97
95
98
96
$ this ->assertResponseRedirects ('/en/admin/post/ ' , Response::HTTP_FOUND );
99
97
100
- $ post = $ client ->getContainer ()->get ('doctrine ' )->getRepository (Post::class)->findOneBy ([
101
- 'title ' => $ postTitle ,
102
- ]);
98
+ /** @var \App\Entity\Post $post */
99
+ $ post = self ::$ container ->get (PostRepository::class)->findOneByTitle ($ postTitle );
103
100
$ this ->assertNotNull ($ post );
104
101
$ this ->assertSame ($ postSummary , $ post ->getSummary ());
105
102
$ this ->assertSame ($ postContent , $ post ->getContent ());
106
103
}
107
104
108
- public function testAdminNewDuplicatedPost ()
105
+ public function testAdminNewDuplicatedPost (): void
109
106
{
110
107
$ postTitle = 'Blog Post Title ' .mt_rand ();
111
108
$ postSummary = $ this ->generateRandomString (255 );
@@ -130,7 +127,7 @@ public function testAdminNewDuplicatedPost()
130
127
$ this ->assertSelectorTextContains ('form .form-group.has-error .help-block ' , 'This title was already used in another blog post, but they must be unique. ' );
131
128
}
132
129
133
- public function testAdminShowPost ()
130
+ public function testAdminShowPost (): void
134
131
{
135
132
$ client = static ::createClient ([], [
136
133
'PHP_AUTH_USER ' => 'jane_admin ' ,
@@ -147,24 +144,23 @@ public function testAdminShowPost()
147
144
* to the database are rolled back when this test completes. This means that
148
145
* all the application tests begin with the same database contents.
149
146
*/
150
- public function testAdminEditPost ()
147
+ public function testAdminEditPost (): void
151
148
{
152
149
$ newBlogPostTitle = 'Blog Post Title ' .mt_rand ();
153
150
154
151
$ client = static ::createClient ([], [
155
152
'PHP_AUTH_USER ' => 'jane_admin ' ,
156
153
'PHP_AUTH_PW ' => 'kitten ' ,
157
154
]);
158
- $ crawler = $ client ->request ('GET ' , '/en/admin/post/1/edit ' );
159
- $ form = $ crawler -> selectButton ('Save changes ' )-> form ( [
155
+ $ client ->request ('GET ' , '/en/admin/post/1/edit ' );
156
+ $ client -> submitForm ('Save changes ' , [
160
157
'post[title] ' => $ newBlogPostTitle ,
161
158
]);
162
- $ client ->submit ($ form );
163
159
164
160
$ this ->assertResponseRedirects ('/en/admin/post/1/edit ' , Response::HTTP_FOUND );
165
161
166
- /** @var Post $post */
167
- $ post = $ client -> getContainer ()-> get (' doctrine ' )-> getRepository (Post ::class)->find (1 );
162
+ /** @var \App\Entity\ Post $post */
163
+ $ post = self :: $ container -> get (PostRepository ::class)->find (1 );
168
164
$ this ->assertSame ($ newBlogPostTitle , $ post ->getTitle ());
169
165
}
170
166
@@ -174,7 +170,7 @@ public function testAdminEditPost()
174
170
* to the database are rolled back when this test completes. This means that
175
171
* all the application tests begin with the same database contents.
176
172
*/
177
- public function testAdminDeletePost ()
173
+ public function testAdminDeletePost (): void
178
174
{
179
175
$ client = static ::createClient ([], [
180
176
'PHP_AUTH_USER ' => 'jane_admin ' ,
@@ -185,7 +181,7 @@ public function testAdminDeletePost()
185
181
186
182
$ this ->assertResponseRedirects ('/en/admin/post/ ' , Response::HTTP_FOUND );
187
183
188
- $ post = $ client -> getContainer ()-> get (' doctrine ' )-> getRepository (Post ::class)->find (1 );
184
+ $ post = self :: $ container -> get (PostRepository ::class)->find (1 );
189
185
$ this ->assertNull ($ post );
190
186
}
191
187
0 commit comments