Skip to content

Commit f16882d

Browse files
committed
Refactor tests
1 parent 8b8ac32 commit f16882d

File tree

5 files changed

+285
-253
lines changed

5 files changed

+285
-253
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.0.4 (Feb 4, 2019)
2+
3+
* Refactor tests
4+
15
## 1.0.3 (Feb 4, 2019)
26

37
* Changelog updated

tests/0IndexTest.php

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,46 @@ public function tearDown()
1616
$this->http = null;
1717
}
1818

19-
public function testLogin()
19+
protected function postLogin($credentials)
2020
{
21-
$credentials = base64_encode('admin:admin1234');
22-
2321
$response = $this->http->request('POST', 'authenticate', [
2422
'headers' => [
2523
'Authorization' => 'Basic ' . $credentials,
2624
]]);
25+
return $response;
26+
}
2727

28-
$this->assertEquals(200, $response->getStatusCode());
28+
protected function getJSON($response)
29+
{
2930
$contentType = $response->getHeaders()["Content-Type"][0];
3031
$this->assertEquals("application/json; charset=UTF-8", $contentType);
3132
$json = json_decode($response->getBody(), true);
33+
return $json;
34+
}
35+
36+
public function testLogin()
37+
{
38+
$response = $this->postLogin(base64_encode('admin:admin1234'));
39+
$this->assertEquals(200, $response->getStatusCode());
40+
$json = $this->getJSON($response);
3241
$this->assertEquals('common.SUCCESSFUL_REQUEST', $json['messages']);
3342
$this->assertEquals('1', $json['data']['user']['id']);
3443
$this->assertEquals('admin', $json['data']['user']['username']);
3544
}
3645

3746
public function testLoginUserNotRegistered()
3847
{
39-
$credentials = base64_encode('admin1234:admin1234');
40-
41-
$response = $this->http->request('POST', 'authenticate', [
42-
'headers' => [
43-
'Authorization' => 'Basic ' . $credentials,
44-
]]);
45-
48+
$response = $this->postLogin(base64_encode('admin1234:admin1234'));
4649
$this->assertEquals(404, $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);
50+
$json = $this->getJSON($response);
5051
$this->assertEquals('login.USER_IS_NOT_REGISTERED', $json['messages']);
5152
}
5253

5354
public function testLoginWrongPassword()
5455
{
55-
$credentials = base64_encode('admin:admin12345');
56-
57-
$response = $this->http->request('POST', 'authenticate', [
58-
'headers' => [
59-
'Authorization' => 'Basic ' . $credentials,
60-
]]);
61-
56+
$response = $this->postLogin(base64_encode('admin:admin12345'));
6257
$this->assertEquals(400, $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);
58+
$json = $this->getJSON($response);
6659
$this->assertEquals('login.WRONG_USER_PASSWORD', $json['messages']);
6760
}
6861
}

tests/1CitiesTest.php

Lines changed: 90 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -9,160 +9,164 @@ class CitiesTest extends TestCase
99
public function setUp()
1010
{
1111
$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();
1313
}
1414

1515
public function tearDown()
1616
{
1717
$this->http = null;
1818
}
1919

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()
2129
{
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', [
2433
'headers' => [
25-
'Authorization' => 'Bearer ' . $this->credentials,
34+
'Authorization' => 'Basic ' . $credentials,
2635
]]);
2736

2837
$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'];
3343
}
3444

35-
protected function createItem()
45+
protected function buildGetRequest($endpoint)
3646
{
37-
$response = $this->http->request('POST', 'cities/create', [
47+
$response = $this->http->request('GET', $endpoint, [
3848
'headers' => [
3949
'Authorization' => 'Bearer ' . $this->credentials,
40-
],
41-
'form_params' => [
42-
'name' => 'Girón',
43-
'country' => 'Colombia',
4450
]]);
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+
}
4562

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);
4698
$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);
50100
$this->assertEquals('common.CREATED_SUCCESSFULLY', $json['messages']);
51101
$id = $json['data']['id'];
52102
return $id;
53103
}
54104

55105
public function testGetCities()
56106
{
57-
$response = $this->http->request('GET', 'cities', [
58-
'headers' => [
59-
'Authorization' => 'Bearer ' . $this->credentials,
60-
]]);
61-
107+
$response = $this->buildGetRequest('cities');
62108
$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);
66110
$this->assertEquals('common.SUCCESSFUL_REQUEST', $json['messages']);
67111
}
68112

69113
public function testCreateCity()
70114
{
71-
// Create new item
72115
$id = $this->createItem();
73-
74-
// Delete just created item
75116
$this->deleteItem($id);
76117
}
77118

78119
public function testCannotCreateDuplicatedCity()
79120
{
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);
89126
$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);
93128
$this->assertEquals('common.THERE_IS_ALREADY_A_RECORD_WITH_THAT_NAME', $json['messages']);
94129
}
95130

96131
public function testGetCityById()
97132
{
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);
103134
$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);
107136
$this->assertEquals('Bogotá', $json['data']['name']);
108137
}
109138

110139
public function testUpdateCity()
111140
{
112-
// Create new item
113141
$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);
125147
$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);
129149
$this->assertEquals('Girón2', $json['data']['name']);
130-
131-
// Delete just created item
132150
$this->deleteItem($id);
133151
}
134152

135153
public function testCannotUpdateCityThatAlreadyExists()
136154
{
137-
// Create new item
138155
$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);
150161
$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);
154163
$this->assertEquals('common.THERE_IS_ALREADY_A_RECORD_WITH_THAT_NAME', $json['messages']);
155-
156-
// Delete just created item
157164
$this->deleteItem($id);
158165
}
159166

160167
public function testDeleteCity()
161168
{
162-
// Create new item
163169
$id = $this->createItem();
164-
165-
// Delete just created item
166170
$this->deleteItem($id);
167171
}
168172
}

0 commit comments

Comments
 (0)