forked from geocoder-php/Geocoder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProviderAggregatorTest.php
218 lines (176 loc) · 5.06 KB
/
ProviderAggregatorTest.php
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
<?php
/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Geocoder\Tests;
use Geocoder\Geocoder;
use Geocoder\Model\Query\GeocodeQuery;
use Geocoder\Model\Query\ReverseQuery;
use Geocoder\ProviderAggregator;
use Geocoder\Model\AddressFactory;
use Geocoder\Provider\LocaleAwareGeocoder;
use Geocoder\Provider\Provider;
/**
* @author William Durand <william.durand1@gmail.com>
*/
class ProviderAggregatorTest extends TestCase
{
/**
* @var TestableGeocoder
*/
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([$provider]);
$this->assertSame($provider, $this->geocoder->getProvider());
}
public function testUsing()
{
$provider1 = new MockProvider('test1');
$provider2 = new MockProvider('test2');
$this->geocoder->registerProviders([$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());
}
/**
* @expectedException \Geocoder\Exception\ProviderNotRegistered
*/
public function testUsingNonExistantProviderShouldThrowAnException()
{
$this->geocoder->using('non_existant');
}
/**
* @expectedException \Geocoder\Exception\ProviderNotRegistered
*/
public function testUsingNullShouldThrowAnException()
{
$this->geocoder->using(null);
}
/**
* @expectedException \Geocoder\Exception\ProviderNotRegistered
*/
public function testUsingAnEmptyProviderNameShouldThrowAnException()
{
$this->geocoder->using('');
}
public function testGetProviders()
{
$provider1 = new MockProvider('test1');
$provider2 = new MockProvider('test2');
$this->geocoder->registerProviders([$provider1, $provider2]);
$result = $this->geocoder->getProviders();
$expected = [
'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()
{
$this->geocoder->registerProviders([
$provider1 = new MockProvider('test1'),
$provider2 = new MockProvider('test2'),
$provider3 = new MockProvider('test3'),
]);
$this->assertSame($provider1, $this->geocoder->getProvider());
}
public function testDefaultMaxResults()
{
$this->assertSame(Geocoder::DEFAULT_RESULT_LIMIT, $this->geocoder->getLimit());
}
private function getAddressMock()
{
return (new AddressFactory())->createFromArray([]);
}
}
class MockProvider implements Provider
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
public function geocodeQuery(GeocodeQuery $query)
{
return $this->returnResult([]);
}
public function reverseQuery(ReverseQuery $query)
{
return $this->returnResult([]);
}
public function getName()
{
return $this->name;
}
public function getLimit()
{
}
public function limit($limit)
{
return $this;
}
public function returnResult(array $data = [])
{
}
}
class MockLocaleAwareProvider extends MockProvider implements LocaleAwareGeocoder
{
}
class MockProviderWithData extends MockProvider
{
public function geocode($address)
{
return $this->returnResult([
'latitude' => 123,
'longitude' => 456,
]);
}
}
class MockProviderWithRequestCount extends MockProvider
{
public $geocodeCount = 0;
public function geocode($address)
{
++$this->geocodeCount;
return parent::geocode($address);
}
}
class TestableGeocoder extends ProviderAggregator
{
public $countCallGetProvider = 0;
public function getProvider()
{
++$this->countCallGetProvider;
return parent::getProvider();
}
}