Skip to content

Commit

Permalink
Add Faker provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ker0x committed Jan 31, 2024
1 parent 9c2224f commit d5f20e3
Show file tree
Hide file tree
Showing 19 changed files with 349 additions and 10 deletions.
1 change: 1 addition & 0 deletions .github/workflows/provider.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
- BingMaps
- Cache
- Chain
- Faker
- FreeGeoIp
- GeoIP2
# - GeoIPs
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ and are highly configurable.

### Special providers

Provider | Package | Features | Stats
:------------- |:------- |:-------- |:-------
[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)
[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)
Provider | Package | Features | Stats
:------------- |:------------------------------|:----------------------------------------------------------------|:-------
[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)
[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)
[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)

### Address

Expand Down Expand Up @@ -218,6 +219,11 @@ var_export($result);

Everything is ok, enjoy!

### The Faker Provider

The `Faker` provider is a special provider that return fake data using [FakerPHP](https://fakerphp.github.io/).
It's main purpose is to provide fake data during tests, avoiding unnecessary http requests.

### The ProviderAggregator

The `ProviderAggregator` is used to register several providers so that you can
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"cache/array-adapter": "^1.0",
"cache/simple-cache-bridge": "^1.0",
"cache/void-adapter": "^1.0",
"fakerphp/faker": "^1.23",
"geocoder-php/provider-integration-tests": "^1.6.3",
"geoip2/geoip2": "~2.0",
"nyholm/nsa": "^1.1",
Expand Down
4 changes: 2 additions & 2 deletions src/Common/Geocoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ interface Geocoder extends Provider
/**
* Geocodes a given value.
*
* @throws \Geocoder\Exception\Exception
* @throws Exception\Exception
*/
public function geocode(string $value): Collection;

/**
* Reverses geocode given latitude and longitude values.
*
* @throws \Geocoder\Exception\Exception
* @throws Exception\Exception
*/
public function reverse(float $latitude, float $longitude): Collection;
}
4 changes: 2 additions & 2 deletions src/Provider/AzureMaps/Tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ class IntegrationTest extends ProviderIntegrationTest
];

