Skip to content

Commit ca7e498

Browse files
author
Jonathan Martin
authored
Merge pull request #4 from MedeirosDev/feature/add-duration-in-traffic-and-modify-json-is-public
Feature/add duration in traffic and modify json is public
2 parents 83c7321 + 6e5c6dd commit ca7e498

10 files changed

+112
-21
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
vendor/
22
composer.lock
3-
.env
3+
.env
4+
5+
.idea/

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ language: php
22
php:
33
- '7.0'
44
- '7.1'
5-
- nightly
65

76
before_script: composer install
8-
script: phpunit --configuration phpunit.xml
7+
script: ./vendor/bin/phpunit
98

109
notifications:
1110
email:

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ $request = DistanceMatrix::license($license);
104104
$response = DistanceMatrix::license($license)
105105
->addOrigin('norwich,gb')
106106
->addDestination('52.603669, 1.223785')
107-
->response();
107+
->request();
108108

109109
// I want to make the following but of API better,
110110
// as it looks horrible at the moment.
@@ -116,4 +116,12 @@ $distance = $element->distance();
116116
$distanceText = $element->distanceText();
117117
$duration = $element->duration();
118118
$durationText = $element->durationText();
119-
```
119+
$durationInTraffic = $element->durationInTraffic();
120+
$durationInTrafficText = $element->durationInTrafficText();
121+
122+
// or
123+
124+
$response->json['destination_addresses'][0];
125+
$response->json['rows'][0]['elements'][0]['distance']['value'];
126+
$response->json['rows'][0]['elements'][0]['duration_in_traffic']['text'];
127+
```

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
"guzzlehttp/guzzle": "^6.3"
1515
},
1616
"require-dev": {
17-
"phpunit/phpunit": "^6.0",
17+
"phpunit/phpunit": "6.0",
1818
"vlucas/phpdotenv": "^2.4",
19-
"illuminate/support": "^5.4||^5.5||^5.6"
19+
"illuminate/support": ">5.4"
2020
},
2121
"suggest": {
2222
"illuminate/support": "Allows you to use this package from within Laravel"

src/Response/DistanceMatrixResponse.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class DistanceMatrixResponse
2121
/**
2222
* @var array
2323
*/
24-
protected $json;
24+
public $json;
2525

2626
/**
2727
* DistanceMatrixResponse constructor.
@@ -37,7 +37,7 @@ public function __construct(ResponseInterface $response)
3737
/**
3838
* @return bool
3939
*/
40-
public function successful()
40+
public function successful(): bool
4141
{
4242
return $this->json['status'] === static::RESPONSE_OKAY;
4343
}
@@ -73,23 +73,23 @@ public function error()
7373
/**
7474
* @return array
7575
*/
76-
public function origins()
76+
public function origins(): array
7777
{
7878
return $this->json["origin_addresses"];
7979
}
8080

8181
/**
8282
* @return array
8383
*/
84-
public function destinations()
84+
public function destinations(): array
8585
{
8686
return $this->json["destination_addresses"];
8787
}
8888

8989
/**
9090
* @return array
9191
*/
92-
public function rows()
92+
public function rows(): array
9393
{
9494
$rows = $this->json['rows'];
9595

@@ -105,7 +105,7 @@ public function rows()
105105
/**
106106
* @param int $row
107107
*
108-
* @return \TeamPickr\DistanceMatrix\Response\Row
108+
* @return null|\TeamPickr\DistanceMatrix\Response\Row
109109
*/
110110
public function row(int $row = 0)
111111
{

src/Response/Element.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,55 @@ public function __construct(array $element)
2727
/**
2828
* @return bool
2929
*/
30-
public function successful()
30+
public function successful(): bool
3131
{
3232
return $this->element['status'] == static::STATUS_OKAY;
3333
}
3434

3535
/**
36-
* @return mixed
36+
* @return null|int
3737
*/
3838
public function distance()
3939
{
4040
return ! empty($this->element['distance']['value']) ? $this->element['distance']['value'] : null;
4141
}
4242

4343
/**
44-
* @return mixed
44+
* @return null|string
4545
*/
4646
public function distanceText()
4747
{
4848
return ! empty($this->element['distance']['text']) ? $this->element['distance']['text'] : null;
4949
}
5050

5151
/**
52-
* @return mixed
52+
* @return null|int
5353
*/
5454
public function duration()
5555
{
56-
return ! empty($this->element['duration']['value']) ? $this->element['duration']['value'] : null;
56+
return ! empty($this->element['duration']['value']) ? (int) $this->element['duration']['value'] : null;
5757
}
5858

5959
/**
60-
* @return mixed
60+
* @return null|string
6161
*/
6262
public function durationText()
6363
{
64-
return ! empty($this->element['duration']['text']) ? $this->element['duration']['text'] : null;
64+
return ! empty($this->element['duration']['text']) ? (string) $this->element['duration']['text'] : null;
65+
}
66+
67+
/**
68+
* @return null|int
69+
*/
70+
public function durationInTraffic()
71+
{
72+
return ! empty($this->element['duration_in_traffic']['value']) ? (int) $this->element['duration_in_traffic']['value'] : null;
73+
}
74+
/**
75+
* @return null|string
76+
*/
77+
public function durationInTrafficText()
78+
{
79+
return ! empty($this->element['duration_in_traffic']['text']) ? (string) $this->element['duration_in_traffic']['text'] : null;
6580
}
6681
}

tests/AbstractTestCase.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,15 @@ public function makeSuccessfulMockHandler()
6666
]);
6767
}
6868

