Skip to content

Commit

Permalink
Update sdk-commons, implement new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertware committed May 15, 2019
1 parent 80b1e2e commit 74c9bdc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 31 deletions.
8 changes: 1 addition & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,14 @@
"require": {
"php": ">=7.1",
"ext-json": "*",
"trafiklab/php-sdk-commons": "~0.3.4"
"trafiklab/php-sdk-commons": "~0.4.0"
},
"require-dev": {
"phpunit/phpunit": "^5.5",
"squizlabs/php_codesniffer": "^3.4",
"phpmd/phpmd": "^2.6.0",
"friendsofphp/php-cs-fixer": "^2.14"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/trafiklab/php-sdk-commons"
}
],
"autoload": {
"psr-0": {
"Trafiklab\\Sl": "src/"
Expand Down
13 changes: 13 additions & 0 deletions src/Trafiklab/Sl/Model/SlVehicle.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class SlVehicle implements Vehicle
private $_operatorCode;
private $_operatorName;
private $_operatorUrl;
private $_line;

/**
* Product constructor.
Expand Down Expand Up @@ -87,6 +88,16 @@ public function getOperatorName(): string
return $this->_operatorName;
}

/**
* The line number of the vehicle, identifying the line on which it runs. Example: 41X.
*
* @return string
*/
public function getLineNumber(): string
{
return $this->_line;
}

private function parseApiResponse(array $json)
{
$this->_name = $json['name'];
Expand All @@ -113,5 +124,7 @@ private function parseApiResponse(array $json)
$this->_operatorCode = 275;
$this->_operatorName = "SL";
$this->_operatorUrl = "https://sl.se";

$this->_line = $json['line'];
}
}
15 changes: 15 additions & 0 deletions src/Trafiklab/Sl/SlWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,19 @@ private function requireValidLookupStopLocationKey()
"No StopLocationLookup API key configured. Obtain a free key at https://www.trafiklab.se/api");
}
}

public function createTimeTableRequestObject(): TimeTableRequest
{
return new SlTimeTableRequest();
}

public function createRoutePlanningRequestObject(): RoutePlanningRequest
{
return new SlRoutePlanningRequest();
}

public function createStopLocationLookupRequestObject(): StopLocationLookupRequest
{
return new SlStopLocationLookupRequest();
}
}
1 change: 1 addition & 0 deletions tests/Trafiklab/Sl/Model/SlRoutePlanningVehicleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ function testConstructor_validDepartureBoardJson_shouldReturnCorrectObjectRepres
self::assertEquals("SL", $vehicle->getOperatorName());
self::assertEquals(275, $vehicle->getOperatorCode());
self::assertEquals("https://sl.se", $vehicle->getOperatorUrl());
self::assertEquals("57", $vehicle->getLineNumber());
}
}
46 changes: 22 additions & 24 deletions tests/Trafiklab/Sl/SlWrapperIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use Trafiklab\Common\Model\Exceptions\InvalidRequestException;
use Trafiklab\Common\Model\Exceptions\InvalidStopLocationException;
use Trafiklab\Common\Model\Exceptions\KeyRequiredException;
use Trafiklab\Sl\Model\SlRoutePlanningRequest;
use Trafiklab\Sl\Model\SlStopLocationLookupRequest;
use Trafiklab\Sl\Model\SlTimeTableRequest;

class SlWrapperIntegrationTest extends PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -50,7 +48,6 @@ public function testGetDepartures()
self::assertEquals(TimeTableType::DEPARTURES, $response->getType());
self::assertEquals("SL", $response->getTimetable()[0]->getOperator());


$departuresRequest = new SlTimeTableRequest();
$departuresRequest->setStopId("1001"); // Nybroplan boats
$departuresRequest->setTimeTableType(TimeTableType::DEPARTURES);
Expand All @@ -77,11 +74,11 @@ public function testGetDepartures_invalidStationId_shouldThrowException()

$this->expectException(InvalidStopLocationException::class);

$departuresRequest = new SlTimeTableRequest();
$slWrapper = new SlWrapper();
$departuresRequest = $slWrapper->createTimeTableRequestObject();
$departuresRequest->setStopId("123.56");
$departuresRequest->setTimeTableType(TimeTableType::DEPARTURES);

