Skip to content

Commit 310a1b2

Browse files
committed
Add Faker provider
1 parent 8a49fa1 commit 310a1b2

File tree

15 files changed

+342
-4
lines changed

15 files changed

+342
-4
lines changed

.github/workflows/provider.yml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
- BingMaps
2222
- Cache
2323
- Chain
24+
- Faker
2425
- FreeGeoIp
2526
- GeoIP2
2627
# - GeoIPs

README.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,11 @@ and are highly configurable.
112112

113113
### Special providers
114114

115-
Provider | Package | Features | Stats
116-
:------------- |:------- |:-------- |:-------
117-
[Cache](https://github.com/geocoder-php/cache-provider) | `geocoder-php/cache-provider` | Wraps a provider and cached the results | [![Latest Stable Version](https://poser.pugx.org/geocoder-php/cache-provider/v/stable)](https://packagist.org/packages/geocoder-php/cache-provider) <br>[![Total Downloads](https://poser.pugx.org/geocoder-php/cache-provider/downloads)](https://packagist.org/packages/geocoder-php/cache-provider)
118-
[Chain](https://github.com/geocoder-php/chain-provider) | `geocoder-php/chain-provider` | Iterates over multiple providers | [![Latest Stable Version](https://poser.pugx.org/geocoder-php/chain-provider/v/stable)](https://packagist.org/packages/geocoder-php/chain-provider) <br>[![Total Downloads](https://poser.pugx.org/geocoder-php/chain-provider/downloads)](https://packagist.org/packages/geocoder-php/chain-provider)
115+
Provider | Package | Features | Stats
116+
:------------- |:------------------------------|:----------------------------------------------------------------|:-------
117+
[Cache](https://github.com/geocoder-php/cache-provider) | `geocoder-php/cache-provider` | Wraps a provider and cached the results | [![Latest Stable Version](https://poser.pugx.org/geocoder-php/cache-provider/v/stable)](https://packagist.org/packages/geocoder-php/cache-provider) <br>[![Total Downloads](https://poser.pugx.org/geocoder-php/cache-provider/downloads)](https://packagist.org/packages/geocoder-php/cache-provider)
118+
[Chain](https://github.com/geocoder-php/chain-provider) | `geocoder-php/chain-provider` | Iterates over multiple providers | [![Latest Stable Version](https://poser.pugx.org/geocoder-php/chain-provider/v/stable)](https://packagist.org/packages/geocoder-php/chain-provider) <br>[![Total Downloads](https://poser.pugx.org/geocoder-php/chain-provider/downloads)](https://packagist.org/packages/geocoder-php/chain-provider)
119+
[Faker](https://github.com/geocoder-php/faker-provider) | `geocoder-php/faker-provider` | Provide fake data using [FakerPHP](https://fakerphp.github.io/) | [![Latest Stable Version](https://poser.pugx.org/geocoder-php/faker-provider/v/stable)](https://packagist.org/packages/geocoder-php/faker-provider) <br>[![Total Downloads](https://poser.pugx.org/geocoder-php/faker-provider/downloads)](https://packagist.org/packages/geocoder-php/faker-provider)
119120

120121
### Address
121122

@@ -218,6 +219,11 @@ var_export($result);
218219

219220
Everything is ok, enjoy!
220221

222+
### The Faker Provider
223+
224+
The `Faker` provider is a special provider that return fake data using [FakerPHP](https://fakerphp.github.io/).
225+
It's main purpose is to provide fake data during tests, avoiding unnecessary http requests.
226+
221227
### The ProviderAggregator
222228

223229
The `ProviderAggregator` is used to register several providers so that you can

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"cache/array-adapter": "^1.0",
3131
"cache/simple-cache-bridge": "^1.0",
3232
"cache/void-adapter": "^1.0",
33+
"fakerphp/faker": "^1.23",
3334
"geocoder-php/provider-integration-tests": "^1.6.3",
3435
"geoip2/geoip2": "^2.0|^3.0",
3536
"nyholm/nsa": "^1.1",

src/Provider/Faker/.gitattributes

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.gitattributes export-ignore
2+
phpunit.xml.dist export-ignore
3+
Tests/ export-ignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Provider
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
test:
11+
name: PHP ${{ matrix.php-version }}
12+
runs-on: ubuntu-latest
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
php-version: ['8.0', '8.1', '8.2']
17+
steps:
18+
- uses: actions/checkout@v3
19+
- name: Use PHP ${{ matrix.php-version }}
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: ${{ matrix.php-version }}
23+
extensions: curl
24+
- name: Validate composer.json and composer.lock
25+
run: composer validate --strict
26+
- name: Install dependencies
27+
run: composer update --prefer-stable --prefer-dist --no-progress
28+
- name: Run test suite
29+
run: composer run-script test-ci
30+
- name: Upload Coverage report
31+
run: |
32+
wget https://scrutinizer-ci.com/ocular.phar
33+
php ocular.phar code-coverage:upload --format=php-clover build/coverage.xml

src/Provider/Faker/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
.phpunit.result.cache

src/Provider/Faker/CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Change Log
2+
3+
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
4+
5+
## 5.0.0
6+
7+
First release of this library.

src/Provider/Faker/Faker.php

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Geocoder\Provider\Faker;
6+
7+
use Faker\Factory;
8+
use Geocoder\Collection;
9+
use Geocoder\Model\AddressBuilder;
10+
use Geocoder\Model\AddressCollection;
11+
use Geocoder\Provider\Provider;
12+
use Geocoder\Query\GeocodeQuery;
13+
use Geocoder\Query\Query;
14+
use Geocoder\Query\ReverseQuery;
15+
16+
/**
17+
* @author Romain Monteil <[email protected]>
18+
*/
19+
final class Faker implements Provider
20+
{
21+
public const PROVIDER_NAME = 'faker';
22+
23+
public function geocodeQuery(GeocodeQuery $query): Collection
24+
{
25+
return $this->generateFakeLocations($query);
26+
}
27+
28+
public function reverseQuery(ReverseQuery $query): Collection
29+
{
30+
return $this->generateFakeLocations($query);
31+
}
32+
33+
public function getName(): string
34+
{
35+
return self::PROVIDER_NAME;
36+
}
37+
38+
private function generateFakeLocations(Query $query): Collection
39+
{
40+
$faker = Factory::create($query->getLocale() ?? Factory::DEFAULT_LOCALE);
41+
42+
$results = [];
43+
44+
$i = 0;
45+
while ($i < $query->getLimit()) {
46+
$builder = new AddressBuilder($this->getName());
47+
$builder
48+
->setCoordinates($faker->latitude(), $faker->longitude())
49+
->setStreetNumber($faker->buildingNumber())
50+
->setStreetName($faker->streetName())
51+
->setPostalCode($faker->postcode())
52+
->setLocality($faker->city())
53+
->setCountry($faker->country())
54+
->setCountryCode($faker->countryCode())
55+
->setTimezone($faker->timezone())
56+
;
57+
58+
$results[] = $builder->build();
59+
++$i;
60+
}
61+
62+
return new AddressCollection($results);
63+
}
64+
}

src/Provider/Faker/LICENSE

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

src/Provider/Faker/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Nominatim Geocoder provider
2+
[![Build Status](https://img.shields.io/github/actions/workflow/status/geocoder-php/Geocoder/provider.yml?style=flat-square)](https://github.com/geocoder-php/faker-provider/actions)
3+
[![Latest Stable Version](https://img.shields.io/packagist/v/geocoder-php/faker-provider?style=flat-square)](https://packagist.org/packages/geocoder-php/faker-provider)
4+
[![Total Downloads](https://img.shields.io/packagist/dt/geocoder-php/faker-provider?style=flat-square)](https://packagist.org/packages/geocoder-php/faker-provider)
5+
[![Monthly Downloads](https://img.shields.io/packagist/dm/geocoder-php/faker-provider?style=flat-square)](https://packagist.org/packages/geocoder-php/faker-provider)
6+
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/geocoder-php/faker-provider.svg?style=flat-square)](https://scrutinizer-ci.com/g/geocoder-php/faker-provider)
7+
[![Quality Score](https://img.shields.io/scrutinizer/g/geocoder-php/faker-provider.svg?style=flat-square)](https://scrutinizer-ci.com/g/geocoder-php/faker-provider)
8+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
9+
10+
This is the Faker provider from the PHP Geocoder. This is a **READ ONLY** repository. See the
11+
[main repo](https://github.com/geocoder-php/Geocoder) for information and documentation.
12+
13+
## Install
14+
15+
```bash
16+
composer require geocoder-php/faker-provider
17+
```
18+
19+
## Contribute
20+
21+
Contributions are very welcome! Send a pull request to the [main repository](https://github.com/geocoder-php/Geocoder) or
22+
report any issues you find on the [issue tracker](https://github.com/geocoder-php/Geocoder/issues).

src/Provider/Faker/Tests/.cached_responses/.gitkeep

Whitespace-only changes.
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Geocoder package.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license MIT License
11+
*/
12+
13+
namespace Geocoder\Provider\Faker\Tests;
14+
15+
use Geocoder\IntegrationTest\BaseTestCase;
16+
use Geocoder\Model\Address;
17+
use Geocoder\Provider\Faker\Faker;
18+
use Geocoder\Query\GeocodeQuery;
19+
use Geocoder\Query\ReverseQuery;
20+
21+
class FakerTest extends BaseTestCase
22+
{
23+
protected function getCacheDir(): string
24+
{
25+
return __DIR__.'/.cached_responses';
26+
}
27+
28+
public function testGetName(): void
29+
{
30+
$provider = new Faker();
31+
32+
$this->assertEquals('faker', $provider->getName());
33+
}
34+
35+
public function testGeocode(): void
36+
{
37+
$provider = new Faker();
38+
$result = $provider->geocodeQuery(GeocodeQuery::create('Dummy address'));
39+
40+
$this->assertContainsOnlyInstancesOf(Address::class, $result);
41+
$this->assertCount(5, $result);
42+
}
43+
44+
public function testReverse(): void
45+
{
46+
$provider = new Faker();
47+
$result = $provider->reverseQuery(ReverseQuery::fromCoordinates(48.86, 2.35));
48+
49+
$this->assertContainsOnlyInstancesOf(Address::class, $result);
50+
$this->assertCount(5, $result);
51+
}
52+
53+
public function testQueryWithLimit(): void
54+
{
55+
$provider = new Faker();
56+
$result = $provider->geocodeQuery(GeocodeQuery::create('Dummy address')->withLimit(10));
57+
58+
$this->assertContainsOnlyInstancesOf(Address::class, $result);
59+
$this->assertCount(10, $result);
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\Faker\Tests;
12+
13+
use Geocoder\IntegrationTest\ProviderIntegrationTest;
14+
use Geocoder\Provider\Faker\Faker;
15+
use Psr\Http\Client\ClientInterface;
16+
17+
/**
18+
* @author Tobias Nyholm <[email protected]>
19+
*/
20+
class IntegrationTest extends ProviderIntegrationTest
21+
{
22+
protected bool $testAddress = false;
23+
24+
protected bool $testReverse = false;
25+
26+
protected bool $testIpv4 = false;
27+
28+
protected bool $testIpv6 = false;
29+
30+
protected function createProvider(ClientInterface $httpClient): Faker
31+
{
32+
return new Faker();
33+
}
34+
35+
protected function getCacheDir(): string
36+
{
37+
return __DIR__.'/.cached_responses';
38+
}
39+
40+
protected function getApiKey(): string
41+
{
42+
return '';
43+
}
44+
}

src/Provider/Faker/composer.json

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "geocoder-php/faker-provider",
3+
"type": "library",
4+
"description": "Geocoder Faker adapter",
5+
"keywords": [],
6+
"homepage": "https://geocoder-php.org/",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Romain Monteil",
11+
"email": "[email protected]"
12+
}
13+
],
14+
"require": {
15+
"php": "^8.0",
16+
"geocoder-php/common-http": "^4.1",
17+
"willdurand/geocoder": "^4.0",
18+
"fakerphp/faker": "^1.23"
19+
},
20+
"provide": {
21+
"geocoder-php/provider-implementation": "1.0"
22+
},
23+
"require-dev": {
24+
"geocoder-php/provider-integration-tests": "^1.6.3",
25+
"php-http/message": "^1.0",
26+
"phpunit/phpunit": "^9.5"
27+
},
28+
"extra": {
29+
"branch-alias": {
30+
"dev-master": "4.0-dev"
31+
}
32+
},
33+
"autoload": {
34+
"psr-4": {
35+
"Geocoder\\Provider\\Faker\\": ""
36+
},
37+
"exclude-from-classmap": [
38+
"/Tests/"
39+
]
40+
},
41+
"minimum-stability": "dev",
42+
"prefer-stable": true,
43+
"scripts": {
44+
"test": "vendor/bin/phpunit",
45+
"test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml"
46+
},
47+
"config": {
48+
"allow-plugins": {
49+
"php-http/discovery": true
50+
}
51+
}
52+
}

src/Provider/Faker/phpunit.xml.dist

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="vendor/autoload.php">
3+
<coverage>
4+
<include>
5+
<directory>./</directory>
6+
</include>
7+
<exclude>
8+
<directory>./Tests</directory>
9+
<directory>./vendor</directory>
10+
</exclude>
11+
</coverage>
12+
<php>
13+
<server name="USE_CACHED_RESPONSES" value="true"/>
14+
</php>
15+
<testsuites>
16+
<testsuite name="Geocoder Test Suite">
17+
<directory>./Tests/</directory>
18+
</testsuite>
19+
</testsuites>
20+
</phpunit>

0 commit comments

Comments
 (0)