forked from geocoder-php/Geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeocoderTest.php
More file actions
295 lines (241 loc) · 9.18 KB
/
Copy pathGeocoderTest.php
File metadata and controls
295 lines (241 loc) · 9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
namespace Geocoder\Tests;
use Geocoder\Geocoder;
use Geocoder\Provider\ProviderInterface;
use Geocoder\Result\Geocoded;
/**
* @author William Durand <william.durand1@gmail.com>
*/
class GeocoderTest extends TestCase
{
protected $geocoder;
protected function setUp()
{
$this->geocoder = new TestableGeocoder();
}
public function testRegisterProvider()
{
$provider = new MockProvider('test');
$this->geocoder->registerProvider($provider);
$this->assertSame($provider, $this->geocoder->getProvider());
}
public function testRegisterProviders()
{
$provider = new MockProvider('test');
$this->geocoder->registerProviders(array($provider));
$this->assertSame($provider, $this->geocoder->getProvider());
}
public function testUsing()
{
$provider1 = new MockProvider('test1');
$provider2 = new MockProvider('test2');
$this->geocoder->registerProviders(array($provider1, $provider2));
$this->assertSame($provider1, $this->geocoder->getProvider());
$this->geocoder->using('test1');
$this->assertSame($provider1, $this->geocoder->getProvider());
$this->geocoder->using('test2');
$this->assertSame($provider2, $this->geocoder->getProvider());
$this->geocoder->using('test1');
$this->assertSame($provider1, $this->geocoder->getProvider());
$this->geocoder->using('non_existant');
$this->assertSame($provider1, $this->geocoder->getProvider());
$this->geocoder->using(null);
$this->assertSame($provider1, $this->geocoder->getProvider());
$this->geocoder->using('');
$this->assertSame($provider1, $this->geocoder->getProvider());
}
public function testGetProviders()
{
$provider1 = new MockProvider('test1');
$provider2 = new MockProvider('test2');
$this->geocoder->registerProviders(array($provider1, $provider2));
$result = $this->geocoder->getProviders();
$expected = array(
'test1' => $provider1,
'test2' => $provider2
);
$this->assertSame($expected, $result);
$this->assertArrayHasKey('test1', $result);
$this->assertArrayHasKey('test2', $result);
}
/**
* @expectedException \RuntimeException
*/
public function testGetProvider()
{
$this->geocoder->getProvider();
$this->fail('getProvider() should throw an exception');
}
public function testGetProviderWithMultipleProvidersReturnsTheFirstOne()
{
$provider1 = new MockProvider('test1');
$provider2 = new MockProvider('test2');
$provider3 = new MockProvider('test3');
$this->geocoder->registerProviders(array($provider1, $provider2, $provider3));
$this->assertSame($provider1, $this->geocoder->getProvider());
}
public function testGeocodeReturnsInstanceOfResultInterface()
{
$this->geocoder->registerProvider(new MockProvider('test1'));
$this->assertInstanceOf('Geocoder\Result\ResultInterface', $this->geocoder->geocode('foobar'));
$this->assertInstanceOf('Geocoder\Result\Geocoded', $this->geocoder->geocode('foobar'));
}
public function testGeocodeEmpty()
{
$this->geocoder->registerProvider(new MockProviderWithRequestCount('test2'));
$this->assertEmptyResult($this->geocoder->geocode(''));
$this->assertEquals(0, $this->geocoder->getProvider('test2')->geocodeCount);
$this->assertEmptyResult($this->geocoder->geocode(null));
$this->assertEquals(0, $this->geocoder->getProvider('test2')->geocodeCount);
}
public function testReverseReturnsInstanceOfResultInterface()
{
$this->geocoder->registerProvider(new MockProvider('test1'));
$this->assertInstanceOf('Geocoder\Result\ResultInterface', $this->geocoder->reverse(1, 2));
$this->assertInstanceOf('Geocoder\Result\Geocoded', $this->geocoder->reverse(1, 2));
}
public function testReverseEmpty()
{
$this->geocoder->registerProvider(new MockProviderWithRequestCount('test2'));
$this->assertEmptyResult($this->geocoder->reverse('', ''));
$this->assertEquals(0, $this->geocoder->getProvider('test2')->geocodeCount);
$this->assertEmptyResult($this->geocoder->reverse(null, null));
$this->assertEquals(0, $this->geocoder->getProvider('test2')->geocodeCount);
}
public function testUseCustomDefaultResultFactory()
{
$factoryMock = $this->getMock('Geocoder\Result\DefaultResultFactory');
$factoryMock
->expects($this->once())
->method('newInstance')
->will($this->returnValue(new DummyResult()));
$geocoder = new TestableGeocoder(null, $factoryMock);
$this->assertInstanceOf('Geocoder\Tests\DummyResult', $geocoder->returnResult(array()));
}
public function testSetAndUseCustomDefaultResultFactory()
{
$factoryMock = $this->getMock('Geocoder\Result\DefaultResultFactory');
$factoryMock
->expects($this->once())
->method('newInstance')
->will($this->returnValue(new DummyResult()));
$geocoder = new TestableGeocoder();
$geocoder->setResultFactory($factoryMock);
$this->assertInstanceOf('Geocoder\Tests\DummyResult', $geocoder->returnResult(array()));
}
public function testUseCustomMultipleResultFactory()
{
$factoryMock = $this->getMock('Geocoder\Result\MultipleResultFactory');
$factoryMock->expects($this->at(0))->method('newInstance')->will($this->returnValue(new DummyResult()));
$factoryMock->expects($this->at(1))->method('newInstance')->will($this->returnValue(new DummyResult()));
$factoryMock->expects($this->at(2))->method('newInstance')->will($this->returnValue(new DummyResult()));
$geocoder = new TestableGeocoder(null, $factoryMock);
$results = $geocoder->returnResult(array(
array(),
array(),
array(),
));
$this->assertInstanceOf('\SplObjectStorage', $results);
$this->assertCount(3, $results);
foreach ($results as $result) {
$this->assertInstanceOf('Geocoder\Tests\DummyResult', $result);
$this->assertInstanceOf('Geocoder\Result\ResultInterface', $result);
}
}
public function testSetAndUseCustomMultipleResultFactory()
{
$factoryMock = $this->getMock('Geocoder\Result\MultipleResultFactory');
$factoryMock->expects($this->at(0))->method('newInstance')->will($this->returnValue(new DummyResult()));
$factoryMock->expects($this->at(1))->method('newInstance')->will($this->returnValue(new DummyResult()));
$factoryMock->expects($this->at(2))->method('newInstance')->will($this->returnValue(new DummyResult()));
$geocoder = new TestableGeocoder();
$geocoder->setResultFactory($factoryMock);
$results = $geocoder->returnResult(array(
array(),
array(),
array(),
));
$this->assertInstanceOf('\SplObjectStorage', $results);
$this->assertCount(3, $results);
foreach ($results as $result) {
$this->assertInstanceOf('Geocoder\Tests\DummyResult', $result);
$this->assertInstanceOf('Geocoder\Result\ResultInterface', $result);
}
}
public function testSetMaxResults()
{
$this->geocoder->limit(3);
$this->assertSame(3, $this->geocoder->getMaxResults());
}
public function testDefaultMaxResults()
{
$this->assertSame(Geocoder::MAX_RESULTS, $this->geocoder->getMaxResults());
}
protected function assertEmptyResult($result)
{
$this->assertEquals(0, $result->getLatitude());
$this->assertEquals(0, $result->getLongitude());
$this->assertEquals('', $result->getCity());
$this->assertEquals('', $result->getZipcode());
$this->assertEquals('', $result->getRegion());
$this->assertEquals('', $result->getCountry());
}
}
class MockProvider implements ProviderInterface
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
public function getGeocodedData($address)
{
return array();
}
public function getReversedData(array $coordinates)
{
return array();
}
public function getName()
{
return $this->name;
}
public function setMaxResults($maxResults)
{
return $this;
}
}
class MockProviderWithData extends MockProvider
{
public function getGeocodedData($address)
{
return array(
'latitude' => 123,
'longitude' => 456
);
}
}
class MockProviderWithRequestCount extends MockProvider
{
public $geocodeCount = 0;
public function getGeocodedData($address)
{
$this->geocodeCount++;
}
}
class TestableGeocoder extends Geocoder
{
public $countCallGetProvider = 0;
public function getProvider()
{
$this->countCallGetProvider++;
return parent::getProvider();
}
public function returnResult(array $data = array())
{
return parent::returnResult($data);
}
}
class DummyResult extends Geocoded
{
}