1 | <?php |
||
2 | |||
3 | /* For licensing terms, see /license.txt */ |
||
4 | |||
5 | use GuzzleHttp\Client; |
||
6 | use PHPUnit\Framework\TestCase; |
||
7 | |||
8 | require_once __DIR__.'/../../../../vendor/autoload.php'; |
||
9 | require_once __DIR__.'/../../../inc/global.inc.php'; |
||
10 | |||
11 | /** |
||
12 | * Class V2Test |
||
13 | * |
||
14 | * Base class for all WebService API v2 tests |
||
15 | */ |
||
16 | abstract class V2TestCase extends TestCase |
||
17 | { |
||
18 | const WEBSERVICE_USERNAME = 'admin'; |
||
19 | const WEBSERVICE_PASSWORD = 'admin'; |
||
20 | const RELATIVE_URI = 'webservices/api/v2.php'; |
||
21 | /** |
||
22 | * @var Client $client |
||
23 | */ |
||
24 | private $client; |
||
25 | private $apiKey; |
||
26 | |||
27 | /** |
||
28 | * Initialises the HTTP client and retrieves the API key from the server |
||
29 | * |
||
30 | * @throws Exception when it cannot get the API key |
||
31 | */ |
||
32 | protected function setUp(): void |
||
33 | { |
||
34 | parent::setUp(); |
||
35 | |||
36 | $this->client = new Client( |
||
37 | [ |
||
38 | 'base_uri' => api_get_path(WEB_CODE_PATH), |
||
39 | ] |
||
40 | ); |
||
41 | |||
42 | $response = $this->client->post( |
||
43 | self::RELATIVE_URI, |
||
44 | [ |
||
45 | 'form_params' => [ |
||
46 | 'action' => 'authenticate', |
||
47 | 'username' => self::WEBSERVICE_USERNAME, |
||
48 | 'password' => self::WEBSERVICE_PASSWORD, |
||
49 | ], |
||
50 | ] |
||
51 | ); |
||
52 | |||
53 | if (200 === $response->getStatusCode()) { |
||
54 | $decodedResponse = json_decode($response->getBody()->getContents(), false, 3, JSON_THROW_ON_ERROR); |
||
55 | |||
56 | if (is_object($decodedResponse)) { |
||
57 | $this->apiKey = $decodedResponse->data->apiKey; |
||
58 | } else { |
||
59 | throw new Exception('The returned JSON document is not an object'); |
||
60 | } |
||
61 | } else { |
||
62 | throw new Exception($response->getReasonPhrase()); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Posts an action request and assert the server returns an error message |
||
68 | * |
||
69 | * @param array $parameters parameters to send with the request |
||
70 | * |
||
71 | * @return string the "message" error string returned by the webservice |
||
72 | */ |
||
73 | protected function errorMessageString($parameters = []) |
||
74 | { |
||
75 | $decodedResponse = $this->decodedResponse($parameters); |
||
76 | $this->assertIsObject($decodedResponse); |
||
77 | $this->assertTrue(property_exists($decodedResponse, 'error')); |
||
78 | $error = $decodedResponse->error; |
||
79 | $this->assertIsBool(true, $error); |
||
80 | $this->assertTrue($error, 'error is not true: '.print_r($decodedResponse, true)); |
||
81 | $this->assertTrue(property_exists($decodedResponse, 'message')); |
||
82 | $message = $decodedResponse->message; |
||
83 | $this->assertIsString($message); |
||
84 | |||
85 | return $message; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Posts an action request to the web server, asserts it returns a valid JSON-encoded response and decodes it |
||
90 | * supplied parameters complete or override the generated base parameters username, api_key and action |
||
91 | * |
||
92 | * @param array $parameters parameters to send with the request as an associative array |
||
93 | * |
||
94 | * @return mixed the decoded response (usually an object with properties data, error, message) |
||
95 | */ |
||
96 | protected function decodedResponse($parameters = []) |
||
97 | { |
||
98 | $baseParams = [ |
||
99 | 'username' => self::WEBSERVICE_USERNAME, |
||
100 | 'api_key' => $this->apiKey, |
||
101 | 'action' => $this->action(), |
||
102 | ]; |
||
103 | |||
104 | $response = $this->client->post(self::RELATIVE_URI, ['form_params' => array_merge($baseParams, $parameters)]); |
||
105 | |||
106 | $this->assertNotNull($response); |
||
107 | $this->assertSame(200, $response->getStatusCode()); |
||
108 | |||
109 | $decodedResponse = json_decode($response->getBody()->getContents()); |
||
110 | |||
111 | // Help debug |
||
112 | if (null === $decodedResponse) { |
||
113 | var_dump($this->action(), $response->getBody()->getContents()); |
||
0 ignored issues
–
show
Security
Debugging Code
introduced
by
Loading history...
|
|||
114 | } |
||
115 | |||
116 | $this->assertNotNull($decodedResponse); |
||
117 | |||
118 | return $decodedResponse; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * returns the name of the webservice, to be passed as the "action" with the HTTP request |
||
123 | * |
||
124 | * @return string name of the webservice action to call |
||
125 | */ |
||
126 | abstract protected function action(); |
||
127 | |||
128 | /** |
||
129 | * Posts an action request and assert it returns an integer value in the "data" property |
||
130 | * |
||
131 | * @param array $parameters parameters to send with the request |
||
132 | * |
||
133 | * @return integer the integer value |
||
134 | */ |
||
135 | protected function integer($parameters = []) |
||
136 | { |
||
137 | $value = $this->singleElementValue($parameters); |
||
138 | $this->assertIsInt($value); |
||
139 | |||
140 | return $value; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Posts an action request and assert the server returns a single value in the "data" array of the returned object |
||
145 | * |
||
146 | * @param array $parameters parameters to send with the request |
||
147 | * |
||
148 | * @return mixed the unique element of the "data" array |
||
149 | */ |
||
150 | protected function singleElementValue($parameters = []) |
||
151 | { |
||
152 | $data = $this->dataArray($parameters); |
||
153 | $this->assertSame(1, count($data)); |
||
154 | |||
155 | return $data[0]; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Posts an action request and assert the server returns a "data" array |
||
160 | * |
||
161 | * @param array $parameters parameters to send with the request |
||
162 | * |
||
163 | * @return array the "data" array returned by the webservice |
||
164 | */ |
||
165 | protected function dataArray($parameters = []) |
||
166 | { |
||
167 | $decodedResponse = $this->decodedResponse($parameters); |
||
168 | $this->assertIsObject($decodedResponse); |
||
169 | $this->assertTrue( |
||
170 | property_exists($decodedResponse, 'data'), |
||
171 | 'response data property is missing: '.print_r($decodedResponse, true) |
||
172 | ); |
||
173 | |||
174 | $data = $decodedResponse->data; |
||
175 | $this->assertIsArray($data); |
||
176 | |||
177 | return $data; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Posts an action request and assert it returns an array with a single boolean value |
||
182 | * |
||
183 | * @param array $parameters parameters to send with the request |
||
184 | * |
||
185 | * @return boolean the boolean value |
||
186 | */ |
||
187 | protected function boolean($parameters = []) |
||
188 | { |
||
189 | $value = $this->singleElementValue($parameters); |
||
190 | $this->assertIsBool($value); |
||
191 | |||
192 | return $value; |
||
193 | } |
||
194 | } |
||
195 |