Mocking
1 year ago
ApiCallTest.php
1 year ago
AuthenticationTest.php
1 year ago
ClientTest.php
1 year ago
CoreTestCaseTest.php
1 year ago
EndToEndTest.php
1 year ago
LoggerTest.php
1 year ago
TypesTest.php
1 year ago
UtilsTest.php
1 year ago
bootstrap.php
1 year ago
ApiCallTest.php
1528 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Core\Tests; |
| 4 | |
| 5 | use apimatic\jsonmapper\AnyOfValidationException; |
| 6 | use apimatic\jsonmapper\JsonMapperException; |
| 7 | use apimatic\jsonmapper\OneOfValidationException; |
| 8 | use Core\Request\Parameters\AdditionalFormParams; |
| 9 | use Core\Request\Parameters\AdditionalQueryParams; |
| 10 | use Core\Request\Parameters\BodyParam; |
| 11 | use Core\Request\Parameters\FormParam; |
| 12 | use Core\Request\Parameters\HeaderParam; |
| 13 | use Core\Request\Parameters\QueryParam; |
| 14 | use Core\Request\Parameters\TemplateParam; |
| 15 | use Core\Request\RequestBuilder; |
| 16 | use Core\Response\Context; |
| 17 | use Core\Response\Types\ErrorType; |
| 18 | use Core\Tests\Mocking\MockHelper; |
| 19 | use Core\Tests\Mocking\Other\MockChild3; |
| 20 | use Core\Tests\Mocking\Other\MockClass; |
| 21 | use Core\Tests\Mocking\Other\MockException; |
| 22 | use Core\Tests\Mocking\Other\MockException1; |
| 23 | use Core\Tests\Mocking\Other\MockException3; |
| 24 | use Core\Tests\Mocking\Response\MockResponse; |
| 25 | use Core\Tests\Mocking\Types\MockApiResponse; |
| 26 | use Core\Tests\Mocking\Types\MockRequest; |
| 27 | use Core\Utils\CoreHelper; |
| 28 | use CoreInterfaces\Core\Format; |
| 29 | use CoreInterfaces\Core\Request\RequestMethod; |
| 30 | use CoreInterfaces\Http\RetryOption; |
| 31 | use CURLFile; |
| 32 | use Exception; |
| 33 | use InvalidArgumentException; |
| 34 | use PHPUnit\Framework\TestCase; |
| 35 | |
| 36 | class ApiCallTest extends TestCase |
| 37 | { |
| 38 | private const DUMMY_BODY = ['res' => 'This is raw body']; |
| 39 | |
| 40 | /** |
| 41 | * @param string $query Just the query path of the url |
| 42 | * @return array<string,string> |
| 43 | */ |
| 44 | private static function convertQueryIntoArray(string $query): array |
| 45 | { |
| 46 | $array = []; |
| 47 | foreach (explode('&', $query) as $item) { |
| 48 | if (empty($item)) { |
| 49 | continue; |
| 50 | } |
| 51 | $keyVal = explode('=', $item); |
| 52 | $key = self::updateKeyForArray(urldecode($keyVal[0]), $array); |
| 53 | $array[$key] = urldecode($keyVal[1]); |
| 54 | } |
| 55 | return $array; |
| 56 | } |
| 57 | |
| 58 | private static function updateKeyForArray(string $key, array $array): string |
| 59 | { |
| 60 | if (key_exists($key, $array)) { |
| 61 | return self::updateKeyForArray("$key*", $array); |
| 62 | } |
| 63 | return $key; |
| 64 | } |
| 65 | |
| 66 | public function testCollectedBodyParams() |
| 67 | { |
| 68 | $request = (new RequestBuilder(RequestMethod::POST, '/some/path')) |
| 69 | ->parameters(BodyParam::init(null)->extract('key1')) |
| 70 | ->build(MockHelper::getClient()); |
| 71 | $this->assertNull($request->getBody()); |
| 72 | |
| 73 | $request = (new RequestBuilder(RequestMethod::POST, '/some/path')) |
| 74 | ->parameters(BodyParam::init('some string')->extract('key1', 'new string')) |
| 75 | ->build(MockHelper::getClient()); |
| 76 | $this->assertEquals('some string', $request->getBody()); |
| 77 | |
| 78 | $options = ['key1' => true, 'key2' => 'some string', 'key3' => 23]; |
| 79 | $request = (new RequestBuilder(RequestMethod::POST, '/some/path')) |
| 80 | ->parameters(BodyParam::init((object)$options)->extract('key1')) |
| 81 | ->build(MockHelper::getClient()); |
| 82 | $this->assertEquals(true, $request->getBody()); |
| 83 | |
| 84 | $request = (new RequestBuilder(RequestMethod::POST, '/some/path')) |
| 85 | ->parameters(BodyParam::init($options)->extract('key1')) |
| 86 | ->build(MockHelper::getClient()); |
| 87 | $this->assertEquals('true', $request->getBody()); |
| 88 | |
| 89 | $request = (new RequestBuilder(RequestMethod::POST, '/some/path')) |
| 90 | ->parameters( |
| 91 | BodyParam::initWrapped('key1', $options)->extract('key1'), |
| 92 | BodyParam::initWrapped('key3', $options)->extract('key3') |
| 93 | ) |
| 94 | ->build(MockHelper::getClient()); |
| 95 | $this->assertEquals('{"key1":true,"key3":23}', $request->getBody()); |
| 96 | |
| 97 | $request = (new RequestBuilder(RequestMethod::POST, '/some/path')) |
| 98 | ->parameters(BodyParam::init($options)->extract('key4', 'MyConstant')) |
| 99 | ->build(MockHelper::getClient()); |
| 100 | $this->assertEquals('MyConstant', $request->getBody()); |
| 101 | } |
| 102 | |
| 103 | public function testCollectedFormParams() |
| 104 | { |
| 105 | $options = ['key1' => true, 'key2' => 'some string', 'key3' => 23]; |
| 106 | |
| 107 | $request = (new RequestBuilder(RequestMethod::POST, '/some/path')) |
| 108 | ->parameters( |
| 109 | FormParam::init('key1', $options)->extract('key1'), |
| 110 | FormParam::init('key3', $options)->extract('key3'), |
| 111 | FormParam::init('key4', $options)->extract('key4', 'MyConstant'), |
| 112 | FormParam::init('key2', $options)->extract('key2', 'new string') |
| 113 | ) |
| 114 | ->build(MockHelper::getClient()); |
| 115 | $this->assertNull($request->getBody()); |
| 116 | $this->assertEquals([ |
| 117 | 'key1' => 'true', |
| 118 | 'key2' => 'some string', |
| 119 | 'key3' => 23, |
| 120 | 'key4' => 'MyConstant' |
| 121 | ], $request->getParameters()); |
| 122 | $this->assertEquals([ |
| 123 | 'key1' => 'key1=true', |
| 124 | 'key2' => 'key2=some+string', |
| 125 | 'key3' => 'key3=23', |
| 126 | 'key4' => 'key4=MyConstant' |
| 127 | ], $request->getEncodedParameters()); |
| 128 | $this->assertEquals([], $request->getMultipartParameters()); |
| 129 | } |
| 130 | |
| 131 | public function testCollectedHeaderParams() |
| 132 | { |
| 133 | $options = ['key1' => true, 'key2' => 'some string', 'key3' => 23]; |
| 134 | |
| 135 | $request = (new RequestBuilder(RequestMethod::POST, '/some/path')) |
| 136 | ->parameters( |
| 137 | HeaderParam::init('key1', $options)->extract('key1'), |
| 138 | HeaderParam::init('key3', $options)->extract('key3'), |
| 139 | HeaderParam::init('key4', $options)->extract('key4', 'MyConstant'), |
| 140 | HeaderParam::init('key2', $options)->extract('key2', 'new string') |
| 141 | ) |
| 142 | ->build(MockHelper::getClient()); |
| 143 | $this->assertEquals(true, $request->getHeaders()['key1']); |
| 144 | $this->assertEquals('some string', $request->getHeaders()['key2']); |
| 145 | $this->assertEquals(23, $request->getHeaders()['key3']); |
| 146 | $this->assertEquals('MyConstant', $request->getHeaders()['key4']); |
| 147 | $this->assertEquals(890.098, $request->getHeaders()['key5']); |
| 148 | } |
| 149 | |
| 150 | public function testCollectedQueryParams() |
| 151 | { |
| 152 | $options = ['key1' => true, 'key2' => 'some string', 'key3' => 23]; |
| 153 | |
| 154 | $request = (new RequestBuilder(RequestMethod::POST, '/path')) |
| 155 | ->parameters( |
| 156 | QueryParam::init('key1', $options)->extract('key1'), |
| 157 | QueryParam::init('key3', $options)->extract('key3'), |
| 158 | QueryParam::init('key4', $options)->extract('key4', 'MyConstant'), |
| 159 | QueryParam::init('key2', $options)->extract('key2', 'new string') |
| 160 | ) |
| 161 | ->build(MockHelper::getClient()); |
| 162 | $this->assertEquals( |
| 163 | 'http://my/path:3000/v1/path?key1=true&key3=23&key4=MyConstant&key2=some+string', |
| 164 | $request->getQueryUrl() |
| 165 | ); |
| 166 | } |
| 167 | |
| 168 | public function testCollectedTemplateParams() |
| 169 | { |
| 170 | $options = ['key1' => true, 'key2' => 'some string', 'key3' => 23]; |
| 171 | |
| 172 | $request = (new RequestBuilder(RequestMethod::POST, '/{key1}/{key2}/{key3}/{key4}')) |
| 173 | ->parameters( |
| 174 | TemplateParam::init('key1', $options)->extract('key1'), |
| 175 | TemplateParam::init('key3', $options)->extract('key3'), |
| 176 | TemplateParam::init('key4', $options)->extract('key4', 'MyConstant'), |
| 177 | TemplateParam::init('key2', $options)->extract('key2', 'new string') |
| 178 | ) |
| 179 | ->build(MockHelper::getClient()); |
| 180 | $this->assertEquals('http://my/path:3000/v1/true/some+string/23/MyConstant', $request->getQueryUrl()); |
| 181 | } |
| 182 | |
| 183 | public function testSendWithConfig() |
| 184 | { |
| 185 | $result = MockHelper::newApiCall() |
| 186 | ->requestBuilder((new RequestBuilder(RequestMethod::PUT, '/2ndServer')) |
| 187 | ->server('Server2') |
| 188 | ->auth('header') |
| 189 | ->retryOption(RetryOption::ENABLE_RETRY)) |
| 190 | ->responseHandler(MockHelper::responseHandler() |
| 191 | ->type(MockClass::class)) |
| 192 | ->execute(); |
| 193 | $this->assertInstanceOf(MockClass::class, $result); |
| 194 | $this->assertEquals(RequestMethod::PUT, $result->body['httpMethod']); |
| 195 | $this->assertEquals('https://my/path/v2/2ndServer', $result->body['queryUrl']); |
| 196 | $this->assertEquals('application/json', $result->body['headers']['Accept']); |
| 197 | $this->assertEquals('headVal1', $result->body['headers']['additionalHead1']); |
| 198 | $this->assertEquals('headVal2', $result->body['headers']['additionalHead2']); |
| 199 | $this->assertEquals('someAuthToken', $result->body['headers']['token']); |
| 200 | $this->assertEquals('accessToken', $result->body['headers']['authorization']); |
| 201 | $this->assertStringStartsWith('my lang|1.*.*|', $result->body['headers']['user-agent']); |
| 202 | $this->assertStringNotContainsString('{', $result->body['headers']['user-agent']); |
| 203 | } |
| 204 | |
| 205 | public function testSendWithoutContentType() |
| 206 | { |
| 207 | $result = MockHelper::newApiCall() |
| 208 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 209 | ->disableContentType()) |
| 210 | ->responseHandler(MockHelper::responseHandler() |
| 211 | ->type(MockClass::class)) |
| 212 | ->execute(); |
| 213 | $this->assertInstanceOf(MockClass::class, $result); |
| 214 | $this->assertArrayNotHasKey('content-type', $result->body['headers']); |
| 215 | $this->assertArrayNotHasKey('Accept', $result->body['headers']); |
| 216 | |
| 217 | $result = MockHelper::newApiCall() |
| 218 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}'))) |
| 219 | ->execute(); |
| 220 | $this->assertArrayNotHasKey('Accept', $result['body']['headers']); |
| 221 | } |
| 222 | |
| 223 | public function testSendWithContentTypeWithBody() |
| 224 | { |
| 225 | $result = MockHelper::newApiCall() |
| 226 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 227 | ->parameters(BodyParam::init(123)) |
| 228 | ->parameters(HeaderParam::init('content-type', 'MyContentType'))) |
| 229 | ->responseHandler(MockHelper::responseHandler() |
| 230 | ->type(MockClass::class)) |
| 231 | ->execute(); |
| 232 | $this->assertInstanceOf(MockClass::class, $result); |
| 233 | $this->assertEquals('MyContentType', $result->body['headers']['content-type']); |
| 234 | $result = MockHelper::newApiCall() |
| 235 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}'))) |
| 236 | ->execute(); |
| 237 | $this->assertArrayNotHasKey('Accept', $result['body']['headers']); |
| 238 | } |
| 239 | |
| 240 | public function testSendDisableContentTypeWithBody() |
| 241 | { |
| 242 | $result = MockHelper::newApiCall() |
| 243 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 244 | ->parameters(BodyParam::init(123)) |
| 245 | ->disableContentType()) |
| 246 | ->responseHandler(MockHelper::responseHandler() |
| 247 | ->type(MockClass::class)) |
| 248 | ->execute(); |
| 249 | $this->assertInstanceOf(MockClass::class, $result); |
| 250 | $this->assertArrayNotHasKey('content-type', $result->body['headers']); |
| 251 | $this->assertArrayNotHasKey('Accept', $result->body['headers']); |
| 252 | |
| 253 | $result = MockHelper::newApiCall() |
| 254 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}'))) |
| 255 | ->execute(); |
| 256 | $this->assertArrayNotHasKey('Accept', $result['body']['headers']); |
| 257 | } |
| 258 | |
| 259 | public function testSendWithCustomContentType() |
| 260 | { |
| 261 | $result = MockHelper::newApiCall() |
| 262 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 263 | ->parameters(HeaderParam::init('content-type', 'MyContentType'))) |
| 264 | ->responseHandler(MockHelper::responseHandler() |
| 265 | ->type(MockClass::class)) |
| 266 | ->execute(); |
| 267 | $this->assertInstanceOf(MockClass::class, $result); |
| 268 | $this->assertEquals('MyContentType', $result->body['headers']['content-type']); |
| 269 | } |
| 270 | |
| 271 | public function testSendNoParams() |
| 272 | { |
| 273 | $result = MockHelper::newApiCall() |
| 274 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}'))) |
| 275 | ->responseHandler(MockHelper::responseHandler() |
| 276 | ->type(MockClass::class)) |
| 277 | ->execute(); |
| 278 | $this->assertInstanceOf(MockClass::class, $result); |
| 279 | $this->assertEquals(RequestMethod::POST, $result->body['httpMethod']); |
| 280 | $this->assertEquals('http://my/path:3000/v1/simple/{tyu}', $result->body['queryUrl']); |
| 281 | $this->assertEquals('application/json', $result->body['headers']['Accept']); |
| 282 | $this->assertEquals('headVal1', $result->body['headers']['additionalHead1']); |
| 283 | $this->assertEquals('headVal2', $result->body['headers']['additionalHead2']); |
| 284 | $this->assertEquals( |
| 285 | 'my lang|1.*.*|PHP|' . phpversion() . '|' . CoreHelper::getOsInfo(), |
| 286 | $result->body['headers']['user-agent'] |
| 287 | ); |
| 288 | } |
| 289 | |
| 290 | public function testSendTemplate() |
| 291 | { |
| 292 | $result = MockHelper::newApiCall() |
| 293 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 294 | ->parameters(TemplateParam::init('tyu', 'val 01'))) |
| 295 | ->responseHandler(MockHelper::responseHandler() |
| 296 | ->type(MockClass::class)) |
| 297 | ->execute(); |
| 298 | $this->assertInstanceOf(MockClass::class, $result); |
| 299 | $this->assertEquals('http://my/path:3000/v1/simple/val+01', $result->body['queryUrl']); |
| 300 | } |
| 301 | |
| 302 | public function testSendTemplateArray() |
| 303 | { |
| 304 | $result = MockHelper::newApiCall() |
| 305 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 306 | ->parameters(TemplateParam::init('tyu', ['val 01', '**sad&?N', 'v4']))) |
| 307 | ->responseHandler(MockHelper::responseHandler() |
| 308 | ->type(MockClass::class)) |
| 309 | ->execute(); |
| 310 | $this->assertInstanceOf(MockClass::class, $result); |
| 311 | $this->assertEquals('http://my/path:3000/v1/simple/val+01/%2A%2Asad%26%3FN/v4', $result->body['queryUrl']); |
| 312 | } |
| 313 | |
| 314 | public function testSendTemplateObject() |
| 315 | { |
| 316 | $mockObj = new MockClass([]); |
| 317 | $mockObj->addAdditionalProperty('key', 'val 01'); |
| 318 | $mockObj->addAdditionalProperty('key2', 'v4'); |
| 319 | $mockObj2 = new MockClass([null, null]); |
| 320 | $mockObj2->addAdditionalProperty('key3', '**sad&?N'); |
| 321 | $mockObj2->addAdditionalProperty('key4', 'v^^'); |
| 322 | $mockObj->addAdditionalProperty('key5', $mockObj2); |
| 323 | $result = MockHelper::newApiCall() |
| 324 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 325 | ->parameters(TemplateParam::init('tyu', $mockObj))) |
| 326 | ->responseHandler(MockHelper::responseHandler() |
| 327 | ->type(MockClass::class)) |
| 328 | ->execute(); |
| 329 | $this->assertInstanceOf(MockClass::class, $result); |
| 330 | $this->assertEquals( |
| 331 | 'http://my/path:3000/v1/simple//val+01/v4///%2A%2Asad%26%3FN/v%5E%5E', |
| 332 | $result->body['queryUrl'] |
| 333 | ); |
| 334 | } |
| 335 | |
| 336 | public function testSendSingleQuery() |
| 337 | { |
| 338 | $result = MockHelper::newApiCall() |
| 339 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 340 | ->parameters( |
| 341 | QueryParam::init('key', 'val 01'), |
| 342 | AdditionalQueryParams::init(null) |
| 343 | )) |
| 344 | ->responseHandler(MockHelper::responseHandler() |
| 345 | ->type(MockClass::class)) |
| 346 | ->execute(); |
| 347 | $this->assertInstanceOf(MockClass::class, $result); |
| 348 | $query = self::convertQueryIntoArray(explode('?', $result->body['queryUrl'])[1]); |
| 349 | $this->assertEquals(['key' => 'val 01'], $query); |
| 350 | } |
| 351 | |
| 352 | public function testSendSingleForm() |
| 353 | { |
| 354 | $result = MockHelper::newApiCall() |
| 355 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 356 | ->parameters( |
| 357 | FormParam::init('key', 'val 01'), |
| 358 | HeaderParam::init('content-type', 'myContentTypeHeader'), |
| 359 | AdditionalFormParams::init(null) |
| 360 | )) |
| 361 | ->responseHandler(MockHelper::responseHandler() |
| 362 | ->type(MockClass::class)) |
| 363 | ->execute(); |
| 364 | $this->assertInstanceOf(MockClass::class, $result); |
| 365 | $this->assertEquals('myContentTypeHeader', $result->body['headers']['content-type']); |
| 366 | $this->assertNull($result->body['body']); |
| 367 | $this->assertEquals(['key' => 'val 01'], $result->body['parameters']); |
| 368 | $this->assertEquals(['key' => 'key=val+01'], $result->body['parametersEncoded']); |
| 369 | $this->assertEquals([], $result->body['parametersMultipart']); |
| 370 | } |
| 371 | |
| 372 | public function testSendMultipartFormParameters() |
| 373 | { |
| 374 | $result = MockHelper::newApiCall() |
| 375 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 376 | ->parameters( |
| 377 | FormParam::init('myFile', MockHelper::getFileWrapper()) |
| 378 | ->encodingHeader('content-type', 'image/png'), |
| 379 | FormParam::init('object', new MockClass(["key" => 234, "myBool" => true])) |
| 380 | ->encodingHeader('content-type', 'application/json'), |
| 381 | FormParam::init('my bool', true) |
| 382 | ->encodingHeader('content-type', 'application/text') |
| 383 | )) |
| 384 | ->responseHandler(MockHelper::responseHandler()->type(MockClass::class)) |
| 385 | ->execute(); |
| 386 | $this->assertInstanceOf(MockClass::class, $result); |
| 387 | $this->assertEquals([], $result->body['parametersEncoded']); |
| 388 | |
| 389 | $file = $result->body['parametersMultipart']['myFile']; |
| 390 | $this->assertInstanceOf(CURLFile::class, $file); |
| 391 | $this->assertStringEndsWith('testFile.txt', $file->getFilename()); |
| 392 | $this->assertEquals('text/plain', $file->getMimeType()); |
| 393 | $this->assertEquals('My Text', $file->getPostFilename()); |
| 394 | |
| 395 | $this->assertEquals([ |
| 396 | 'myFile' => $file, |
| 397 | 'object' => '{"body":{"key":234,"myBool":true}}', |
| 398 | 'my bool' => 'true' |
| 399 | ], $result->body['parametersMultipart']); |
| 400 | } |
| 401 | |
| 402 | public function testSendFileFormWithEncodingHeader() |
| 403 | { |
| 404 | $result = MockHelper::newApiCall() |
| 405 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 406 | ->parameters( |
| 407 | FormParam::init('myFile', MockHelper::getFileWrapper())->encodingHeader('content-type', 'image/png') |
| 408 | )) |
| 409 | ->responseHandler(MockHelper::responseHandler() |
| 410 | ->type(MockClass::class)) |
| 411 | ->execute(); |
| 412 | $this->assertInstanceOf(MockClass::class, $result); |
| 413 | $this->assertEquals([], $result->body['parametersEncoded']); |
| 414 | $file = $result->body['parametersMultipart']['myFile']; |
| 415 | $this->assertInstanceOf(CURLFile::class, $file); |
| 416 | $this->assertStringEndsWith('testFile.txt', $file->getFilename()); |
| 417 | $this->assertEquals('text/plain', $file->getMimeType()); |
| 418 | $this->assertEquals('My Text', $file->getPostFilename()); |
| 419 | $this->assertEquals($file, $result->body['parameters']['myFile']); |
| 420 | } |
| 421 | |
| 422 | public function testSendFileFormWithOtherTypes() |
| 423 | { |
| 424 | $result = MockHelper::newApiCall() |
| 425 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 426 | ->parameters( |
| 427 | FormParam::init('myFile', MockHelper::getFileWrapper()), |
| 428 | FormParam::init('key', 'val 01'), |
| 429 | FormParam::init('my bool', true), |
| 430 | FormParam::init('object', new MockClass(["key" => 234, "myBool" => true])), |
| 431 | FormParam::init('special', ['%^&&*^?.. + @214', true]) |
| 432 | )) |
| 433 | ->responseHandler(MockHelper::responseHandler() |
| 434 | ->type(MockClass::class)) |
| 435 | ->execute(); |
| 436 | $this->assertInstanceOf(MockClass::class, $result); |
| 437 | $this->assertEquals([ |
| 438 | 'key' => 'key=val+01', |
| 439 | 'special' => 'special%5B0%5D=%25%5E%26%26%2A%5E%3F..+%2B+%40214&special%5B1%5D=true', |
| 440 | 'my bool' => 'my+bool=true', |
| 441 | 'object' => 'object%5Bbody%5D%5Bkey%5D=234&object%5Bbody%5D%5BmyBool%5D=true' |
| 442 | ], $result->body['parametersEncoded']); |
| 443 | $this->assertEquals(1, count($result->body['parametersMultipart'])); |
| 444 | $file = $result->body['parametersMultipart']['myFile']; |
| 445 | $this->assertInstanceOf(CURLFile::class, $file); |
| 446 | $this->assertStringEndsWith('testFile.txt', $file->getFilename()); |
| 447 | $this->assertEquals('text/plain', $file->getMimeType()); |
| 448 | $this->assertEquals('My Text', $file->getPostFilename()); |
| 449 | $this->assertEquals([ |
| 450 | 'myFile' => $file, |
| 451 | 'key' => 'val 01', |
| 452 | 'special' => ['%^&&*^?.. + @214', 'true'], |
| 453 | 'my bool' => 'true', |
| 454 | 'object' => [ 'body' => [ 'key' => 234, 'myBool' => 'true']] |
| 455 | ], $result->body['parameters']); |
| 456 | } |
| 457 | |
| 458 | public function testAdditionalQuery() |
| 459 | { |
| 460 | $additionalQueryParamsUI = ['key0' => [2, 4], 'key5' => 'a']; |
| 461 | $additionalQueryParamsPL = ['key1' => [2, 4], 'key6' => 'b']; |
| 462 | $additionalQueryParamsC = ['key2' => [2, 4], 'key7' => 'c']; |
| 463 | $additionalQueryParamsT = ['key3' => [2, 4], 'key8' => 'd']; |
| 464 | $additionalQueryParamsP = ['key4' => [2, 4], 'key9' => 'e']; |
| 465 | $result = MockHelper::newApiCall() |
| 466 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 467 | ->parameters( |
| 468 | AdditionalQueryParams::init($additionalQueryParamsUI)->unIndexed(), |
| 469 | AdditionalQueryParams::init($additionalQueryParamsPL)->plain(), |
| 470 | AdditionalQueryParams::init($additionalQueryParamsC)->commaSeparated(), |
| 471 | AdditionalQueryParams::init($additionalQueryParamsT)->tabSeparated(), |
| 472 | AdditionalQueryParams::init($additionalQueryParamsP)->pipeSeparated() |
| 473 | )) |
| 474 | ->responseHandler(MockHelper::responseHandler() |
| 475 | ->type(MockClass::class)) |
| 476 | ->execute(); |
| 477 | $this->assertInstanceOf(MockClass::class, $result); |
| 478 | $query = self::convertQueryIntoArray(explode('?', $result->body['queryUrl'])[1]); |
| 479 | $this->assertEquals([ |
| 480 | 'key0[]' => '2', |
| 481 | 'key0[]*' => '4', |
| 482 | 'key1' => '2', |
| 483 | 'key1*' => '4', |
| 484 | 'key2' => '2,4', |
| 485 | 'key3' => "2\t4", |
| 486 | 'key4' => '2|4', |
| 487 | 'key5' => 'a', |
| 488 | 'key6' => 'b', |
| 489 | 'key7' => 'c', |
| 490 | 'key8' => 'd', |
| 491 | 'key9' => 'e', |
| 492 | ], $query); |
| 493 | } |
| 494 | |
| 495 | public function testSendMultipleQuery() |
| 496 | { |
| 497 | $additionalQueryParams = [ |
| 498 | 'keyH' => [2, 4], |
| 499 | 'newKey' => 'asad' |
| 500 | ]; |
| 501 | $result = MockHelper::newApiCall() |
| 502 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 503 | ->parameters( |
| 504 | QueryParam::init('key A', 'val 1'), |
| 505 | QueryParam::init('keyB', new MockClass([])), |
| 506 | QueryParam::init('keyB2', [2, 4]), |
| 507 | QueryParam::init('keyC', new MockClass([23, 24, 'asad'])), |
| 508 | QueryParam::init('keyD', new MockClass([23, 24]))->unIndexed(), |
| 509 | QueryParam::init('keyE', new MockClass([true, false, null]))->plain(), |
| 510 | QueryParam::init('keyF', new MockClass(['A', 'B', 'C']))->commaSeparated(), |
| 511 | QueryParam::init('keyG', new MockClass(['A', 'B', 'C']))->tabSeparated(), |
| 512 | QueryParam::init('keyH', new MockClass(['A', 'B', 'C']))->pipeSeparated(), |
| 513 | QueryParam::init('keyI', new MockClass(['A', 'B', new MockClass([1])]))->pipeSeparated(), |
| 514 | QueryParam::init('keyJ', new MockClass(['innerKey1' => 'A', 'innerKey2' => 'B']))->pipeSeparated(), |
| 515 | QueryParam::init('keyK', new MockChild3("body", ['innerKey1' => 'A'])) |
| 516 | ->commaSeparated(), |
| 517 | AdditionalQueryParams::init($additionalQueryParams) |
| 518 | )) |
| 519 | ->responseHandler(MockHelper::responseHandler() |
| 520 | ->type(MockClass::class)) |
| 521 | ->execute(); |
| 522 | $this->assertInstanceOf(MockClass::class, $result); |
| 523 | $query = self::convertQueryIntoArray(explode('?', $result->body['queryUrl'])[1]); |
| 524 | $this->assertEquals([ |
| 525 | 'key A' => 'val 1', |
| 526 | 'keyB2[0]' => '2', |
| 527 | 'keyB2[1]' => '4', |
| 528 | 'keyC[body][0]' => '23', |
| 529 | 'keyC[body][1]' => '24', |
| 530 | 'keyC[body][2]' => 'asad', |
| 531 | 'keyD[body][]' => '23', |
| 532 | 'keyD[body][]*' => '24', |
| 533 | 'keyE[body]' => 'true', |
| 534 | 'keyE[body]*' => 'false', |
| 535 | 'keyF[body]' => 'A,B,C', |
| 536 | 'keyG[body]' => "A\tB\tC", |
| 537 | 'keyH[body]' => 'A|B|C', |
| 538 | 'keyI[body]' => 'A|B', |
| 539 | 'keyI[body][2][body]' => '1', |
| 540 | 'keyJ[body][innerKey1]' => 'A', |
| 541 | 'keyJ[body][innerKey2]' => 'B', |
| 542 | 'keyH[0]' => '2', |
| 543 | 'keyH[1]' => '4', |
| 544 | 'newKey' => 'asad' |
| 545 | ], $query); |
| 546 | } |
| 547 | |
| 548 | public function testAdditionalForm() |
| 549 | { |
| 550 | $additionalFormParamsUI = ['key0' => [2, 4], 'key2' => 'a']; |
| 551 | $additionalFormParamsPL = ['key1' => [2, 4], 'key3' => 'b']; |
| 552 | $result = MockHelper::newApiCall() |
| 553 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 554 | ->parameters( |
| 555 | AdditionalFormParams::init($additionalFormParamsUI)->unIndexed(), |
| 556 | AdditionalFormParams::init($additionalFormParamsPL)->plain() |
| 557 | )) |
| 558 | ->responseHandler(MockHelper::responseHandler() |
| 559 | ->type(MockClass::class)) |
| 560 | ->execute(); |
| 561 | $this->assertInstanceOf(MockClass::class, $result); |
| 562 | $this->assertEquals([ |
| 563 | 'key0' => 'key0%5B%5D=2&key0%5B%5D=4', |
| 564 | 'key1' => 'key1=2&key1=4', |
| 565 | 'key2' => 'key2=a', |
| 566 | 'key3' => 'key3=b', |
| 567 | ], $result->body['parametersEncoded']); |
| 568 | $this->assertEquals([ |
| 569 | 'key0' => [2, 4], |
| 570 | 'key1' => [2, 4], |
| 571 | 'key2' => 'a', |
| 572 | 'key3' => 'b', |
| 573 | ], $result->body['parameters']); |
| 574 | } |
| 575 | |
| 576 | public function testSendMultipleForm() |
| 577 | { |
| 578 | $additionalFormParams = [ |
| 579 | 'keyH' => [2, 4], |
| 580 | 'newKey' => 'asad' |
| 581 | ]; |
| 582 | $result = MockHelper::newApiCall() |
| 583 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 584 | ->parameters( |
| 585 | FormParam::init('key A', 'val 1'), |
| 586 | FormParam::init('keyB', new MockClass([])), |
| 587 | FormParam::init('keyB2', [2, 4]), |
| 588 | FormParam::init('keyB3', ['key1' => 2, 'key2' => 4]), |
| 589 | FormParam::init('keyC', new MockClass([23, 24, 'asad'])), |
| 590 | FormParam::init('keyD', new MockClass([23, 24]))->unIndexed(), |
| 591 | FormParam::init('keyE', new MockClass([23, 24, new MockClass([1])]))->unIndexed(), |
| 592 | FormParam::init('keyF', new MockClass([true, false, null]))->plain(), |
| 593 | FormParam::init('keyG', new MockClass(['innerKey1' => 'A', 'innerKey2' => 'B']))->plain(), |
| 594 | AdditionalFormParams::init($additionalFormParams)->unIndexed() |
| 595 | )) |
| 596 | ->responseHandler(MockHelper::responseHandler() |
| 597 | ->type(MockClass::class)) |
| 598 | ->execute(); |
| 599 | $this->assertInstanceOf(MockClass::class, $result); |
| 600 | $this->assertEquals([ |
| 601 | 'key A' => 'key+A=val+1', |
| 602 | 'keyB2' => 'keyB2%5B0%5D=2&keyB2%5B1%5D=4', |
| 603 | 'keyB3' => 'keyB3%5Bkey1%5D=2&keyB3%5Bkey2%5D=4', |
| 604 | 'keyC' => 'keyC%5Bbody%5D%5B0%5D=23&keyC%5Bbody%5D%5B1%5D=24&keyC%5Bbody%5D%5B2%5D=asad', |
| 605 | 'keyD' => 'keyD%5Bbody%5D%5B%5D=23&keyD%5Bbody%5D%5B%5D=24', |
| 606 | 'keyE' => 'keyE%5Bbody%5D%5B%5D=23&keyE%5Bbody%5D%5B%5D=24&keyE%5Bbody%5D%5B2%5D%5Bbody%5D%5B%5D=1', |
| 607 | 'keyF' => 'keyF%5Bbody%5D=true&keyF%5Bbody%5D=false', |
| 608 | 'keyG' => 'keyG%5Bbody%5D%5BinnerKey1%5D=A&keyG%5Bbody%5D%5BinnerKey2%5D=B', |
| 609 | 'keyH' => 'keyH%5B%5D=2&keyH%5B%5D=4', |
| 610 | 'newKey' => 'newKey=asad' |
| 611 | ], $result->body['parametersEncoded']); |
| 612 | $this->assertEquals([ |
| 613 | 'key A' => 'val 1', |
| 614 | 'keyB2' => [2, 4], |
| 615 | 'keyB3' => ['key1' => 2, 'key2' => 4], |
| 616 | 'keyC' => ['body' => [23, 24, 'asad']], |
| 617 | 'keyD' => ['body' => [23, 24]], |
| 618 | 'keyE' => ['body' => [23, 24, ['body' => [1]]]], |
| 619 | 'keyF' => ['body' => ['true', 'false', null]], |
| 620 | 'keyG' => ['body' => ['innerKey1' => 'A', 'innerKey2' => 'B']], |
| 621 | 'keyH' => [2, 4], |
| 622 | 'newKey' => 'asad' |
| 623 | ], $result->body['parameters']); |
| 624 | } |
| 625 | |
| 626 | public function testSendBodyParam() |
| 627 | { |
| 628 | $result = MockHelper::newApiCall() |
| 629 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 630 | ->parameters(BodyParam::init('this is string'))) |
| 631 | ->responseHandler(MockHelper::responseHandler() |
| 632 | ->type(MockClass::class)) |
| 633 | ->execute(); |
| 634 | $this->assertInstanceOf(MockClass::class, $result); |
| 635 | $this->assertEquals(Format::SCALAR, $result->body['headers']['content-type']); |
| 636 | $this->assertEquals('this is string', $result->body['body']); |
| 637 | } |
| 638 | |
| 639 | public function testSendBodyParamObject() |
| 640 | { |
| 641 | $result = MockHelper::newApiCall() |
| 642 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 643 | ->parameters(BodyParam::init(new MockClass([])))) |
| 644 | ->responseHandler(MockHelper::responseHandler() |
| 645 | ->type(MockClass::class)) |
| 646 | ->execute(); |
| 647 | $this->assertInstanceOf(MockClass::class, $result); |
| 648 | $this->assertEquals(Format::JSON, $result->body['headers']['content-type']); |
| 649 | $this->assertEquals('{"body":[]}', $result->body['body']); |
| 650 | } |
| 651 | |
| 652 | public function testSendBodyParamFile() |
| 653 | { |
| 654 | $result = MockHelper::newApiCall() |
| 655 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 656 | ->parameters(BodyParam::init(MockHelper::getFileWrapper()))) |
| 657 | ->responseHandler(MockHelper::responseHandler() |
| 658 | ->type(MockClass::class)) |
| 659 | ->execute(); |
| 660 | $this->assertInstanceOf(MockClass::class, $result); |
| 661 | $this->assertEquals('application/octet-stream', $result->body['headers']['content-type']); |
| 662 | $this->assertEquals('This test file is created to test CoreFileWrapper functionality', $result->body['body']); |
| 663 | } |
| 664 | |
| 665 | public function testSendMultipleBodyParams() |
| 666 | { |
| 667 | $result = MockHelper::newApiCall() |
| 668 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 669 | ->parameters( |
| 670 | BodyParam::initWrapped('key1', 'this is string'), |
| 671 | BodyParam::initWrapped('key2', new MockClass(['asad' => 'item1', 'ali' => 'item2'])) |
| 672 | )) |
| 673 | ->responseHandler(MockHelper::responseHandler() |
| 674 | ->type(MockClass::class)) |
| 675 | ->execute(); |
| 676 | $this->assertInstanceOf(MockClass::class, $result); |
| 677 | $this->assertEquals(Format::JSON, $result->body['headers']['content-type']); |
| 678 | $this->assertEquals( |
| 679 | '{"key1":"this is string","key2":{"body":{"asad":"item1","ali":"item2"}}}', |
| 680 | $result->body['body'] |
| 681 | ); |
| 682 | } |
| 683 | |
| 684 | public function testSendXMLBodyParam() |
| 685 | { |
| 686 | $result = MockHelper::newApiCall() |
| 687 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 688 | ->parameters(BodyParam::init('this is string')) |
| 689 | ->bodyXml('myRoot')) |
| 690 | ->responseHandler(MockHelper::responseHandler() |
| 691 | ->type(MockClass::class)) |
| 692 | ->execute(); |
| 693 | $this->assertInstanceOf(MockClass::class, $result); |
| 694 | $this->assertEquals(Format::XML, $result->body['headers']['content-type']); |
| 695 | $this->assertEquals( |
| 696 | '<?xml version="1.0"?>' . "\n" . '<myRoot>this is string</myRoot>' . "\n", |
| 697 | $result->body['body'] |
| 698 | ); |
| 699 | } |
| 700 | |
| 701 | public function testSendXMLBodyParamModel() |
| 702 | { |
| 703 | $result = MockHelper::newApiCall() |
| 704 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 705 | ->parameters(BodyParam::init(new MockClass([34, 'asad']))) |
| 706 | ->bodyXml('mockClass')) |
| 707 | ->responseHandler(MockHelper::responseHandler() |
| 708 | ->type(MockClass::class)) |
| 709 | ->execute(); |
| 710 | $this->assertInstanceOf(MockClass::class, $result); |
| 711 | $this->assertEquals(Format::XML, $result->body['headers']['content-type']); |
| 712 | $this->assertEquals("<?xml version=\"1.0\"?>\n" . |
| 713 | "<mockClass attr=\"this is attribute\">" . |
| 714 | "<body>34</body><body>asad</body><new1>this is new</new1><new2><entry key=\"key1\">val1</entry>" . |
| 715 | "<entry key=\"key2\">val2</entry></new2></mockClass>\n", $result->body['body']); |
| 716 | } |
| 717 | |
| 718 | public function testSendXMLNullBodyParam() |
| 719 | { |
| 720 | $result = MockHelper::newApiCall() |
| 721 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 722 | ->parameters(BodyParam::init(null)) |
| 723 | ->bodyXml('myRoot')) |
| 724 | ->responseHandler(MockHelper::responseHandler() |
| 725 | ->type(MockClass::class)) |
| 726 | ->execute(); |
| 727 | $this->assertInstanceOf(MockClass::class, $result); |
| 728 | $this->assertNull($result->body['body']); |
| 729 | } |
| 730 | |
| 731 | public function testSendXMLArrayBodyParam() |
| 732 | { |
| 733 | $result = MockHelper::newApiCall() |
| 734 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 735 | ->parameters(BodyParam::init(['this is string', 345, false, null])) |
| 736 | ->bodyXmlArray('myRoot', 'innerItem')) |
| 737 | ->responseHandler(MockHelper::responseHandler() |
| 738 | ->type(MockClass::class)) |
| 739 | ->execute(); |
| 740 | $this->assertInstanceOf(MockClass::class, $result); |
| 741 | $this->assertEquals(Format::XML, $result->body['headers']['content-type']); |
| 742 | $this->assertEquals( |
| 743 | '<?xml version="1.0"?>' . "\n" . '<myRoot><innerItem>this is string</innerItem>' . |
| 744 | '<innerItem>345</innerItem><innerItem>false</innerItem></myRoot>' . "\n", |
| 745 | $result->body['body'] |
| 746 | ); |
| 747 | } |
| 748 | |
| 749 | public function testSendMultipleXMLBodyParams() |
| 750 | { |
| 751 | $result = MockHelper::newApiCall() |
| 752 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}')) |
| 753 | ->parameters( |
| 754 | BodyParam::initWrapped('key1', 'this is string'), |
| 755 | BodyParam::initWrapped('key2', 'this is item 2'), |
| 756 | BodyParam::initWrapped('key3', null) |
| 757 | ) |
| 758 | ->bodyXmlMap('bodyRoot')) |
| 759 | ->responseHandler(MockHelper::responseHandler() |
| 760 | ->type(MockClass::class)) |
| 761 | ->execute(); |
| 762 | $this->assertInstanceOf(MockClass::class, $result); |
| 763 | $this->assertEquals(Format::XML, $result->body['headers']['content-type']); |
| 764 | $this->assertEquals( |
| 765 | '<?xml version="1.0"?>' . "\n" . '<bodyRoot><entry key="key1">this is string</entry>' . |
| 766 | '<entry key="key2">this is item 2</entry></bodyRoot>' . "\n", |
| 767 | $result->body['body'] |
| 768 | ); |
| 769 | } |
| 770 | |
| 771 | public function testReceiveByWrongType() |
| 772 | { |
| 773 | $this->expectException(InvalidArgumentException::class); |
| 774 | $this->expectExceptionMessage('JsonMapper::mapClass() requires second argument to be a class name, ' . |
| 775 | 'InvalidClass given.'); |
| 776 | MockHelper::newApiCall() |
| 777 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}'))) |
| 778 | ->responseHandler(MockHelper::responseHandler() |
| 779 | ->type('InvalidClass')) |
| 780 | ->execute(); |
| 781 | } |
| 782 | |
| 783 | /** |
| 784 | * @throws Exception |
| 785 | */ |
| 786 | public function fakeSerializeBy($argument) |
| 787 | { |
| 788 | throw new Exception('Invalid argument found'); |
| 789 | } |
| 790 | |
| 791 | public function testReceiveByWrongDeserializerMethod() |
| 792 | { |
| 793 | $this->expectException(Exception::class); |
| 794 | $this->expectExceptionMessage('Invalid argument found'); |
| 795 | MockHelper::newApiCall() |
| 796 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}'))) |
| 797 | ->responseHandler(MockHelper::responseHandler() |
| 798 | ->deserializerMethod([$this, 'fakeSerializeBy'])) |
| 799 | ->execute(); |
| 800 | } |
| 801 | |
| 802 | public function testReceiveByWrongAnyOfTypeGroup() |
| 803 | { |
| 804 | $this->expectException(AnyOfValidationException::class); |
| 805 | $this->expectExceptionMessage('We could not match any acceptable type from (MockCla,string) on: '); |
| 806 | MockHelper::newApiCall() |
| 807 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}'))) |
| 808 | ->responseHandler(MockHelper::responseHandler() |
| 809 | ->typeGroup('anyOf(MockCla,string)')) |
| 810 | ->execute(); |
| 811 | } |
| 812 | |
| 813 | public function testReceiveByWrongOneOfTypeGroup() |
| 814 | { |
| 815 | $this->expectException(OneOfValidationException::class); |
| 816 | $this->expectExceptionMessage('There are more than one matching types i.e. { object and MockClass } on: '); |
| 817 | MockHelper::newApiCall() |
| 818 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}'))) |
| 819 | ->responseHandler(MockHelper::responseHandler() |
| 820 | ->typeGroup('oneOf(MockClass,object)')) |
| 821 | ->execute(); |
| 822 | } |
| 823 | |
| 824 | public function testReceiveByAccurateTypeGroup() |
| 825 | { |
| 826 | $result = MockHelper::newApiCall() |
| 827 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}'))) |
| 828 | ->responseHandler(MockHelper::responseHandler() |
| 829 | ->typeGroup('oneOf(MockClass,string)')) |
| 830 | ->execute(); |
| 831 | |
| 832 | $this->assertInstanceOf(MockClass::class, $result); |
| 833 | } |
| 834 | |
| 835 | public function testReceiveApiResponse() |
| 836 | { |
| 837 | $result = MockHelper::newApiCall() |
| 838 | ->requestBuilder((new RequestBuilder(RequestMethod::POST, '/simple/{tyu}'))) |
| 839 | ->responseHandler(MockHelper::responseHandler() |
| 840 | ->typeGroup('oneOf(MockClass,string)') |
| 841 | ->returnApiResponse()) |
| 842 | ->execute(); |
| 843 | $this->assertInstanceOf(MockApiResponse::class, $result); |
| 844 | $this->assertInstanceOf(MockRequest::class, $result->getRequest()); |
| 845 | $this->assertInstanceOf(MockClass::class, $result->getResult()); |
| 846 | $this->assertStringContainsString('{"body":{"httpMethod":"Post","queryUrl":"http:\/\/my\/path:3000\/v1' . |
| 847 | '\/simple\/{tyu}","headers":{"additionalHead1":"headVal1","additionalHead2":"headVal2","user-agent":' . |
| 848 | '"my lang|1.*.*|', $result->getBody()); |
| 849 | $this->assertStringContainsString(',"Accept":"application\/json"' . |
| 850 | '},"parameters":[],"parametersEncoded":[],"parametersMultipart":[],"body":null,' . |
| 851 | '"retryOption":"useGlobalSettings"},"additionalProperties":[]}', $result->getBody()); |
| 852 | $this->assertEquals(200, $result->getStatusCode()); |
| 853 | $this->assertTrue($result->isSuccess()); |
| 854 | $this->assertNull($result->getReasonPhrase()); |
| 855 | } |
| 856 | |
| 857 | public function testResponseMissingInApiResponse() |
| 858 | { |
| 859 | $mockRequest = MockHelper::getClient()->getGlobalRequest()->convert(); |
| 860 | $response = new MockApiResponse($mockRequest, null, null, null, null, null); |
| 861 | $this->assertInstanceOf(MockRequest::class, $response->getRequest()); |
| 862 | $this->assertTrue($response->isError()); |
| 863 | } |
| 864 | |
| 865 | public function testApiResponseWith400() |
| 866 | { |
| 867 | $response = new MockResponse(); |
| 868 | $response->setStatusCode(400); |
| 869 | $response->setBody(self::DUMMY_BODY); |
| 870 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 871 | $result = MockHelper::responseHandler()->type(MockClass::class)->returnApiResponse()->getResult($context); |
| 872 | $this->assertInstanceOf(MockApiResponse::class, $result); |
| 873 | $this->assertEquals(self::DUMMY_BODY, $result->getResult()); |
| 874 | $this->assertTrue($result->isError()); |
| 875 | } |
| 876 | |
| 877 | public function testApiResponseWith100() |
| 878 | { |
| 879 | $response = new MockResponse(); |
| 880 | $response->setStatusCode(100); |
| 881 | $response->setBody(self::DUMMY_BODY); |
| 882 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 883 | $result = MockHelper::responseHandler()->type(MockClass::class)->returnApiResponse()->getResult($context); |
| 884 | $this->assertInstanceOf(MockApiResponse::class, $result); |
| 885 | $this->assertEquals(self::DUMMY_BODY, $result->getResult()); |
| 886 | $this->assertTrue($result->isError()); |
| 887 | } |
| 888 | |
| 889 | public function testApiResponseWithNullOn404() |
| 890 | { |
| 891 | $response = new MockResponse(); |
| 892 | $response->setStatusCode(404); |
| 893 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 894 | $result = MockHelper::responseHandler()->nullOn404()->returnApiResponse()->getResult($context); |
| 895 | $this->assertInstanceOf(MockApiResponse::class, $result); |
| 896 | $this->assertNull($result->getResult()); |
| 897 | } |
| 898 | |
| 899 | public function testNullOn404() |
| 900 | { |
| 901 | $response = new MockResponse(); |
| 902 | $response->setStatusCode(404); |
| 903 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 904 | $result = MockHelper::responseHandler()->nullOn404()->getResult($context); |
| 905 | $this->assertNull($result); |
| 906 | } |
| 907 | |
| 908 | public function testStatus404WithoutNullOn404() |
| 909 | { |
| 910 | $this->expectException(MockException::class); |
| 911 | $this->expectExceptionMessage('HTTP Response Not OK'); |
| 912 | $response = new MockResponse(); |
| 913 | $response->setStatusCode(404); |
| 914 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 915 | MockHelper::responseHandler()->getResult($context); |
| 916 | } |
| 917 | |
| 918 | public function testNullOn404WithStatus400() |
| 919 | { |
| 920 | $this->expectException(MockException::class); |
| 921 | $this->expectExceptionMessage('Exception num 1'); |
| 922 | $response = new MockResponse(); |
| 923 | $response->setStatusCode(400); |
| 924 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 925 | MockHelper::responseHandler()->nullOn404()->getResult($context); |
| 926 | } |
| 927 | |
| 928 | public function testNullableTypeWithMissingBody() |
| 929 | { |
| 930 | $response = new MockResponse(); |
| 931 | $response->setStatusCode(200); |
| 932 | $response->setBody(''); |
| 933 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 934 | $result = MockHelper::responseHandler()->nullableType()->getResult($context); |
| 935 | $this->assertNull($result); |
| 936 | } |
| 937 | |
| 938 | public function testNullableTypeWithMissingBodyAndApiResponse() |
| 939 | { |
| 940 | $response = new MockResponse(); |
| 941 | $response->setStatusCode(200); |
| 942 | $response->setBody(''); |
| 943 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 944 | $result = MockHelper::responseHandler()->nullableType()->returnApiResponse()->getResult($context); |
| 945 | $this->assertInstanceOf(MockApiResponse::class, $result); |
| 946 | $this->assertNull($result->getResult()); |
| 947 | $this->assertFalse($result->isError()); |
| 948 | } |
| 949 | |
| 950 | public function testNullableTypeWithNullBody() |
| 951 | { |
| 952 | $response = new MockResponse(); |
| 953 | $response->setStatusCode(200); |
| 954 | $response->setBody(null); |
| 955 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 956 | $result = MockHelper::responseHandler()->nullableType()->getResult($context); |
| 957 | $this->assertNull($result); |
| 958 | } |
| 959 | |
| 960 | public function testNullableTypeWithWhiteSpacedBody() |
| 961 | { |
| 962 | $response = new MockResponse(); |
| 963 | $response->setStatusCode(200); |
| 964 | $response->setBody(' '); |
| 965 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 966 | $result = MockHelper::responseHandler()->nullableType()->getResult($context); |
| 967 | $this->assertNull($result); |
| 968 | } |
| 969 | |
| 970 | public function testNullableTypeWithBody() |
| 971 | { |
| 972 | $response = new MockResponse(); |
| 973 | $response->setStatusCode(200); |
| 974 | $response->setBody(214); |
| 975 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 976 | $result = MockHelper::responseHandler()->nullableType()->getResult($context); |
| 977 | $this->assertEquals(214, $result); |
| 978 | } |
| 979 | |
| 980 | public function testNonNullableTypeWithMissingBody() |
| 981 | { |
| 982 | $response = new MockResponse(); |
| 983 | $response->setStatusCode(200); |
| 984 | $response->setBody(''); |
| 985 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 986 | $result = MockHelper::responseHandler()->getResult($context); |
| 987 | $this->assertEquals('', $result); |
| 988 | } |
| 989 | |
| 990 | public function testNonNullableTypeWithWhiteSpacedBody() |
| 991 | { |
| 992 | $response = new MockResponse(); |
| 993 | $response->setStatusCode(200); |
| 994 | $response->setBody(' '); |
| 995 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 996 | $result = MockHelper::responseHandler()->getResult($context); |
| 997 | $this->assertEquals(' ', $result); |
| 998 | } |
| 999 | |
| 1000 | public function testNonNullableTypeWithMissingBodyAndApiResponse() |
| 1001 | { |
| 1002 | $response = new MockResponse(); |
| 1003 | $response->setStatusCode(200); |
| 1004 | $response->setBody(''); |
| 1005 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1006 | $result = MockHelper::responseHandler()->returnApiResponse()->getResult($context); |
| 1007 | $this->assertInstanceOf(MockApiResponse::class, $result); |
| 1008 | $this->assertEquals('', $result->getResult()); |
| 1009 | $this->assertFalse($result->isError()); |
| 1010 | } |
| 1011 | |
| 1012 | public function testNonNullableTypeWithNullBody() |
| 1013 | { |
| 1014 | $response = new MockResponse(); |
| 1015 | $response->setStatusCode(200); |
| 1016 | $response->setBody(null); |
| 1017 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1018 | $result = MockHelper::responseHandler()->getResult($context); |
| 1019 | $this->assertNull($result); |
| 1020 | } |
| 1021 | |
| 1022 | public function testNonNullableTypeWithBody() |
| 1023 | { |
| 1024 | $response = new MockResponse(); |
| 1025 | $response->setStatusCode(200); |
| 1026 | $response->setBody(214); |
| 1027 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1028 | $result = MockHelper::responseHandler()->getResult($context); |
| 1029 | $this->assertEquals(214, $result); |
| 1030 | } |
| 1031 | |
| 1032 | public function testGlobalMockException() |
| 1033 | { |
| 1034 | $this->expectException(MockException::class); |
| 1035 | $this->expectExceptionMessage('HTTP Response Not OK'); |
| 1036 | $response = new MockResponse(); |
| 1037 | $response->setStatusCode(500); |
| 1038 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1039 | MockHelper::responseHandler()->getResult($context); |
| 1040 | } |
| 1041 | |
| 1042 | public function testJsonPointersWithJsonArrayTypePointer() |
| 1043 | { |
| 1044 | $this->expectExceptionMessage( |
| 1045 | 'Failed to make request: 409-headerValue, 409 - Failed to make the request 409 status code' |
| 1046 | ); |
| 1047 | $response = new MockResponse(); |
| 1048 | $response->setHeaders(["header key" => "headerValue"]); |
| 1049 | $response->setStatusCode(409); |
| 1050 | $response->setBody('{"OtherJsonField":2,"AnotherJsonField":{"Name":"name","Value":3},' . |
| 1051 | '"Error":[{"Code":409,"Detail":"Failed to make the request 409 status code"},' . |
| 1052 | '{"Code":410,"Detail":"Failed to make the request 410 status code"}]}'); |
| 1053 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1054 | MockHelper::responseHandler() |
| 1055 | ->throwErrorOn("409", ErrorType::initWithErrorTemplate( |
| 1056 | 'Failed to make request: {$statusCode}-{$response.header.header Key}, {$response.body#/Error/0/Code}' . |
| 1057 | ' - {$response.body#/Error/0/Detail}' |
| 1058 | )) |
| 1059 | ->getResult($context); |
| 1060 | } |
| 1061 | |
| 1062 | public function testJsonPointersWithJsonArray() |
| 1063 | { |
| 1064 | $this->expectExceptionMessage( |
| 1065 | 'Failed to make request: 409-headerValue, 409 - Failed to make the request 409 status code' |
| 1066 | ); |
| 1067 | $response = new MockResponse(); |
| 1068 | $response->setHeaders(["header key" => "headerValue"]); |
| 1069 | $response->setStatusCode(409); |
| 1070 | $response->setBody('[{"OtherJsonField":2,"AnotherJsonField":{"Name":"name","Value":3},' . |
| 1071 | '"Error":[{"Code":409,"Detail":"Failed to make the request 409 status code"},' . |
| 1072 | '{"Code":410,"Detail":"Failed to make the request 410 status code"}]}]'); |
| 1073 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1074 | MockHelper::responseHandler() |
| 1075 | ->throwErrorOn("409", ErrorType::initWithErrorTemplate( |
| 1076 | 'Failed to make request: {$statusCode}-{$response.header.header Key},' . |
| 1077 | ' {$response.body#/0/Error/0/Code}' . |
| 1078 | ' - {$response.body#/0/Error/0/Detail}' |
| 1079 | )) |
| 1080 | ->getResult($context); |
| 1081 | } |
| 1082 | |
| 1083 | public function testJsonPointersWithNullJson() |
| 1084 | { |
| 1085 | $this->expectExceptionMessage( |
| 1086 | 'Failed to make request: 409-headerValue, - ' |
| 1087 | ); |
| 1088 | $response = new MockResponse(); |
| 1089 | $response->setHeaders(["header key" => "headerValue"]); |
| 1090 | $response->setStatusCode(409); |
| 1091 | $response->setBody(null); |
| 1092 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1093 | MockHelper::responseHandler() |
| 1094 | ->throwErrorOn("409", ErrorType::initWithErrorTemplate( |
| 1095 | 'Failed to make request: {$statusCode}-{$response.header.header Key},' . |
| 1096 | ' {$response.body#/0/Error/0/Code}' . |
| 1097 | ' - {$response.body#/0/Error/0/Detail}' |
| 1098 | )) |
| 1099 | ->getResult($context); |
| 1100 | } |
| 1101 | |
| 1102 | public function testJsonPointersWithInvalidJson() |
| 1103 | { |
| 1104 | $this->expectExceptionMessage( |
| 1105 | 'Failed to make request: 409-headerValue, - ' |
| 1106 | ); |
| 1107 | $response = new MockResponse(); |
| 1108 | $response->setHeaders(["header key" => "headerValue"]); |
| 1109 | $response->setStatusCode(409); |
| 1110 | $response->setBody('{"invalidJson"}'); |
| 1111 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1112 | MockHelper::responseHandler() |
| 1113 | ->throwErrorOn("409", ErrorType::initWithErrorTemplate( |
| 1114 | 'Failed to make request: {$statusCode}-{$response.header.header Key},' . |
| 1115 | ' {$response.body#/0/Error/0/Code}' . |
| 1116 | ' - {$response.body#/0/Error/0/Detail}' |
| 1117 | )) |
| 1118 | ->getResult($context); |
| 1119 | } |
| 1120 | |
| 1121 | public function testJsonPointersWithInvalidJsonAndEmptyPointer() |
| 1122 | { |
| 1123 | $this->expectExceptionMessage( |
| 1124 | 'Failed to make request: 409-headerValue, - ' |
| 1125 | ); |
| 1126 | $response = new MockResponse(); |
| 1127 | $response->setHeaders(["header key" => "headerValue"]); |
| 1128 | $response->setStatusCode(409); |
| 1129 | $response->setBody('{"invalidJson"}'); |
| 1130 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1131 | MockHelper::responseHandler() |
| 1132 | ->throwErrorOn("409", ErrorType::initWithErrorTemplate( |
| 1133 | 'Failed to make request: {$statusCode}-{$response.header.header Key},' . |
| 1134 | ' {$response.body#}' . |
| 1135 | ' - {$response.body#/0/Error/0/Detail}' |
| 1136 | )) |
| 1137 | ->getResult($context); |
| 1138 | } |
| 1139 | |
| 1140 | public function testJsonPointersWithInvalidPointer() |
| 1141 | { |
| 1142 | $this->expectExceptionMessage( |
| 1143 | 'Failed to make request: 409-headerValue, - ' |
| 1144 | ); |
| 1145 | $response = new MockResponse(); |
| 1146 | $response->setHeaders(["header key" => "headerValue"]); |
| 1147 | $response->setStatusCode(409); |
| 1148 | $response->setBody('{"key":"value"}'); |
| 1149 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1150 | MockHelper::responseHandler() |
| 1151 | ->throwErrorOn("409", ErrorType::initWithErrorTemplate( |
| 1152 | 'Failed to make request: {$statusCode}-{$response.header.header Key},' . |
| 1153 | ' {$response.body#/0/Error/0/Code}' . |
| 1154 | ' - {$response.body#////0/Error/0/Detail}' |
| 1155 | )) |
| 1156 | ->getResult($context); |
| 1157 | } |
| 1158 | |
| 1159 | public function testJsonPointersWithNativeResponse() |
| 1160 | { |
| 1161 | $this->expectExceptionMessage( |
| 1162 | 'Failed to make request: 409-headerValue, - ' |
| 1163 | ); |
| 1164 | $response = new MockResponse(); |
| 1165 | $response->setHeaders(["header key" => "headerValue"]); |
| 1166 | $response->setStatusCode(409); |
| 1167 | $response->setBody(10); |
| 1168 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1169 | MockHelper::responseHandler() |
| 1170 | ->throwErrorOn("409", ErrorType::initWithErrorTemplate( |
| 1171 | 'Failed to make request: {$statusCode}-{$response.header.header Key},' . |
| 1172 | ' {$response.body#/0/Error/0/Code}' . |
| 1173 | ' - {$response.body#/0/Error/0/Detail}' |
| 1174 | )) |
| 1175 | ->getResult($context); |
| 1176 | } |
| 1177 | |
| 1178 | public function testJsonPointersWithJsonMapTypePointer() |
| 1179 | { |
| 1180 | $this->expectExceptionMessage( |
| 1181 | 'Failed to make request: 410, 410 - Failed to make the request 410 status code - false' |
| 1182 | ); |
| 1183 | $response = new MockResponse(); |
| 1184 | $response->setStatusCode(410); |
| 1185 | $response->setBody( |
| 1186 | '{"OtherJsonField":2,"AnotherJsonField":{"Name":"name","Value":3},' . |
| 1187 | '"Error":{"409":{"Code":409,"Detail":"Failed to make the request 409 status code"},' . |
| 1188 | '"410":{"Code":410,"Detail":"Failed to make the request 410 status code","IsSuccess":false}}}' |
| 1189 | ); |
| 1190 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1191 | MockHelper::responseHandler() |
| 1192 | ->throwErrorOn("410", ErrorType::initWithErrorTemplate( |
| 1193 | 'Failed to make request: {$statusCode}, ' . |
| 1194 | '{$response.body#/Error/410/Code} - {$response.body#/Error/410/Detail} - ' . |
| 1195 | '{$response.body#/Error/410/IsSuccess}' |
| 1196 | )) |
| 1197 | ->getResult($context); |
| 1198 | } |
| 1199 | |
| 1200 | public function testErrorTypeNoJsonPointer() |
| 1201 | { |
| 1202 | $this->expectExceptionMessage( |
| 1203 | 'Failed to make request: 410, {"OtherJsonField":2,"AnotherJsonField":' . |
| 1204 | '{"Name":"name","Value":3},"Error":{"409":{"Code":409,"Detail":"Failed' . |
| 1205 | ' to make the request 409 status code"},"410":{"Code":410,"Detail":"Failed' . |
| 1206 | ' to make the request 410 status code"}}}' |
| 1207 | ); |
| 1208 | $response = new MockResponse(); |
| 1209 | $response->setStatusCode(410); |
| 1210 | $response->setBody(CoreHelper::deserialize('{"OtherJsonField":2,"AnotherJsonField":{"Name":"name","Value":3},' . |
| 1211 | '"Error":{"409":{"Code":409,"Detail":"Failed to make the request 409 status code"}' . |
| 1212 | ',"410":{"Code":410,"Detail":"Failed to make the request 410 status code"}}}')); |
| 1213 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1214 | MockHelper::responseHandler() |
| 1215 | ->throwErrorOn("410", ErrorType::initWithErrorTemplate( |
| 1216 | 'Failed to make request: {$statusCode}, {$response.body}' |
| 1217 | )) |
| 1218 | ->getResult($context); |
| 1219 | } |
| 1220 | |
| 1221 | public function testJsonPointersWithValueObject() |
| 1222 | { |
| 1223 | $this->expectExceptionMessage( |
| 1224 | 'Failed to make request: 409, 409 - {"Value":"Failed to make the request 409 status code"}' |
| 1225 | ); |
| 1226 | $response = new MockResponse(); |
| 1227 | $response->setStatusCode(409); |
| 1228 | $response->setBody('{"OtherJsonField":2,"AnotherJsonField":{"Name":"name","Value":3},' . |
| 1229 | '"Error":[{"Code":409,"Detail":{"Value":"Failed to make the request 409 status code"}},' . |
| 1230 | '{"Code":410,"Detail":"Failed to make the request 410 status code"}]}'); |
| 1231 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1232 | MockHelper::responseHandler() |
| 1233 | ->throwErrorOn("409", ErrorType::initWithErrorTemplate( |
| 1234 | 'Failed to make request: {$statusCode}, {$response.body#/Error/0/Code}' . |
| 1235 | ' - {$response.body#/Error/0/Detail}' |
| 1236 | )) |
| 1237 | ->getResult($context); |
| 1238 | } |
| 1239 | |
| 1240 | public function testJsonPointersWithNonExistentValuePointer() |
| 1241 | { |
| 1242 | $this->expectExceptionMessage( |
| 1243 | 'Failed to make request: 409, 409 - ' |
| 1244 | ); |
| 1245 | $response = new MockResponse(); |
| 1246 | $response->setStatusCode(409); |
| 1247 | $response->setBody('{"OtherJsonField":2,"AnotherJsonField":{"Name":"name","Value":3},' . |
| 1248 | '"Error":[{"Code":409,"Detail":{"Value":"Failed to make the request 409 status code"}},' . |
| 1249 | '{"Code":410,"Detail":"Failed to make the request 410 status code"}]}'); |
| 1250 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1251 | MockHelper::responseHandler() |
| 1252 | ->throwErrorOn("409", ErrorType::initWithErrorTemplate( |
| 1253 | 'Failed to make request: {$statusCode}, {$response.body#/Error/0/Code}' . |
| 1254 | ' - {$response.body#/Error/0/NonExistentPointer}' |
| 1255 | )) |
| 1256 | ->getResult($context); |
| 1257 | } |
| 1258 | |
| 1259 | public function testErrorTypeNoJsonPointerScalarType() |
| 1260 | { |
| 1261 | $this->expectExceptionMessage( |
| 1262 | 'Failed to make request: 410, 10' |
| 1263 | ); |
| 1264 | $response = new MockResponse(); |
| 1265 | $response->setStatusCode(410); |
| 1266 | $response->setBody(10); |
| 1267 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1268 | MockHelper::responseHandler() |
| 1269 | ->throwErrorOn("410", ErrorType::initWithErrorTemplate( |
| 1270 | 'Failed to make request: {$statusCode}, {$response.body}' |
| 1271 | )) |
| 1272 | ->getResult($context); |
| 1273 | } |
| 1274 | |
| 1275 | public function testErrorTypeNoJsonPointerBooleanType() |
| 1276 | { |
| 1277 | $this->expectExceptionMessage( |
| 1278 | 'Failed to make request: 410, false' |
| 1279 | ); |
| 1280 | $response = new MockResponse(); |
| 1281 | $response->setStatusCode(410); |
| 1282 | $response->setBody(false); |
| 1283 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1284 | MockHelper::responseHandler() |
| 1285 | ->throwErrorOn("410", ErrorType::initWithErrorTemplate( |
| 1286 | 'Failed to make request: {$statusCode}, {$response.body}' |
| 1287 | )) |
| 1288 | ->getResult($context); |
| 1289 | } |
| 1290 | |
| 1291 | public function testErrorTypeTemplateWithEmptyJsonPoint() |
| 1292 | { |
| 1293 | $this->expectExceptionMessage( |
| 1294 | 'Failed to make request: 410, ' |
| 1295 | ); |
| 1296 | $response = new MockResponse(); |
| 1297 | $response->setStatusCode(410); |
| 1298 | $response->setBody('{"SomeKey":"value"}'); |
| 1299 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1300 | MockHelper::responseHandler() |
| 1301 | ->throwErrorOn("410", ErrorType::initWithErrorTemplate( |
| 1302 | 'Failed to make request: {$statusCode}, {$response.body#}' |
| 1303 | )) |
| 1304 | ->getResult($context); |
| 1305 | } |
| 1306 | |
| 1307 | public function testGlobalMockException1() |
| 1308 | { |
| 1309 | $this->expectException(MockException1::class); |
| 1310 | $this->expectExceptionMessage('Exception num 1'); |
| 1311 | $response = new MockResponse(); |
| 1312 | $response->setStatusCode(400); |
| 1313 | $response->setBody([]); |
| 1314 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1315 | MockHelper::responseHandler()->getResult($context); |
| 1316 | } |
| 1317 | |
| 1318 | public function testGlobalMockException3() |
| 1319 | { |
| 1320 | $this->expectException(MockException::class); |
| 1321 | $this->expectExceptionMessage('Exception num 3'); |
| 1322 | $response = new MockResponse(); |
| 1323 | $response->setStatusCode(403); |
| 1324 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1325 | MockHelper::responseHandler()->getResult($context); |
| 1326 | } |
| 1327 | |
| 1328 | public function testLocalMockException3() |
| 1329 | { |
| 1330 | $this->expectException(MockException3::class); |
| 1331 | $this->expectExceptionMessage('Local exception num 3'); |
| 1332 | $response = new MockResponse(); |
| 1333 | $response->setStatusCode(403); |
| 1334 | $response->setBody([]); |
| 1335 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1336 | MockHelper::responseHandler() |
| 1337 | ->throwErrorOn("403", ErrorType::init('Local exception num 3', MockException3::class)) |
| 1338 | ->getResult($context); |
| 1339 | } |
| 1340 | |
| 1341 | public function testLocalMockException3WhenBodyNotObject() |
| 1342 | { |
| 1343 | $this->expectException(MockException::class); |
| 1344 | $this->expectExceptionMessage('Local exception num 3'); |
| 1345 | $response = new MockResponse(); |
| 1346 | $response->setStatusCode(403); |
| 1347 | $response->setBody("some erroneous response"); |
| 1348 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1349 | MockHelper::responseHandler() |
| 1350 | ->throwErrorOn("403", ErrorType::init('Local exception num 3', MockException3::class)) |
| 1351 | ->getResult($context); |
| 1352 | } |
| 1353 | |
| 1354 | public function testDefaultMockException1() |
| 1355 | { |
| 1356 | $this->expectException(MockException1::class); |
| 1357 | $this->expectExceptionMessage('Default exception'); |
| 1358 | $response = new MockResponse(); |
| 1359 | $response->setStatusCode(500); |
| 1360 | $response->setBody([]); |
| 1361 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1362 | MockHelper::responseHandler() |
| 1363 | ->throwErrorOn("403", ErrorType::init('local exception num 3', MockException3::class)) |
| 1364 | ->throwErrorOn("0", ErrorType::init('Default exception', MockException1::class)) |
| 1365 | ->getResult($context); |
| 1366 | } |
| 1367 | |
| 1368 | public function testDefaultExceptionMessage() |
| 1369 | { |
| 1370 | $this->expectException(MockException::class); |
| 1371 | $this->expectExceptionMessage('Default exception'); |
| 1372 | $response = new MockResponse(); |
| 1373 | $response->setStatusCode(500); |
| 1374 | $response->setBody([]); |
| 1375 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1376 | MockHelper::responseHandler() |
| 1377 | ->throwErrorOn("403", ErrorType::init('local exception num 3', MockException3::class)) |
| 1378 | ->throwErrorOn("0", ErrorType::init('Default exception')) |
| 1379 | ->getResult($context); |
| 1380 | } |
| 1381 | |
| 1382 | public function testScalarResponse() |
| 1383 | { |
| 1384 | $response = new MockResponse(); |
| 1385 | $response->setBody("This is string"); |
| 1386 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1387 | $result = MockHelper::responseHandler() |
| 1388 | ->getResult($context); |
| 1389 | $this->assertEquals('This is string', $result); |
| 1390 | } |
| 1391 | |
| 1392 | public function testObjectResponse() |
| 1393 | { |
| 1394 | $response = new MockResponse(); |
| 1395 | $response->setBody(CoreHelper::deserialize('{"key":"value"}', false)); |
| 1396 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1397 | $result = MockHelper::responseHandler() |
| 1398 | ->getResult($context); |
| 1399 | $this->assertEquals(['key' => 'value'], $result); |
| 1400 | } |
| 1401 | |
| 1402 | public function testTypeXmlSimple() |
| 1403 | { |
| 1404 | $response = new MockResponse(); |
| 1405 | $response->setRawBody("<?xml version=\"1.0\"?>\n<root>This is string</root>\n"); |
| 1406 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1407 | $result = MockHelper::responseHandler() |
| 1408 | ->typeXml('string', 'root') |
| 1409 | ->getResult($context); |
| 1410 | $this->assertEquals('This is string', $result); |
| 1411 | } |
| 1412 | |
| 1413 | public function testTypeXml() |
| 1414 | { |
| 1415 | $response = new MockResponse(); |
| 1416 | $response->setRawBody("<?xml version=\"1.0\"?>\n" . |
| 1417 | "<mockClass attr=\"this is attribute\">\n" . |
| 1418 | " <body>34</body>\n" . |
| 1419 | " <body>asad</body>\n" . |
| 1420 | " <new1>this is new</new1>\n" . |
| 1421 | " <new2>\n" . |
| 1422 | " <entry key=\"key1\">val1</entry>\n" . |
| 1423 | " <entry key=\"key2\">val2</entry>\n" . |
| 1424 | " </new2>\n" . |
| 1425 | "</mockClass>\n"); |
| 1426 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1427 | $result = MockHelper::responseHandler() |
| 1428 | ->typeXml(MockClass::class, 'mockClass') |
| 1429 | ->getResult($context); |
| 1430 | $this->assertInstanceOf(MockClass::class, $result); |
| 1431 | $this->assertEquals( |
| 1432 | ["34", "asad", "this is new", ["key1" => "val1", "key2" => "val2"], "this is attribute", null], |
| 1433 | $result->body |
| 1434 | ); |
| 1435 | } |
| 1436 | |
| 1437 | public function testTypeXmlFailure() |
| 1438 | { |
| 1439 | $this->expectException(Exception::class); |
| 1440 | $this->expectExceptionMessage( |
| 1441 | 'Required value not found at XML path "/mockClass/new1[1]" during deserialization.' |
| 1442 | ); |
| 1443 | $response = new MockResponse(); |
| 1444 | $response->setRawBody("<?xml version=\"1.0\"?>\n" . |
| 1445 | "<mockClass attr=\"this is attribute\">\n" . |
| 1446 | " <body>34</body>\n" . |
| 1447 | " <body>asad</body>\n" . |
| 1448 | " <newInvalid>this is new</newInvalid>\n" . |
| 1449 | " <new2>\n" . |
| 1450 | " <entry key=\"key1\">val1</entry>\n" . |
| 1451 | " <entry key=\"key2\">val2</entry>\n" . |
| 1452 | " </new2>\n" . |
| 1453 | "</mockClass>\n"); |
| 1454 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1455 | MockHelper::responseHandler() |
| 1456 | ->typeXml(MockClass::class, 'mockClass') |
| 1457 | ->getResult($context); |
| 1458 | } |
| 1459 | |
| 1460 | public function testTypeInvalidJsonFailure() |
| 1461 | { |
| 1462 | $this->expectException(JsonMapperException::class); |
| 1463 | $this->expectExceptionMessage( |
| 1464 | 'Could not find required constructor arguments for Core\Tests\Mocking\Other\MockClass: body' |
| 1465 | ); |
| 1466 | $response = new MockResponse(); |
| 1467 | $response->setBody(json_decode('{"invalidItem":"wrong item"}')); |
| 1468 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1469 | MockHelper::responseHandler() |
| 1470 | ->type(MockClass::class) |
| 1471 | ->getResult($context); |
| 1472 | } |
| 1473 | |
| 1474 | public function testTypeXmlArray() |
| 1475 | { |
| 1476 | $response = new MockResponse(); |
| 1477 | $response->setRawBody("<?xml version=\"1.0\"?>\n" . |
| 1478 | "<mockClassArray>\n" . |
| 1479 | "<mockClass attr=\"this is attribute\">\n" . |
| 1480 | " <body>34</body>\n" . |
| 1481 | " <body>asad</body>\n" . |
| 1482 | " <new1>this is new</new1>\n" . |
| 1483 | " <new2>\n" . |
| 1484 | " <entry key=\"key1\">val1</entry>\n" . |
| 1485 | " <entry key=\"key2\">val2</entry>\n" . |
| 1486 | " </new2>\n" . |
| 1487 | "</mockClass>\n" . |
| 1488 | "</mockClassArray>\n"); |
| 1489 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1490 | $result = MockHelper::responseHandler() |
| 1491 | ->typeXmlArray(MockClass::class, 'mockClassArray', 'mockClass') |
| 1492 | ->getResult($context); |
| 1493 | $this->assertIsArray($result); |
| 1494 | $this->assertInstanceOf(MockClass::class, $result[0]); |
| 1495 | $this->assertEquals( |
| 1496 | ["34", "asad", "this is new", ["key1" => "val1", "key2" => "val2"], "this is attribute", null], |
| 1497 | $result[0]->body |
| 1498 | ); |
| 1499 | } |
| 1500 | |
| 1501 | public function testTypeXmlMap() |
| 1502 | { |
| 1503 | $response = new MockResponse(); |
| 1504 | $response->setRawBody("<?xml version=\"1.0\"?>\n" . |
| 1505 | "<mockClassMap>\n" . |
| 1506 | "<entry key=\"mockClass\" attr=\"this is attribute\">\n" . |
| 1507 | " <body>34</body>\n" . |
| 1508 | " <body>asad</body>\n" . |
| 1509 | " <new1>this is new</new1>\n" . |
| 1510 | " <new2>\n" . |
| 1511 | " <entry key=\"key1\">val1</entry>\n" . |
| 1512 | " <entry key=\"key2\">val2</entry>\n" . |
| 1513 | " </new2>\n" . |
| 1514 | "</entry>\n" . |
| 1515 | "</mockClassMap>\n"); |
| 1516 | $context = new Context(MockHelper::getClient()->getGlobalRequest(), $response, MockHelper::getClient()); |
| 1517 | $result = MockHelper::responseHandler() |
| 1518 | ->typeXmlMap(MockClass::class, 'mockClassMap') |
| 1519 | ->getResult($context); |
| 1520 | $this->assertIsArray($result); |
| 1521 | $this->assertInstanceOf(MockClass::class, $result['mockClass']); |
| 1522 | $this->assertEquals( |
| 1523 | ["34", "asad", "this is new", ["key1" => "val1", "key2" => "val2"], "this is attribute", null], |
| 1524 | $result['mockClass']->body |
| 1525 | ); |
| 1526 | } |
| 1527 | } |
| 1528 |