Skip to content

Commit 95fe3b2

Browse files
committed
Added tests and fixes
1 parent ca63b09 commit 95fe3b2

File tree

5 files changed

+179
-3
lines changed

5 files changed

+179
-3
lines changed

.travis.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ matrix:
1212
before_script:
1313
- composer install --dev --prefer-source
1414

15-
script: phpunit
15+
script:
16+
- vendor/bin/phpunit
17+
- vendor/bin/phpspec run

docs/api_autocomplete.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Available methods
2020
```php
2121
<?php
2222

23-
array Autocomplete::autocomplete($input [, array $other = []])
23+
array Autocomplete::complete($input [, array $other = []])
2424
```
2525

2626
* `input`: the input of your user
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
namespace spec\Nekland\PlacesApi\Api;
4+
5+
use Nekland\BaseApi\Http\AbstractHttpClient;
6+
use PhpSpec\ObjectBehavior;
7+
use Prophecy\Argument;
8+
9+
class AutocompleteSpec extends ObjectBehavior
10+
{
11+
function it_is_initializable()
12+
{
13+
$this->shouldHaveType('Nekland\PlacesApi\Api\Autocomplete');
14+
}
15+
16+
function let(AbstractHttpClient $client)
17+
{
18+
$this->beConstructedWith($client);
19+
}
20+
21+
function it_should_return_request_result(AbstractHttpClient $client)
22+
{
23+
$client->send(Argument::cetera())->willReturn('{
24+
"status": "OK",
25+
"predictions" : [
26+
{
27+
"description" : "Paris, France",
28+
"id" : "691b237b0322f28988f3ce03e321ff72a12167fd",
29+
"matched_substrings" : [
30+
{
31+
"length" : 5,
32+
"offset" : 0
33+
}
34+
],
35+
"place_id" : "ChIJD7fiBh9u5kcRYJSMaMOCCwQ",
36+
"reference" : "CjQlAAAA_KB6EEceSTfkteSSF6U0pvumHCoLUboRcDlAH05N1pZJLmOQbYmboEi0SwXBSoI2EhAhj249tFDCVh4R-PXZkPK8GhTBmp_6_lWljaf1joVs1SH2ttB_tw",
37+
"terms" : [
38+
{
39+
"offset" : 0,
40+
"value" : "Paris"
41+
},
42+
{
43+
"offset" : 7,
44+
"value" : "France"
45+
}
46+
],
47+
"types" : [ "locality", "political", "geocode" ]
48+
},
49+
{
50+
"description" : "Paris Avenue, Earlwood, New South Wales, Australia",
51+
"id" : "359a75f8beff14b1c94f3d42c2aabfac2afbabad",
52+
"matched_substrings" : [
53+
{
54+
"length" : 5,
55+
"offset" : 0
56+
}
57+
],
58+
"place_id" : "ChIJrU3KAHG6EmsR5Uwfrk7azrI",
59+
"reference" : "CkQ2AAAARbzLE-tsSQPgwv8JKBaVtbjY48kInQo9tny0k07FOYb3Z_z_yDTFhQB_Ehpu-IKhvj8Msdb1rJlX7xMr9kfOVRIQVuL4tOtx9L7U8pC0Zx5bLBoUTFbw9R2lTn_EuBayhDvugt8T0Oo",
60+
"terms" : [
61+
{
62+
"offset" : 0,
63+
"value" : "Paris Avenue"
64+
},
65+
{
66+
"offset" : 14,
67+
"value" : "Earlwood"
68+
},
69+
{
70+
"offset" : 24,
71+
"value" : "New South Wales"
72+
},
73+
{
74+
"offset" : 41,
75+
"value" : "Australia"
76+
}
77+
],
78+
"types" : [ "route", "geocode" ]
79+
},
80+
{
81+
"description" : "Paris Street, Carlton, New South Wales, Australia",
82+
"id" : "bee539812eeda477dad282bcc8310758fb31d64d",
83+
"matched_substrings" : [
84+
{
85+
"length" : 5,
86+
"offset" : 0
87+
}
88+
],
89+
"place_id" : "ChIJCfeffMi5EmsRp7ykjcnb3VY",
90+
"reference" : "CkQ1AAAAAERlxMXkaNPLDxUJFLm4xkzX_h8I49HvGPvmtZjlYSVWp9yUhQSwfsdveHV0yhzYki3nguTBTVX2NzmJDukq9RIQNcoFTuz642b4LIzmLgcr5RoUrZhuNqnFHegHsAjtoUUjmhy4_rA",
91+
"terms" : [
92+
{
93+
"offset" : 0,
94+
"value" : "Paris Street"
95+
},
96+
{
97+
"offset" : 14,
98+
"value" : "Carlton"
99+
},
100+
{
101+
"offset" : 23,
102+
"value" : "New South Wales"
103+
},
104+
{
105+
"offset" : 40,
106+
"value" : "Australia"
107+
}
108+
],
109+
"types" : [ "route", "geocode" ]
110+
}
111+
]
112+
}');
113+
$this->complete('Paris')->shouldContainsPlaceId('ChIJD7fiBh9u5kcRYJSMaMOCCwQ');
114+
}
115+
116+
function it_shoud_return_empty_array_when_no_results(AbstractHttpClient $client)
117+
{
118+
$client->send(Argument::cetera())->willReturn('{"status": "ZERO_RESULTS"}');
119+
120+
$this->complete('arfzegreggdfgdsfd')->shouldReturn([]);
121+
}
122+
123+
function it_should_throw_exception_when_quota_is_out(AbstractHttpClient $client)
124+
{
125+
$client->send(Argument::cetera())->willReturn('{"status": "OVER_QUERY_LIMIT"}');
126+
127+
$this->shouldThrow('Nekland\BaseApi\Exception\QuotaLimitException')->duringComplete('Paris');
128+
}
129+
130+
function it_should_throw_exception_when_request_is_denied(AbstractHttpClient $client)
131+
{
132+
$client->send(Argument::cetera())->willReturn('{"status": "REQUEST_DENIED"}');
133+
134+
$this->shouldThrow('Nekland\BaseApi\Exception\AuthenticationException')->duringComplete('Paris');
135+
}
136+
137+
function it_should_throw_exception_when_request_is_invalid(AbstractHttpClient $client)
138+
{
139+
$client->send(Argument::cetera())->willReturn('{"status": "INVALID_REQUEST"}');
140+
141+
$this->shouldThrow('\InvalidArgumentException')->duringComplete('Paris', ['someweird_param' => 'foobar']);
142+
}
143+
144+
public function getMatchers()
145+
{
146+
return [
147+
'containsPlaceId' => function ($subject, $param) {
148+
return isset($subject[0]) && $subject[0]['place_id'] == $param;
149+
}
150+
];
151+
}
152+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace spec\Nekland\PlacesApi\Iterator;
4+
5+
use Nekland\PlacesApi\Api\Search;
6+
use PhpSpec\ObjectBehavior;
7+
use Prophecy\Argument;
8+
use Symfony\Bridge\Twig\Tests\Node\SearchAndRenderBlockNodeTest;
9+
10+
class SearchIteratorSpec extends ObjectBehavior
11+
{
12+
function it_is_initializable()
13+
{
14+
$this->shouldHaveType('Nekland\PlacesApi\Iterator\SearchIterator');
15+
$this->shouldHaveType('\Iterator');
16+
}
17+
18+
function let(Search $search)
19+
{
20+
$this->beConstructedWith($search, []);
21+
}
22+
}

src/Nekland/PlacesApi/Api/Autocomplete.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Autocomplete extends AbstractApi
2020
{
2121
const URL = 'autocomplete/json';
2222

23-
public function autocomplete($input, array $other = [])
23+
public function complete($input, array $other = [])
2424
{
2525
$body = array_merge(['input' => $input], $other);
2626

0 commit comments

Comments
 (0)