* @copyright 2021 DIGITAL RETAIL TECHNOLOGIES SL * @license https://opensource.org/licenses/MIT The MIT License * * Don't forget to prefix your containers with your own identifier * to avoid any conflicts with others containers. */ class RetailcrmExceptionMiddlewareTest extends RetailcrmTestCase { private $api; protected function setUp() { parent::setUp(); $this->api = RetailcrmTools::getApiClient(); $this->apiMock = $this->getApiMock( [ 'ordersGet', 'ordersEdit', 'ordersPaymentEdit', 'ordersCreate', 'ordersUpload', ] ); } public function getRequestsBadParams() { return [ [ 'method' => 'ordersGet', 'params' => [406, 'idd'], 'errorMsg' => 'Value "idd" for "by" param is not valid. Allowed values are externalId, id.', ], [ 'method' => 'ordersEdit', 'params' => [['id' => 406], 'idd'], 'errorMsg' => 'Value "idd" for "by" param is not valid. Allowed values are externalId, id.', ], [ 'method' => 'ordersEdit', 'params' => [['id' => 406], 'externalId'], 'errorMsg' => 'Order array must contain the "externalId" parameter.', ], [ 'method' => 'ordersFixExternalIds', 'params' => [[]], 'errorMsg' => 'Method parameter must contains at least one IDs pair', ], [ 'method' => 'ordersCreate', 'params' => [[]], 'errorMsg' => 'Parameter `order` must contains a data', ], [ 'method' => 'ordersUpload', 'params' => [[]], 'errorMsg' => 'Parameter `orders` must contains array of the orders', ], [ 'method' => 'ordersPaymentCreate', 'params' => [[]], 'errorMsg' => 'Parameter `payment` must contains a data', ], [ 'method' => 'ordersPaymentEdit', 'params' => [['id' => 406], 'idd'], 'errorMsg' => 'Value "idd" for "by" param is not valid. Allowed values are externalId, id.', ], [ 'method' => 'ordersPaymentEdit', 'params' => [['id' => 406], 'externalId'], 'errorMsg' => 'Order array must contain the "externalId" parameter.', ], ]; } /** * @dataProvider getRequestsBadParams */ public function testRequestBadParams($method, $params, $errorMsg) { /** @var RetailcrmApiResponse $response */ $response = call_user_func_array([$this->api, $method], $params); $this->checkResponse($response, $errorMsg); } public function getRequestsException() { return [ [ 'method' => 'ordersGet', 'params' => [406, 'id'], ], [ 'method' => 'ordersEdit', 'params' => [['id' => 406], 'id'], ], [ 'method' => 'ordersPaymentEdit', 'params' => [['id' => 406], 'id'], ], [ 'method' => 'ordersCreate', 'params' => [['id' => 407]], ], [ 'method' => 'ordersUpload', 'params' => [[['externalId' => 1]]], ], ]; } /** * @dataProvider getRequestsException */ public function testRequestException($method, $params) { $errorMsg = 'Test exception ' . md5(json_encode([$method, $params])); $this->makeRequestException($method, $params, $errorMsg, function () use ($errorMsg) { throw new Exception($errorMsg); }); if (class_exists('Error')) { $this->makeRequestException($method, $params, $errorMsg, function () use ($errorMsg) { throw new Error($errorMsg); }); } } private function makeRequestException($method, $params, $errorMsg, $return) { $this->apiClientMock->expects($this->any())->method($method)->willReturnCallback($return); /** @var RetailcrmApiResponse $response */ $response = call_user_func_array([$this->apiMock, $method], $params); $this->checkResponse($response, $errorMsg); } private function checkResponse(RetailcrmApiResponse $response, $errorMsg) { $this->assertInstanceOf(RetailcrmApiResponse::class, $response); $this->assertFalse($response->isSuccessful()); $this->assertStringStartsWith('Internal error: ', $response['errorMsg']); $this->assertStringEndsWith($errorMsg, $response['errorMsg']); } }