1
+ <?php
2
+
3
+ namespace App \Tests ;
4
+
5
+ use Symfony \Bundle \FrameworkBundle \Test \WebTestCase ;
6
+ use App \Tests \Utils \LoggedUserTrait ;
7
+ use App \Entity \Todo ;
8
+ use App \Application \Sonata \UserBundle \Entity \User ;
9
+
10
+
11
+ class PostControllerTest extends WebTestCase
12
+ {
13
+ use LoggedUserTrait;
14
+
15
+ private $ todo ;
16
+ private $ user ;
17
+
18
+ protected function setUp ()
19
+ {
20
+ self ::bootKernel ();
21
+
22
+ $ this ->em = static ::$ kernel ->getContainer ()
23
+ ->get ('doctrine ' )
24
+ ->getManager ();
25
+ $ user = new User ();
26
+ $ user ->setUserName ('TestUserName ' );
27
+ $ user ->setFirstName ('TestAdmin ' );
28
+ $ user ->setLastname ('TestSurname ' );
29
+ $ user ->setUsername ('testadmin ' );
30
+ $ user ->setEmail ('email@tests.com ' );
31
+ $ user ->setPlainPassword ('testadmin ' );
32
+ $ user ->setEnabled (true );
33
+ $ user ->setRoles (array ('ROLE_SONATA_ADMIN ' , 'ROLE_SUPER_ADMIN ' ));
34
+
35
+ $ this ->em ->persist ($ user );
36
+ $ this ->user = $ user ;
37
+
38
+ $ todo = new Todo ();
39
+ $ todo ->setTitle ('Test title ' );
40
+ $ todo ->setOwner ($ user );
41
+ $ todo ->setCompleted (false );
42
+
43
+ $ this ->em ->persist ($ todo );
44
+ $ this ->em ->flush ();
45
+ $ this ->todo = $ todo ;
46
+
47
+ }
48
+
49
+ protected function tearDown ()
50
+ {
51
+ //Cleaning things :)
52
+ $ this ->em ->remove ($ this ->em ->getRepository ('ApplicationSonataUserBundle:User ' )
53
+ ->find ($ this ->user ->getId ()));
54
+ $ this ->em ->remove ($ this ->em ->getRepository ('App:Todo ' )
55
+ ->find ($ this ->todo ->getId ()));
56
+ $ this ->em ->flush ();
57
+ $ this ->em = null ;
58
+ }
59
+
60
+ /*
61
+ * Verify that a not authenticated user cannot see the API
62
+ */
63
+ public function testRedirectNotAuthenticated ()
64
+ {
65
+ $ client = static ::createClient ();
66
+
67
+ $ client ->request ('GET ' , '/api/doc ' );
68
+
69
+ $ this ->assertEquals (302 , $ client ->getResponse ()->getStatusCode ());
70
+ }
71
+
72
+ /*
73
+ * Verify that home is reachable
74
+ */
75
+ public function testHome ()
76
+ {
77
+ $ client = static ::createClient ();
78
+
79
+ $ client ->request ('GET ' , '/ ' );
80
+ $ this ->assertEquals (302 , $ client ->getResponse ()->getStatusCode ());
81
+ }
82
+
83
+ /*
84
+ * Verify that the login page contains the login action
85
+ */
86
+ public function testLogin ()
87
+ {
88
+ $ client = static ::createClient ();
89
+
90
+ $ crawler = $ client ->request ('GET ' , '/admin/login ' );
91
+ $ this ->assertGreaterThan (
92
+ 0 ,
93
+ $ crawler ->filter ('html:contains("Login") ' )->count ()
94
+ );
95
+ }
96
+ /*
97
+ * Verify that, once logged, admin can see his dashboard
98
+ */
99
+ public function testAdminAuthenticatedIndexAction ()
100
+ {
101
+ $ client = $ this ->getLoggedClient ('admin ' , 'ROLE_SUPER_ADMIN ' , 'user ' );
102
+ $ crawler = $ client ->request ('GET ' , '/admin/dashboard ' );
103
+ $ this ->assertEquals (200 , $ client ->getResponse ()->getStatusCode ());
104
+
105
+ }
106
+ /*
107
+ * Verify that a todo has an ID (it's saved in database)
108
+ */
109
+ public function testTodoHasId ()
110
+ {
111
+ $ this ->assertGreaterThan (0 ,$ this ->todo ->getId ());
112
+ }
113
+
114
+ }
0 commit comments