Skip to content

Commit 5dfcf4e

Browse files
authored
Merge pull request #33 from aligent/feature/get-customer-by-id
Add helper to get customer by id.
2 parents 636ed40 + 12f9293 commit 5dfcf4e

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
### New Features
22

3-
Implemented create and update Product Variant as this has been missed previously.
3+
Until BigCommerce decide to be consistent and include a _Get Customer_ endpoint, add `CustomersApi::getById(int $id)`.

src/BigCommerce/Api/Customers/CustomersApi.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class CustomersApi extends CustomerApiBase
1212
use DeleteInIdList;
1313

1414
public const FILTER__EMAIL_IN = 'email:in';
15+
public const FILTER__ID_IN = 'id:in';
1516

1617
private const RESOURCE_NAME = 'customers';
1718

@@ -33,6 +34,13 @@ public function getByEmail(string $email): ?Customer
3334
return $customers[0];
3435
}
3536

37+
public function getById(int $id): ?Customer
38+
{
39+
$customers = $this->getAll([self::FILTER__ID_IN => $id])->getCustomers();
40+
41+
return $customers[0] ?? null;
42+
}
43+
3644
public function create(array $customers): CustomersResponse
3745
{
3846
return new CustomersResponse($this->createResources($customers));

tests/BigCommerce/Api/Customers/CustomersApiTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,31 @@ public function testCanGetCustomers()
1212
$this->setReturnData('customers__get_all.json');
1313
$customersResponse = $this->getApi()->customers()->getAll();
1414
$this->assertEquals(1, $customersResponse->getPagination()->total);
15-
$this->assertEquals('Jan', $customersResponse->getCustomers()[0]->first_name);
15+
$this->assertEquals('John', $customersResponse->getCustomers()[0]->first_name);
1616
}
1717

1818
public function testCanGetCustomerByEmail()
1919
{
20-
$this->markTestIncomplete();
20+
$this->setReturnData('customers__get_all.json');
21+
$customer = $this->getApi()->customers()->getByEmail('[email protected]');
22+
23+
$this->assertEquals('John', $customer->first_name);
24+
}
25+
26+
public function testCanGetCustomerById()
27+
{
28+
$this->setReturnData('customers__get_all.json');
29+
$customer = $this->getApi()->customers()->getById(1);
30+
31+
$this->assertEquals('[email protected]', $customer->email);
32+
}
33+
34+
public function testCanGetNullCustomerById()
35+
{
36+
$this->setReturnData('customers__get_all_no_results.json');
37+
$customer = $this->getApi()->customers()->getById(2);
38+
39+
$this->assertNull($customer);
2140
}
2241

2342
public function testCanCreateCustomers()

tests/BigCommerce/responses/customers__get_all.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
},
88
"company": "Aligent",
99
"customer_group_id": 1,
10-
"email": "[email protected]",
11-
"first_name": "Jan",
12-
"last_name": "Plank",
10+
"email": "[email protected]",
11+
"first_name": "John",
12+
"last_name": "Smith",
1313
"notes": "",
1414
"phone": "",
1515
"registration_ip_address": "",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"data": [],
3+
"meta": {
4+
"pagination": {
5+
"total": 0,
6+
"count": 0,
7+
"per_page": 50,
8+
"current_page": 1,
9+
"total_pages": 1
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)