69+
70+
/**
71+
* @return \GuzzleHttp\Handler\MockHandler
72+
*/
73+
public function makeSuccessfulWithDurationInTrafficMockHandler()
74+
{
75+
return new MockHandler([
76+
new Response(200, [], file_get_contents(__DIR__ . '/responses/with_duration_in_traffic.json')),
77+
]);
78+
}
79+
6980
}

tests/DistanceMatrixRequestTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,30 @@ public function can_make_request()
168168
$this->assertTrue($response->successful());
169169
$this->assertNotNull($response->successful());
170170
}
171+
172+
173+
/** @test */
174+
public function return_duration_in_traffic_request()
175+
{
176+
$date = 'now';
177+
178+
$distanceMatrix = $this->newInstance()
179+
->addOrigin('Campinas, SP, BR')
180+
->addDestination('Campinas - SP, Brasil')
181+
->setArrivalTime($date);
182+
183+
$response = $this->makeTestRequest($distanceMatrix, $this->makeSuccessfulWithDurationInTrafficMockHandler())->request();
184+
$element = $response->rows()[0]->elements()[0];
185+
186+
$request = $this->container[0]['request'];
187+
188+
$this->assertContains("arrival_time=" . $date, $request->getUri()->getQuery());
189+
$this->assertInstanceOf(DistanceMatrixResponse::class, $response);
190+
$this->assertTrue($response->successful());
191+
$this->assertNotNull($response->successful());
192+
$this->assertInstanceOf(Element::class, $element);
193+
$this->assertEquals(5496, $element->durationInTraffic());
194+
$this->assertEquals('1 hora 32 minutos', $element->durationInTrafficText());
195+
196+
}
171197
}

tests/PremiumLicenseTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace TeamPickr\DistanceMatrix\Tests;
44

5+
use GuzzleHttp\Handler\MockHandler;
6+
use GuzzleHttp\Psr7\Response;
57
use PHPUnit\Framework\TestCase;
68
use TeamPickr\DistanceMatrix\DistanceMatrix;
79
use TeamPickr\DistanceMatrix\Licenses\PremiumLicense;
@@ -18,7 +20,9 @@ public function can_make_premium_license()
1820
->addOrigin('norwich,gb')
1921
->addDestination('ipswich,gb');
2022

21-
$this->makeTestRequest($distanceMatrix)->request();
23+
$mock = new MockHandler([new Response(200)]);
24+
25+
$this->makeTestRequest($distanceMatrix, $mock)->request();
2226

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"destination_addresses" : [ "São Paulo, SP, Brasil" ],
3+
"origin_addresses" : [ "Campinas - SP, Brasil" ],
4+
"rows" : [
5+
{
6+
"elements" : [
7+
{
8+
"distance" : {
9+
"text" : "95,8 km",
10+
"value" : 95845
11+
},
12+
"duration" : {
13+
"text" : "1 hora 14 minutos",
14+
"value" : 4445
15+
},
16+
"duration_in_traffic" : {
17+
"text" : "1 hora 32 minutos",
18+
"value" : 5496
19+
},
20+
"status" : "OK"
21+
}
22+
]
23+
}
24+
],
25+
"status" : "OK"
26+
}

0 commit comments

Comments
 (0)