/**
* @return \Geocoder\Provider\Provider that is used in the tests
* @return Geocoder\Provider\Provider that is used in the tests
*/
protected function createProvider(ClientInterface $httpClient)
{
return new \Geocoder\Provider\AzureMaps\AzureMaps($httpClient, $_SERVER['AZURE_MAPS_SUBSCRIPTION_KEY']);
return new Geocoder\Provider\AzureMaps\AzureMaps($httpClient, $_SERVER['AZURE_MAPS_SUBSCRIPTION_KEY']);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Provider/Faker/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.gitattributes export-ignore
.travis.yml export-ignore
phpunit.xml.dist export-ignore
Tests/ export-ignore
33 changes: 33 additions & 0 deletions src/Provider/Faker/.github/workflows/provider.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Provider

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
test:
name: PHP ${{ matrix.php-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php-version: ['8.0', '8.1', '8.2']
steps:
- uses: actions/checkout@v3
- name: Use PHP ${{ matrix.php-version }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: curl
- name: Validate composer.json and composer.lock
run: composer validate --strict
- name: Install dependencies
run: composer update --prefer-stable --prefer-dist --no-progress
- name: Run test suite
run: composer run-script test-ci
- name: Upload Coverage report
run: |
wget https://scrutinizer-ci.com/ocular.phar
php ocular.phar code-coverage:upload --format=php-clover build/coverage.xml
3 changes: 3 additions & 0 deletions src/Provider/Faker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
.phpunit.result.cache
7 changes: 7 additions & 0 deletions src/Provider/Faker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Change Log

The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.

## 5.0.0

First release of this library.
64 changes: 64 additions & 0 deletions src/Provider/Faker/Faker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Geocoder\Provider\Faker;

use Faker\Factory;
use Geocoder\Collection;
use Geocoder\Model\AddressBuilder;
use Geocoder\Model\AddressCollection;
use Geocoder\Provider\Provider;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\Query;
use Geocoder\Query\ReverseQuery;

/**
* @author Romain Monteil <monteil.romain@gmail.com>
*/
final class Faker implements Provider
{
public const PROVIDER_NAME = 'faker';

public function geocodeQuery(GeocodeQuery $query): Collection
{
return $this->generateFakeLocations($query);
}

public function reverseQuery(ReverseQuery $query): Collection
{
return $this->generateFakeLocations($query);
}

public function getName(): string
{
return self::PROVIDER_NAME;
}

private function generateFakeLocations(Query $query): Collection
{
$faker = Factory::create($query->getLocale() ?? Factory::DEFAULT_LOCALE);

$results = [];

$i = 0;
while ($i < $query->getLimit()) {
$builder = new AddressBuilder($this->getName());
$builder
->setCoordinates($faker->latitude(), $faker->longitude())
->setStreetNumber($faker->buildingNumber())
->setStreetName($faker->streetName())
->setPostalCode($faker->postcode())
->setLocality($faker->city())
->setCountry($faker->country())
->setCountryCode($faker->countryCode())
->setTimezone($faker->timezone())
;

$results[] = $builder->build();
++$i;
}

return new AddressCollection($results);
}
}
21 changes: 21 additions & 0 deletions src/Provider/Faker/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2011 — William Durand <william.durand1@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 22 additions & 0 deletions src/Provider/Faker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Nominatim Geocoder provider
[![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)
[![Latest Stable Version](https://img.shields.io/packagist/v/geocoder-php/faker-provider?style=flat-square)](https://packagist.org/packages/geocoder-php/faker-provider)
[![Total Downloads](https://img.shields.io/packagist/dt/geocoder-php/faker-provider?style=flat-square)](https://packagist.org/packages/geocoder-php/faker-provider)
[![Monthly Downloads](https://img.shields.io/packagist/dm/geocoder-php/faker-provider?style=flat-square)](https://packagist.org/packages/geocoder-php/faker-provider)
[![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)
[![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)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)

This is the Faker provider from the PHP Geocoder. This is a **READ ONLY** repository. See the
[main repo](https://github.com/geocoder-php/Geocoder) for information and documentation.

## Install

```bash
composer require geocoder-php/faker-provider
```

## Contribute

Contributions are very welcome! Send a pull request to the [main repository](https://github.com/geocoder-php/Geocoder) or
report any issues you find on the [issue tracker](https://github.com/geocoder-php/Geocoder/issues).
Empty file.
61 changes: 61 additions & 0 deletions src/Provider/Faker/Tests/FakerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

/*
* 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\Provider\Faker\Tests;

use Geocoder\IntegrationTest\BaseTestCase;
use Geocoder\Model\Address;
use Geocoder\Provider\Faker\Faker;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;

class FakerTest extends BaseTestCase
{
protected function getCacheDir(): string
{
return __DIR__.'/.cached_responses';
}

public function testGetName(): void
{
$provider = new Faker();

$this->assertEquals('faker', $provider->getName());
}

public function testGeocode(): void
{
$provider = new Faker();
$result = $provider->geocodeQuery(GeocodeQuery::create('Dummy address'));

$this->assertContainsOnlyInstancesOf(Address::class, $result);
$this->assertCount(5, $result);
}

public function testReverse(): void
{
$provider = new Faker();
$result = $provider->reverseQuery(ReverseQuery::fromCoordinates(48.86, 2.35));

$this->assertContainsOnlyInstancesOf(Address::class, $result);
$this->assertCount(5, $result);
}

public function testQueryWithLimit(): void
{
$provider = new Faker();
$result = $provider->geocodeQuery(GeocodeQuery::create('Dummy address')->withLimit(10));

$this->assertContainsOnlyInstancesOf(Address::class, $result);
$this->assertCount(10, $result);
}
}
44 changes: 44 additions & 0 deletions src/Provider/Faker/Tests/IntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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\Provider\Faker\Tests;

use Geocoder\IntegrationTest\ProviderIntegrationTest;
use Geocoder\Provider\Faker\Faker;
use Psr\Http\Client\ClientInterface;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class IntegrationTest extends ProviderIntegrationTest
{
protected bool $testAddress = false;

protected bool $testReverse = false;

protected bool $testIpv4 = false;

protected bool $testIpv6 = false;

protected function createProvider(ClientInterface $httpClient): Faker
{
return new Faker();
}

protected function getCacheDir(): string
{
return __DIR__.'/.cached_responses';
}

protected function getApiKey(): string
{
return '';
}
}
52 changes: 52 additions & 0 deletions src/Provider/Faker/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "geocoder-php/faker-provider",
"type": "library",
"description": "Geocoder Faker adapter",
"keywords": [],
"homepage": "https://geocoder-php.org/",
"license": "MIT",
"authors": [
{
"name": "Romain Monteil",
"email": "monteil.romain@gmail.com"
}
],
"require": {
"php": "^8.0",
"geocoder-php/common-http": "^4.1",
"willdurand/geocoder": "^4.0",
"fakerphp/faker": "^1.23"
},
"provide": {
"geocoder-php/provider-implementation": "1.0"
},
"require-dev": {
"geocoder-php/provider-integration-tests": "^1.6.3",
"php-http/message": "^1.0",
"phpunit/phpunit": "^9.5"
},
"extra": {
"branch-alias": {
"dev-master": "4.0-dev"
}
},
"autoload": {
"psr-4": {
"Geocoder\\Provider\\Faker\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"test": "vendor/bin/phpunit",
"test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml"
},
"config": {
"allow-plugins": {
"php-http/discovery": true
}
}
}

0 comments on commit d5f20e3

Please sign in to comment.