1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
4
|
|
|
* |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CakeDC\Api\TestSuite; |
13
|
|
|
|
14
|
|
|
use Cake\TestSuite\IntegrationTestTrait; |
15
|
|
|
use Cake\View\Exception\MissingTemplateException; |
16
|
|
|
use CakeDC\Api\Service\ServiceRegistry; |
17
|
|
|
use Cake\Core\Configure; |
18
|
|
|
use Cake\Datasource\EntityInterface; |
19
|
|
|
use Cake\ORM\TableRegistry; |
20
|
|
|
use Cake\Utility\Hash; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class IntegrationTestCase |
24
|
|
|
* |
25
|
|
|
* @package CakeDC\Api\TestSuite |
26
|
|
|
*/ |
27
|
|
|
class IntegrationTestCase extends \Cake\TestSuite\TestCase |
28
|
|
|
{ |
29
|
|
|
use IntegrationTestTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string|int Current logged in user |
33
|
|
|
*/ |
34
|
|
|
protected $_defaultUserId; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* setUp |
38
|
|
|
* |
39
|
56 |
|
* @return void |
40
|
|
|
*/ |
41
|
56 |
|
public function setUp() |
42
|
56 |
|
{ |
43
|
56 |
|
parent::setUp(); |
44
|
|
|
Configure::write('Api', []); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* tearDown |
49
|
|
|
* |
50
|
56 |
|
* @return void |
51
|
|
|
*/ |
52
|
56 |
|
public function tearDown() |
53
|
56 |
|
{ |
54
|
56 |
|
parent::tearDown(); |
55
|
|
|
ServiceRegistry::getServiceLocator()->clear(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Default user api method. |
60
|
|
|
* |
61
|
|
|
* @param string $userId User id. |
62
|
56 |
|
* @return string |
63
|
|
|
*/ |
64
|
56 |
|
public function getDefaultUser($userId = null) |
65
|
56 |
|
{ |
66
|
56 |
|
if ($userId === null) { |
67
|
47 |
|
$userId = $this->_defaultUserId; |
68
|
|
|
} else { |
69
|
|
|
$this->_defaultUserId = $userId; |
70
|
56 |
|
} |
71
|
|
|
|
72
|
|
|
return $userId; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns user token. |
77
|
|
|
* |
78
|
|
|
* @param string $userId User id. |
79
|
56 |
|
* @return mixed|null |
80
|
|
|
*/ |
81
|
56 |
|
protected function _userToken($userId = null) |
82
|
56 |
|
{ |
83
|
56 |
|
if ($userId === null) { |
84
|
56 |
|
$userId = $this->getDefaultUser(); |
85
|
56 |
|
} |
86
|
56 |
|
$Users = TableRegistry::getTableLocator()->get('CakeDC/Users.Users'); |
87
|
47 |
|
$user = $Users->find()->where(['id' => $userId])->first(); |
88
|
|
|
if ($user instanceof EntityInterface) { |
89
|
|
|
return $user['api_token']; |
90
|
9 |
|
} |
91
|
|
|
|
92
|
|
|
return null; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Send api request. |
97
|
|
|
* |
98
|
|
|
* @param string $url Url. |
99
|
|
|
* @param string $method HTTP method. |
100
|
|
|
* @param array $data Api parameters. |
101
|
|
|
* @param string $userId Current user id. |
102
|
56 |
|
* @return void |
103
|
|
|
*/ |
104
|
56 |
|
public function sendRequest($url, $method, $data = [], $userId = null) |
105
|
56 |
|
{ |
106
|
|
|
ServiceRegistry::getServiceLocator()->clear(); |
107
|
56 |
|
$userToken = $this->_userToken($userId); |
108
|
|
|
|
109
|
56 |
|
Configure::load('api'); |
110
|
|
|
|
111
|
|
|
if (!is_string($url)) { |
112
|
|
|
$this->_sendRequest($url, $method, $data); |
113
|
|
|
|
114
|
56 |
|
return; |
115
|
56 |
|
} |
116
|
56 |
|
$url = '/api' . $url; |
117
|
47 |
|
if (is_string($url)) { |
118
|
47 |
|
if ($userToken !== null) { |
119
|
56 |
|
$url = $this->_appendGetParam($url, 'token', $userToken); |
120
|
56 |
|
} |
121
|
47 |
|
} |
122
|
27 |
|
if ($method == 'GET' && is_string($url)) { |
123
|
27 |
|
if (!empty($data)) { |
124
|
27 |
|
foreach ($data as $key => $value) { |
125
|
27 |
|
if (!is_array($value)) { |
126
|
27 |
|
$url = $this->_appendGetParam($url, $key, $value); |
127
|
27 |
|
} |
128
|
47 |
|
} |
129
|
56 |
|
} |
130
|
56 |
|
} |
131
|
56 |
|
$this->useHttpServer(true); |
132
|
|
|
try { |
133
|
|
|
ServiceRegistry::getServiceLocator()->clear(); |
134
|
|
|
TableRegistry::getTableLocator()->clear(); |
135
|
|
|
$this->_sendRequest($url, $method, $data); |
136
|
|
|
} catch (MissingTemplateException $ex) { |
137
|
|
|
throw new MissingTemplateException(sprintf('Possibly related to %s', $this->_exception->getMessage()), 500, $ex); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
48 |
|
/** |
142
|
|
|
* Add param to request. |
143
|
48 |
|
* |
144
|
26 |
|
* @param string $url Url. |
145
|
26 |
|
* @param string $key Param name. |
146
|
48 |
|
* @param string $value Param value. |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
48 |
|
protected function _appendGetParam($url, $key, $value) |
150
|
|
|
{ |
151
|
|
|
if (strpos($url, '?') !== false) { |
152
|
|
|
$appendChar = '&'; |
153
|
|
|
} else { |
154
|
|
|
$appendChar = '?'; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $url . $appendChar . urlencode($key) . '=' . urlencode($value); |
158
|
52 |
|
} |
159
|
|
|
|
160
|
52 |
|
/** |
161
|
52 |
|
* Assert result is success. |
162
|
52 |
|
* |
163
|
52 |
|
* @param array $result Response. |
164
|
|
|
* @return void |
165
|
|
|
*/ |
166
|
|
|
public function assertSuccess($result) |
167
|
|
|
{ |
168
|
55 |
|
$this->assertTrue(is_array($result)); |
169
|
|
|
$this->assertEquals($result['status'], 'success'); |
170
|
55 |
|
$this->assertEquals(200, $this->_response->getStatusCode()); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return mixed |
175
|
|
|
*/ |
176
|
|
|
public function getJsonResponse() |
177
|
|
|
{ |
178
|
|
|
return json_decode((string)$this->_response->getBody(), true); |
179
|
|
|
} |
180
|
7 |
|
|
181
|
|
|
/** |
182
|
7 |
|
* Assert result is error. |
183
|
7 |
|
* |
184
|
7 |
|
* @param array $result Response. |
185
|
7 |
|
* @param int $code Result code. |
186
|
7 |
|
* @return void |
187
|
7 |
|
*/ |
188
|
7 |
|
public function assertError($result, $code = null) |
189
|
|
|
{ |
190
|
|
|
$this->assertTrue(is_array($result)); |
191
|
|
|
$this->assertEquals($result['status'], 'error'); |
192
|
|
|
$this->assertEquals(200, $this->_response->getStatusCode()); |
193
|
|
|
if (!empty($code)) { |
194
|
|
|
$this->assertEquals($code, $result['code']); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Helper method for status assertions. |
200
|
|
|
* |
201
|
|
|
* @param int $code Status code. |
202
|
|
|
* @param string $message The error message. |
203
|
|
|
* @return void |
204
|
|
|
*/ |
205
|
|
|
public function assertStatus($code, $message = null) |
206
|
|
|
{ |
207
|
|
|
if ($message === null) { |
208
|
|
|
$message = "Status code $code does not match"; |
209
|
|
|
} |
210
|
|
|
$this->_assertStatus($code, $code, $message); |
|
|
|
|
211
|
|
|
} |
212
|
3 |
|
|
213
|
|
|
/** |
214
|
3 |
|
* Assert error message. |
215
|
3 |
|
* |
216
|
3 |
|
* @param array $result Response. |
217
|
|
|
* @param string $expectedMessage Message. |
218
|
|
|
* @return void |
219
|
|
|
*/ |
220
|
|
|
public function assertErrorMessage($result, $expectedMessage) |
221
|
|
|
{ |
222
|
|
|
$message = Hash::get($result, 'message'); |
223
|
|
|
$this->assertTrue(is_string($message) && strpos($message, $expectedMessage) === 0); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.