1
0
mirror of synced 2024-11-28 08:16:06 +03:00

add ordersLoyaltyApply method (#169)

This commit is contained in:
azgalot 2023-06-21 12:57:37 +03:00 committed by GitHub
parent 2efa527817
commit 140e48e6e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 166 additions and 25 deletions

2
.gitignore vendored
View File

@ -16,3 +16,5 @@ phpunit.xml
docker-compose*.yml
.php_cs*
.phpunit*
.env*
!*.dist

View File

@ -15,7 +15,8 @@
"php": ">=5.4.0",
"ext-curl": "*",
"ext-json": "*",
"ext-fileinfo": "*"
"ext-fileinfo": "*",
"symfony/dotenv": "3.4.*"
},
"require-dev": {
"phpunit/phpunit": "6.*",

View File

@ -150,4 +150,51 @@ trait Orders
"POST"
);
}
/**
* Application of loyalty program bonuses
*
* @param array $payment order data
* @param float $bonuses bonuses count
* @param null $site site code
*
* @throws \InvalidArgumentException
* @throws \RetailCrm\Exception\CurlException
* @throws \RetailCrm\Exception\InvalidJsonException
*
* @return \RetailCrm\Response\ApiResponse
*/
public function ordersLoyaltyApply(array $order, float $bonuses, $site = null)
{
if (!count($order)) {
throw new \InvalidArgumentException(
'Parameter `order` must contains a data'
);
}
if (empty($order['id']) && empty($order['externalId'])) {
throw new \InvalidArgumentException(
'Parameter `order` must contain an identifier: `id` or `externalId`'
);
}
if (empty($bonuses)) {
throw new \InvalidArgumentException(
'Specify a different amount of bonuses'
);
}
/* @noinspection PhpUndefinedMethodInspection */
return $this->client->makeRequest(
'/orders/loyalty/apply',
"POST",
$this->fillSite(
$site,
[
'order' => json_encode($order),
'bonuses' => $bonuses,
]
)
);
}
}

5
tests/.env.dist Normal file
View File

@ -0,0 +1,5 @@
RETAILCRM_URL=
RETAILCRM_KEY=
RETAILCRM_VERSION=
RETAILCRM_SITE=
RETAILCRM_USER=

View File

