diff --git a/.github/workflows/provider.yml b/.github/workflows/provider.yml index c7a515b24..b6dd2cf42 100644 --- a/.github/workflows/provider.yml +++ b/.github/workflows/provider.yml @@ -21,6 +21,7 @@ jobs: - BingMaps - Cache - Chain + - Faker - FreeGeoIp - GeoIP2 # - GeoIPs diff --git a/README.md b/README.md index 31b250196..d40bea6b8 100644 --- a/README.md +++ b/README.md @@ -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)
[![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)
[![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)
[![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)
[![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)
[![Total Downloads](https://poser.pugx.org/geocoder-php/faker-provider/downloads)](https://packagist.org/packages/geocoder-php/faker-provider) ### Address @@ -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 diff --git a/composer.json b/composer.json index 1b1c70d83..420dc5326 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Provider/Faker/.gitattributes b/src/Provider/Faker/.gitattributes new file mode 100644 index 000000000..e40b3651f --- /dev/null +++ b/src/Provider/Faker/.gitattributes @@ -0,0 +1,3 @@ +.gitattributes export-ignore +phpunit.xml.dist export-ignore +Tests/ export-ignore diff --git a/src/Provider/Faker/.github/workflows/provider.yml b/src/Provider/Faker/.github/workflows/provider.yml new file mode 100644 index 000000000..d7fbb64c2 --- /dev/null +++ b/src/Provider/Faker/.github/workflows/provider.yml @@ -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 diff --git a/src/Provider/Faker/.gitignore b/src/Provider/Faker/.gitignore new file mode 100644 index 000000000..3d7e132f3 --- /dev/null +++ b/src/Provider/Faker/.gitignore @@ -0,0 +1,3 @@ +vendor/ +composer.lock +.phpunit.result.cache diff --git a/src/Provider/Faker/CHANGELOG.md b/src/Provider/Faker/CHANGELOG.md new file mode 100644 index 000000000..d7d84791e --- /dev/null +++ b/src/Provider/Faker/CHANGELOG.md @@ -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. diff --git a/src/Provider/Faker/Faker.php b/src/Provider/Faker/Faker.php new file mode 100644 index 000000000..293b88e2d --- /dev/null +++ b/src/Provider/Faker/Faker.php @@ -0,0 +1,64 @@ + + */ +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); + } +} diff --git a/src/Provider/Faker/LICENSE b/src/Provider/Faker/LICENSE new file mode 100644 index 000000000..8aa8246ef --- /dev/null +++ b/src/Provider/Faker/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2011 — William Durand + +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. diff --git a/src/Provider/Faker/README.md b/src/Provider/Faker/README.md new file mode 100644 index 000000000..11f7efa8d --- /dev/null +++ b/src/Provider/Faker/README.md @@ -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). diff --git a/src/Provider/Faker/Tests/.cached_responses/.gitkeep b/src/Provider/Faker/Tests/.cached_responses/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/src/Provider/Faker/Tests/FakerTest.php b/src/Provider/Faker/Tests/FakerTest.php new file mode 100644 index 000000000..9a571449c --- /dev/null +++ b/src/Provider/Faker/Tests/FakerTest.php @@ -0,0 +1,61 @@ +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); + } +} diff --git a/src/Provider/Faker/Tests/IntegrationTest.php b/src/Provider/Faker/Tests/IntegrationTest.php new file mode 100644 index 000000000..d299194e5 --- /dev/null +++ b/src/Provider/Faker/Tests/IntegrationTest.php @@ -0,0 +1,44 @@ + + */ +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 ''; + } +} diff --git a/src/Provider/Faker/composer.json b/src/Provider/Faker/composer.json new file mode 100644 index 000000000..3b7b8235b --- /dev/null +++ b/src/Provider/Faker/composer.json @@ -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 + } + } +} diff --git a/src/Provider/Faker/phpunit.xml.dist b/src/Provider/Faker/phpunit.xml.dist new file mode 100644 index 000000000..26265855a --- /dev/null +++ b/src/Provider/Faker/phpunit.xml.dist @@ -0,0 +1,20 @@ + + + + + ./ + + + ./Tests + ./vendor + + + + + + + + ./Tests/ + + +