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)); diff --git a/tests/BigCommerce/Api/Customers/CustomersApiTest.php b/tests/BigCommerce/Api/Customers/CustomersApiTest.php index 796b6b7b..c22ebb04 100644 --- a/tests/BigCommerce/Api/Customers/CustomersApiTest.php +++ b/tests/BigCommerce/Api/Customers/CustomersApiTest.php @@ -12,12 +12,31 @@ 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->markTestIncomplete(); + $this->setReturnData('customers__get_all.json'); + $customer = $this->getApi()->customers()->getByEmail('john.smith@spam.me'); + + $this->assertEquals('John', $customer->first_name); + } + + public function testCanGetCustomerById() + { + $this->setReturnData('customers__get_all.json'); + $customer = $this->getApi()->customers()->getById(1); + + $this->assertEquals('john.smith@spam.me', $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() 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": "", 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 + } + } +}