$slWrapper = new SlWrapper();
$slWrapper->setUserAgent("SDK Integration tests");
$slWrapper->setTimeTablesApiKey($this->_TIMETABLES_API_KEY);
$slWrapper->getTimeTable($departuresRequest);
Expand All @@ -95,11 +92,11 @@ public function testGetDepartures_invalidStationId_shouldThrowException()
public function testGetDepartures_invalidApiKey_shouldThrowException()
{
$this->expectException(InvalidKeyException::class);
$departuresRequest = new SlTimeTableRequest();
$slWrapper = new SlWrapper();
$departuresRequest = $slWrapper->createTimeTableRequestObject();
$departuresRequest->setStopId("1001");
$departuresRequest->setTimeTableType(TimeTableType::DEPARTURES);

$slWrapper = new SlWrapper();
$slWrapper->setUserAgent("SDK Integration tests");
$slWrapper->setTimeTablesApiKey("ABC123");
$slWrapper->getTimeTable($departuresRequest);
Expand All @@ -112,11 +109,11 @@ public function testGetDepartures_missingApiKey_shouldThrowException()
{
$this->expectException(KeyRequiredException::class);

$departuresRequest = new SlTimeTableRequest();
$slWrapper = new SlWrapper();
$departuresRequest = $slWrapper->createTimeTableRequestObject();
$departuresRequest->setStopId("1001");
$departuresRequest->setTimeTableType(TimeTableType::DEPARTURES);

$slWrapper = new SlWrapper();
$slWrapper->setUserAgent("SDK Integration tests");
$slWrapper->setTimeTablesApiKey("");
$slWrapper->getTimeTable($departuresRequest);
Expand All @@ -134,12 +131,12 @@ public function testGetRoutePlanning_validParameters_shouldReturnResponse()
$queryTime = new DateTime();
$queryTime->setTime(18, 0);

$routePlanningRequest = new SlRoutePlanningRequest();
$slWrapper = new SlWrapper();
$routePlanningRequest = $slWrapper->createRoutePlanningRequestObject();
$routePlanningRequest->setOriginStopId("9192");
$routePlanningRequest->setDestinationStopId("1002");
$routePlanningRequest->setDateTime($queryTime);

$slWrapper = new SlWrapper();
$slWrapper->setUserAgent("SDK Integration tests");
$slWrapper->setRoutePlanningApiKey($this->_ROUTEPLANNING_API_KEY);
$response = $slWrapper->getRoutePlanning($routePlanningRequest);
Expand All @@ -162,13 +159,13 @@ public function testGetRoutePlanning_WithVia_shouldReturnResponse()
$queryTime = new DateTime();
$queryTime->setTime(18, 0);

$routePlanningRequest = new SlRoutePlanningRequest();
$slWrapper = new SlWrapper();
$routePlanningRequest = $slWrapper->createRoutePlanningRequestObject();
$routePlanningRequest->setOriginStopId("1002");
$routePlanningRequest->setDestinationStopId("9192");
$routePlanningRequest->setViaStopId("9180");
$routePlanningRequest->setDateTime($queryTime);

$slWrapper = new SlWrapper();
$slWrapper->setUserAgent("SDK Integration tests");
$slWrapper->setRoutePlanningApiKey($this->_ROUTEPLANNING_API_KEY);
$response = $slWrapper->getRoutePlanning($routePlanningRequest);
Expand Down Expand Up @@ -197,18 +194,18 @@ public function testGetRoutePlanning_invalidStationId_shouldThrowException()
$this->markTestIncomplete();
}

$slWrapper = new SlWrapper();
$this->expectException(InvalidRequestException::class);
$routePlanningRequest = new SlRoutePlanningRequest();
$routePlanningRequest = $slWrapper->createRoutePlanningRequestObject();
$routePlanningRequest->setOriginStopId("1001");
$routePlanningRequest->setDestinationStopId("0");

$slWrapper = new SlWrapper();
$slWrapper->setUserAgent("SDK Integration tests");
$slWrapper->setRoutePlanningApiKey($this->_ROUTEPLANNING_API_KEY);
$slWrapper->getRoutePlanning($routePlanningRequest);

$this->expectException(InvalidStoplocationException::class);
$routePlanningRequest = new SlRoutePlanningRequest();
$routePlanningRequest = $slWrapper->createRoutePlanningRequestObject();
$routePlanningRequest->setOriginStopId("1001");
$routePlanningRequest->setDestinationStopId("45.45");
$slWrapper->getRoutePlanning($routePlanningRequest);
Expand All @@ -221,11 +218,11 @@ public function testGetRoutePlanning_invalidApiKey_shouldThrowException()
{
$this->expectException(InvalidKeyException::class);

$routePlanningRequest = new SlRoutePlanningRequest();
$slWrapper = new SlWrapper();
$routePlanningRequest = $slWrapper->createRoutePlanningRequestObject();
$routePlanningRequest->setOriginStopId("1001");
$routePlanningRequest->setDestinationStopId("2002");

$slWrapper = new SlWrapper();
$slWrapper->setUserAgent("SDK Integration tests");
$slWrapper->setRoutePlanningApiKey("ABC123");
$slWrapper->getRoutePlanning($routePlanningRequest);
Expand All @@ -241,11 +238,11 @@ public function testGetRoutePlanning_missingApiKey_shouldThrowException()
$queryTime = new DateTime();
$queryTime->setTime(18, 0);

$routePlanningRequest = new SlRoutePlanningRequest();
$slWrapper = new SlWrapper();
$routePlanningRequest = $slWrapper->createRoutePlanningRequestObject();
$routePlanningRequest->setOriginStopId("1001");
$routePlanningRequest->setDestinationStopId("2002");

$slWrapper = new SlWrapper();
$slWrapper->setUserAgent("SDK Integration tests");
$slWrapper->setRoutePlanningApiKey("");
$slWrapper->getRoutePlanning($routePlanningRequest);
Expand All @@ -265,12 +262,13 @@ public function testGetRoutePlanning_invalidDate_shouldThrowException()
$queryTime = new DateTime();
$queryTime->setDate(2100, 1, 1);

$routePlanningRequest = new SlRoutePlanningRequest();
$slWrapper = new SlWrapper();

$routePlanningRequest = $slWrapper->createRoutePlanningRequestObject();
$routePlanningRequest->setOriginStopId("1001");
$routePlanningRequest->setDestinationStopId("2002");
$routePlanningRequest->setDateTime($queryTime);

$slWrapper = new SlWrapper();
$slWrapper->setUserAgent("SDK Integration tests");
$slWrapper->setRoutePlanningApiKey($this->_ROUTEPLANNING_API_KEY);
$slWrapper->getRoutePlanning($routePlanningRequest);
Expand All @@ -286,7 +284,7 @@ public function testGetStopLocation_searchForCity_shouldReturnLargestStopsFirst(
$slWrapper->setUserAgent("SDK Integration tests");
$slWrapper->setStopLocationLookupApiKey($this->_STOPLOCATIONLOOKUP_API_KEY);

$stopLocationLookupRequest = new SlStopLocationLookupRequest();
$stopLocationLookupRequest = $slWrapper->createStopLocationLookupRequestObject();
$stopLocationLookupRequest->setSearchQuery("Stockholm");
$response = $slWrapper->lookupStopLocation($stopLocationLookupRequest);
/**
Expand Down Expand Up @@ -324,7 +322,7 @@ public function testGetStopLocation_searchForCity_shouldReturnLargestStopsFirst(
self::assertEquals("1080", $response->getFoundStopLocations()[0]->getId());
self::assertEquals("9000", $response->getFoundStopLocations()[1]->getId());

$stopLocationLookupRequest = new SlStopLocationLookupRequest();
$stopLocationLookupRequest = $slWrapper->createStopLocationLookupRequestObject();
$stopLocationLookupRequest->setSearchQuery("Sollentuna");
$response = $slWrapper->lookupStopLocation($stopLocationLookupRequest);
/**
Expand Down

0 comments on commit 74c9bdc

Please sign in to comment.