Skip to content

Commit

Permalink
Merge pull request #4 from MedeirosDev/feature/add-duration-in-traffi…
Browse files Browse the repository at this point in the history
…c-and-modify-json-is-public

Feature/add duration in traffic and modify json is public
  • Loading branch information
Jonathan Martin authored Dec 12, 2019
2 parents 83c7321 + 6e5c6dd commit ca7e498
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 21 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
vendor/
composer.lock
.env
.env

.idea/
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ language: php
php:
- '7.0'
- '7.1'
- nightly

before_script: composer install
script: phpunit --configuration phpunit.xml
script: ./vendor/bin/phpunit

notifications:
email:
Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ $request = DistanceMatrix::license($license);
$response = DistanceMatrix::license($license)
->addOrigin('norwich,gb')
->addDestination('52.603669, 1.223785')
->response();
->request();

// I want to make the following but of API better,
// as it looks horrible at the moment.
Expand All @@ -116,4 +116,12 @@ $distance = $element->distance();
$distanceText = $element->distanceText();
$duration = $element->duration();
$durationText = $element->durationText();
```
$durationInTraffic = $element->durationInTraffic();
$durationInTrafficText = $element->durationInTrafficText();

// or

$response->json['destination_addresses'][0];
$response->json['rows'][0]['elements'][0]['distance']['value'];
$response->json['rows'][0]['elements'][0]['duration_in_traffic']['text'];
```
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"guzzlehttp/guzzle": "^6.3"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
"phpunit/phpunit": "6.0",
"vlucas/phpdotenv": "^2.4",
"illuminate/support": "^5.4||^5.5||^5.6"
"illuminate/support": ">5.4"
},
"suggest": {
"illuminate/support": "Allows you to use this package from within Laravel"
Expand Down
12 changes: 6 additions & 6 deletions src/Response/DistanceMatrixResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DistanceMatrixResponse
/**
* @var array
*/
protected $json;
public $json;

/**
* DistanceMatrixResponse constructor.
Expand All @@ -37,7 +37,7 @@ public function __construct(ResponseInterface $response)
/**
* @return bool
*/
public function successful()
public function successful(): bool
{
return $this->json['status'] === static::RESPONSE_OKAY;
}
Expand Down Expand Up @@ -73,23 +73,23 @@ public function error()
/**
* @return array
*/
public function origins()
public function origins(): array
{
return $this->json["origin_addresses"];
}

/**
* @return array
*/
public function destinations()
public function destinations(): array
{
return $this->json["destination_addresses"];
}

/**
* @return array
*/
public function rows()
public function rows(): array
{
$rows = $this->json['rows'];

Expand All @@ -105,7 +105,7 @@ public function rows()
/**
* @param int $row
*
* @return \TeamPickr\DistanceMatrix\Response\Row
* @return null|\TeamPickr\DistanceMatrix\Response\Row
*/
public function row(int $row = 0)
{
Expand Down
29 changes: 22 additions & 7 deletions src/Response/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,55 @@ public function __construct(array $element)
/**
* @return bool
*/
public function successful()
public function successful(): bool
{
return $this->element['status'] == static::STATUS_OKAY;
}

/**
* @return mixed
* @return null|int
*/
public function distance()
{
return ! empty($this->element['distance']['value']) ? $this->element['distance']['value'] : null;
}

/**
* @return mixed
* @return null|string
*/
public function distanceText()
{
return ! empty($this->element['distance']['text']) ? $this->element['distance']['text'] : null;
}

/**
* @return mixed
* @return null|int
*/
public function duration()
{
return ! empty($this->element['duration']['value']) ? $this->element['duration']['value'] : null;
return ! empty($this->element['duration']['value']) ? (int) $this->element['duration']['value'] : null;
}

/**
* @return mixed
* @return null|string
*/
public function durationText()
{
return ! empty($this->element['duration']['text']) ? $this->element['duration']['text'] : null;
return ! empty($this->element['duration']['text']) ? (string) $this->element['duration']['text'] : null;
}

/**
* @return null|int
*/
public function durationInTraffic()
{
return ! empty($this->element['duration_in_traffic']['value']) ? (int) $this->element['duration_in_traffic']['value'] : null;
}
/**
* @return null|string
*/
public function durationInTrafficText()
{
return ! empty($this->element['duration_in_traffic']['text']) ? (string) $this->element['duration_in_traffic']['text'] : null;
}
}
11 changes: 11 additions & 0 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,15 @@ public function makeSuccessfulMockHandler()
]);
}


/**
* @return \GuzzleHttp\Handler\MockHandler
*/
public function makeSuccessfulWithDurationInTrafficMockHandler()
{
return new MockHandler([
new Response(200, [], file_get_contents(__DIR__ . '/responses/with_duration_in_traffic.json')),
]);
}

}
26 changes: 26 additions & 0 deletions tests/DistanceMatrixRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,30 @@ public function can_make_request()
$this->assertTrue($response->successful());
$this->assertNotNull($response->successful());
}


/** @test */
public function return_duration_in_traffic_request()
{
$date = 'now';

$distanceMatrix = $this->newInstance()
->addOrigin('Campinas, SP, BR')
->addDestination('Campinas - SP, Brasil')
->setArrivalTime($date);

$response = $this->makeTestRequest($distanceMatrix, $this->makeSuccessfulWithDurationInTrafficMockHandler())->request();
$element = $response->rows()[0]->elements()[0];

$request = $this->container[0]['request'];

$this->assertContains("arrival_time=" . $date, $request->getUri()->getQuery());
$this->assertInstanceOf(DistanceMatrixResponse::class, $response);
$this->assertTrue($response->successful());
$this->assertNotNull($response->successful());
$this->assertInstanceOf(Element::class, $element);
$this->assertEquals(5496, $element->durationInTraffic());
$this->assertEquals('1 hora 32 minutos', $element->durationInTrafficText());

}
}
6 changes: 5 additions & 1 deletion tests/PremiumLicenseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace TeamPickr\DistanceMatrix\Tests;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
use TeamPickr\DistanceMatrix\DistanceMatrix;
use TeamPickr\DistanceMatrix\Licenses\PremiumLicense;
Expand All @@ -18,7 +20,9 @@ public function can_make_premium_license()
->addOrigin('norwich,gb')
->addDestination('ipswich,gb');

$this->makeTestRequest($distanceMatrix)->request();
$mock = new MockHandler([new Response(200)]);

$this->makeTestRequest($distanceMatrix, $mock)->request();

$request = $this->container[0]['request'];

Expand Down
26 changes: 26 additions & 0 deletions tests/responses/with_duration_in_traffic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"destination_addresses" : [ "São Paulo, SP, Brasil" ],
"origin_addresses" : [ "Campinas - SP, Brasil" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "95,8 km",
"value" : 95845
},
"duration" : {
"text" : "1 hora 14 minutos",
"value" : 4445
},
"duration_in_traffic" : {
"text" : "1 hora 32 minutos",
"value" : 5496
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}

0 comments on commit ca7e498

Please sign in to comment.