From aff95ec7913353c0f29eeb49a37843f820d78ef3 Mon Sep 17 00:00:00 2001 From: Ivan Chaplygin Date: Wed, 25 Sep 2024 15:29:03 +0300 Subject: [PATCH 1/4] support /api/v5/customers/{externalId}/subscriptions method --- lib/RetailCrm/Methods/V5/Customers.php | 41 ++++++++++++++++++ .../Version5/ApiClientCustomersTest.php | 43 +++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/lib/RetailCrm/Methods/V5/Customers.php b/lib/RetailCrm/Methods/V5/Customers.php index daeb6d9..9489ceb 100644 --- a/lib/RetailCrm/Methods/V5/Customers.php +++ b/lib/RetailCrm/Methods/V5/Customers.php @@ -141,4 +141,45 @@ trait Customers "POST" ); } + + /** + * Update subscriptions a customer + * + * @param array $subscriptions subscriptions data + * @param integer $customerId identifier customer + * @param string $by (default: 'externalId') + * @param string|null $site (default: null) + * + * @throws \InvalidArgumentException + * @throws \RetailCrm\Exception\InvalidJsonException + * @throws \RetailCrm\Exception\CurlException + * + * @return \RetailCrm\Response\ApiResponse + */ + public function customerSubscriptionsUpdate(array $subscriptions, $customerId, $by = 'externalId', $site = null) + { + if (!count($subscriptions)) { + throw new \InvalidArgumentException( + 'Parameter `subscriptions` must contains a data' + ); + } + + if (!is_int($customerId)) { + throw new \InvalidArgumentException( + 'Parameter `customerId` must be an integer' + ); + } + + $this->checkIdParameter($by); + + /* @noinspection PhpUndefinedMethodInspection */ + return $this->client->makeRequest( + sprintf('/customer/%s/subscriptions', $customerId), + 'POST', + $this->fillSite( + $site, + ['subscriptions' => json_encode($subscriptions), 'by' => $by] + ) + ); + } } diff --git a/tests/RetailCrm/Tests/Methods/Version5/ApiClientCustomersTest.php b/tests/RetailCrm/Tests/Methods/Version5/ApiClientCustomersTest.php index 5353d1e..89bbee0 100644 --- a/tests/RetailCrm/Tests/Methods/Version5/ApiClientCustomersTest.php +++ b/tests/RetailCrm/Tests/Methods/Version5/ApiClientCustomersTest.php @@ -11,6 +11,8 @@ namespace RetailCrm\Tests\Methods\Version5; +use RetailCrm\Http\Client; +use RetailCrm\Response\ApiResponse; use RetailCrm\Test\TestCase; /** @@ -441,4 +443,45 @@ class ApiClientCustomersTest extends TestCase static::assertTrue($responseDelete->isSuccessful(), 'Note deleted'); static::assertEquals(200, $responseDelete->getStatusCode()); } + + public function testCustomerSubscriptionsUpdate() + { + $clientMock = $this->getMockBuilder(\RetailCrm\Http\Client::class) + ->disableOriginalConstructor() + ->setMethods(['makeRequest']) + ->getMock() + ; + + $parameters = [ + 'subscriptions' => [ + [ + 'channel' => 'email', + 'active' => false + ] + ], + 'by' => 'externalId', + 'site' => 'test' + ]; + + $clientMock->expects(self::once())->method('makeRequest')->with( + '/customer/123/subscriptions', + 'POST', + [ + 'subscriptions' => json_encode($parameters['subscriptions']), + 'by' => $parameters['by'], + 'site' => $parameters['site'] + ] + )->willReturn((new ApiResponse(200, json_encode(['success' => true])))->asJsonResponse()); + + $client = static::getMockedApiClient($clientMock); + + $response = $client->request->customerSubscriptionsUpdate( + $parameters['subscriptions'], + 123, + $parameters['by'], + $parameters['site'] + ); + + static::assertTrue($response->isSuccessful()); + } } From 21c0a3eac5dc22156e836b9e807be4cc72fe1fcb Mon Sep 17 00:00:00 2001 From: Ivan Chaplygin Date: Wed, 25 Sep 2024 16:33:21 +0300 Subject: [PATCH 2/4] fix path --- lib/RetailCrm/Methods/V5/Customers.php | 2 +- .../RetailCrm/Tests/Methods/Version5/ApiClientCustomersTest.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/RetailCrm/Methods/V5/Customers.php b/lib/RetailCrm/Methods/V5/Customers.php index 9489ceb..e4fb992 100644 --- a/lib/RetailCrm/Methods/V5/Customers.php +++ b/lib/RetailCrm/Methods/V5/Customers.php @@ -174,7 +174,7 @@ trait Customers /* @noinspection PhpUndefinedMethodInspection */ return $this->client->makeRequest( - sprintf('/customer/%s/subscriptions', $customerId), + sprintf('/customers/%s/subscriptions', $customerId), 'POST', $this->fillSite( $site, diff --git a/tests/RetailCrm/Tests/Methods/Version5/ApiClientCustomersTest.php b/tests/RetailCrm/Tests/Methods/Version5/ApiClientCustomersTest.php index 89bbee0..2cb2e20 100644 --- a/tests/RetailCrm/Tests/Methods/Version5/ApiClientCustomersTest.php +++ b/tests/RetailCrm/Tests/Methods/Version5/ApiClientCustomersTest.php @@ -11,7 +11,6 @@ namespace RetailCrm\Tests\Methods\Version5; -use RetailCrm\Http\Client; use RetailCrm\Response\ApiResponse; use RetailCrm\Test\TestCase; From eb55d769b640be40b9f6c19eb1a502ee49c589a4 Mon Sep 17 00:00:00 2001 From: Ivan Chaplygin Date: Wed, 25 Sep 2024 16:33:46 +0300 Subject: [PATCH 3/4] fix path --- .../RetailCrm/Tests/Methods/Version5/ApiClientCustomersTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/RetailCrm/Tests/Methods/Version5/ApiClientCustomersTest.php b/tests/RetailCrm/Tests/Methods/Version5/ApiClientCustomersTest.php index 2cb2e20..29f3b3b 100644 --- a/tests/RetailCrm/Tests/Methods/Version5/ApiClientCustomersTest.php +++ b/tests/RetailCrm/Tests/Methods/Version5/ApiClientCustomersTest.php @@ -463,7 +463,7 @@ class ApiClientCustomersTest extends TestCase ]; $clientMock->expects(self::once())->method('makeRequest')->with( - '/customer/123/subscriptions', + '/customers/123/subscriptions', 'POST', [ 'subscriptions' => json_encode($parameters['subscriptions']), From b7ce7fafd4d01539af57e15ea0e752534b4c6341 Mon Sep 17 00:00:00 2001 From: Ivan Chaplygin Date: Thu, 26 Sep 2024 10:19:25 +0300 Subject: [PATCH 4/4] update validation parameter --- lib/RetailCrm/Methods/V5/Customers.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/RetailCrm/Methods/V5/Customers.php b/lib/RetailCrm/Methods/V5/Customers.php index e4fb992..a9df396 100644 --- a/lib/RetailCrm/Methods/V5/Customers.php +++ b/lib/RetailCrm/Methods/V5/Customers.php @@ -164,9 +164,9 @@ trait Customers ); } - if (!is_int($customerId)) { + if ($customerId === null || $customerId === '') { throw new \InvalidArgumentException( - 'Parameter `customerId` must be an integer' + 'Parameter `customerId` is empty' ); }