27
27
* Execute the application tests using this command (requires PHPUnit to be installed):
28
28
*
29
29
* $ cd your-symfony-project/
30
- * $ phpunit -c app
30
+ * $ ./vendor/bin/ phpunit
31
31
*/
32
32
class BlogControllerTest extends WebTestCase
33
33
{
34
- public function testRegularUsersCannotAccessToTheBackend ()
34
+ /**
35
+ * @dataProvider getUrlsForRegularUsers
36
+ */
37
+ public function testRegularUsers ($ httpMethod , $ url , $ statusCode )
35
38
{
36
39
$ client = static ::createClient ([], [
37
40
'PHP_AUTH_USER ' => 'john_user ' ,
38
41
'PHP_AUTH_PW ' => 'kitten ' ,
39
42
]);
40
43
41
- $ client ->request ('GET ' , '/en/admin/post/ ' );
44
+ $ client ->request ($ httpMethod , $ url );
45
+ $ this ->assertEquals ($ statusCode , $ client ->getResponse ()->getStatusCode ());
46
+ }
42
47
43
- $ this ->assertEquals (Response::HTTP_FORBIDDEN , $ client ->getResponse ()->getStatusCode ());
48
+ public function getUrlsForRegularUsers ()
49
+ {
50
+ yield ['GET ' , '/en/admin/post/ ' , Response::HTTP_FORBIDDEN ];
51
+ yield ['GET ' , '/en/admin/post/1 ' , Response::HTTP_FORBIDDEN ];
52
+ yield ['GET ' , '/en/admin/post/1/edit ' , Response::HTTP_FORBIDDEN ];
53
+ yield ['POST ' , '/en/admin/post/1/delete ' , Response::HTTP_FORBIDDEN ];
44
54
}
45
55
46
- public function testAdministratorUsersCanAccessToTheBackend ()
56
+ /**
57
+ * @dataProvider getUrlsForAdminUsers
58
+ */
59
+ public function testAdminUsers ($ httpMethod , $ url , $ statusCode )
47
60
{
48
61
$ client = static ::createClient ([], [
49
62
'PHP_AUTH_USER ' => 'anna_admin ' ,
50
63
'PHP_AUTH_PW ' => 'kitten ' ,
51
64
]);
52
65
53
- $ client ->request ('GET ' , '/en/admin/post/ ' );
66
+ $ client ->request ($ httpMethod , $ url );
67
+ $ this ->assertEquals ($ statusCode , $ client ->getResponse ()->getStatusCode ());
68
+ }
54
69
55
- $ this ->assertEquals (Response::HTTP_OK , $ client ->getResponse ()->getStatusCode ());
70
+ public function getUrlsForAdminUsers ()
71
+ {
72
+ yield ['GET ' , '/en/admin/post/ ' , Response::HTTP_OK ];
73
+ yield ['GET ' , '/en/admin/post/1 ' , Response::HTTP_OK ];
74
+ yield ['GET ' , '/en/admin/post/1/edit ' , Response::HTTP_OK ];
75
+ yield ['POST ' , '/en/admin/post/1/delete ' , Response::HTTP_FOUND ];
56
76
}
57
77
58
- public function testIndex ()
78
+ public function testBackendHomepage ()
59
79
{
60
80
$ client = static ::createClient ([], [
61
81
'PHP_AUTH_USER ' => 'anna_admin ' ,
@@ -70,26 +90,4 @@ public function testIndex()
70
90
'The backend homepage displays all the available posts. '
71
91
);
72
92
}
73
-
74
- /**
75
- * @dataProvider getPostUrls
76
- */
77
- public function testOnlyAuthorCanAccessToThePostInTheBackend ($ method , $ url , $ statusCode )
78
- {
79
- $ client = static ::createClient ([], [
80
- 'PHP_AUTH_USER ' => 'anna_admin ' ,
81
- 'PHP_AUTH_PW ' => 'kitten ' ,
82
- ]);
83
-
84
- $ client ->request ($ method , $ url );
85
-
86
- $ this ->assertEquals ($ statusCode , $ client ->getResponse ()->getStatusCode ());
87
- }
88
-
89
- public function getPostUrls ()
90
- {
91
- yield ['GET ' , '/en/admin/post/1 ' , Response::HTTP_OK ];
92
- yield ['GET ' , '/en/admin/post/1/edit ' , Response::HTTP_OK ];
93
- yield ['POST ' , '/en/admin/post/1/delete ' , Response::HTTP_FOUND ];
94
- }
95
93
}
0 commit comments