From 410906b2cdf78e456d33b6e296b281e993fa6fce Mon Sep 17 00:00:00 2001 From: Mathias Palmqvist Date: Mon, 30 Jun 2025 10:59:53 +0200 Subject: [PATCH] Added customer all and get --- src/Bokio.php | 12 +++++++- src/BokioClient.php | 2 +- src/Resources/Customer.php | 28 +++++++++++++++++ tests/CustomerTest.php | 44 +++++++++++++++++++++++++++ tests/ExampleTest.php | 5 --- tests/Responses/Customer/all.200.json | 31 +++++++++++++++++++ tests/Responses/Customer/get.200.json | 25 +++++++++++++++ 7 files changed, 140 insertions(+), 7 deletions(-) create mode 100644 src/Resources/Customer.php create mode 100644 tests/CustomerTest.php delete mode 100644 tests/ExampleTest.php create mode 100644 tests/Responses/Customer/all.200.json create mode 100644 tests/Responses/Customer/get.200.json diff --git a/src/Bokio.php b/src/Bokio.php index 48aff62..d3d3279 100755 --- a/src/Bokio.php +++ b/src/Bokio.php @@ -2,4 +2,14 @@ namespace Mattitja\BokioApiLaravel; -class Bokio {} +use Mattitja\BokioApiLaravel\Resources\Customer; + +class Bokio +{ + public function __construct(protected BokioClient $client) {} + + public function customers(): Customer + { + return new Customer($this->client); + } +} diff --git a/src/BokioClient.php b/src/BokioClient.php index 85efa04..220ace2 100644 --- a/src/BokioClient.php +++ b/src/BokioClient.php @@ -12,7 +12,7 @@ class BokioClient public function __construct( private string $accessToken, private string $companyId, - private string $baseUrl + private string $baseUrl = 'https://api.bokio.se' ) { $this->request = Http::acceptJson() ->asJson() diff --git a/src/Resources/Customer.php b/src/Resources/Customer.php new file mode 100644 index 0000000..186451f --- /dev/null +++ b/src/Resources/Customer.php @@ -0,0 +1,28 @@ +client->get('customers', [ + 'page' => $page, + 'pageSize' => $pageSize, + ]); + } + + public function get(string $customerId): object + { + return $this->client->get('customers/'.$customerId); + } +} diff --git a/tests/CustomerTest.php b/tests/CustomerTest.php new file mode 100644 index 0000000..8118bb9 --- /dev/null +++ b/tests/CustomerTest.php @@ -0,0 +1,44 @@ +method() === 'GET' && preg_match('#/customers/\w+#', $request->url())) { + return Http::response(loadFixture('Customer/get.200.json'), 200); + } + + if ($request->method() === 'GET' && str_contains($request->url(), '/customers')) { + return Http::response(loadFixture('Customer/all.200.json'), 200); + } + + // fallback + return Http::response([], 404); + }); + + $this->client = new BokioClient('fake-token', '123456'); +}); + +it('can fetch a list of customers from Bokio', function () { + $response = (new Customer($this->client))->all(); + + expect($response->items)->toHaveCount(1); + expect($response->items[0]->name)->toBe('customer 1'); +}); + +it('can fetch a single customer from Bokio', function () { + $customerId = '55c899c5-82b2-47fa-9c51-e35fc9b26443'; + $response = (new Customer($this->client))->get($customerId); + + expect($response->id)->toBe($customerId); + expect($response->name)->toBe('customer 1'); +}); diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php deleted file mode 100644 index 5d36321..0000000 --- a/tests/ExampleTest.php +++ /dev/null @@ -1,5 +0,0 @@ -toBeTrue(); -}); diff --git a/tests/Responses/Customer/all.200.json b/tests/Responses/Customer/all.200.json new file mode 100644 index 0000000..0cbfad2 --- /dev/null +++ b/tests/Responses/Customer/all.200.json @@ -0,0 +1,31 @@ +{ + "totalItems": 0, + "totalPages": 0, + "currentPage": 0, + "items": [ + { + "id": "55c899c5-82b2-47fa-9c51-e35fc9b26443", + "name": "customer 1", + "type": "company", + "vatNumber": "SE1234567890", + "orgNumber": "123456-7890", + "paymentTerms": "30", + "contactsDetails": [ + { + "name": "John Doe", + "email": "john@email.com", + "phone": "0927-5631505", + "isDefault": true + } + ], + "address": { + "line1": "Älvsborgsvägen 10", + "line2": null, + "city": "Göteborg", + "postalCode": "123 45", + "country": "SE" + }, + "language": "sv" + } + ] +} diff --git a/tests/Responses/Customer/get.200.json b/tests/Responses/Customer/get.200.json new file mode 100644 index 0000000..2b34730 --- /dev/null +++ b/tests/Responses/Customer/get.200.json @@ -0,0 +1,25 @@ +{ + "id": "55c899c5-82b2-47fa-9c51-e35fc9b26443", + "name": "customer 1", + "type": "company", + "vatNumber": "SE1234567890", + "orgNumber": "123456-7890", + "paymentTerms": "30", + "contactsDetails": [ + { + "id": "240a4af0-edfd-47b1-b4ab-f30450eaac19", + "name": "John Doe", + "email": "john@email.com", + "phone": "0927-5631505", + "isDefault": true + } + ], + "address": { + "line1": "Älvsborgsvägen 10", + "line2": null, + "city": "Göteborg", + "postalCode": "123 45", + "country": "SE" + }, + "language": "sv" +}