Skip to content

Commit c4691d9

Browse files
committed
Refactor tests
1 parent 18f0d5b commit c4691d9

File tree

2 files changed

+57
-200
lines changed

2 files changed

+57
-200
lines changed

tests/1CitiesTest.php

Lines changed: 29 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ public function tearDown()
1717
$this->http = null;
1818
}
1919

20-
public function testGetCities()
20+
protected function deleteItem($id)
2121
{
22-
$response = $this->http->request('GET', 'cities', [
22+
// Delete just created item
23+
$response = $this->http->request('DELETE', 'cities/delete/' . $id, [
2324
'headers' => [
2425
'Authorization' => 'Bearer ' . $this->credentials,
2526
]]);
@@ -28,10 +29,10 @@ public function testGetCities()
2829
$contentType = $response->getHeaders()["Content-Type"][0];
2930
$this->assertEquals("application/json; charset=UTF-8", $contentType);
3031
$json = json_decode($response->getBody(), true);
31-
$this->assertEquals('common.SUCCESSFUL_REQUEST', $json['messages']);
32+
$this->assertEquals('common.DELETED_SUCCESSFULLY', $json['messages']);
3233
}
3334

34-
public function testCreateCity()
35+
protected function createItem()
3536
{
3637
$response = $this->http->request('POST', 'cities/create', [
3738
'headers' => [
@@ -48,9 +49,12 @@ public function testCreateCity()
4849
$json = json_decode($response->getBody(), true);
4950
$this->assertEquals('common.CREATED_SUCCESSFULLY', $json['messages']);
5051
$id = $json['data']['id'];
52+
return $id;
53+
}
5154

52-
// Delete just created item
53-
$response = $this->http->request('DELETE', 'cities/delete/' . $id, [
55+
public function testGetCities()
56+
{
57+
$response = $this->http->request('GET', 'cities', [
5458
'headers' => [
5559
'Authorization' => 'Bearer ' . $this->credentials,
5660
]]);
@@ -59,7 +63,16 @@ public function testCreateCity()
5963
$contentType = $response->getHeaders()["Content-Type"][0];
6064
$this->assertEquals("application/json; charset=UTF-8", $contentType);
6165
$json = json_decode($response->getBody(), true);
62-
$this->assertEquals('common.DELETED_SUCCESSFULLY', $json['messages']);
66+
$this->assertEquals('common.SUCCESSFUL_REQUEST', $json['messages']);
67+
}
68+
69+
public function testCreateCity()
70+
{
71+
// Create new item
72+
$id = $this->createItem();
73+
74+
// Delete just created item
75+
$this->deleteItem($id);
6376
}
6477

6578
public function testCannotCreateDuplicatedCity()
@@ -96,22 +109,8 @@ public function testGetCityById()
96109

97110
public function testUpdateCity()
98111
{
99-
// Create city
100-
$response = $this->http->request('POST', 'cities/create', [
101-
'headers' => [
102-
'Authorization' => 'Bearer ' . $this->credentials,
103-
],
104-
'form_params' => [
105-
'name' => 'Girón',
106-
'country' => 'Colombia',
107-
]]);
108-
109-
$this->assertEquals(201, $response->getStatusCode());
110-
$contentType = $response->getHeaders()["Content-Type"][0];
111-
$this->assertEquals("application/json; charset=UTF-8", $contentType);
112-
$json = json_decode($response->getBody(), true);
113-
$this->assertEquals('common.CREATED_SUCCESSFULLY', $json['messages']);
114-
$id = $json['data']['id'];
112+
// Create new item
113+
$id = $this->createItem();
115114

116115
// updates city
117116
$response = $this->http->request('PATCH', 'cities/update/' . $id, [
@@ -130,36 +129,13 @@ public function testUpdateCity()
130129
$this->assertEquals('Girón2', $json['data']['name']);
131130

132131
// Delete just created item
133-
$response = $this->http->request('DELETE', 'cities/delete/' . $id, [
134-
'headers' => [
135-
'Authorization' => 'Bearer ' . $this->credentials,
136-
]]);
137-
138-
$this->assertEquals(200, $response->getStatusCode());
139-
$contentType = $response->getHeaders()["Content-Type"][0];
140-
$this->assertEquals("application/json; charset=UTF-8", $contentType);
141-
$json = json_decode($response->getBody(), true);
142-
$this->assertEquals('common.DELETED_SUCCESSFULLY', $json['messages']);
132+
$this->deleteItem($id);
143133
}
144134

145135
public function testCannotUpdateCityThatAlreadyExists()
146136
{
147-
// Create city
148-
$response = $this->http->request('POST', 'cities/create', [
149-
'headers' => [
150-
'Authorization' => 'Bearer ' . $this->credentials,
151-
],
152-
'form_params' => [
153-
'name' => 'Girón',
154-
'country' => 'Colombia',
155-
]]);
156-
157-
$this->assertEquals(201, $response->getStatusCode());
158-
$contentType = $response->getHeaders()["Content-Type"][0];
159-
$this->assertEquals("application/json; charset=UTF-8", $contentType);
160-
$json = json_decode($response->getBody(), true);
161-
$this->assertEquals('common.CREATED_SUCCESSFULLY', $json['messages']);
162-
$id = $json['data']['id'];
137+
// Create new item
138+
$id = $this->createItem();
163139

164140
// updates city
165141
$response = $this->http->request('PATCH', 'cities/update/' . $id, [
@@ -178,46 +154,15 @@ public function testCannotUpdateCityThatAlreadyExists()
178154
$this->assertEquals('common.THERE_IS_ALREADY_A_RECORD_WITH_THAT_NAME', $json['messages']);
179155

180156
// Delete just created item
181-
$response = $this->http->request('DELETE', 'cities/delete/' . $id, [
182-
'headers' => [
183-
'Authorization' => 'Bearer ' . $this->credentials,
184-
]]);
185-
186-
$this->assertEquals(200, $response->getStatusCode());
187-
$contentType = $response->getHeaders()["Content-Type"][0];
188-
$this->assertEquals("application/json; charset=UTF-8", $contentType);
189-
$json = json_decode($response->getBody(), true);
190-
$this->assertEquals('common.DELETED_SUCCESSFULLY', $json['messages']);
157+
$this->deleteItem($id);
191158
}
192159

193160
public function testDeleteCity()
194161
{
195-
$response = $this->http->request('POST', 'cities/create', [
196-
'headers' => [
197-
'Authorization' => 'Bearer ' . $this->credentials,
198-
],
199-
'form_params' => [
200-
'name' => 'Girón',
201-
'country' => 'Colombia',
202-
]]);
203-
204-
$this->assertEquals(201, $response->getStatusCode());
205-
$contentType = $response->getHeaders()["Content-Type"][0];
206-
$this->assertEquals("application/json; charset=UTF-8", $contentType);
207-
$json = json_decode($response->getBody(), true);
208-
$this->assertEquals('common.CREATED_SUCCESSFULLY', $json['messages']);
209-
$id = $json['data']['id'];
162+
// Create new item
163+
$id = $this->createItem();
210164

211165
// Delete just created item
212-
$response = $this->http->request('DELETE', 'cities/delete/' . $id, [
213-
'headers' => [
214-
'Authorization' => 'Bearer ' . $this->credentials,
215-
]]);
216-
217-
$this->assertEquals(200, $response->getStatusCode());
218-
$contentType = $response->getHeaders()["Content-Type"][0];
219-
$this->assertEquals("application/json; charset=UTF-8", $contentType);
220-
$json = json_decode($response->getBody(), true);
221-
$this->assertEquals('common.DELETED_SUCCESSFULLY', $json['messages']);
166+
$this->deleteItem($id);
222167
}
223168
}

tests/2UsersTest.php

Lines changed: 28 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public function tearDown()
1717
$this->http = null;
1818
}
1919

20-
public function testGetUsers()
20+
protected function deleteItem($id)
2121
{
22-
$response = $this->http->request('GET', 'users', [
22+
$response = $this->http->request('DELETE', 'users/delete/' . $id, [
2323
'headers' => [
2424
'Authorization' => 'Bearer ' . $this->credentials,
2525
]]);
@@ -28,10 +28,10 @@ public function testGetUsers()
2828
$contentType = $response->getHeaders()["Content-Type"][0];
2929
$this->assertEquals("application/json; charset=UTF-8", $contentType);
3030
$json = json_decode($response->getBody(), true);
31-
$this->assertEquals('common.SUCCESSFUL_REQUEST', $json['messages']);
31+
$this->assertEquals('common.DELETED_SUCCESSFULLY', $json['messages']);
3232
}
3333

34-
public function testCreateUser()
34+
protected function createItem()
3535
{
3636
$response = $this->http->request('POST', 'users/create', [
3737
'headers' => [
@@ -59,9 +59,12 @@ public function testCreateUser()
5959
$json = json_decode($response->getBody(), true);
6060
$this->assertEquals('common.CREATED_SUCCESSFULLY', $json['messages']);
6161
$id = $json['data']['id'];
62+
return $id;
63+
}
6264

63-
// Delete just created item
64-
$response = $this->http->request('DELETE', 'users/delete/' . $id, [
65+
public function testGetUsers()
66+
{
67+
$response = $this->http->request('GET', 'users', [
6568
'headers' => [
6669
'Authorization' => 'Bearer ' . $this->credentials,
6770
]]);
@@ -70,37 +73,22 @@ public function testCreateUser()
7073
$contentType = $response->getHeaders()["Content-Type"][0];
7174
$this->assertEquals("application/json; charset=UTF-8", $contentType);
7275
$json = json_decode($response->getBody(), true);
73-
$this->assertEquals('common.DELETED_SUCCESSFULLY', $json['messages']);
76+
$this->assertEquals('common.SUCCESSFUL_REQUEST', $json['messages']);
7477
}
7578

76-
public function testCannotCreateDuplicatedUser()
79+
public function testCreateUser()
7780
{
78-
$response = $this->http->request('POST', 'users/create', [
79-
'headers' => [
80-
'Authorization' => 'Bearer ' . $this->credentials,
81-
],
82-
'form_params' => [
83-
'email' => 'another@email.com',
84-
'new_password' => 'test123',
85-
'username' => 'admintest',
86-
'firstname' => 'My name',
87-
'lastname' => 'My last name',
88-
'level' => 'Superuser',
89-
'phone' => '12312312',
90-
'mobile' => '31312312',
91-
'address' => 'Calle 10',
92-
'city' => 'Bogotá',
93-
'country' => 'Colombia',
94-
'birthday' => '1979-01-01',
95-
'authorised' => '1',
96-
]]);
81+
// Create new item
82+
$id = $this->createItem();
9783

98-
$this->assertEquals(201, $response->getStatusCode());
99-
$contentType = $response->getHeaders()["Content-Type"][0];
100-
$this->assertEquals("application/json; charset=UTF-8", $contentType);
101-
$json = json_decode($response->getBody(), true);
102-
$this->assertEquals('common.CREATED_SUCCESSFULLY', $json['messages']);
103-
$id = $json['data']['id'];
84+
// Delete just created item
85+
$this->deleteItem($id);
86+
}
87+
88+
public function testCannotCreateDuplicatedUser()
89+
{
90+
// Create new item
91+
$id = $this->createItem();
10492

10593
// creates duplicated
10694
$response = $this->http->request('POST', 'users/create', [
@@ -130,16 +118,7 @@ public function testCannotCreateDuplicatedUser()
130118
$this->assertEquals('profile.ANOTHER_USER_ALREADY_REGISTERED_WITH_THIS_USERNAME', $json['messages']);
131119

132120
// Delete just created item
133-
$response = $this->http->request('DELETE', 'users/delete/' . $id, [
134-
'headers' => [
135-
'Authorization' => 'Bearer ' . $this->credentials,
136-
]]);
137-
138-
$this->assertEquals(200, $response->getStatusCode());
139-
$contentType = $response->getHeaders()["Content-Type"][0];
140-
$this->assertEquals("application/json; charset=UTF-8", $contentType);
141-
$json = json_decode($response->getBody(), true);
142-
$this->assertEquals('common.DELETED_SUCCESSFULLY', $json['messages']);
121+
$this->deleteItem($id);
143122
}
144123

145124
public function testGetUserById()
@@ -158,33 +137,8 @@ public function testGetUserById()
158137

159138
public function testUpdateUser()
160139
{
161-
// Create user
162-
$response = $this->http->request('POST', 'users/create', [
163-
'headers' => [
164-
'Authorization' => 'Bearer ' . $this->credentials,
165-
],
166-
'form_params' => [
167-
'email' => 'another@email.com',
168-
'new_password' => 'test123',
169-
'username' => 'admintest',
170-
'firstname' => 'My name',
171-
'lastname' => 'My last name',
172-
'level' => 'Superuser',
173-
'phone' => '12312312',
174-
'mobile' => '31312312',
175-
'address' => 'Calle 10',
176-
'city' => 'Bogotá',
177-
'country' => 'Colombia',
178-
'birthday' => '1979-01-01',
179-
'authorised' => '1',
180-
]]);
181-
182-
$this->assertEquals(201, $response->getStatusCode());
183-
$contentType = $response->getHeaders()["Content-Type"][0];
184-
$this->assertEquals("application/json; charset=UTF-8", $contentType);
185-
$json = json_decode($response->getBody(), true);
186-
$this->assertEquals('common.CREATED_SUCCESSFULLY', $json['messages']);
187-
$id = $json['data']['id'];
140+
// Create new item
141+
$id = $this->createItem();
188142

189143
// updates user
190144
$response = $this->http->request('PATCH', 'users/update/' . $id, [
@@ -214,57 +168,15 @@ public function testUpdateUser()
214168
$this->assertEquals('onemore@email.com', $json['data']['email']);
215169

216170
// Delete just created item
217-
$response = $this->http->request('DELETE', 'users/delete/' . $id, [
218-
'headers' => [
219-
'Authorization' => 'Bearer ' . $this->credentials,
220-
]]);
221-
222-
$this->assertEquals(200, $response->getStatusCode());
223-
$contentType = $response->getHeaders()["Content-Type"][0];
224-
$this->assertEquals("application/json; charset=UTF-8", $contentType);
225-
$json = json_decode($response->getBody(), true);
226-
$this->assertEquals('common.DELETED_SUCCESSFULLY', $json['messages']);
171+
$this->deleteItem($id);
227172
}
228173

229174
public function testDeleteUser()
230175
{
231-
$response = $this->http->request('POST', 'users/create', [
232-
'headers' => [
233-
'Authorization' => 'Bearer ' . $this->credentials,
234-
],
235-
'form_params' => [
236-
'email' => 'another@email.com',
237-
'new_password' => 'test123',
238-
'username' => 'admintest',
239-
'firstname' => 'My name',
240-
'lastname' => 'My last name',
241-
'level' => 'Superuser',
242-
'phone' => '12312312',
243-
'mobile' => '31312312',
244-
'address' => 'Calle 10',
245-
'city' => 'Bogotá',
246-
'country' => 'Colombia',
247-
'birthday' => '1979-01-01',
248-
'authorised' => '1',
249-
]]);
250-
251-
$this->assertEquals(201, $response->getStatusCode());
252-
$contentType = $response->getHeaders()["Content-Type"][0];
253-
$this->assertEquals("application/json; charset=UTF-8", $contentType);
254-
$json = json_decode($response->getBody(), true);
255-
$this->assertEquals('common.CREATED_SUCCESSFULLY', $json['messages']);
256-
$id = $json['data']['id'];
176+
// Create new item
177+
$id = $this->createItem();
257178

258179
// Delete just created item
259-
$response = $this->http->request('DELETE', 'users/delete/' . $id, [
260-
'headers' => [
261-
'Authorization' => 'Bearer ' . $this->credentials,
262-
]]);
263-
264-
$this->assertEquals(200, $response->getStatusCode());
265-
$contentType = $response->getHeaders()["Content-Type"][0];
266-
$this->assertEquals("application/json; charset=UTF-8", $contentType);
267-
$json = json_decode($response->getBody(), true);
268-
$this->assertEquals('common.DELETED_SUCCESSFULLY', $json['messages']);
180+
$this->deleteItem($id);
269181
}
270182
}

0 commit comments

Comments
 (0)