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 #4 from duffelhq/api-airlines
Browse files Browse the repository at this point in the history
API: Airlines
  • Loading branch information
sgerrand authored Apr 28, 2022
2 parents 39f2ce8 + 5bef884 commit 24ea742
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
14 changes: 13 additions & 1 deletion examples/exploring-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
$client = new Client();
$client->setAccessToken(getenv('DUFFEL_ACCESS_TOKEN'));

echo "Duffel Flights API - book and change example\n";
echo "Duffel Flights API - exploring data example\n";
$start = time();

echo "Loading aircraft...\n";
Expand All @@ -26,5 +26,17 @@

echo sprintf("Aircraft's IATA code is %s\n", $singleAircraft['iata_code']);

echo "Loading airlines...\n";

$airlines = $client->airlines()->all();

echo sprintf("Got %d airlines\n", count($airlines));

echo sprintf("Found airline %s\n", $airlines[0]['name']);

$airline = $client->airlines()->show($airlines[0]['id']);

echo sprintf("Airline's IATA code is %s\n", $airline['iata_code']);

$finish = time();
echo sprintf("Finished in %d seconds.\n", ($finish - $start));
15 changes: 15 additions & 0 deletions src/Duffel/Api/Airlines.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Duffel\Api;

class Airlines extends AbstractApi {
public function all(array $parameters = []) {
return $this->get('/air/airlines');
}

public function show(string $id) {
return $this->get('/air/airlines/'.self::encodePath($id));
}
}
5 changes: 5 additions & 0 deletions src/Duffel/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Duffel;

use Duffel\Api\Aircraft;
use Duffel\Api\Airlines;
use Duffel\Exception\InvalidAccessTokenException;
use Duffel\HttpClient\Builder;
use Http\Client\Common\HttpMethodsClientInterface;
Expand Down Expand Up @@ -43,6 +44,10 @@ public function aircraft(): Aircraft {
return new Aircraft($this);
}

public function airlines(): Airlines {
return new Airlines($this);
}

public function getAccessToken() {
return $this->accessToken;
}
Expand Down
40 changes: 40 additions & 0 deletions tests/Duffel/Api/AirlinesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Duffel\Tests\Api;

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

class TestAirlines 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/airlines'));

$actual = new Airlines($this->stub);
$actual->all();
}

public function testShowCallsGetWithExpectedUri(): void {
$this->mock->expects($this->once())
->method('get')
->with($this->equalTo('/air/airlines/some-id'));

$actual = new Airlines($this->stub);
$actual->show('some-id');
}
}

0 comments on commit 24ea742

Please sign in to comment.