From 546afd380e791b452bf2a7eb83a5729f84a22bc0 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Mon, 11 Jan 2021 14:16:08 +1030 Subject: [PATCH 1/4] Add a helper for getting a single customer by id since this doesn't exist as an api endpoint --- RELEASE_NOTES.md | 2 +- src/BigCommerce/Api/Customers/CustomersApi.php | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 8f5851c0..570f1d33 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,3 +1,3 @@ ### New Features -Implemented create and update Product Variant as this has been missed previously. +Until BigCommerce decide to be consistent and include a _Get Customer_ endpoint, add `CustomersApi::getById(int $id)`. diff --git a/src/BigCommerce/Api/Customers/CustomersApi.php b/src/BigCommerce/Api/Customers/CustomersApi.php index 99ba333b..a976d47c 100644 --- a/src/BigCommerce/Api/Customers/CustomersApi.php +++ b/src/BigCommerce/Api/Customers/CustomersApi.php @@ -12,6 +12,7 @@ class CustomersApi extends CustomerApiBase use DeleteInIdList; public const FILTER__EMAIL_IN = 'email:in'; + public const FILTER__ID_IN = 'id:in'; private const RESOURCE_NAME = 'customers'; @@ -33,6 +34,13 @@ public function getByEmail(string $email): ?Customer return $customers[0]; } + public function getById(int $id): ?Customer + { + $customers = $this->getAll([self::FILTER__ID_IN => $id])->getCustomers(); + + return $customers[0] ?? null; + } + public function create(array $customers): CustomersResponse { return new CustomersResponse($this->createResources($customers)); From f2e096c1371b4fe7fdcf10ea80b9dc13f81427cb Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Mon, 11 Jan 2021 14:23:12 +1030 Subject: [PATCH 2/4] Add test for Customers::getById --- .../Api/Customers/CustomersApiTest.php | 16 ++++++++++++++++ .../responses/customers__get_all_no_results.json | 12 ++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 tests/BigCommerce/responses/customers__get_all_no_results.json diff --git a/tests/BigCommerce/Api/Customers/CustomersApiTest.php b/tests/BigCommerce/Api/Customers/CustomersApiTest.php index 796b6b7b..2f111a5f 100644 --- a/tests/BigCommerce/Api/Customers/CustomersApiTest.php +++ b/tests/BigCommerce/Api/Customers/CustomersApiTest.php @@ -20,6 +20,22 @@ public function testCanGetCustomerByEmail() $this->markTestIncomplete(); } + public function testCanGetCustomerById() + { + $this->setReturnData('customers__get_all.json'); + $customer = $this->getApi()->customers()->getById(1); + + $this->assertEquals('jan.plank@aligent.com.au', $customer->email); + } + + public function testCanGetNullCustomerById() + { + $this->setReturnData('customers__get_all_no_results.json'); + $customer = $this->getApi()->customers()->getById(2); + + $this->assertNull($customer); + } + public function testCanCreateCustomers() { $customers = [ diff --git a/tests/BigCommerce/responses/customers__get_all_no_results.json b/tests/BigCommerce/responses/customers__get_all_no_results.json new file mode 100644 index 00000000..c326510b --- /dev/null +++ b/tests/BigCommerce/responses/customers__get_all_no_results.json @@ -0,0 +1,12 @@ +{ + "data": [], + "meta": { + "pagination": { + "total": 0, + "count": 0, + "per_page": 50, + "current_page": 1, + "total_pages": 1 + } + } +} From 0b88cf05627a1f3bc77678968d3046779bc62c50 Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Mon, 11 Jan 2021 14:24:13 +1030 Subject: [PATCH 3/4] Add test for Customers::getByEmail --- tests/BigCommerce/Api/Customers/CustomersApiTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/BigCommerce/Api/Customers/CustomersApiTest.php b/tests/BigCommerce/Api/Customers/CustomersApiTest.php index 2f111a5f..d11ada78 100644 --- a/tests/BigCommerce/Api/Customers/CustomersApiTest.php +++ b/tests/BigCommerce/Api/Customers/CustomersApiTest.php @@ -17,7 +17,10 @@ public function testCanGetCustomers() public function testCanGetCustomerByEmail() { - $this->markTestIncomplete(); + $this->setReturnData('customers__get_all.json'); + $customer = $this->getApi()->customers()->getByEmail('jan.plank@aligent.com.au'); + + $this->assertEquals('Jan', $customer->first_name); } public function testCanGetCustomerById() From 12f92936fad179234ab6d326a66c31455d29930d Mon Sep 17 00:00:00 2001 From: Jarrod Swift Date: Tue, 12 Jan 2021 08:47:58 +1030 Subject: [PATCH 4/4] Use John Smith for tests --- tests/BigCommerce/Api/Customers/CustomersApiTest.php | 8 ++++---- tests/BigCommerce/responses/customers__get_all.json | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/BigCommerce/Api/Customers/CustomersApiTest.php b/tests/BigCommerce/Api/Customers/CustomersApiTest.php index d11ada78..c22ebb04 100644 --- a/tests/BigCommerce/Api/Customers/CustomersApiTest.php +++ b/tests/BigCommerce/Api/Customers/CustomersApiTest.php @@ -12,15 +12,15 @@ public function testCanGetCustomers() $this->setReturnData('customers__get_all.json'); $customersResponse = $this->getApi()->customers()->getAll(); $this->assertEquals(1, $customersResponse->getPagination()->total); - $this->assertEquals('Jan', $customersResponse->getCustomers()[0]->first_name); + $this->assertEquals('John', $customersResponse->getCustomers()[0]->first_name); } public function testCanGetCustomerByEmail() { $this->setReturnData('customers__get_all.json'); - $customer = $this->getApi()->customers()->getByEmail('jan.plank@aligent.com.au'); + $customer = $this->getApi()->customers()->getByEmail('john.smith@spam.me'); - $this->assertEquals('Jan', $customer->first_name); + $this->assertEquals('John', $customer->first_name); } public function testCanGetCustomerById() @@ -28,7 +28,7 @@ public function testCanGetCustomerById() $this->setReturnData('customers__get_all.json'); $customer = $this->getApi()->customers()->getById(1); - $this->assertEquals('jan.plank@aligent.com.au', $customer->email); + $this->assertEquals('john.smith@spam.me', $customer->email); } public function testCanGetNullCustomerById() diff --git a/tests/BigCommerce/responses/customers__get_all.json b/tests/BigCommerce/responses/customers__get_all.json index 13637e01..286d6661 100644 --- a/tests/BigCommerce/responses/customers__get_all.json +++ b/tests/BigCommerce/responses/customers__get_all.json @@ -7,9 +7,9 @@ }, "company": "Aligent", "customer_group_id": 1, - "email": "jan.plank@aligent.com.au", - "first_name": "Jan", - "last_name": "Plank", + "email": "john.smith@spam.me", + "first_name": "John", + "last_name": "Smith", "notes": "", "phone": "", "registration_ip_address": "",