@@ -9,160 +9,164 @@ class CitiesTest extends TestCase
9
9
public function setUp ()
10
10
{
11
11
$ this ->http = new GuzzleHttp \Client (['base_uri ' => 'http://api.myproject.local/ ' , 'http_errors ' => false ]);
12
- $ this ->credentials = ' m2ZWs+fUkNCSl/EwRaupfrRpkJDf5L4cc5RJILHjMzgifMIXplDz70yQXiCTbbvzu2BzBTPADBF83YxZQfAEPlCKfTSIPIHOY3Z6M4/1HsOKeIfBomxTBDlP6xyHNUH1VRKtIIm8ClRC8QJZvQO5I5ATHU488/W6Mx+JyE94Wceot2C57JDELvt8lY9Buu2ORZU0CLH6Ih7znOEUE3V7GodYdYNrCCm/hEwTw1V9XpHFUBNKqLjrG/Rh0KYHvIRBDfNXlKzyKJFnFnSfzvJBjk64N0smUfxrLILqaMAFVGlcs5HsItIzq0il5o4Wc1Ng7R3HbVHK8BpUyPeqguBB8Q0OnhGmlQmD4sjIpsHwJk2Db7rCQY+4xl7KxB05SMC/Qxl++I9ldXj3Hc4suAJvgowO673zwIcrrcnNb9QcJ9er0UVCV8W6mAW0wDzysWfMbbHFJ/LAdooF7UwayH3hkSh6BIzY1tMDh7FOMJ7tkH8zWFJPWThnLMgUK+f98NGjU9Ld1YwV11x2RYF5lCrx ' ;
12
+ $ this ->credentials = $ this -> getToken () ;
13
13
}
14
14
15
15
public function tearDown ()
16
16
{
17
17
$ this ->http = null ;
18
18
}
19
19
20
- protected function deleteItem ($ id )
20
+ protected function getJSON ($ response )
21
+ {
22
+ $ contentType = $ response ->getHeaders ()["Content-Type " ][0 ];
23
+ $ this ->assertEquals ("application/json; charset=UTF-8 " , $ contentType );
24
+ $ json = json_decode ($ response ->getBody (), true );
25
+ return $ json ;
26
+ }
27
+
28
+ protected function getToken ()
21
29
{
22
- // Delete just created item
23
- $ response = $ this ->http ->request ('DELETE ' , 'cities/delete/ ' . $ id , [
30
+ $ credentials = base64_encode ('admin:admin1234 ' );
31
+
32
+ $ response = $ this ->http ->request ('POST ' , 'authenticate ' , [
24
33
'headers ' => [
25
- 'Authorization ' => 'Bearer ' . $ this -> credentials ,
34
+ 'Authorization ' => 'Basic ' . $ credentials ,
26
35
]]);
27
36
28
37
$ this ->assertEquals (200 , $ response ->getStatusCode ());
29
- $ contentType = $ response ->getHeaders ()["Content-Type " ][0 ];
30
- $ this ->assertEquals ("application/json; charset=UTF-8 " , $ contentType );
31
- $ json = json_decode ($ response ->getBody (), true );
32
- $ this ->assertEquals ('common.DELETED_SUCCESSFULLY ' , $ json ['messages ' ]);
38
+ $ json = $ this ->getJSON ($ response );
39
+ $ this ->assertEquals ('common.SUCCESSFUL_REQUEST ' , $ json ['messages ' ]);
40
+ $ this ->assertEquals ('1 ' , $ json ['data ' ]['user ' ]['id ' ]);
41
+ $ this ->assertEquals ('admin ' , $ json ['data ' ]['user ' ]['username ' ]);
42
+ return $ json ['data ' ]['token ' ];
33
43
}
34
44
35
- protected function createItem ( )
45
+ protected function buildGetRequest ( $ endpoint )
36
46
{
37
- $ response = $ this ->http ->request ('POST ' , ' cities/create ' , [
47
+ $ response = $ this ->http ->request ('GET ' , $ endpoint , [
38
48
'headers ' => [
39
49
'Authorization ' => 'Bearer ' . $ this ->credentials ,
40
- ],
41
- 'form_params ' => [
42
- 'name ' => 'Girón ' ,
43
- 'country ' => 'Colombia ' ,
44
50
]]);
51
+ return $ response ;
52
+ }
53
+
54
+ protected function buildGetOrDeleteRequestById ($ method , $ endpoint , $ id )
55
+ {
56
+ $ response = $ this ->http ->request ($ method , $ endpoint . $ id , [
57
+ 'headers ' => [
58
+ 'Authorization ' => 'Bearer ' . $ this ->credentials ,
59
+ ]]);
60
+ return $ response ;
61
+ }
45
62
63
+ protected function buildPostRequest ($ endpoint , $ params )
64
+ {
65
+ $ response = $ this ->http ->request ('POST ' , $ endpoint , [
66
+ 'headers ' => [
67
+ 'Authorization ' => 'Bearer ' . $ this ->credentials ,
68
+ ],
69
+ 'form_params ' => $ params ]);
70
+ return $ response ;
71
+ }
72
+
73
+ protected function buildPatchRequest ($ endpoint , $ params , $ id )
74
+ {
75
+ $ response = $ this ->http ->request ('PATCH ' , $ endpoint . $ id , [
76
+ 'headers ' => [
77
+ 'Authorization ' => 'Bearer ' . $ this ->credentials ,
78
+ ],
79
+ 'form_params ' => $ params ]);
80
+ return $ response ;
81
+ }
82
+
83
+ protected function deleteItem ($ id )
84
+ {
85
+ $ response = $ this ->buildGetOrDeleteRequestById ('DELETE ' , 'cities/delete/ ' , $ id );
86
+ $ this ->assertEquals (200 , $ response ->getStatusCode ());
87
+ $ json = $ this ->getJSON ($ response );
88
+ $ this ->assertEquals ('common.DELETED_SUCCESSFULLY ' , $ json ['messages ' ]);
89
+ }
90
+
91
+ protected function createItem ()
92
+ {
93
+ $ params = [
94
+ 'name ' => 'Girón ' ,
95
+ 'country ' => 'Colombia ' ,
96
+ ];
97
+ $ response = $ this ->buildPostRequest ('cities/create ' , $ params );
46
98
$ this ->assertEquals (201 , $ response ->getStatusCode ());
47
- $ contentType = $ response ->getHeaders ()["Content-Type " ][0 ];
48
- $ this ->assertEquals ("application/json; charset=UTF-8 " , $ contentType );
49
- $ json = json_decode ($ response ->getBody (), true );
99
+ $ json = $ this ->getJSON ($ response );
50
100
$ this ->assertEquals ('common.CREATED_SUCCESSFULLY ' , $ json ['messages ' ]);
51
101
$ id = $ json ['data ' ]['id ' ];
52
102
return $ id ;
53
103
}
54
104
55
105
public function testGetCities ()
56
106
{
57
- $ response = $ this ->http ->request ('GET ' , 'cities ' , [
58
- 'headers ' => [
59
- 'Authorization ' => 'Bearer ' . $ this ->credentials ,
60
- ]]);
61
-
107
+ $ response = $ this ->buildGetRequest ('cities ' );
62
108
$ this ->assertEquals (200 , $ response ->getStatusCode ());
63
- $ contentType = $ response ->getHeaders ()["Content-Type " ][0 ];
64
- $ this ->assertEquals ("application/json; charset=UTF-8 " , $ contentType );
65
- $ json = json_decode ($ response ->getBody (), true );
109
+ $ json = $ this ->getJSON ($ response );
66
110
$ this ->assertEquals ('common.SUCCESSFUL_REQUEST ' , $ json ['messages ' ]);
67
111
}
68
112
69
113
public function testCreateCity ()
70
114
{
71
- // Create new item
72
115
$ id = $ this ->createItem ();
73
-
74
- // Delete just created item
75
116
$ this ->deleteItem ($ id );
76
117
}
77
118
78
119
public function testCannotCreateDuplicatedCity ()
79
120
{
80
- $ response = $ this ->http ->request ('POST ' , 'cities/create ' , [
81
- 'headers ' => [
82
- 'Authorization ' => 'Bearer ' . $ this ->credentials ,
83
- ],
84
- 'form_params ' => [
85
- 'name ' => 'Bogotá ' ,
86
- 'country ' => 'Colombia ' ,
87
- ]]);
88
-
121
+ $ params = [
122
+ 'name ' => 'Bogotá ' ,
123
+ 'country ' => 'Colombia ' ,
124
+ ];
125
+ $ response = $ this ->buildPostRequest ('cities/create ' , $ params );
89
126
$ this ->assertEquals (409 , $ response ->getStatusCode ());
90
- $ contentType = $ response ->getHeaders ()["Content-Type " ][0 ];
91
- $ this ->assertEquals ("application/json; charset=UTF-8 " , $ contentType );
92
- $ json = json_decode ($ response ->getBody (), true );
127
+ $ json = $ this ->getJSON ($ response );
93
128
$ this ->assertEquals ('common.THERE_IS_ALREADY_A_RECORD_WITH_THAT_NAME ' , $ json ['messages ' ]);
94
129
}
95
130
96
131
public function testGetCityById ()
97
132
{
98
- $ response = $ this ->http ->request ('GET ' , 'cities/get/1 ' , [
99
- 'headers ' => [
100
- 'Authorization ' => 'Bearer ' . $ this ->credentials ,
101
- ]]);
102
-
133
+ $ response = $ this ->buildGetOrDeleteRequestById ('GET ' , 'cities/get/ ' , 1 );
103
134
$ this ->assertEquals (200 , $ response ->getStatusCode ());
104
- $ contentType = $ response ->getHeaders ()["Content-Type " ][0 ];
105
- $ this ->assertEquals ("application/json; charset=UTF-8 " , $ contentType );
106
- $ json = json_decode ($ response ->getBody (), true );
135
+ $ json = $ this ->getJSON ($ response );
107
136
$ this ->assertEquals ('Bogotá ' , $ json ['data ' ]['name ' ]);
108
137
}
109
138
110
139
public function testUpdateCity ()
111
140
{
112
- // Create new item
113
141
$ id = $ this ->createItem ();
114
-
115
- // updates city
116
- $ response = $ this ->http ->request ('PATCH ' , 'cities/update/ ' . $ id , [
117
- 'headers ' => [
118
- 'Authorization ' => 'Bearer ' . $ this ->credentials ,
119
- ],
120
- 'form_params ' => [
121
- 'name ' => 'Girón2 ' ,
122
- 'country ' => 'Colombia2 ' ,
123
- ]]);
124
-
142
+ $ params = [
143
+ 'name ' => 'Girón2 ' ,
144
+ 'country ' => 'Colombia2 ' ,
145
+ ];
146
+ $ response = $ this ->buildPatchRequest ('cities/update/ ' , $ params , $ id );
125
147
$ this ->assertEquals (200 , $ response ->getStatusCode ());
126
- $ contentType = $ response ->getHeaders ()["Content-Type " ][0 ];
127
- $ this ->assertEquals ("application/json; charset=UTF-8 " , $ contentType );
128
- $ json = json_decode ($ response ->getBody (), true );
148
+ $ json = $ this ->getJSON ($ response );
129
149
$ this ->assertEquals ('Girón2 ' , $ json ['data ' ]['name ' ]);
130
-
131
- // Delete just created item
132
150
$ this ->deleteItem ($ id );
133
151
}
134
152
135
153
public function testCannotUpdateCityThatAlreadyExists ()
136
154
{
137
- // Create new item
138
155
$ id = $ this ->createItem ();
139
-
140
- // updates city
141
- $ response = $ this ->http ->request ('PATCH ' , 'cities/update/ ' . $ id , [
142
- 'headers ' => [
143
- 'Authorization ' => 'Bearer ' . $ this ->credentials ,
144
- ],
145
- 'form_params ' => [
146
- 'name ' => 'Bogotá ' ,
147
- 'country ' => 'Colombia ' ,
148
- ]]);
149
-
156
+ $ params = [
157
+ 'name ' => 'Bogotá ' ,
158
+ 'country ' => 'Colombia ' ,
159
+ ];
160
+ $ response = $ this ->buildPatchRequest ('cities/update/ ' , $ params , $ id );
150
161
$ this ->assertEquals (409 , $ response ->getStatusCode ());
151
- $ contentType = $ response ->getHeaders ()["Content-Type " ][0 ];
152
- $ this ->assertEquals ("application/json; charset=UTF-8 " , $ contentType );
153
- $ json = json_decode ($ response ->getBody (), true );
162
+ $ json = $ this ->getJSON ($ response );
154
163
$ this ->assertEquals ('common.THERE_IS_ALREADY_A_RECORD_WITH_THAT_NAME ' , $ json ['messages ' ]);
155
-
156
- // Delete just created item
157
164
$ this ->deleteItem ($ id );
158
165
}
159
166
160
167
public function testDeleteCity ()
161
168
{
162
- // Create new item
163
169
$ id = $ this ->createItem ();
164
-
165
- // Delete just created item
166
170
$ this ->deleteItem ($ id );
167
171
}
168
172
}
0 commit comments