Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from duffelhq/api-orders
Browse files Browse the repository at this point in the history
API: Orders
  • Loading branch information
sgerrand authored Apr 28, 2022
2 parents 62ccc3c + e1154a2 commit a76cb63
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Duffel/Api/Orders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Duffel\Api;

class Orders extends AbstractApi {
public function all() {
return $this->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);
}));
}
}
5 changes: 5 additions & 0 deletions src/Duffel/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
118 changes: 118 additions & 0 deletions tests/Duffel/Api/OrdersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

declare(strict_types=1);

namespace Duffel\Tests\Api;

use Duffel\Api\Orders;
use Duffel\Client;
use Http\Client\Common\HttpMethodsClientInterface;
use PHPUnit\Framework\TestCase;

class TestOrders extends TestCase {
private $mock;
private $stub;

public function setUp(): void {
$this->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',
]
]
]
);
}
}
5 changes: 5 additions & 0 deletions tests/Duffel/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');

Expand Down

0 comments on commit a76cb63

Please sign in to comment.