Skip to content

Commit 453d1d1

Browse files
committed
Added iterator and its test
1 parent 1a93a78 commit 453d1d1

File tree

6 files changed

+525
-2
lines changed

6 files changed

+525
-2
lines changed

Tests/Iterator/SearchIteratorTest.php

+334
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,334 @@
1+
<?php
2+
3+
namespace Nekland\PlacesApi\Tests\Iterator;
4+
5+
6+
use Nekland\BaseApi\Http\Request;
7+
use Nekland\PlacesApi\Api\Search;
8+
use Prophecy\Argument;
9+
10+
class SearchIteratorTest extends \PHPUnit_Framework_TestCase
11+
{
12+
/**
13+
* @var \Nekland\BaseApi\Http\AbstractHttpClient|\Prophecy\Prophecy\ObjectProphecy
14+
*/
15+
private $httpClient;
16+
17+
/**
18+
* @var Search
19+
*/
20+
private $searchApi;
21+
22+
protected function setUp()
23+
{
24+
parent::setUp();
25+
26+
$this->httpClient = $this->prophesize('Nekland\BaseApi\Http\AbstractHttpClient');
27+
28+
$this->httpClient->send(Argument::that(function ($arg) {
29+
if (!$arg instanceof Request) {
30+
return false;
31+
}
32+
$body = $arg->getBody();
33+
34+
if (!empty($body['pagetoken'])) {
35+
return false;
36+
}
37+
38+
return true;
39+
}))->will([$this, 'fakeHttpSend']);
40+
41+
/*
42+
$this->httpClient->send(Argument::that(function ($arg) {
43+
if (!$arg instanceof Request) {
44+
return false;
45+
}
46+
$body = $arg->getBody();
47+
48+
if (!empty($body['pagetoken'])) {
49+
return false;
50+
}
51+
52+
return true;
53+
}))->willReturn($this->getLastPageJson())->shouldBeCalledTimes(1);
54+
55+
*/
56+
$this->searchApi = new Search($this->httpClient->reveal());
57+
}
58+
59+
public function testIterator()
60+
{
61+
$iterator = $this->searchApi->getSearchIterator('12,23', '100');
62+
63+
foreach($iterator as $key => $value) {
64+
if ($key === 0 && $value['results'][0]['id'] !== 'foobaz') {
65+
throw new \Exception('Test fails: the first page is not what it should looks like.');
66+
}
67+
68+
if ($key === 1 && $value['results'][0]['id'] !== 'foobar') {
69+
throw new \Exception('Test fails: the second page is not what it should looks like.');
70+
}
71+
72+
73+
if ($key === 2) {
74+
throw new \Exception('Too much iterations');
75+
}
76+
}
77+
}
78+
79+
public function fakeHttpSend($args)
80+
{
81+
/** @var Request $request */
82+
$request = $args[0];
83+
$body = $request->getParameters();
84+
85+
if (!empty($body['pagetoken'])) {
86+
return $this->getLastPageJson();
87+
}
88+
89+
return $this->getFirstPageJson();
90+
}
91+
92+
private function getFirstPageJson()
93+
{
94+
return '{
95+
"html_attributions" : [],
96+
"next_page_token" : "CpQCAgEAAFxg8o-eU7_uKn7Yqjana-HQIx1hr5BrT4zBaEko29ANsXtp9mrqN0yrKWhf-y2PUpHRLQb1GT-mtxNcXou8TwkXhi1Jbk-ReY7oulyuvKSQrw1lgJElggGlo0d6indiH1U-tDwquw4tU_UXoQ_sj8OBo8XBUuWjuuFShqmLMP-0W59Vr6CaXdLrF8M3wFR4dUUhSf5UC4QCLaOMVP92lyh0OdtF_m_9Dt7lz-Wniod9zDrHeDsz_by570K3jL1VuDKTl_U1cJ0mzz_zDHGfOUf7VU1kVIs1WnM9SGvnm8YZURLTtMLMWx8-doGUE56Af_VfKjGDYW361OOIj9GmkyCFtaoCmTMIr5kgyeUSnB-IEhDlzujVrV6O9Mt7N4DagR6RGhT3g1viYLS4kO5YindU6dm3GIof1Q",
97+
"results" : [
98+
{
99+
"geometry" : {
100+
"location" : {
101+
"lat" : -33.870775,
102+
"lng" : 151.199025
103+
}
104+
},
105+
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/travel_agent-71.png",
106+
"id" : "foobaz",
107+
"name" : "Rhythmboat Cruises",
108+
"opening_hours" : {
109+
"open_now" : true
110+
},
111+
"photos" : [
112+
{
113+
"height" : 270,
114+
"html_attributions" : [],
115+
"photo_reference" : "CnRnAAAAF-LjFR1ZV93eawe1cU_3QNMCNmaGkowY7CnOf-kcNmPhNnPEG9W979jOuJJ1sGr75rhD5hqKzjD8vbMbSsRnq_Ni3ZIGfY6hKWmsOf3qHKJInkm4h55lzvLAXJVc-Rr4kI9O1tmIblblUpg2oqoq8RIQRMQJhFsTr5s9haxQ07EQHxoUO0ICubVFGYfJiMUPor1GnIWb5i8",
116+
"width" : 519
117+
}
118+
],
119+
"place_id" : "ChIJyWEHuEmuEmsRm9hTkapTCrk",
120+
"scope" : "GOOGLE",
121+
"alt_ids" : [
122+
{
123+
"place_id" : "D9iJyWEHuEmuEmsRm9hTkapTCrk",
124+
"scope" : "APP"
125+
}
126+
],
127+
"reference" : "CoQBdQAAAFSiijw5-cAV68xdf2O18pKIZ0seJh03u9h9wk_lEdG-cP1dWvp_QGS4SNCBMk_fB06YRsfMrNkINtPez22p5lRIlj5ty_HmcNwcl6GZXbD2RdXsVfLYlQwnZQcnu7ihkjZp_2gk1-fWXql3GQ8-1BEGwgCxG-eaSnIJIBPuIpihEhAY1WYdxPvOWsPnb2-nGb6QGhTipN0lgaLpQTnkcMeAIEvCsSa0Ww",
128+
"types" : [ "travel_agency", "restaurant", "food", "establishment" ],
129+
"vicinity" : "Pyrmont Bay Wharf Darling Dr, Sydney"
130+
},
131+
{
132+
"geometry" : {
133+
"location" : {
134+
"lat" : -33.866891,
135+
"lng" : 151.200814
136+
}
137+
},
138+
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
139+
"id" : "45a27fd8d56c56dc62afc9b49e1d850440d5c403",
140+
"name" : "Private Charter Sydney Habour Cruise",
141+
"photos" : [
142+
{
143+
"height" : 426,
144+
"html_attributions" : [],
145+
"photo_reference" : "CnRnAAAAL3n0Zu3U6fseyPl8URGKD49aGB2Wka7CKDZfamoGX2ZTLMBYgTUshjr-MXc0_O2BbvlUAZWtQTBHUVZ-5Sxb1-P-VX2Fx0sZF87q-9vUt19VDwQQmAX_mjQe7UWmU5lJGCOXSgxp2fu1b5VR_PF31RIQTKZLfqm8TA1eynnN4M1XShoU8adzJCcOWK0er14h8SqOIDZctvU",
146+
"width" : 640
147+
}
148+
],
149+
"place_id" : "ChIJqwS6fjiuEmsRJAMiOY9MSms",
150+
"scope" : "GOOGLE",
151+
"reference" : "CpQBhgAAAFN27qR_t5oSDKPUzjQIeQa3lrRpFTm5alW3ZYbMFm8k10ETbISfK9S1nwcJVfrP-bjra7NSPuhaRulxoonSPQklDyB-xGvcJncq6qDXIUQ3hlI-bx4AxYckAOX74LkupHq7bcaREgrSBE-U6GbA1C3U7I-HnweO4IPtztSEcgW09y03v1hgHzL8xSDElmkQtRIQzLbyBfj3e0FhJzABXjM2QBoUE2EnL-DzWrzpgmMEulUBLGrtu2Y",
152+
"types" : [ "restaurant", "food", "establishment" ],
153+
"vicinity" : "Australia"
154+
},
155+
{
156+
"geometry" : {
157+
"location" : {
158+
"lat" : -33.870943,
159+
"lng" : 151.190311
160+
}
161+
},
162+
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
163+
"id" : "30bee58f819b6c47bd24151802f25ecf11df8943",
164+
"name" : "Bucks Party Cruise",
165+
"opening_hours" : {
166+
"open_now" : true
167+
},
168+
"photos" : [
169+
{
170+
"height" : 600,
171+
"html_attributions" : [],
172+
"photo_reference" : "CnRnAAAA48AX5MsHIMiuipON_Lgh97hPiYDFkxx_vnaZQMOcvcQwYN92o33t5RwjRpOue5R47AjfMltntoz71hto40zqo7vFyxhDuuqhAChKGRQ5mdO5jv5CKWlzi182PICiOb37PiBtiFt7lSLe1SedoyrD-xIQD8xqSOaejWejYHCN4Ye2XBoUT3q2IXJQpMkmffJiBNftv8QSwF4",
173+
"width" : 800
174+
}
175+
],
176+
"place_id" : "ChIJLfySpTOuEmsRsc_JfJtljdc",
177+
"scope" : "GOOGLE",
178+
"reference" : "CoQBdQAAANQSThnTekt-UokiTiX3oUFT6YDfdQJIG0ljlQnkLfWefcKmjxax0xmUpWjmpWdOsScl9zSyBNImmrTO9AE9DnWTdQ2hY7n-OOU4UgCfX7U0TE1Vf7jyODRISbK-u86TBJij0b2i7oUWq2bGr0cQSj8CV97U5q8SJR3AFDYi3ogqEhCMXjNLR1k8fiXTkG2BxGJmGhTqwE8C4grdjvJ0w5UsAVoOH7v8HQ",
179+
"types" : [ "restaurant", "food", "establishment" ],
180+
"vicinity" : "37 Bank St, Pyrmont"
181+
},
182+
{
183+
"geometry" : {
184+
"location" : {
185+
"lat" : -33.867591,
186+
"lng" : 151.201196
187+
}
188+
},
189+
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/travel_agent-71.png",
190+
"id" : "a97f9fb468bcd26b68a23072a55af82d4b325e0d",
191+
"name" : "Australian Cruise Group",
192+
"opening_hours" : {
193+
"open_now" : true
194+
},
195+
"photos" : [
196+
{
197+
"height" : 242,
198+
"html_attributions" : [],
199+
"photo_reference" : "CnRnAAAABjeoPQ7NUU3pDitV4Vs0BgP1FLhf_iCgStUZUr4ZuNqQnc5k43jbvjKC2hTGM8SrmdJYyOyxRO3D2yutoJwVC4Vp_dzckkjG35L6LfMm5sjrOr6uyOtr2PNCp1xQylx6vhdcpW8yZjBZCvVsjNajLBIQ-z4ttAMIc8EjEZV7LsoFgRoU6OrqxvKCnkJGb9F16W57iIV4LuM",
200+
"width" : 200
201+
}
202+
],
203+
"place_id" : "ChIJrTLr-GyuEmsRBfy61i59si0",
204+
"scope" : "GOOGLE",
205+
"reference" : "CoQBeQAAAFvf12y8veSQMdIMmAXQmus1zqkgKQ-O2KEX0Kr47rIRTy6HNsyosVl0CjvEBulIu_cujrSOgICdcxNioFDHtAxXBhqeR-8xXtm52Bp0lVwnO3LzLFY3jeo8WrsyIwNE1kQlGuWA4xklpOknHJuRXSQJVheRlYijOHSgsBQ35mOcEhC5IpbpqCMe82yR136087wZGhSziPEbooYkHLn9e5njOTuBprcfVw",
206+
"types" : [ "travel_agency", "restaurant", "food", "establishment" ],
207+
"vicinity" : "32 The Promenade, King Street Wharf 5, Sydney"
208+
}
209+
],
210+
"status" : "OK"
211+
}';
212+
}
213+
214+
private function getLastPageJson()
215+
{
216+
return '{
217+
"html_attributions" : [],
218+
"results" : [
219+
{
220+
"geometry" : {
221+
"location" : {
222+
"lat" : -33.870775,
223+
"lng" : 151.199025
224+
}
225+
},
226+
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/travel_agent-71.png",
227+
"id" : "foobar",
228+
"name" : "Rhythmboat Cruises",
229+
"opening_hours" : {
230+
"open_now" : true
231+
},
232+
"photos" : [
233+
{
234+
"height" : 270,
235+
"html_attributions" : [],
236+
"photo_reference" : "CnRnAAAAF-LjFR1ZV93eawe1cU_3QNMCNmaGkowY7CnOf-kcNmPhNnPEG9W979jOuJJ1sGr75rhD5hqKzjD8vbMbSsRnq_Ni3ZIGfY6hKWmsOf3qHKJInkm4h55lzvLAXJVc-Rr4kI9O1tmIblblUpg2oqoq8RIQRMQJhFsTr5s9haxQ07EQHxoUO0ICubVFGYfJiMUPor1GnIWb5i8",
237+
"width" : 519
238+
}
239+
],
240+
"place_id" : "ChIJyWEHuEmuEmsRm9hTkapTCrk",
241+
"scope" : "GOOGLE",
242+
"alt_ids" : [
243+
{
244+
"place_id" : "D9iJyWEHuEmuEmsRm9hTkapTCrk",
245+
"scope" : "APP"
246+
}
247+
],
248+
"reference" : "CoQBdQAAAFSiijw5-cAV68xdf2O18pKIZ0seJh03u9h9wk_lEdG-cP1dWvp_QGS4SNCBMk_fB06YRsfMrNkINtPez22p5lRIlj5ty_HmcNwcl6GZXbD2RdXsVfLYlQwnZQcnu7ihkjZp_2gk1-fWXql3GQ8-1BEGwgCxG-eaSnIJIBPuIpihEhAY1WYdxPvOWsPnb2-nGb6QGhTipN0lgaLpQTnkcMeAIEvCsSa0Ww",
249+
"types" : [ "travel_agency", "restaurant", "food", "establishment" ],
250+
"vicinity" : "Pyrmont Bay Wharf Darling Dr, Sydney"
251+
},
252+
{
253+
"geometry" : {
254+
"location" : {
255+
"lat" : -33.866891,
256+
"lng" : 151.200814
257+
}
258+
},
259+
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
260+
"id" : "45a27fd8d56c56dc62afc9b49e1d850440d5c403",
261+
"name" : "Private Charter Sydney Habour Cruise",
262+
"photos" : [
263+
{
264+
"height" : 426,
265+
"html_attributions" : [],
266+
"photo_reference" : "CnRnAAAAL3n0Zu3U6fseyPl8URGKD49aGB2Wka7CKDZfamoGX2ZTLMBYgTUshjr-MXc0_O2BbvlUAZWtQTBHUVZ-5Sxb1-P-VX2Fx0sZF87q-9vUt19VDwQQmAX_mjQe7UWmU5lJGCOXSgxp2fu1b5VR_PF31RIQTKZLfqm8TA1eynnN4M1XShoU8adzJCcOWK0er14h8SqOIDZctvU",
267+
"width" : 640
268+
}
269+
],
270+
"place_id" : "ChIJqwS6fjiuEmsRJAMiOY9MSms",
271+
"scope" : "GOOGLE",
272+
"reference" : "CpQBhgAAAFN27qR_t5oSDKPUzjQIeQa3lrRpFTm5alW3ZYbMFm8k10ETbISfK9S1nwcJVfrP-bjra7NSPuhaRulxoonSPQklDyB-xGvcJncq6qDXIUQ3hlI-bx4AxYckAOX74LkupHq7bcaREgrSBE-U6GbA1C3U7I-HnweO4IPtztSEcgW09y03v1hgHzL8xSDElmkQtRIQzLbyBfj3e0FhJzABXjM2QBoUE2EnL-DzWrzpgmMEulUBLGrtu2Y",
273+
"types" : [ "restaurant", "food", "establishment" ],
274+
"vicinity" : "Australia"
275+
},
276+
{
277+
"geometry" : {
278+
"location" : {
279+
"lat" : -33.870943,
280+
"lng" : 151.190311
281+
}
282+
},
283+
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
284+
"id" : "30bee58f819b6c47bd24151802f25ecf11df8943",
285+
"name" : "Bucks Party Cruise",
286+
"opening_hours" : {
287+
"open_now" : true
288+
},
289+
"photos" : [
290+
{
291+
"height" : 600,
292+
"html_attributions" : [],
293+
"photo_reference" : "CnRnAAAA48AX5MsHIMiuipON_Lgh97hPiYDFkxx_vnaZQMOcvcQwYN92o33t5RwjRpOue5R47AjfMltntoz71hto40zqo7vFyxhDuuqhAChKGRQ5mdO5jv5CKWlzi182PICiOb37PiBtiFt7lSLe1SedoyrD-xIQD8xqSOaejWejYHCN4Ye2XBoUT3q2IXJQpMkmffJiBNftv8QSwF4",
294+
"width" : 800
295+
}
296+
],
297+
"place_id" : "ChIJLfySpTOuEmsRsc_JfJtljdc",
298+
"scope" : "GOOGLE",
299+
"reference" : "CoQBdQAAANQSThnTekt-UokiTiX3oUFT6YDfdQJIG0ljlQnkLfWefcKmjxax0xmUpWjmpWdOsScl9zSyBNImmrTO9AE9DnWTdQ2hY7n-OOU4UgCfX7U0TE1Vf7jyODRISbK-u86TBJij0b2i7oUWq2bGr0cQSj8CV97U5q8SJR3AFDYi3ogqEhCMXjNLR1k8fiXTkG2BxGJmGhTqwE8C4grdjvJ0w5UsAVoOH7v8HQ",
300+
"types" : [ "restaurant", "food", "establishment" ],
301+
"vicinity" : "37 Bank St, Pyrmont"
302+
},
303+
{
304+
"geometry" : {
305+
"location" : {
306+
"lat" : -33.867591,
307+
"lng" : 151.201196
308+
}
309+
},
310+
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/travel_agent-71.png",
311+
"id" : "a97f9fb468bcd26b68a23072a55af82d4b325e0d",
312+
"name" : "Australian Cruise Group",
313+
"opening_hours" : {
314+
"open_now" : true
315+
},
316+
"photos" : [
317+
{
318+
"height" : 242,
319+
"html_attributions" : [],
320+
"photo_reference" : "CnRnAAAABjeoPQ7NUU3pDitV4Vs0BgP1FLhf_iCgStUZUr4ZuNqQnc5k43jbvjKC2hTGM8SrmdJYyOyxRO3D2yutoJwVC4Vp_dzckkjG35L6LfMm5sjrOr6uyOtr2PNCp1xQylx6vhdcpW8yZjBZCvVsjNajLBIQ-z4ttAMIc8EjEZV7LsoFgRoU6OrqxvKCnkJGb9F16W57iIV4LuM",
321+
"width" : 200
322+
}
323+
],
324+
"place_id" : "ChIJrTLr-GyuEmsRBfy61i59si0",
325+
"scope" : "GOOGLE",
326+
"reference" : "CoQBeQAAAFvf12y8veSQMdIMmAXQmus1zqkgKQ-O2KEX0Kr47rIRTy6HNsyosVl0CjvEBulIu_cujrSOgICdcxNioFDHtAxXBhqeR-8xXtm52Bp0lVwnO3LzLFY3jeo8WrsyIwNE1kQlGuWA4xklpOknHJuRXSQJVheRlYijOHSgsBQ35mOcEhC5IpbpqCMe82yR136087wZGhSziPEbooYkHLn9e5njOTuBprcfVw",
327+
"types" : [ "travel_agency", "restaurant", "food", "establishment" ],
328+
"vicinity" : "32 The Promenade, King Street Wharf 5, Sydney"
329+
}
330+
],
331+
"status" : "OK"
332+
}';
333+
}
334+
}

Tests/bootstrap.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
if (!is_file($autoloadFile = __DIR__.'/../vendor/autoload.php')) {
4+
throw new \LogicException('Could not find autoload.php in vendor/. Did you run "composer install --dev"?');
5+
}
6+
7+
require $autoloadFile;

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"nekland/base-api": "~1.0"
2121
},
2222
"require-dev": {
23-
"phpspec/phpspec": "~2.0"
23+
"phpspec/phpspec": "~2.0",
24+
"phpunit/phpunit": "~4.6"
2425
},
2526
"minimum-stability": "dev",
2627
"extra": {

phpunit.xml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit bootstrap="./Tests/bootstrap.php" colors="true">
4+
5+
<testsuites>
6+
<testsuite name="Nekland Google Places Api">
7+
<directory suffix="Test.php">./Tests</directory>
8+
</testsuite>
9+
</testsuites>
10+
11+
<filter>
12+
<whitelist>
13+
<directory>./</directory>
14+
<exclude>
15+
<directory>./Resources</directory>
16+
<directory>./Tests</directory>
17+
<directory>./vendor</directory>
18+
</exclude>
19+
</whitelist>
20+
</filter>
21+
</phpunit>

0 commit comments

Comments
 (0)