1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Geocoder package. |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @license MIT License |
||
9 | */ |
||
10 | |||
11 | namespace Geocoder\Provider\BANFrance\Tests; |
||
12 | |||
13 | use Geocoder\IntegrationTest\ProviderIntegrationTest; |
||
14 | use Geocoder\IntegrationTest\CachedResponseClient; |
||
15 | use Geocoder\Provider\BANFrance\BANFrance; |
||
16 | use Geocoder\Collection; |
||
17 | use Geocoder\Location; |
||
18 | use Geocoder\Model\AdminLevelCollection; |
||
19 | use Geocoder\Model\Bounds; |
||
20 | use Geocoder\Model\Coordinates; |
||
21 | use Geocoder\Model\Country; |
||
22 | use Geocoder\Provider\Provider; |
||
23 | use Geocoder\Query\GeocodeQuery; |
||
24 | use Geocoder\Query\ReverseQuery; |
||
25 | use Http\Client\HttpClient; |
||
26 | use Http\Discovery\HttpClientDiscovery; |
||
27 | |||
28 | /** |
||
29 | * @author Sébastien Barré <[email protected]> |
||
30 | */ |
||
31 | class IntegrationTest extends ProviderIntegrationTest |
||
32 | { |
||
33 | protected $testIpv4 = false; |
||
34 | |||
35 | protected $testIpv6 = false; |
||
36 | |||
37 | protected function createProvider(HttpClient $httpClient) |
||
38 | { |
||
39 | return new BANFrance($httpClient, 'Geocoder PHP/BANFrance Provider/BANFrance Test'); |
||
0 ignored issues
–
show
|
|||
40 | } |
||
41 | |||
42 | protected function getCacheDir() |
||
43 | { |
||
44 | return __DIR__.'/.cached_responses'; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * This client will make real request if cache was not found. |
||
49 | * |
||
50 | * @return CachedResponseClient |
||
51 | */ |
||
52 | private function getCachedHttpClient() |
||
53 | { |
||
54 | try { |
||
55 | $client = HttpClientDiscovery::find(); |
||
56 | } catch (\Http\Discovery\NotFoundException $e) { |
||
57 | $client = $this->getMockForAbstractClass(HttpClient::class); |
||
58 | |||
59 | $client |
||
60 | ->expects($this->any()) |
||
61 | ->method('sendRequest') |
||
62 | ->willThrowException($e); |
||
63 | } |
||
64 | |||
65 | return new CachedResponseClient( |
||
66 | $client, |
||
67 | $this->getCacheDir() |
||
68 | ); |
||
69 | } |
||
70 | |||
71 | |||
72 | public function testGeocodeQuery() |
||
73 | { |
||
74 | if (isset($this->skippedTests[__FUNCTION__])) { |
||
75 | $this->markTestSkipped($this->skippedTests[__FUNCTION__]); |
||
76 | } |
||
77 | if (!$this->testAddress) { |
||
78 | $this->markTestSkipped('Geocoding address is not supported by this provider'); |
||
79 | } |
||
80 | |||
81 | $provider = $this->createProvider($this->getCachedHttpClient()); |
||
82 | $query = GeocodeQuery::create('8 Boulevard du Port 80000 Amiens'); |
||
83 | $query = $query->withLimit(1); |
||
84 | |||
85 | $result = $provider->geocodeQuery($query); |
||
86 | $this->assertWellFormattedResult($result); |
||
87 | |||
88 | // Check Downing Street |
||
89 | $location = $result->first(); |
||
90 | $this->assertEquals(49.897446, $location->getCoordinates()->getLatitude(), 'Latitude should be in Amiens', 0.1); |
||
91 | $this->assertEquals(2.29009, $location->getCoordinates()->getLongitude(), 'Longitude should be in Amiens', 0.1); |
||
92 | $this->assertContains('Boulevard du Port', $location->getStreetName(), 'Street name should contain "Boulevard du Port"'); |
||
93 | |||
94 | if (null !== $streetNumber = $location->getStreetNumber()) { |
||
95 | $this->assertContains('8', $streetNumber, 'Street number should contain "8"'); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | public function testGeocodeQueryWithNoResults() |
||
100 | { |
||
101 | if (isset($this->skippedTests[__FUNCTION__])) { |
||
102 | $this->markTestSkipped($this->skippedTests[__FUNCTION__]); |
||
103 | } |
||
104 | if (!$this->testAddress) { |
||
105 | $this->markTestSkipped('Geocoding address is not supported by this provider'); |
||
106 | } |
||
107 | |||
108 | $provider = $this->createProvider($this->getCachedHttpClient()); |
||
109 | $query = GeocodeQuery::create('jsajhgsdkfjhsfkjhaldkadjaslgldasd'); |
||
110 | $result = $provider->geocodeQuery($query); |
||
111 | $this->assertWellFormattedResult($result); |
||
112 | $this->assertEquals(0, $result->count()); |
||
113 | } |
||
114 | |||
115 | public function testReverseQuery() |
||
116 | { |
||
117 | if (isset($this->skippedTests[__FUNCTION__])) { |
||
118 | $this->markTestSkipped($this->skippedTests[__FUNCTION__]); |
||
119 | } |
||
120 | if (!$this->testReverse) { |
||
121 | $this->markTestSkipped('Reverse geocoding address is not supported by this provider'); |
||
122 | } |
||
123 | |||
124 | $provider = $this->createProvider($this->getCachedHttpClient()); |
||
125 | |||
126 | // Close to the white house |
||
127 | $result = $provider->reverseQuery(ReverseQuery::fromCoordinates(2.37, 48.357)); |
||
128 | $this->assertWellFormattedResult($result); |
||
129 | } |
||
130 | |||
131 | public function testReverseQueryWithNoResults() |
||
132 | { |
||
133 | if (isset($this->skippedTests[__FUNCTION__])) { |
||
134 | $this->markTestSkipped($this->skippedTests[__FUNCTION__]); |
||
135 | } |
||
136 | |||
137 | if (!$this->testReverse) { |
||
138 | $this->markTestSkipped('Reverse geocoding address is not supported by this provider'); |
||
139 | } |
||
140 | |||
141 | $provider = $this->createProvider($this->getCachedHttpClient()); |
||
142 | |||
143 | $result = $provider->reverseQuery(ReverseQuery::fromCoordinates(0, 52.52)); |
||
144 | $this->assertEquals(0, $result->count()); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Make sure that a result for a Geocoder is well formatted. Be aware that even |
||
149 | * a Location with no data may be well formatted. |
||
150 | * |
||
151 | * @param $result |
||
152 | */ |
||
153 | private function assertWellFormattedResult(Collection $result) |
||
154 | { |
||
155 | $this->assertInstanceOf( |
||
156 | Collection::class, |
||
157 | $result, |
||
158 | 'The result must be an instance of a Geocoder\Collection' |
||
159 | ); |
||
160 | |||
161 | /** @var Location $location */ |
||
162 | foreach ($result as $location) { |
||
163 | $this->assertInstanceOf( |
||
164 | Location::class, |
||
165 | $location, |
||
166 | 'All items in Geocoder\Collection must implement Geocoder\Location' |
||
167 | ); |
||
168 | |||
169 | $this->assertInstanceOf( |
||
170 | AdminLevelCollection::class, |
||
171 | $location->getAdminLevels(), |
||
172 | 'Location::getAdminLevels MUST always return a AdminLevelCollection' |
||
173 | ); |
||
174 | $arrayData = $location->toArray(); |
||
175 | $this->assertTrue(is_array($arrayData), 'Location::toArray MUST return an array.'); |
||
176 | $this->assertNotEmpty($arrayData, 'Location::toArray cannot be empty.'); |
||
177 | |||
178 | // Verify coordinates |
||
179 | if (null !== $coords = $location->getCoordinates()) { |
||
180 | $this->assertInstanceOf( |
||
181 | Coordinates::class, |
||
182 | $coords, |
||
183 | 'Location::getCoordinates MUST always return a Coordinates or null' |
||
184 | ); |
||
185 | |||
186 | // Using "assertNotEmpty" means that we can not have test code where coordinates is on equator or long = 0 |
||
187 | $this->assertNotEmpty($coords->getLatitude(), 'If coordinate object exists it cannot have an empty latitude.'); |
||
188 | $this->assertNotEmpty($coords->getLongitude(), 'If coordinate object exists it cannot have an empty longitude.'); |
||
189 | } |
||
190 | } |
||
191 | } |
||
192 | |||
193 | protected function getApiKey() |
||
194 | { |
||
195 | } |
||
196 | } |
||
197 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.