Skip to content

Commit 801320a

Browse files
committed
Added integation tests
0 parents  commit 801320a

File tree

6 files changed

+343
-0
lines changed

6 files changed

+343
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
composer.lock
2+
vendor

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) Since 2011 — William Durand <[email protected]>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Geocoder integration tests
2+
3+
[![Latest Stable Version](https://poser.pugx.org/geocoder-php/provider-integration-tests/v/stable)](https://packagist.org/packages/geocoder-php/provider-integration-tests)
4+
[![Total Downloads](https://poser.pugx.org/geocoder-php/provider-integration-tests/downloads)](https://packagist.org/packages/geocoder-php/provider-integration-tests)
5+
[![Monthly Downloads](https://poser.pugx.org/geocoder-php/provider-integration-tests/d/monthly.png)](https://packagist.org/packages/geocoder-php/provider-integration-tests)
6+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
7+
8+
This repository contains integration tests to make sure your implementation of a Geocoder Provider is correct.
9+
10+
### Install
11+
12+
```bash
13+
composer require --dev geocoder-php/provider-integration-tests:dev-master
14+
```
15+
16+
### Use
17+
18+
Create a test that looks like this:
19+
20+
```php
21+
use Http\Client\HttpClient;
22+
use Geocoder\IntegrationTests\ProviderIntegrationTest;
23+
use Geocoder\Provider\GoogleMaps\GoogleMaps;
24+
25+
class IntegrationTest extends ProviderIntegrationTest
26+
{
27+
protected function createProvider(HttpClient $httpClient)
28+
{
29+
return new GoogleMaps($httpClient);
30+
}
31+
}
32+
```
33+

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "geocoder-php/provider-integration-tests",
3+
"type": "library",
4+
"description": "Integration tests Geocoder provider",
5+
"keywords": [
6+
"geocoder",
7+
"provider",
8+
"test"
9+
],
10+
"license": "MIT",
11+
"minimum-stability": "dev",
12+
"authors": [
13+
{
14+
"name": "Tobias Nyholm",
15+
"email": "[email protected]",
16+
"homepage": "https://github.com/nyholm"
17+
}
18+
],
19+
"require": {
20+
"php": "^5.5 || ^7.0",
21+
"phpunit/phpunit": "^4.8 || ^5.4",
22+
"psr/http-message-implementation": "^1.0",
23+
"willdurand/geocoder": "^4.0",
24+
"guzzlehttp/psr7": "^1.4"
25+
},
26+
"require-dev": {
27+
"php-http/curl-client": "^1.7"
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"Geocoder\\IntegrationTests\\": "src/"
32+
}
33+
}
34+
}

src/CachedResponseClient.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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\IntegrationTests;
12+
13+
use Http\Client\HttpClient;
14+
use Psr\Http\Message\RequestInterface;
15+
use GuzzleHttp\Psr7\Response;
16+
use GuzzleHttp\Psr7;
17+
18+
/**
19+
* Serve responses from local file cache
20+
*
21+
* @author Tobias Nyholm <[email protected]>
22+
*/
23+
class CachedResponseClient implements HttpClient
24+
{
25+
/**
26+
* @var HttpClient
27+
*/
28+
private $delegate;
29+
30+
/**
31+
* @var null|string
32+
*/
33+
private $apiKey;
34+
35+
/**
36+
* @var string
37+
*/
38+
private $cacheDir;
39+
40+
/**
41+
* @param HttpClient $delegate
42+
* @param string $cacheDir
43+
* @param string|null $apiKey
44+
*/
45+
public function __construct(HttpClient $delegate, $cacheDir, $apiKey = null)
46+
{
47+
$this->delegate = $delegate;
48+
$this->cacheDir = $cacheDir;
49+
$this->apiKey = $apiKey;
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function sendRequest(RequestInterface $request)
56+
{
57+
$url = (string) $request->getUri();
58+
$host = (string) $request->getUri()->getHost();
59+
if ($this->apiKey) {
60+
$url = str_replace($this->apiKey, '[apikey]', $url);
61+
}
62+
63+
$file = sprintf('%s/%s/%s_%s', __DIR__, $this->cacheDir, $host, sha1($url));
64+
if (is_file($file) && is_readable($file)) {
65+
return new Response(200, [], Psr7\stream_for(unserialize(file_get_contents($file))));
66+
}
67+
68+
$response = $this->delegate->sendRequest($request);
69+
file_put_contents($file, serialize($response->getBody()->getContents()));
70+
71+
return $response;
72+
}
73+
}

src/ProviderIntegrationTest.php

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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\IntegrationTests;
12+
13+
use Geocoder\Model\Query\GeocodeQuery;
14+
use Geocoder\Model\Query\ReverseQuery;
15+
use Geocoder\Provider\Provider;
16+
use GuzzleHttp\Psr7\Response;
17+
use Http\Client\HttpClient;
18+
use PHPUnit\Framework\TestCase;
19+
use Psr\Http\Message\ResponseInterface;
20+
21+
/**
22+
* @author Tobias Nyholm <[email protected]>
23+
*/
24+
abstract class ProviderIntegrationTest extends TestCase
25+
{
26+
/**
27+
* @var array with functionName => reason
28+
*/
29+
protected $skippedTests = [];
30+
31+
/**
32+
* @return Provider that is used in the tests
33+
*/
34+
abstract protected function createProvider(HttpClient $httpClient);
35+
36+
/**
37+
* @param ResponseInterface $response
38+
*
39+
* @return \PHPUnit_Framework_MockObject_MockObject|HttpClient
40+
*/
41+
private function getHttpClient(ResponseInterface $response)
42+
{
43+
$client = $this->getMockForAbstractClass(HttpClient::class);
44+
45+
$client
46+
->expects($this->any())
47+
->method('sendRequest')
48+
->willReturn($response);
49+
50+
return $client;
51+
}
52+
53+
/**
54+
* @expectedException \Geocoder\Exception\InvalidServerResponse
55+
*/
56+
public function testServer500Error()
57+
{
58+
if (isset($this->skippedTests[__FUNCTION__])) {
59+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
60+
}
61+
62+
$provider = $this->createProvider($this->getHttpClient(new Response(500)));
63+
$provider->geocodeQuery(GeocodeQuery::create('foo'));
64+
}
65+
66+
/**
67+
* @expectedException \Geocoder\Exception\InvalidServerResponse
68+
*/
69+
public function testServer500ErrorReverse()
70+
{
71+
if (isset($this->skippedTests[__FUNCTION__])) {
72+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
73+
}
74+
75+
$provider = $this->createProvider($this->getHttpClient(new Response(500)));
76+
$provider->reverseQuery(ReverseQuery::fromCoordinates(0, 0));
77+
}
78+
79+
/**
80+
* @expectedException \Geocoder\Exception\InvalidServerResponse
81+
*/
82+
public function testServer400Error()
83+
{
84+
if (isset($this->skippedTests[__FUNCTION__])) {
85+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
86+
}
87+
88+
$provider = $this->createProvider($this->getHttpClient(new Response(400)));
89+
$provider->geocodeQuery(GeocodeQuery::create('foo'));
90+
}
91+
92+
/**
93+
* @expectedException \Geocoder\Exception\InvalidServerResponse
94+
*/
95+
public function testServer400ErrorReverse()
96+
{
97+
if (isset($this->skippedTests[__FUNCTION__])) {
98+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
99+
}
100+
101+
$provider = $this->createProvider($this->getHttpClient(new Response(400)));
102+
$provider->reverseQuery(ReverseQuery::fromCoordinates(0, 0));
103+
}
104+
105+
/**
106+
* @expectedException \Geocoder\Exception\InvalidServerResponse
107+
*/
108+
public function testServerEmptyResponse()
109+
{
110+
if (isset($this->skippedTests[__FUNCTION__])) {
111+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
112+
}
113+
114+
$provider = $this->createProvider($this->getHttpClient(new Response(200)));
115+
$provider->geocodeQuery(GeocodeQuery::create('foo'));
116+
}
117+
118+
/**
119+
* @expectedException \Geocoder\Exception\InvalidServerResponse
120+
*/
121+
public function testServerEmptyResponseReverse()
122+
{
123+
if (isset($this->skippedTests[__FUNCTION__])) {
124+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
125+
}
126+
127+
$provider = $this->createProvider($this->getHttpClient(new Response(200)));
128+
$provider->reverseQuery(ReverseQuery::fromCoordinates(0, 0));
129+
}
130+
131+
/**
132+
* @expectedException \Geocoder\Exception\InvalidCredentials
133+
*/
134+
public function testInvalidCredentialsResponse()
135+
{
136+
if (isset($this->skippedTests[__FUNCTION__])) {
137+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
138+
}
139+
140+
$provider = $this->createProvider($this->getHttpClient(new Response(401)));
141+
$provider->geocodeQuery(GeocodeQuery::create('foo'));
142+
}
143+
144+
/**
145+
* @expectedException \Geocoder\Exception\InvalidCredentials
146+
*/
147+
public function testInvalidCredentialsResponseReverse()
148+
{
149+
if (isset($this->skippedTests[__FUNCTION__])) {
150+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
151+
}
152+
153+
$provider = $this->createProvider($this->getHttpClient(new Response(401)));
154+
$provider->reverseQuery(ReverseQuery::fromCoordinates(0, 0));
155+
}
156+
157+
/**
158+
* @expectedException \Geocoder\Exception\QuotaExceeded
159+
*/
160+
public function testQuotaExceededResponse()
161+
{
162+
if (isset($this->skippedTests[__FUNCTION__])) {
163+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
164+
}
165+
166+
$provider = $this->createProvider($this->getHttpClient(new Response(429)));
167+
$provider->geocodeQuery(GeocodeQuery::create('foo'));
168+
}
169+
170+
/**
171+
* @expectedException \Geocoder\Exception\QuotaExceeded
172+
*/
173+
public function testQuotaExceededResponseReverse()
174+
{
175+
if (isset($this->skippedTests[__FUNCTION__])) {
176+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
177+
}
178+
179+
$provider = $this->createProvider($this->getHttpClient(new Response(429)));
180+
$provider->reverseQuery(ReverseQuery::fromCoordinates(0, 0));
181+
}
182+
}

0 commit comments

Comments
 (0)