Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Faker provider #1214

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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|^3.0",
"nyholm/nsa": "^1.1",
Expand Down
3 changes: 3 additions & 0 deletions src/Provider/Faker/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.gitattributes 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 <[email protected]>
*/
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 <[email protected]>

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 <[email protected]>
*/
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": "[email protected]"
}
],
"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
}
}
}
20 changes: 20 additions & 0 deletions src/Provider/Faker/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<coverage>
<include>
<directory>./</directory>
</include>
<exclude>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</coverage>
<php>
<server name="USE_CACHED_RESPONSES" value="true"/>
</php>
<testsuites>
<testsuite name="Geocoder Test Suite">
<directory>./Tests/</directory>
</testsuite>
</testsuites>
</phpunit>