From 140e48e6e91f9906c8ea188f0bb76d8cc142e28d Mon Sep 17 00:00:00 2001 From: azgalot Date: Wed, 21 Jun 2023 12:57:37 +0300 Subject: [PATCH] add ordersLoyaltyApply method (#169) --- .gitignore | 4 +- composer.json | 3 +- lib/RetailCrm/Methods/V5/Orders.php | 47 +++++++++++ tests/.env.dist | 5 ++ .../Tests/ApiClientCustomersTest.php | 46 +++++------ .../Methods/Version5/ApiClientOrdersTest.php | 80 +++++++++++++++++++ tests/bootstrap.php | 6 ++ 7 files changed, 166 insertions(+), 25 deletions(-) create mode 100644 tests/.env.dist diff --git a/.gitignore b/.gitignore index 9a31b67..224c95b 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,6 @@ phpunit.xml .docker docker-compose*.yml .php_cs* -.phpunit* \ No newline at end of file +.phpunit* +.env* +!*.dist \ No newline at end of file diff --git a/composer.json b/composer.json index 481417e..dd85e01 100644 --- a/composer.json +++ b/composer.json @@ -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.*", diff --git a/lib/RetailCrm/Methods/V5/Orders.php b/lib/RetailCrm/Methods/V5/Orders.php index e2b44f5..c442cbd 100644 --- a/lib/RetailCrm/Methods/V5/Orders.php +++ b/lib/RetailCrm/Methods/V5/Orders.php @@ -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, + ] + ) + ); + } } diff --git a/tests/.env.dist b/tests/.env.dist new file mode 100644 index 0000000..f48e1a1 --- /dev/null +++ b/tests/.env.dist @@ -0,0 +1,5 @@ +RETAILCRM_URL= +RETAILCRM_KEY= +RETAILCRM_VERSION= +RETAILCRM_SITE= +RETAILCRM_USER= \ No newline at end of file diff --git a/tests/RetailCrm/Tests/ApiClientCustomersTest.php b/tests/RetailCrm/Tests/ApiClientCustomersTest.php index 476654a..d1d7203 100644 --- a/tests/RetailCrm/Tests/ApiClientCustomersTest.php +++ b/tests/RetailCrm/Tests/ApiClientCustomersTest.php @@ -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(), diff --git a/tests/RetailCrm/Tests/Methods/Version5/ApiClientOrdersTest.php b/tests/RetailCrm/Tests/Methods/Version5/ApiClientOrdersTest.php index b9398b8..79b15a8 100644 --- a/tests/RetailCrm/Tests/Methods/Version5/ApiClientOrdersTest.php +++ b/tests/RetailCrm/Tests/Methods/Version5/ApiClientOrdersTest.php @@ -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', + ], + ]; + } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 26ce5dc..4136eab 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,10 +1,16 @@ load(__DIR__.'/.env'); +} + $loader = include dirname(__DIR__) . '/vendor/autoload.php'; $loader->add('RetailCrm\\Test', __DIR__);