Skip to content

Commit a6c7e72

Browse files
committed
fix an issue if timetable returns only one flight
1 parent 275b923 commit a6c7e72

3 files changed

Lines changed: 92 additions & 2 deletions

File tree

src/SDK/Helper/GetTimetableHelper.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public function __construct(array $responseData)
1515
public function getFlightExtraInfo() : array
1616
{
1717
$extraOTAAirScheduleRS = $this->responseData['extendedOTAAirScheduleRS']['extraOTAAirScheduleRS'];
18-
$flightExtraInfo = $extraOTAAirScheduleRS['extraOTAAirScheduleRSListType']['flightExtraInfo'][0];
18+
$flightExtraInfo = $this
19+
->getFlightExtraInfoData($extraOTAAirScheduleRS['extraOTAAirScheduleRSListType']['flightExtraInfo']);
1920
return [
2021
'totalDuration' => DurationConverter::toMinute($flightExtraInfo['totalDuration']),
2122
'flightDuration' => DurationConverter::toMinute($flightExtraInfo['flightDuration']),
@@ -24,9 +25,20 @@ public function getFlightExtraInfo() : array
2425
];
2526
}
2627

28+
private function getFlightExtraInfoData($flightExtraInfo) : array
29+
{
30+
if (array_key_exists('totalDuration', $flightExtraInfo)) {
31+
return $flightExtraInfo;
32+
}
33+
return $flightExtraInfo[0];
34+
}
35+
2736
public function getOriginDestinationOptions() : array
2837
{
2938
$airScheduleRS = $this->responseData['extendedOTAAirScheduleRS']['OTA_AirScheduleRS'];
39+
if (array_key_exists('FlightSegment', $airScheduleRS['OriginDestinationOptions']['OriginDestinationOption'])) {
40+
return [$airScheduleRS['OriginDestinationOptions']['OriginDestinationOption']];
41+
}
3042
return $airScheduleRS['OriginDestinationOptions']['OriginDestinationOption'];
3143
}
3244

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php namespace Helper;
2+
3+
use TK\SDK\ClientBuilder;
4+
use DateTimeImmutable;
5+
use TK\SDK\Helper\GetTimetableHelper;
6+
use TK\SDK\ValueObject;
7+
use Dotenv;
8+
9+
class GetTimetableHelperSingleResultTest extends \Codeception\Test\Unit
10+
{
11+
/**
12+
* @var \UnitTester
13+
*/
14+
protected $tester;
15+
private $response;
16+
17+
protected function _before()
18+
{
19+
if (file_exists(__DIR__.'/../../../.env')) {
20+
$dotenv = new Dotenv\Dotenv(__DIR__ . '/../../..');
21+
$dotenv->load();
22+
}
23+
$client = ClientBuilder::create()
24+
->setEnvironment(getenv('TK_API_URL'), getenv('TK_API_KEY'), getenv('TK_API_SECRET'))
25+
->build();
26+
$departureTime = gmdate('Y-m-d H:i:s', strtotime('+4 days'));
27+
$originLocation = new ValueObject\Location('IST', ValueObject\Location::MULTIPLE_AIRPORT_TRUE);
28+
$destinationLocation = new ValueObject\Location('BOG', ValueObject\Location::MULTIPLE_AIRPORT_TRUE);
29+
$departureDateTime = new ValueObject\DepartureDateTime(
30+
new DateTimeImmutable($departureTime),
31+
'P3D',
32+
'P3D'
33+
);
34+
$originDestinationInformation = new ValueObject\OriginDestinationInformation(
35+
$departureDateTime,
36+
$originLocation,
37+
$destinationLocation
38+
);
39+
$airScheduleRQ = (new ValueObject\AirScheduleRQ($originDestinationInformation))
40+
->withDirectAndNonStopOnlyInd();
41+
$getTimetableParameters = new ValueObject\GetTimetableParameters(
42+
$airScheduleRQ,
43+
ValueObject\GetTimetableParameters::SCHEDULE_TYPE_WEEKLY,
44+
ValueObject\GetTimetableParameters::TRIP_TYPE_ONE_WAY
45+
);
46+
$this->response = $client->getTimetable($getTimetableParameters);
47+
}
48+
49+
protected function _after()
50+
{
51+
}
52+
53+
/**
54+
* @test
55+
*/
56+
public function shouldGetDataSuccessfully() : void
57+
{
58+
$helper = new GetTimetableHelper($this->response['data']);
59+
$flightInfo = $helper->getFlightExtraInfo();
60+
$this->assertArrayHasKey('durationType', $flightInfo);
61+
62+
$originDestinationOptions = $helper->getOriginDestinationOptions();
63+
64+
foreach ($originDestinationOptions as $originDestinationOption) {
65+
$flightDetails = $helper->getFlightDetails($originDestinationOption);
66+
67+
$this->assertArrayHasKey('FlightNumber', $flightDetails);
68+
$this->assertArrayHasKey('DepartureDateTime', $flightDetails);
69+
$this->assertArrayHasKey('OperationTime', $flightDetails);
70+
$this->assertArrayHasKey('ScheduleValidEndDate', $flightDetails);
71+
72+
73+
$operationAirline = $helper->getOperationAirline($originDestinationOption);
74+
$this->assertArrayHasKey('FlightNumber', $operationAirline);
75+
$this->assertArrayHasKey('Code', $operationAirline);
76+
}
77+
}
78+
}

tests/unit/Helper/GetTimetableHelperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected function _before()
2525
->build();
2626
$departureTime = gmdate('Y-m-d H:i:s', strtotime('+4 days'));
2727
$originLocation = new ValueObject\Location('IST', ValueObject\Location::MULTIPLE_AIRPORT_TRUE);
28-
$destinationLocation = new ValueObject\Location('ESB', ValueObject\Location::MULTIPLE_AIRPORT_TRUE);
28+
$destinationLocation = new ValueObject\Location('JFK', ValueObject\Location::MULTIPLE_AIRPORT_TRUE);
2929
$departureDateTime = new ValueObject\DepartureDateTime(
3030
new DateTimeImmutable($departureTime),
3131
'P3D',

0 commit comments

Comments
 (0)