Skip to content

Commit

Permalink
Merge pull request #33 from aligent/feature/get-customer-by-id
Browse files Browse the repository at this point in the history
Add helper to get customer by id.
  • Loading branch information
jswift authored Jan 11, 2021
2 parents 636ed40 + 12f9293 commit 5dfcf4e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -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)`.
8 changes: 8 additions & 0 deletions src/BigCommerce/Api/Customers/CustomersApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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));
Expand Down
23 changes: 21 additions & 2 deletions tests/BigCommerce/Api/Customers/CustomersApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('[email protected]');

$this->assertEquals('John', $customer->first_name);
}

public function testCanGetCustomerById()
{
$this->setReturnData('customers__get_all.json');
$customer = $this->getApi()->customers()->getById(1);

$this->assertEquals('[email protected]', $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()
Expand Down
6 changes: 3 additions & 3 deletions tests/BigCommerce/responses/customers__get_all.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
},
"company": "Aligent",
"customer_group_id": 1,
"email": "[email protected]",
"first_name": "Jan",
"last_name": "Plank",
"email": "[email protected]",
"first_name": "John",
"last_name": "Smith",
"notes": "",
"phone": "",
"registration_ip_address": "",
Expand Down
12 changes: 12 additions & 0 deletions tests/BigCommerce/responses/customers__get_all_no_results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"data": [],
"meta": {
"pagination": {
"total": 0,
"count": 0,
"per_page": 50,
"current_page": 1,
"total_pages": 1
}
}
}

0 comments on commit 5dfcf4e

Please sign in to comment.