@ -39,7 +39,7 @@ class ApiClientCustomersTest extends TestCase
$externalId = 'c-create-' . time();
$response = $client->customersCreate(array(
$response = $client->request->customersCreate(array(
'firstName' => self::FIRST_NAME,
'externalId' => $externalId,
));
@ -61,7 +61,7 @@ class ApiClientCustomersTest extends TestCase
public function testCreateExceptionEmpty()
{
$client = static::getApiClient();
$client->customersCreate(array());
$client->request->customersCreate(array());
}
/**
@ -76,19 +76,19 @@ class ApiClientCustomersTest extends TestCase
{
$client = static::getApiClient();
$response = $client->customersGet(678678678);
$response = $client->request->customersGet(678678678);
static::assertInstanceOf('RetailCrm\Response\ApiResponse', $response);
static::assertEquals(404, $response->getStatusCode());
static::assertFalse($response->isSuccessful());
$response = $client->customersGet($ids['id'], 'id');
$response = $client->request->customersGet($ids['id'], 'id');
$customerById = $response['customer'];
static::assertInstanceOf('RetailCrm\Response\ApiResponse', $response);
static::assertEquals(200, $response->getStatusCode());
static::assertTrue($response->isSuccessful());
static::assertEquals(self::FIRST_NAME, $response['customer']['firstName']);
$response = $client->customersGet($ids['externalId'], 'externalId');
$response = $client->request->customersGet($ids['externalId'], 'externalId');
static::assertEquals($customerById['id'], $response['customer']['id']);
return $ids;
@ -101,7 +101,7 @@ class ApiClientCustomersTest extends TestCase
public function testCustomersGetException()
{
$client = static::getApiClient();
$client->customersGet(678678678, 'asdf');
$client->request->customersGet(678678678, 'asdf');
}
/**
@ -114,7 +114,7 @@ class ApiClientCustomersTest extends TestCase
{
$client = static::getApiClient();
$response = $client->customersEdit(
$response = $client->request->customersEdit(
array(
'id' => 22342134,
'lastName' => '12345',
@ -124,7 +124,7 @@ class ApiClientCustomersTest extends TestCase
static::assertInstanceOf('RetailCrm\Response\ApiResponse', $response);
static::assertEquals(404, $response->getStatusCode());
$response = $client->customersEdit(array(
$response = $client->request->customersEdit(array(
'externalId' => $ids['externalId'],
'lastName' => '12345',
));
@ -140,7 +140,7 @@ class ApiClientCustomersTest extends TestCase
public function testCustomersEditExceptionEmpty()
{
$client = static::getApiClient();
$client->customersEdit(array(), 'asdf');
$client->request->customersEdit(array(), 'asdf');
}
/**
@ -150,7 +150,7 @@ class ApiClientCustomersTest extends TestCase
public function testCustomersEditException()
{
$client = static::getApiClient();
$client->customersEdit(array('id' => 678678678), 'asdf');
$client->request->customersEdit(array('id' => 678678678), 'asdf');
}
/**
@ -160,19 +160,19 @@ class ApiClientCustomersTest extends TestCase
{
$client = static::getApiClient();
$response = $client->customersList();
$response = $client->request->customersList();
static::assertInstanceOf('RetailCrm\Response\ApiResponse', $response);
static::assertTrue($response->isSuccessful());
static::assertTrue(isset($response['customers']));
$response = $client->customersList(array(), 1, 300);
$response = $client->request->customersList(array(), 1, 300);
static::assertInstanceOf('RetailCrm\Response\ApiResponse', $response);
static::assertFalse(
$response->isSuccessful(),
'Pagination error'
);
$response = $client->customersList(array('maxOrdersCount' => 10), 1);
$response = $client->request->customersList(array('maxOrdersCount' => 10), 1);
static::assertInstanceOf('RetailCrm\Response\ApiResponse', $response);
static::assertTrue(
$response->isSuccessful(),
@ -187,7 +187,7 @@ class ApiClientCustomersTest extends TestCase
public function testCustomersFixExternalIdsException()
{
$client = static::getApiClient();
$client->customersFixExternalIds(array());
$client->request->customersFixExternalIds(array());
}
/**
@ -197,7 +197,7 @@ class ApiClientCustomersTest extends TestCase
{
$client = static::getApiClient();
$response = $client->ordersCreate(array(
$response = $client->request->ordersCreate(array(
'firstName' => 'Aaa111',
));
@ -206,7 +206,7 @@ class ApiClientCustomersTest extends TestCase
'Order created'
);
$response = $client->ordersGet($response['id'], 'id');
$response = $client->request->ordersGet($response['id'], 'id');
static::assertTrue(
$response->isSuccessful(),
'Order fetched'
@ -215,7 +215,7 @@ class ApiClientCustomersTest extends TestCase
$id = $response['order']['customer']['id'];
$externalId = 'asdf' . time();
$response = $client->customersFixExternalIds(array(
$response = $client->request->customersFixExternalIds(array(
array('id' => $id, 'externalId' => $externalId)
));
@ -224,7 +224,7 @@ class ApiClientCustomersTest extends TestCase
'Fixed customer ids'
);
$response = $client->customersGet($externalId);
$response = $client->request->customersGet($externalId);
static::assertTrue(
$response->isSuccessful(),
'Got customer'
@ -248,7 +248,7 @@ class ApiClientCustomersTest extends TestCase
public function testCustomersUploadExceptionEmpty()
{
$client = static::getApiClient();
$client->customersUpload(array());
$client->request->customersUpload(array());
}
/**
@ -261,7 +261,7 @@ class ApiClientCustomersTest extends TestCase
$externalIdA = 'upload-a-' . time();
$externalIdB = 'upload-b-' . time();
$response = $client->customersUpload(array(
$response = $client->request->customersUpload(array(
array(
'externalId' => $externalIdA,
'firstName' => 'Aaa',
@ -292,7 +292,7 @@ class ApiClientCustomersTest extends TestCase
{
$client = static::getApiClient();
$responseCreateFirst = $client->customersCreate(array(
$responseCreateFirst = $client->request->customersCreate(array(
'firstName' => 'Aaa111',
'externalId' => 'AA-' . time(),
'phones' => array(
@ -307,7 +307,7 @@ class ApiClientCustomersTest extends TestCase
'Got customer'
);
$responseCreateSecond = $client->customersCreate(array(
$responseCreateSecond = $client->request->customersCreate(array(
'firstName' => 'Aaa222',
'externalId' => 'BB-' . time(),
'phones' => array(
@ -328,7 +328,7 @@ class ApiClientCustomersTest extends TestCase
$resultCustomer = array('id' => $responseCreateSecond['id']);
$response = $client->customersCombine($customers, $resultCustomer);
$response = $client->request->customersCombine($customers, $resultCustomer);
static::assertTrue(
$response->isSuccessful(),

View File

@ -417,4 +417,84 @@ class ApiClientOrdersTest extends TestCase
'Delete payment'
);
}
/**
* @dataProvider ordersLoyaltyApplyProvider
*
* @group orders_v5
*
* @param array $order
* @param float $bonuses
* @param string $site
* @param string|null $exceptionClass
*
* @return void
*/
public function testOrdersLoyaltyApply(array $order, float $bonuses, string $site, $exceptionClass)
{
$client = static::getApiClient();
if (!empty($exceptionClass)) {
$this->expectException($exceptionClass);
}
$response = $client->request->ordersLoyaltyApply($order, $bonuses, $site);
if (empty($exceptionClass)) {
static::assertContains($response->getStatusCode(), [200, 201]);
static::assertTrue($response->isSuccessful());
}
}
/**
* @return array[]
*/
public function ordersLoyaltyApplyProvider(): array
{
return [
'success_id' => [
[
'id' => 111111111,
],
100,
'moysklad',
null,
],
'success_externalId' => [
[
'externalId' => 111111111,
],
100,
'moysklad',
null,
],
'success_site_empty' => [
[
'externalId' => 111111111,
],
100,
'',
null,
],
'error_bonus_zero' => [
[
'externalId' => 111111111,
],
0,
'moysklad',
'InvalidArgumentException',
],
'error_missing_orderId' => [
[
'firstName' => 111111111,
],
0,
'moysklad',
'InvalidArgumentException',
],
'error_empty_order' => [
[],
0,
'moysklad',
'InvalidArgumentException',
],
];
}
}

View File

@ -1,10 +1,16 @@
<?php
use Symfony\Component\Dotenv\Dotenv;
if (function_exists('date_default_timezone_set')
&& function_exists('date_default_timezone_get')
) {
date_default_timezone_set(date_default_timezone_get());
}
if (class_exists(Dotenv::class) and file_exists(__DIR__.'/.env')) {
(new Dotenv())->load(__DIR__.'/.env');
}
$loader = include dirname(__DIR__) . '/vendor/autoload.php';
$loader->add('RetailCrm\\Test', __DIR__);