prestashop-module/tests/lib/api/RetailcrmExceptionMiddlewareTest.php

183 lines
6.3 KiB
PHP
Raw Normal View History

2021-10-15 11:20:18 +03:00
<?php
/**
* MIT License
*
* Copyright (c) 2021 DIGITAL RETAIL TECHNOLOGIES SL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author DIGITAL RETAIL TECHNOLOGIES SL <mail@simlachat.com>
* @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.
*/
2021-10-15 11:20:18 +03:00
class RetailcrmExceptionMiddlewareTest extends RetailcrmTestCase
{
private $api;
2021-11-03 16:19:39 +07:00
protected function setUp()
2021-10-15 11:20:18 +03:00
{
parent::setUp();
$this->api = RetailcrmTools::getApiClient();
$this->apiMock = $this->getApiMock(
[
'ordersGet',
'ordersEdit',
'ordersPaymentEdit',
'ordersCreate',
'ordersUpload',
]
);
2021-10-15 11:20:18 +03:00
}
public function getRequestsBadParams()
2021-10-15 11:20:18 +03:00
{
return [
[
'method' => 'ordersGet',
'params' => [406, 'idd'],
2021-11-03 16:19:39 +07:00
'errorMsg' => 'Value "idd" for "by" param is not valid. Allowed values are externalId, id.',
2021-10-15 11:20:18 +03:00
],
[
'method' => 'ordersEdit',
'params' => [['id' => 406], 'idd'],
2021-11-03 16:19:39 +07:00
'errorMsg' => 'Value "idd" for "by" param is not valid. Allowed values are externalId, id.',
2021-10-15 11:20:18 +03:00
],
[
'method' => 'ordersEdit',
'params' => [['id' => 406], 'externalId'],
2021-11-03 16:19:39 +07:00
'errorMsg' => 'Order array must contain the "externalId" parameter.',
2021-10-15 11:20:18 +03:00
],
[
'method' => 'ordersFixExternalIds',
'params' => [[]],
2021-11-03 16:19:39 +07:00
'errorMsg' => 'Method parameter must contains at least one IDs pair',
2021-10-15 11:20:18 +03:00
],
[
'method' => 'ordersCreate',
'params' => [[]],
2021-11-03 16:19:39 +07:00
'errorMsg' => 'Parameter `order` must contains a data',
2021-10-15 11:20:18 +03:00
],
[
'method' => 'ordersUpload',
'params' => [[]],
2021-11-03 16:19:39 +07:00
'errorMsg' => 'Parameter `orders` must contains array of the orders',
2021-10-15 11:20:18 +03:00
],
[
'method' => 'ordersPaymentCreate',
'params' => [[]],
2021-11-03 16:19:39 +07:00
'errorMsg' => 'Parameter `payment` must contains a data',
2021-10-15 11:20:18 +03:00
],
[
'method' => 'ordersPaymentEdit',
'params' => [['id' => 406], 'idd'],
2021-11-03 16:19:39 +07:00
'errorMsg' => 'Value "idd" for "by" param is not valid. Allowed values are externalId, id.',
2021-10-15 11:20:18 +03:00
],
[
'method' => 'ordersPaymentEdit',
'params' => [['id' => 406], 'externalId'],
2021-11-03 16:19:39 +07:00
'errorMsg' => 'Order array must contain the "externalId" parameter.',
2021-10-15 11:20:18 +03:00
],
];
}
/**
* @dataProvider getRequestsBadParams
2021-10-15 11:20:18 +03:00
*/
public function testRequestBadParams($method, $params, $errorMsg)
2021-10-15 11:20:18 +03:00
{
/** @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)
{
2021-10-15 11:20:18 +03:00
$this->assertInstanceOf(RetailcrmApiResponse::class, $response);
$this->assertFalse($response->isSuccessful());
$this->assertStringStartsWith('Internal error: ', $response['errorMsg']);
$this->assertStringEndsWith($errorMsg, $response['errorMsg']);
}
}