From eabaf4115c01582114b64100e8bc62f3ca24624c Mon Sep 17 00:00:00 2001 From: Sasha Gerrand Date: Thu, 28 Apr 2022 15:40:38 +0100 Subject: [PATCH 1/2] Add Orders API functionality --- src/Duffel/Api/Orders.php | 21 ++++++ tests/Duffel/Api/OrdersTest.php | 118 ++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 src/Duffel/Api/Orders.php create mode 100644 tests/Duffel/Api/OrdersTest.php diff --git a/src/Duffel/Api/Orders.php b/src/Duffel/Api/Orders.php new file mode 100644 index 0000000..ff54bf2 --- /dev/null +++ b/src/Duffel/Api/Orders.php @@ -0,0 +1,21 @@ +get('/air/orders'); + } + + public function show(string $id) { + return $this->get('/air/orders/'.self::encodePath($id)); + } + + public function create(array $params) { + return $this->post('/air/orders', \array_filter($params, function ($value) { + return null !== $value && (!\is_string($value) || '' !== $value); + })); + } +} diff --git a/tests/Duffel/Api/OrdersTest.php b/tests/Duffel/Api/OrdersTest.php new file mode 100644 index 0000000..7a74b31 --- /dev/null +++ b/tests/Duffel/Api/OrdersTest.php @@ -0,0 +1,118 @@ +mock = $this->createMock(HttpMethodsClientInterface::class); + $this->stub = $this->createStub(Client::class); + $this->stub->method('getHttpClient') + ->willReturn($this->mock); + } + + public function testAllCallsGetWithExpectedUri(): void { + $this->mock->expects($this->once()) + ->method('get') + ->with($this->equalTo('/air/orders')); + + $actual = new Orders($this->stub); + $actual->all(); + } + + public function testShowWithIdCallsGetWithExpectedUri(): void { + $this->mock->expects($this->once()) + ->method('get') + ->with($this->equalTo('/air/orders/some-id')); + + $actual = new Orders($this->stub); + $actual->show('some-id'); + } + + public function testCreateWithOfferAndPassengersAndPaymentsCallsGetWithExpectedUri(): void { + $this->mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('/air/orders'), + $this->equalTo(['Content-Type' => 'application/json']), + $this->equalTo('{"data":{"selected_offers":"off_00009htyDGjIfajdNBZRlw","payments":[{"type":"balance","amount":"30.20","currency":"GBP"}],"passengers":[{"id":"pas_00009hj8USM7Ncg31cBCLL","title":"ms","gender":"f","given_name":"Amelia","family_name":"Earhart","born_on":"1987-07-24","phone_number":"+442080160509","email":"amelia@duffel.dev"}]}}') + ); + + $actual = new Orders($this->stub); + $actual->create( + [ + 'selected_offers' => 'off_00009htyDGjIfajdNBZRlw', + 'payments' => [ + [ + 'type' => 'balance', + 'amount' => '30.20', + 'currency' => 'GBP' + ] + ], + 'passengers' => [ + [ + 'id' => 'pas_00009hj8USM7Ncg31cBCLL', + 'title' => 'ms', + 'gender' => 'f', + 'given_name' => 'Amelia', + 'family_name' => 'Earhart', + 'born_on' => '1987-07-24', + 'phone_number' => '+442080160509', + 'email' => 'amelia@duffel.dev', + ] + ] + ] + ); + } + + public function testCreateWithOfferAndServicesAndPassengersAndPaymentsCallsGetWithExpectedUri(): void { + $this->mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('/air/orders'), + $this->equalTo(['Content-Type' => 'application/json']), + $this->equalTo('{"data":{"selected_offers":"off_00009htyDGjIfajdNBZRlw","services":[{"id":"ase_00009hj8USM7Ncg31cB123","quantity":1}],"payments":[{"type":"balance","amount":"30.20","currency":"GBP"}],"passengers":[{"id":"pas_00009hj8USM7Ncg31cBCLL","title":"ms","gender":"f","given_name":"Amelia","family_name":"Earhart","born_on":"1987-07-24","phone_number":"+442080160509","email":"amelia@duffel.dev"}]}}') + ); + + $actual = new Orders($this->stub); + $actual->create( + [ + 'selected_offers' => 'off_00009htyDGjIfajdNBZRlw', + 'services' => [ + [ + 'id' => 'ase_00009hj8USM7Ncg31cB123', + "quantity" => 1, + ] + ], + 'payments' => [ + [ + 'type' => 'balance', + 'amount' => '30.20', + 'currency' => 'GBP' + ] + ], + 'passengers' => [ + [ + 'id' => 'pas_00009hj8USM7Ncg31cBCLL', + 'title' => 'ms', + 'gender' => 'f', + 'given_name' => 'Amelia', + 'family_name' => 'Earhart', + 'born_on' => '1987-07-24', + 'phone_number' => '+442080160509', + 'email' => 'amelia@duffel.dev', + ] + ] + ] + ); + } +} From e1154a20412b287ecdef1f119551abcb31b39d84 Mon Sep 17 00:00:00 2001 From: Sasha Gerrand Date: Thu, 28 Apr 2022 15:40:53 +0100 Subject: [PATCH 2/2] Integrate Orders API into client --- src/Duffel/Client.php | 5 +++++ tests/Duffel/ClientTest.php | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/Duffel/Client.php b/src/Duffel/Client.php index a7c983b..a37085b 100644 --- a/src/Duffel/Client.php +++ b/src/Duffel/Client.php @@ -9,6 +9,7 @@ use Duffel\Api\Airports; use Duffel\Api\OfferRequests; use Duffel\Api\Offers; +use Duffel\Api\Orders; use Duffel\Exception\InvalidAccessTokenException; use Duffel\HttpClient\Builder; use Http\Client\Common\HttpMethodsClientInterface; @@ -63,6 +64,10 @@ public function offers(): Offers { return new Offers($this); } + public function orders(): Orders { + return new Orders($this); + } + public function getAccessToken() { return $this->accessToken; } diff --git a/tests/Duffel/ClientTest.php b/tests/Duffel/ClientTest.php index 4304240..65fb6c0 100644 --- a/tests/Duffel/ClientTest.php +++ b/tests/Duffel/ClientTest.php @@ -9,6 +9,7 @@ use Duffel\Api\Airports; use Duffel\Api\OfferRequests; use Duffel\Api\Offers; +use Duffel\Api\Orders; use Duffel\Client; use Duffel\Exception\InvalidAccessTokenException; use Duffel\HttpClient\Builder; @@ -57,6 +58,10 @@ public function testOffersUsesApiClass(): void { $this->assertInstanceOf(Offers::class, $this->subject->offers()); } + public function testOrdersUsesApiClass(): void { + $this->assertInstanceOf(Orders::class, $this->subject->orders()); + } + public function testSetAccessTokenChangesValue(): void { $this->subject->setAccessToken('some-token');