diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ec03dcd7..76760d58 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,28 +10,24 @@ jobs: strategy: fail-fast: false matrix: - php: [ '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ] + php: [ '8.2', '8.3', '8.4', '8.5' ] strategy: [ 'highest' ] sf_version: [''] include: - - php: 7.3 - sf_version: '4.*' - - php: 7.4 - strategy: 'lowest' - - php: 8.0 - sf_version: '6.*' - - php: 8.1 - sf_version: '6.*' - php: 8.2 sf_version: '6.*' - php: 8.3 sf_version: '7.*' - php: 8.4 sf_version: '7.*' + - php: 8.5 + sf_version: '7.*' + - php: 8.5 + sf_version: '8.*' steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v6 - name: Generate locales run: sudo apt-get update && sudo apt-get install tzdata locales -y && sudo locale-gen sv_SE && sudo locale-gen sv_SE.UTF-8 && sudo locale-gen en_US && sudo locale-gen en_US.UTF-8 @@ -43,7 +39,7 @@ jobs: coverage: none - name: Download dependencies - uses: ramsey/composer-install@v2 + uses: ramsey/composer-install@v4 env: SYMFONY_REQUIRE: ${{ matrix.sf_version }} with: diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml new file mode 100644 index 00000000..fbccabad --- /dev/null +++ b/.github/workflows/stale.yml @@ -0,0 +1,27 @@ +name: Mark stale issues and pull requests + +on: + schedule: + - cron: '0 0 * * *' + workflow_dispatch: + +jobs: + stale: + runs-on: ubuntu-latest + permissions: + issues: write + pull-requests: write + + steps: + - uses: actions/stale@v9 + with: + stale-issue-message: 'This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs.' + close-issue-message: 'This issue has been automatically closed due to inactivity. Feel free to reopen it if it is still relevant.' + stale-pr-message: 'This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs.' + close-pr-message: 'This pull request has been automatically closed due to inactivity. Feel free to reopen it if it is still relevant.' + days-before-stale: 60 + days-before-close: 14 + stale-issue-label: 'stale' + stale-pr-label: 'stale' + exempt-issue-labels: 'pinned,security,bug' + exempt-pr-labels: 'pinned,security' diff --git a/.gitignore b/.gitignore index 732b9404..3d7e132f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,3 @@ -.idea vendor/ composer.lock -composer.phar -phpunit.xml -php-cs-fixer.phar .phpunit.result.cache diff --git a/README.md b/README.md index dbe50f94..238bbb5d 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,12 @@ Geotools ======== -**Geotools** is a PHP geo-related library, built atop [Geocoder](https://github.com/willdurand/Geocoder) and +**Geotools** is a PHP geo-related library, built atop [Geocoder](https://github.com/geocoder-php/Geocoder) and [React](https://github.com/reactphp/react) libraries. [![Latest Version](https://poser.pugx.org/league/geotools/v/stable)](https://github.com/thephpleague/geotools/releases) [![Total Downloads](https://poser.pugx.org/league/geotools/downloads)](https://packagist.org/packages/league/geotools) +[![CI](https://github.com/thephpleague/geotools/actions/workflows/ci.yml/badge.svg)](https://github.com/thephpleague/geotools/actions/workflows/ci.yml) [![Quality Score](https://img.shields.io/scrutinizer/g/thephpleague/geotools.svg?style=flat-square)](https://scrutinizer-ci.com/g/thephpleague/geotools/?branch=master) Features @@ -35,10 +36,9 @@ coordinate, read more in [wikipedia](http://en.wikipedia.org/wiki/Cardinal_direc * Encode a coordinate to a **geo hash** string and decode it to a coordinate, read more in [wikipedia](http://en.wikipedia.org/wiki/Geohash) and on [geohash.org](http://geohash.org/). [»](#geohash) * Encode a coordinate via the 10:10 algorithm. [»](#1010) -* **Polygon** class provides methods to check either a poing (coordinate) is in, or on the polygon's boundaries. +* **Polygon** class provides methods to check either a point (coordinate) is in, or on the polygon's boundaries. [»](#polygon) * A **command-line interface** (CLI) for **Distance**, **Point**, **Geohash** and **Convert** classes. [»](#cli) -* Integration with Frameworks: **Laravel 4**, **Silex** ... [»](#integration-with-frameworks) * ... more to come ... @@ -50,17 +50,11 @@ The recommended way to install **Geotools** is through [composer](http://getcomp Run the following on the command line: -``` +```bash composer require league/geotools ``` -**Important:** you should use the `0.4` version if you use Geocoder `2.x` or/and PHP `5.3`. - -And install dependencies: - -``` -composer install -``` +**Requirements:** PHP 8.2 or higher. Now you can add the autoloader, and you will have access to the library: @@ -84,18 +78,34 @@ Here are the available ellipsoids: `AIRY`, `AUSTRALIAN_NATIONAL`, `BESSEL_1841`, `HELMERT_1906`, `HOUGH`, `INTERNATIONAL`, `KRASSOVSKY`, `MODIFIED_AIRY`, `MODIFIED_EVEREST`, `MODIFIED_FISCHER_1960`, `SOUTH_AMERICAN_1969`, `WGS60`, `WGS66`, `WGS72`, and `WGS84`. -If you need to use an other ellipsoid, just create an array like this: -``` php +You can reference ellipsoids either by their string name or via the `EllipsoidName` enum (PHP 8.1+): + +```php + 'My Ellipsoid', // The name of the Ellipsoid - 'a' => 123.0, // The semi-major axis (equatorial radius) in meters - 'invF' => 456.0 // The inverse flattening + 'a' => 123.0, // The semi-major axis (equatorial radius) in meters + 'invF' => 456.0 // The inverse flattening ]); ``` -**Geotools** is built atop [Geocoder](https://github.com/willdurand/Geocoder). It means it's possible to use the +**Geotools** is built atop [Geocoder](https://github.com/geocoder-php/Geocoder). It means it's possible to use a `\Geocoder\Model\Address` directly but it's also possible to use a *string* or a simple *array* with its latitude and longitude. @@ -117,11 +127,12 @@ Longitudes below -180.0 or above 180.0 degrees are *wrapped* through `\League\Ge use League\Geotools\Coordinate\Coordinate; use League\Geotools\Coordinate\Ellipsoid; +use League\Geotools\Coordinate\EllipsoidName; -// from an \Geocoder\Model\Address instance within Airy ellipsoid -$coordinate = new Coordinate($geocoderResult, Ellipsoid::createFromName(Ellipsoid::AIRY)); +// from a \Geocoder\Model\Address instance within Airy ellipsoid +$coordinate = new Coordinate($geocoderResult, Ellipsoid::createFromName(EllipsoidName::AIRY)); // or in an array of latitude/longitude coordinate within GRS 1980 ellipsoid -$coordinate = new Coordinate([48.8234055, 2.3072664], Ellipsoid::createFromName(Ellipsoid::GRS_1980)); +$coordinate = new Coordinate([48.8234055, 2.3072664], Ellipsoid::createFromName(EllipsoidName::GRS_1980)); // or in latitude/longitude coordinate within WGS84 ellipsoid $coordinate = new Coordinate('48.8234055, 2.3072664'); // or in degrees minutes seconds coordinate within WGS84 ellipsoid @@ -129,22 +140,23 @@ $coordinate = new Coordinate('48°49′24″N, 2°18′26″E'); // or in decimal minutes coordinate within WGS84 ellipsoid $coordinate = new Coordinate('48 49.4N, 2 18.43333E'); // the result will be: -printf("Latitude: %F\n", $coordinate->getLatitude()); // 48.8234055 +printf("Latitude: %F\n", $coordinate->getLatitude()); // 48.8234055 printf("Longitude: %F\n", $coordinate->getLongitude()); // 2.3072664 printf("Ellipsoid name: %s\n", $coordinate->getEllipsoid()->getName()); // WGS 84 -printf("Equatorial radius: %F\n", $coordinate->getEllipsoid()->getA()); // 6378136.0 -printf("Polar distance: %F\n", $coordinate->getEllipsoid()->getB()); // 6356751.317598 +printf("Equatorial radius: %F\n", $coordinate->getEllipsoid()->getA()); // 6378137.0 +printf("Polar distance: %F\n", $coordinate->getEllipsoid()->getB()); // 6356752.314245 printf("Inverse flattening: %F\n", $coordinate->getEllipsoid()->getInvF()); // 298.257224 -printf("Mean radius: %F\n", $coordinate->getEllipsoid()->getArithmeticMeanRadius()); // 6371007.772533 -// it's also possible to modify the coordinate without creating an other coodinate +printf("Mean radius: %F\n", $coordinate->getEllipsoid()->getArithmeticMeanRadius()); // 6371008.771415 +// it's also possible to modify the coordinate without creating another coordinate $coordinate->setFromString('40°26′47″N 079°58′36″W'); -printf("Latitude: %F\n", $coordinate->getLatitude()); // 40.446388888889 +printf("Latitude: %F\n", $coordinate->getLatitude()); // 40.446388888889 printf("Longitude: %F\n", $coordinate->getLongitude()); // -79.976666666667 ``` + ## Convert -It provides methods (and aliases) to convert *decimal degrees* WGS84 coordinates to *degrees minutes seconds* +It provides methods to convert *decimal degrees* WGS84 coordinates to *degrees minutes seconds* or *decimal minutes* WGS84 coordinates. You can format the output string easily. You can also convert them in the Universal Transverse Mercator (UTM) projection (Southwest coast of Norway and the @@ -156,7 +168,7 @@ region of Svalbard are covered). $geotools = new \League\Geotools\Geotools(); $coordinate = new \League\Geotools\Coordinate\Coordinate('40.446195, -79.948862'); $converted = $geotools->convert($coordinate); -// convert to decimal degrees without and with format string +// convert to decimal minutes without and with format string printf("%s\n", $converted->toDecimalMinutes()); // 40 26.7717N, -79 56.93172W // convert to degrees minutes seconds without and with format string printf("%s\n", $converted->toDegreesMinutesSeconds('

%P%D:%M:%S, %p%d:%m:%s

')); //

40:26:46, -79:56:56

@@ -185,43 +197,40 @@ Seconds | `%S` | `%s` It provides a very handy way to batch geocode and reverse geocoding requests in *serie* or in *parallel* against a set of providers. -Thanks to [Geocoder](https://github.com/willdurand/Geocoder) and [React](https://github.com/reactphp/react) libraries. +Thanks to [Geocoder](https://github.com/geocoder-php/Geocoder) and [React](https://github.com/reactphp/react) libraries. -It's possible to batch *one request* (a string) or a *set of request* (an array) against *one provider* or +It's possible to batch *one request* (a string) or a *set of requests* (an array) against *one provider* or a *set of providers*. -You can use a provided **cache engine** or use your own by setting a cache object which should implement -`League\Geotools\Cache\CacheInterface` and extend `League\Geotools\Cache\AbstractCache` if needed. +**Cache:** any [PSR-6](https://www.php-fig.org/psr/psr-6/) compatible cache pool is supported (e.g. +`symfony/cache`, `cache/array-adapter`, etc.). -At the moment Geotools supports any PSR-6 cache. - -NB: Before you implement caching in your app please be sure that doing so does not violate the Terms of Service -for your(s) geocoding provider(s). +> **Note:** Before implementing caching in your app, please be sure that doing so does not violate the Terms of +> Service of your geocoding provider(s). ```php registerProviders([ new \Geocoder\Provider\GoogleMaps\GoogleMaps($httpClient), - new \Geocoder\Provider\OpenStreetMap\OpenStreetMap($httpClient), - new \Geocoder\Provider\BingMaps\BingMaps($httpClient, ''), // throws InvalidCredentialsException - new \Geocoder\Provider\Yandex\Yandex($httpClient), - new \Geocoder\Provider\FreeGeoIp\FreeGeoIp($httpClient), - new \Geocoder\Provider\Geoip\Geoip(), + new \Geocoder\Provider\Nominatim\Nominatim($httpClient, 'https://nominatim.openstreetmap.org', 'my-app'), ]); +// Use any PSR-6 cache implementation, e.g. Symfony ArrayAdapter: +// $cache = new \Symfony\Component\Cache\Adapter\ArrayAdapter(); + try { $geotools = new \League\Geotools\Geotools(); - $cache = new \Cache\Adapter\PHPArray\ArrayCachePool(); - $results = $geotools->batch($geocoder)->setCache($cache)->geocode([ + $results = $geotools->batch($geocoder)->geocode([ 'Paris, France', 'Copenhagen, Denmark', - '74.200.247.59', - '::ffff:66.147.244.214' ])->parallel(); } catch (\Exception $e) { die($e->getMessage()); @@ -229,53 +238,21 @@ try { $dumper = new \Geocoder\Dumper\WktDumper(); foreach ($results as $result) { - // if a provider throws an exception (UnsupportedException, InvalidCredentialsException ...) - // an custom /Geocoder/Result/Geocoded instance is returned which embedded the name of the provider, - // the query string and the exception string. It's possible to use dumpers - // and/or formatters from the Geocoder library. + // if a provider throws an exception, a BatchGeocoded instance is returned + // which embeds the provider name, the query and the exception message. printf("%s|%s|%s\n", $result->getProviderName(), $result->getQuery(), - '' == $result->getExceptionMessage() ? $dumper->dump($result) : $result->getExceptionMessage() + '' === $result->getExceptionMessage() ? $dumper->dump($result) : $result->getExceptionMessage() ); } ``` -You should get 24 results (4 values to geocode against 6 providers) something like: - -``` -google_maps|Paris, France|POINT(2.352222 48.856614) -google_maps|Copenhagen, Denmark|POINT(12.568337 55.676097) -google_maps|74.200.247.59|The GoogleMapsProvider does not support IP addresses. -google_maps|::ffff:66.147.244.214|The GoogleMapsProvider does not support IP addresses. -openstreetmap|Paris, France|POINT(2.352133 48.856506) -openstreetmap|Copenhagen, Denmark|POINT(12.570072 55.686724) -openstreetmap|74.200.247.59|Could not execute query http://nominatim.openstreetmap.org/search?q=74.200.247.59&format=xml&addressdetails=1&limit=1 -openstreetmap|::ffff:66.147.244.214|The OpenStreetMapProvider does not support IPv6 addresses. -bing_maps|Paris, France|Could not execute query http://dev.virtualearth.net/REST/v1/Locations/?q=Paris%2C+France&key= -bing_maps|Copenhagen, Denmark|Could not execute query http://dev.virtualearth.net/REST/v1/Locations/?q=Copenhagen%2C+Denmark&key= -bing_maps|74.200.247.59|The BingMapsProvider does not support IP addresses. -bing_maps|::ffff:66.147.244.214|The BingMapsProvider does not support IP addresses. -yandex|Paris, France|POINT(2.341198 48.856929) -yandex|Copenhagen, Denmark|POINT(12.567602 55.675682) -yandex|74.200.247.59|The YandexProvider does not support IP addresses. -yandex|::ffff:66.147.244.214|The YandexProvider does not support IP addresses. -free_geo_ip|Paris, France|The FreeGeoIpProvider does not support Street addresses. -free_geo_ip|Copenhagen, Denmark|The FreeGeoIpProvider does not support Street addresses. -free_geo_ip|74.200.247.59|POINT(-122.415600 37.748400) -free_geo_ip|::ffff:66.147.244.214|POINT(-111.613300 40.218100) -geoip|Paris, France|The GeoipProvider does not support Street addresses. -geoip|Copenhagen, Denmark|The GeoipProvider does not support Street addresses. -geoip|74.200.247.59|POINT(-122.415604 37.748402) -geoip|::ffff:66.147.244.214|The GeoipProvider does not support IPv6 addresses. -``` - -Batch reverse geocoding is something like: +Batch reverse geocoding: ```php batch($geocoder)->reverse( @@ -284,21 +261,17 @@ try { } catch (\Exception $e) { die($e->getMessage()); } -// Or if you want to reverse geocoding 3 coordinates + +// Or reverse geocode multiple coordinates $coordinates = [ new \League\Geotools\Coordinate\Coordinate([2.307266, 48.823405]), new \League\Geotools\Coordinate\Coordinate([12.568337, 55.676097]), - new \League\Geotools\Coordinate\Coordinate('-74.005973 40.714353')), + new \League\Geotools\Coordinate\Coordinate('-74.005973 40.714353'), ]; $results = $geotools->batch($geocoder)->reverse($coordinates)->parallel(); -// ... ``` -If you want to batch it in serie, replace the method `parallel()` by `serie()`. - -To optimize batch requests you need to register providers according to their **capabilities** and what you're -**looking for** (geocode street addresses, geocode IPv4, geocode IPv6 or reverse geocoding), -please read more at the [Geocoder library doc](https://github.com/willdurand/Geocoder#freegeoipprovider). +If you want to batch in serie, replace `parallel()` with `serie()`. ## Distance @@ -315,17 +288,17 @@ $coordA = new \League\Geotools\Coordinate\Coordinate([48.8234055, 2.3072664]); $coordB = new \League\Geotools\Coordinate\Coordinate([43.296482, 5.36978]); $distance = $geotools->distance()->setFrom($coordA)->setTo($coordB); -printf("%s\n",$distance->flat()); // 659166.50038742 (meters) -printf("%s\n",$distance->greatCircle()); // 659021.90812846 -printf("%s\n",$distance->in('km')->haversine()); // 659.02190812846 -printf("%s\n",$distance->in('mi')->vincenty()); // 409.05330679648 -printf("%s\n",$distance->in('ft')->flat()); // 2162619.7519272 +printf("%s\n", $distance->flat()); // 659166.50038742 (meters) +printf("%s\n", $distance->greatCircle()); // 659021.90812846 +printf("%s\n", $distance->in('km')->haversine()); // 659.02190812846 +printf("%s\n", $distance->in('mi')->vincenty()); // 409.05330679648 +printf("%s\n", $distance->in('ft')->flat()); // 2162619.7519272 ``` ## Point It provides methods to compute the initial and final *bearing* in degrees, the initial and final *cardinal direction*, -the *middle point* and the *destination point*. The middle and the destination points returns a +the *middle point* and the *destination point*. The middle and the destination points return a `\League\Geotools\Coordinate\Coordinate` object with the same ellipsoid. ```php @@ -334,19 +307,19 @@ the *middle point* and the *destination point*. The middle and the destination p $geotools = new \League\Geotools\Geotools(); $coordA = new \League\Geotools\Coordinate\Coordinate([48.8234055, 2.3072664]); $coordB = new \League\Geotools\Coordinate\Coordinate([43.296482, 5.36978]); -$vertex = $geotools->vertex()->setFrom($coordA)->setTo($coordB); +$vertex = $geotools->vertex()->setFrom($coordA)->setTo($coordB); printf("%d\n", $vertex->initialBearing()); // 157 (degrees) printf("%s\n", $vertex->initialCardinal()); // SSE (SouthSouthEast) -printf("%d\n", $vertex->finalBearing()); // 160 (degrees) -printf("%s\n", $vertex->finalCardinal()); // SSE (SouthSouthEast) +printf("%d\n", $vertex->finalBearing()); // 160 (degrees) +printf("%s\n", $vertex->finalCardinal()); // SSE (SouthSouthEast) $middlePoint = $vertex->middle(); // \League\Geotools\Coordinate\Coordinate -printf("%s\n", $middlePoint->getLatitude()); // 46.070143125815 +printf("%s\n", $middlePoint->getLatitude()); // 46.070143125815 printf("%s\n", $middlePoint->getLongitude()); // 3.9152401085931 $destinationPoint = $geotools->vertex()->setFrom($coordA)->destination(180, 200000); // \League\Geotools\Coordinate\Coordinate -printf("%s\n", $destinationPoint->getLatitude()); // 47.026774650075 +printf("%s\n", $destinationPoint->getLatitude()); // 47.026774650075 printf("%s\n", $destinationPoint->getLongitude()); // 2.3072664 ``` @@ -363,7 +336,6 @@ $coordToGeohash = new \League\Geotools\Coordinate\Coordinate('43.296482, 5.36978 // encoding $encoded = $geotools->geohash()->encode($coordToGeohash, 4); // 12 is the default length / precision -// encoded printf("%s\n", $encoded->getGeohash()); // spey // encoded bounding box $boundingBox = $encoded->getBoundingBox(); // array of \League\Geotools\Coordinate\CoordinateInterface @@ -376,40 +348,38 @@ printf("http://www.openstreetmap.org/?minlon=%s&minlat=%s&maxlon=%s&maxlat=%s&bo // decoding $decoded = $geotools->geohash()->decode('spey61y'); -// decoded coordinate -printf("%s\n", $decoded->getCoordinate()->getLatitude()); // 43.296432495117 +printf("%s\n", $decoded->getCoordinate()->getLatitude()); // 43.296432495117 printf("%s\n", $decoded->getCoordinate()->getLongitude()); // 5.3702545166016 // decoded bounding box -$boundingBox = $decoded->getBoundingBox(); //array of \League\Geotools\Coordinate\CoordinateInterface +$boundingBox = $decoded->getBoundingBox(); // array of \League\Geotools\Coordinate\CoordinateInterface $southWest = $boundingBox[0]; $northEast = $boundingBox[1]; printf("http://www.openstreetmap.org/?minlon=%s&minlat=%s&maxlon=%s&maxlat=%s&box=yes\n", $southWest->getLongitude(), $southWest->getLatitude(), $northEast->getLongitude(), $northEast->getLatitude() -); // http://www.openstreetmap.org/?minlon=5.3695678710938&minlat=43.295745849609&maxlon=5.3709411621094&maxlat=43.297119140625&box=yes +); ``` -You can also get information about neighbor points ([image](art/geohash_neighbor_points.png)). +You can also get information about neighbor geohash points: ```php geohash()->decode('spey61y'); -// decoding -$decoded = $geotools->geohash()->decode('spey61y'); -// get neighbor geohash -printf("%s\n", $decoded->getNeighbor(\League\Geotools\Geohash\Geohash::DIRECTION_NORTH)); // spey64n +printf("%s\n", $decoded->getNeighbor(\League\Geotools\Geohash\Geohash::DIRECTION_NORTH)); // spey64n printf("%s\n", $decoded->getNeighbor(\League\Geotools\Geohash\Geohash::DIRECTION_SOUTH_EAST)); // spey61x + // get all neighbor geohashes print_r($decoded->getNeighbors(true)); /** * Array * ( - * [north] => spey64n - * [south] => spey61w - * [west] => spey61v - * [east] => spey61z + * [north] => spey64n + * [south] => spey61w + * [west] => spey61v + * [east] => spey61z * [north_west] => spey64j * [north_east] => spey64p * [south_west] => spey61t @@ -426,28 +396,34 @@ entering the code. Read more about the algorithm [here](http://blog.jgc.org/2006 ```php encode(new Coordinate([51.09559, 1.12207])); // MEQ N6G 7NY5 +$tenten = new \League\Geotools\Geohash\TenTen; +$tenten->encode(new \League\Geotools\Coordinate\Coordinate([51.09559, 1.12207])); // MEQ N6G 7NY5 ``` ## Vertex -Represents a segment with a direction. -You can find if two vertexes are on the same line. +Represents a segment with a direction. You can find if two vertices are on the same line. ```php setFrom(48.8234055); - $vertexA->setTo(2.3072664); - $vertexB->setFrom(48.8234055); - $vertexB->setTo(2.3072664); - $vertexA->isOnSameLine($vertexB); +use League\Geotools\Coordinate\Coordinate; +use League\Geotools\Vertex\Vertex; + +$vertexA = new Vertex(); +$vertexA->setFrom(new Coordinate([48.8234055, 2.3072664])); +$vertexA->setTo(new Coordinate([43.296482, 5.36978])); + +$vertexB = new Vertex(); +$vertexB->setFrom(new Coordinate([48.8234055, 2.3072664])); +$vertexB->setTo(new Coordinate([43.296482, 5.36978])); + +$vertexA->isOnSameLine($vertexB); // true ``` ## Polygon -It helps you to know if a point (coordinate) is in a Polygon or on the Polygon's boundaries and if this in on +It helps you to know if a point (coordinate) is in a Polygon or on the Polygon's boundaries and if it is on a Polygon's vertex. First you need to create the polygon, you can provide: @@ -465,14 +441,14 @@ $polygon = new \League\Geotools\Polygon\Polygon([ [49.3895245, 2.6119995], ]); -$polygon->setPrecision(5); // set the comparision precision +$polygon->setPrecision(5); // set the comparison precision $polygon->pointInPolygon(new \League\Geotools\Coordinate\Coordinate([49.1785607, 2.4444580])); // true -$polygon->pointInPolygon(new \League\Geotools\Coordinate\Coordinate([49.1785607, 5])); // false +$polygon->pointInPolygon(new \League\Geotools\Coordinate\Coordinate([49.1785607, 5])); // false $polygon->pointOnBoundary(new \League\Geotools\Coordinate\Coordinate([48.7193486, 2.13546755])); // true $polygon->pointOnBoundary(new \League\Geotools\Coordinate\Coordinate([47.1587188, 2.87841795])); // false $polygon->pointOnVertex(new \League\Geotools\Coordinate\Coordinate([48.4711003, 2.5268555])); // true $polygon->pointOnVertex(new \League\Geotools\Coordinate\Coordinate([49.1785607, 2.4444580])); // false -$polygon->getBoundingBox(); // return the BoundingBox object +$polygon->getBoundingBox(); // returns the BoundingBox object ``` ## CLI @@ -481,103 +457,37 @@ It provides command lines to compute methods provided by **Distance**, **Point** Thanks to the [Symfony Console Component](https://github.com/symfony/Console). ```bash -$ php geotools list // list of available commands -$ php geotools help distance:flat // get the help -$ php geotools distance:flat "40° 26.7717, -79° 56.93172" "30°16′57″N 029°48′32″W" // 4690203.1048522 -$ php geotools distance:haversine "35,45" "45,35" --ft // 4593030.9787593 -$ php geotools distance:vincenty "35,45" "45,35" --km // 1398.4080717661 -$ php geotools d:v "35,45" "45,35" --km --ellipsoid=WGS60 // 1398.4145201642 -$ php geotools point:initial-cardinal "40:26:46.302N 079:56:55.903W" "43.296482, 5.36978" // NE (NordEast) -$ php geotools point:final-cardinal "40:26:46.302N 079:56:55.903W" "43.296482, 5.36978" // ESE (EastSouthEast) -$ php geotools point:destination "40° 26.7717, -79° 56.93172" 25 10000 // 40.527599285543, -79.898914904538 -$ php geotools p:d "40° 26.7717, -79° 56.93172" 25 10000 --ellipsoid=GRS_1980 // 40.527599272782, -79.898914912379 -$ php geotools geohash:encode "40° 26.7717, -79° 56.93172" --length=3 // dpp -$ php geotools convert:dm "40.446195, -79.948862" --format="%P%D°%N %p%d°%n" // 40°26.7717 -79°56.93172 -$ php geotools convert:dms "40.446195, -79.948862" --format="%P%D:%M:%S, %p%d:%m:%s" // 40:26:46, -79:56:56 -$ php geotools convert:utm "60.3912628, 5.3220544" // 32V 297351 6700644 -$ php geotools c:u "60.3912628, 5.3220544" --ellipsoid=AIRY // 32V 297371 6700131 -... -``` - -Compute street addresses, IPv4s or IPv6s geocoding and reverse geocoding right in your console. - -It's possible to define and precise your request through these options: -* `--provider`: `bing_maps`, `yahoo`, `maxmind`... `google_maps` is the default one. See the full list -[here](https://github.com/willdurand/Geocoder#providers). -* `--raw`: the result output in RAW format, shows Adapter, Provider and Arguments if any. -* `--json`: the result output in JSON string format. -* `--args`: this option accepts multiple values (e.g. --args="API_KEY" --args="LOCALE") if your provider needs or -can have arguments. -* `--dumper`: this option is available for geocoding, `gpx`, `geojson`, `kml`, `wkb` and `wkt` by default. -Read more [here](https://github.com/willdurand/Geocoder#dumpers). -* `--format`: this option is available for reverse geocoding, see the mapping -[here](https://github.com/willdurand/Geocoder#formatter). - -```bash -$ php geotools help geocoder:geocode // get the help -$ php geotools geocoder:geocode "Copenhagen, Denmark" // 55.6760968, 12.5683371 -$ php geotools geocoder:geocode "74.200.247.59" --provider="free_geo_ip" // 37.7484, -122.4156 -$ php geotools geocoder:geocode Paris --args="fr_FR" --args="France" --args="true" // 48.856614, 2.3522219 -$ php geotools geocoder:geocode Paris --dumper=wkt // POINT(2.352222 48.856614) -... -$ php geotools geocoder:reverse "48.8631507, 2.388911" // Avenue Gambetta 10, 75020 Paris -$ php geotools geocoder:reverse "48.8631507, 2.388911" --format="%L, %A1, %C" // Paris, Île-De-France, France -$ php geotools geocoder:reverse "48.8631507, 2.388911" --format="%L, %A1, %C" --provider="openstreetmap" -// Paris, Île-De-France, France Métropolitaine -... -$ php geotools geocoder:geocode "Tagensvej 47, Copenhagen" --raw --args=da_DK --args=Denmark -``` - -The last command will show an output like this: - +$ php geotools list # list available commands +$ php geotools help distance:flat # get help for a command +$ php geotools distance:flat "40° 26.7717, -79° 56.93172" "30°16′57″N 029°48′32″W" # 4690203.1048522 +$ php geotools distance:haversine "35,45" "45,35" --ft # 4593030.9787593 +$ php geotools distance:vincenty "35,45" "45,35" --km # 1398.4080717661 +$ php geotools d:v "35,45" "45,35" --km --ellipsoid=WGS60 # 1398.4145201642 +$ php geotools point:initial-cardinal "40:26:46.302N 079:56:55.903W" "43.296482, 5.36978" # NE +$ php geotools point:final-cardinal "40:26:46.302N 079:56:55.903W" "43.296482, 5.36978" # ESE +$ php geotools point:destination "40° 26.7717, -79° 56.93172" 25 10000 # 40.527599285543, -79.898914904538 +$ php geotools p:d "40° 26.7717, -79° 56.93172" 25 10000 --ellipsoid=GRS_1980 +$ php geotools geohash:encode "40° 26.7717, -79° 56.93172" --length=3 # dpp +$ php geotools convert:dm "40.446195, -79.948862" --format="%P%D°%N %p%d°%n" # 40°26.7717 -79°56.93172 +$ php geotools convert:dms "40.446195, -79.948862" --format="%P%D:%M:%S, %p%d:%m:%s" # 40:26:46, -79:56:56 +$ php geotools convert:utm "60.3912628, 5.3220544" # 32V 297351 6700644 +$ php geotools c:u "60.3912628, 5.3220544" --ellipsoid=AIRY # 32V 297371 6700131 ``` -HttpClient: \Http\Client\Curl\Client -Provider: \Geocoder\Provider\GoogleMaps -Cache: \League\Geotools\Cache\Redis -Arguments: da_DK,Denmark ---- -Latitude: 55.699953 -Longitude: 12.552736 -Bounds - - South: 55.699953 - - West: 12.552736 - - North: 55.699953 - - East: 12.552736 -Street Number: 47 -Street Name: Tagensvej -Zipcode: 2200 -City: Copenhagen -City District: København N -County: København -County Code: KØBENHAVN -Region: Capital Region Of Denmark -Region Code: CAPITAL REGION OF DENMARK -Country: Denmark -Country Code: DK -Timezone: -``` - -Integration with Frameworks ---------------------------- - -* [Laravel 4 & 5](https://github.com/toin0u/Geotools-laravel) -* [Silex](https://github.com/toin0u/Geotools-silex) -* ... Unit Tests ---------- -To run unit tests, you'll need the `cURL` extension and a set of dependencies, you can install them using Composer: +Install dependencies: ```bash -$ php composer.phar install --dev +composer install ``` -Once installed, just launch the following command: +Run the test suite: ```bash -$ phpunit --coverage-text +./vendor/bin/phpunit ``` @@ -588,21 +498,19 @@ Credits * [Pascal Borreli](https://twitter.com/pborreli) * [Phil Sturgeon](https://twitter.com/philsturgeon) * [Gabriel Bull](mailto:me@gabrielbull.com) -* [All contributors](https://github.com/toin0u/Geotools/contributors) +* [All contributors](https://github.com/thephpleague/geotools/contributors) Acknowledgments --------------- -* [Geocoder](https://github.com/willdurand/Geocoder) - -[MIT](https://raw.github.com/willdurand/Geocoder/master/LICENSE) +* [Geocoder](https://github.com/geocoder-php/Geocoder) - +[MIT](https://raw.github.com/geocoder-php/Geocoder/master/LICENSE) * [ReactPHP](https://github.com/reactphp/) - [MIT](https://raw.github.com/reactphp/react/master/LICENSE) * [Symfony Console Component](https://github.com/symfony/Console) - [MIT](https://raw.github.com/symfony/Console/master/LICENSE) * [Symfony Serializer Component](https://github.com/symfony/Serializer) - [MIT](https://raw.github.com/symfony/Serializer/master/LICENSE) -* [PHP client library for Redis](https://github.com/nrk/predis) - -[MIT](https://raw.github.com/nrk/predis/master/LICENSE) * [Geokit](https://github.com/jsor/Geokit), [Geotools-for-CodeIgniter](https://github.com/weejames/Geotools-for-CodeIgniter), [geotools-php](https://github.com/jillesvangurp/geotools-php) ... @@ -621,7 +529,7 @@ Please see [CONTRIBUTING](https://github.com/thephpleague/geotools/blob/master/C Support ------- -Bugs and feature request are tracked on [GitHub](https://github.com/thephpleague/geotools/issues) +Bugs and feature requests are tracked on [GitHub](https://github.com/thephpleague/geotools/issues) Contributor Code of Conduct --------------------------- @@ -657,5 +565,3 @@ License Geotools is released under the MIT License. See the bundled [LICENSE](https://github.com/thephpleague/geotools/blob/master/LICENSE) file for details. - -[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/toin0u/Geotools/trend.png)](https://bitdeli.com/free "Bitdeli Badge") diff --git a/composer.json b/composer.json index ff348376..88d05275 100644 --- a/composer.json +++ b/composer.json @@ -1,61 +1,71 @@ { - "name" : "league/geotools", - "type" : "library", - "description" : "Geo-related tools PHP 7.3+ library", - "keywords" : ["geotools", "geometry", "geocoder", "geocoding", "geoip", "bounds", "distance", "batch", "async", "geohash", "geolocation", "latlong", "latitude", "longitude"], - "license" : "MIT", - - "authors" : [{ - "name" : "Antoine Corcy", - "email" : "contact@sbin.dk", - "homepage" : "http://sbin.dk", - "role" : "Developer" - }], - - "support" : { - "issues" : "https://github.com/thephpleague/geotools/issues", - "source" : "https://github.com/thephpleague/geotools" - }, - - "replace" : { - "toin0u/geotools" : "*" - }, - - "require" : { - "php" : "^7.3 || ^7.4 || ^8.0", - "psr/cache" : "^1.0 || ^2.0 || ^3.0", - "willdurand/geocoder" : "^4.5 || ^5.0", - "react/promise" : "^2.8 || ^3.0", - "symfony/console" : "^4.4 || ^5.0 || ^6.0 || ^7.0", - "symfony/property-access" : "^4.4 || ^5.0 || ^6.0 || ^7.0", - "symfony/serializer" : "^4.4 || ^5.0 || ^6.0 || ^7.0", - "react/event-loop" : "^1.0", - "php-http/discovery" : "^1.0", - "ext-bcmath": "*", - "ext-json": "*" - }, - - "require-dev" : { - "phpunit/phpunit" : "^9.5", - "cache/array-adapter" : "^1.0" - }, - - "autoload" : { - "psr-4" : { - "League\\Geotools\\" : "src" - } - }, - - "autoload-dev" : { - "psr-4" : { - "League\\Geotools\\Tests\\" : "tests" - } - }, - - "bin" : ["bin/geotools"], - "config": { - "allow-plugins": { - "php-http/discovery": true - } + "name": "league/geotools", + "type": "library", + "description": "Geo-related tools PHP 8.2+ library", + "keywords": [ + "geotools", + "geometry", + "geocoder", + "geocoding", + "geoip", + "bounds", + "distance", + "batch", + "async", + "geohash", + "geolocation", + "latlong", + "latitude", + "longitude" + ], + "license": "MIT", + "authors": [ + { + "name": "Antoine Corcy", + "email": "contact@sbin.dk", + "homepage": "http://sbin.dk", + "role": "Developer" } + ], + "support": { + "issues": "https://github.com/thephpleague/geotools/issues", + "source": "https://github.com/thephpleague/geotools" + }, + "replace": { + "toin0u/geotools": "*" + }, + "require": { + "php": "^8.2", + "psr/cache": "^3.0", + "willdurand/geocoder": "^4.5 || ^5.0", + "react/promise": "^2.8 || ^3.0", + "symfony/console": "^7.0 || ^8.0", + "symfony/property-access": "^7.0 || ^8.0", + "symfony/serializer": "^7.0 || ^8.0", + "react/event-loop": "^1.0", + "php-http/discovery": "^1.0", + "ext-bcmath": "*", + "ext-json": "*" + }, + "require-dev": { + "phpunit/phpunit": "^11.0" + }, + "autoload": { + "psr-4": { + "League\\Geotools\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "League\\Geotools\\Tests\\": "tests" + } + }, + "bin": [ + "bin/geotools" + ], + "config": { + "allow-plugins": { + "php-http/discovery": true + } + } } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 00000000..72d0f3fc --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,6 @@ +parameters: + level: 6 + paths: + - src + phpVersion: 80200 + checkMissingIterableValueType: false diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 00000000..8656d336 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,13 @@ + + + + + tests + + + + + src + + + diff --git a/phpunit.xml.dist b/phpunit.xml.dist deleted file mode 100644 index 466b0ee6..00000000 --- a/phpunit.xml.dist +++ /dev/null @@ -1,13 +0,0 @@ - - - - - ./src/ - - - - - ./tests/ - - - diff --git a/src/CLI/Command/Convert/DM.php b/src/CLI/Command/Convert/DM.php index 9fdc8d5e..b5e35d5f 100644 --- a/src/CLI/Command/Convert/DM.php +++ b/src/CLI/Command/Convert/DM.php @@ -27,7 +27,7 @@ */ class DM extends \Symfony\Component\Console\Command\Command { - protected function configure() + protected function configure(): void { $availableEllipsoids = Ellipsoid::getAvailableEllipsoidNames(); diff --git a/src/CLI/Command/Convert/DMS.php b/src/CLI/Command/Convert/DMS.php index 7cf425c5..3b686d15 100644 --- a/src/CLI/Command/Convert/DMS.php +++ b/src/CLI/Command/Convert/DMS.php @@ -27,7 +27,7 @@ */ class DMS extends \Symfony\Component\Console\Command\Command { - protected function configure() + protected function configure(): void { $availableEllipsoids = Ellipsoid::getAvailableEllipsoidNames(); diff --git a/src/CLI/Command/Convert/UTM.php b/src/CLI/Command/Convert/UTM.php index 28de864f..8fe07e3b 100644 --- a/src/CLI/Command/Convert/UTM.php +++ b/src/CLI/Command/Convert/UTM.php @@ -26,7 +26,7 @@ */ class UTM extends \Symfony\Component\Console\Command\Command { - protected function configure() + protected function configure(): void { $availableEllipsoids = Ellipsoid::getAvailableEllipsoidNames(); diff --git a/src/CLI/Command/Distance/All.php b/src/CLI/Command/Distance/All.php index 1c5f15b3..b5344b34 100644 --- a/src/CLI/Command/Distance/All.php +++ b/src/CLI/Command/Distance/All.php @@ -26,7 +26,7 @@ */ class All extends \Symfony\Component\Console\Command\Command { - protected function configure() + protected function configure(): void { $availableEllipsoids = Ellipsoid::getAvailableEllipsoidNames(); diff --git a/src/CLI/Command/Distance/Flat.php b/src/CLI/Command/Distance/Flat.php index bba024e5..26caed3c 100644 --- a/src/CLI/Command/Distance/Flat.php +++ b/src/CLI/Command/Distance/Flat.php @@ -26,7 +26,7 @@ */ class Flat extends \Symfony\Component\Console\Command\Command { - protected function configure() + protected function configure(): void { $availableEllipsoids = Ellipsoid::getAvailableEllipsoidNames(); diff --git a/src/CLI/Command/Distance/GreatCircle.php b/src/CLI/Command/Distance/GreatCircle.php index 77c442c1..1754a0ac 100644 --- a/src/CLI/Command/Distance/GreatCircle.php +++ b/src/CLI/Command/Distance/GreatCircle.php @@ -26,7 +26,7 @@ */ class GreatCircle extends \Symfony\Component\Console\Command\Command { - protected function configure() + protected function configure(): void { $availableEllipsoids = Ellipsoid::getAvailableEllipsoidNames(); diff --git a/src/CLI/Command/Distance/Haversine.php b/src/CLI/Command/Distance/Haversine.php index d22bc6e4..01751522 100644 --- a/src/CLI/Command/Distance/Haversine.php +++ b/src/CLI/Command/Distance/Haversine.php @@ -26,7 +26,7 @@ */ class Haversine extends \Symfony\Component\Console\Command\Command { - protected function configure() + protected function configure(): void { $availableEllipsoids = Ellipsoid::getAvailableEllipsoidNames(); diff --git a/src/CLI/Command/Distance/Vincenty.php b/src/CLI/Command/Distance/Vincenty.php index 7c8874d2..4d2edba7 100644 --- a/src/CLI/Command/Distance/Vincenty.php +++ b/src/CLI/Command/Distance/Vincenty.php @@ -26,7 +26,7 @@ */ class Vincenty extends \Symfony\Component\Console\Command\Command { - protected function configure() + protected function configure(): void { $availableEllipsoids = Ellipsoid::getAvailableEllipsoidNames(); diff --git a/src/CLI/Command/Geocoder/Geocode.php b/src/CLI/Command/Geocoder/Geocode.php index e34caec6..64f38602 100644 --- a/src/CLI/Command/Geocoder/Geocode.php +++ b/src/CLI/Command/Geocoder/Geocode.php @@ -26,7 +26,7 @@ */ class Geocode extends Command { - protected function configure() + protected function configure(): void { $this ->setName('geocoder:geocode') diff --git a/src/CLI/Command/Geocoder/Reverse.php b/src/CLI/Command/Geocoder/Reverse.php index 7bb2b0f4..9c9767e9 100644 --- a/src/CLI/Command/Geocoder/Reverse.php +++ b/src/CLI/Command/Geocoder/Reverse.php @@ -28,7 +28,7 @@ */ class Reverse extends Command { - protected function configure() + protected function configure(): void { $this ->setName('geocoder:reverse') diff --git a/src/CLI/Command/Geohash/Decode.php b/src/CLI/Command/Geohash/Decode.php index ddb69592..2695ff43 100644 --- a/src/CLI/Command/Geohash/Decode.php +++ b/src/CLI/Command/Geohash/Decode.php @@ -23,7 +23,7 @@ */ class Decode extends \Symfony\Component\Console\Command\Command { - protected function configure() + protected function configure(): void { $this ->setName('geohash:decode') diff --git a/src/CLI/Command/Geohash/Encode.php b/src/CLI/Command/Geohash/Encode.php index 42c1d170..ae7ba4f8 100644 --- a/src/CLI/Command/Geohash/Encode.php +++ b/src/CLI/Command/Geohash/Encode.php @@ -26,7 +26,7 @@ */ class Encode extends \Symfony\Component\Console\Command\Command { - protected function configure() + protected function configure(): void { $this ->setName('geohash:encode') diff --git a/src/CLI/Command/Vertex/Destination.php b/src/CLI/Command/Vertex/Destination.php index 4ab0e62c..f9fe398d 100644 --- a/src/CLI/Command/Vertex/Destination.php +++ b/src/CLI/Command/Vertex/Destination.php @@ -26,7 +26,7 @@ */ class Destination extends \Symfony\Component\Console\Command\Command { - protected function configure() + protected function configure(): void { $availableEllipsoids = Ellipsoid::getAvailableEllipsoidNames(); diff --git a/src/CLI/Command/Vertex/FinalBearing.php b/src/CLI/Command/Vertex/FinalBearing.php index 2c659206..9dbb4a87 100644 --- a/src/CLI/Command/Vertex/FinalBearing.php +++ b/src/CLI/Command/Vertex/FinalBearing.php @@ -26,7 +26,7 @@ */ class FinalBearing extends \Symfony\Component\Console\Command\Command { - protected function configure() + protected function configure(): void { $availableEllipsoids = Ellipsoid::getAvailableEllipsoidNames(); diff --git a/src/CLI/Command/Vertex/FinalCardinal.php b/src/CLI/Command/Vertex/FinalCardinal.php index 18946b43..f8590212 100644 --- a/src/CLI/Command/Vertex/FinalCardinal.php +++ b/src/CLI/Command/Vertex/FinalCardinal.php @@ -26,7 +26,7 @@ */ class FinalCardinal extends \Symfony\Component\Console\Command\Command { - protected function configure() + protected function configure(): void { $availableEllipsoids = Ellipsoid::getAvailableEllipsoidNames(); diff --git a/src/CLI/Command/Vertex/InitialBearing.php b/src/CLI/Command/Vertex/InitialBearing.php index cd84b1ff..5eac54e6 100644 --- a/src/CLI/Command/Vertex/InitialBearing.php +++ b/src/CLI/Command/Vertex/InitialBearing.php @@ -26,7 +26,7 @@ */ class InitialBearing extends \Symfony\Component\Console\Command\Command { - protected function configure() + protected function configure(): void { $availableEllipsoids = Ellipsoid::getAvailableEllipsoidNames(); diff --git a/src/CLI/Command/Vertex/InitialCardinal.php b/src/CLI/Command/Vertex/InitialCardinal.php index 444db18e..d769a1ea 100644 --- a/src/CLI/Command/Vertex/InitialCardinal.php +++ b/src/CLI/Command/Vertex/InitialCardinal.php @@ -26,7 +26,7 @@ */ class InitialCardinal extends \Symfony\Component\Console\Command\Command { - protected function configure() + protected function configure(): void { $availableEllipsoids = Ellipsoid::getAvailableEllipsoidNames(); diff --git a/src/CLI/Command/Vertex/Middle.php b/src/CLI/Command/Vertex/Middle.php index 684d0ced..3a876013 100644 --- a/src/CLI/Command/Vertex/Middle.php +++ b/src/CLI/Command/Vertex/Middle.php @@ -26,7 +26,7 @@ */ class Middle extends \Symfony\Component\Console\Command\Command { - protected function configure() + protected function configure(): void { $availableEllipsoids = Ellipsoid::getAvailableEllipsoidNames(); diff --git a/src/Coordinate/Coordinate.php b/src/Coordinate/Coordinate.php index 305e712e..8aae7553 100644 --- a/src/Coordinate/Coordinate.php +++ b/src/Coordinate/Coordinate.php @@ -94,7 +94,7 @@ public function normalizeLatitude($latitude) */ public function normalizeLongitude($longitude) { - if (180 === floor($longitude) % 360) { + if (180 === (int) floor($longitude) % 360) { return '180.0'; } diff --git a/src/Coordinate/EllipsoidName.php b/src/Coordinate/EllipsoidName.php new file mode 100644 index 00000000..e1b86120 --- /dev/null +++ b/src/Coordinate/EllipsoidName.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace League\Geotools\Coordinate; + +enum EllipsoidName: string +{ + case AIRY = 'AIRY'; + case AUSTRALIAN_NATIONAL = 'AUSTRALIAN_NATIONAL'; + case BESSEL_1841 = 'BESSEL_1841'; + case BESSEL_1841_NAMBIA = 'BESSEL_1841_NAMBIA'; + case CLARKE_1866 = 'CLARKE_1866'; + case CLARKE_1880 = 'CLARKE_1880'; + case EVEREST = 'EVEREST'; + case FISCHER_1960_MERCURY = 'FISCHER_1960_MERCURY'; + case FISCHER_1968 = 'FISCHER_1968'; + case GRS_1967 = 'GRS_1967'; + case GRS_1980 = 'GRS_1980'; + case HELMERT_1906 = 'HELMERT_1906'; + case HOUGH = 'HOUGH'; + case INTERNATIONAL = 'INTERNATIONAL'; + case KRASSOVSKY = 'KRASSOVSKY'; + case MODIFIED_AIRY = 'MODIFIED_AIRY'; + case MODIFIED_EVEREST = 'MODIFIED_EVEREST'; + case MODIFIED_FISCHER_1960 = 'MODIFIED_FISCHER_1960'; + case SOUTH_AMERICAN_1969 = 'SOUTH_AMERICAN_1969'; + case WGS60 = 'WGS60'; + case WGS66 = 'WGS66'; + case WGS72 = 'WGS72'; + case WGS84 = 'WGS84'; +} diff --git a/tests/ArrayCollectionTest.php b/tests/ArrayCollectionTest.php index 27a8ca5d..c4688870 100644 --- a/tests/ArrayCollectionTest.php +++ b/tests/ArrayCollectionTest.php @@ -3,12 +3,11 @@ namespace League\Geotools\Tests; use League\Geotools\ArrayCollection; +use PHPUnit\Framework\Attributes\Test; class ArrayCollectionTest extends TestCase { - /** - * @test - */ + #[Test] public function itShouldBeConvertibleToArray() { $array = ['foo', 'bar']; @@ -18,9 +17,7 @@ public function itShouldBeConvertibleToArray() $this->assertEquals($array, $collection->toArray()); } - /** - * @test - */ + #[Test] public function itShouldSerializeTheInnerElements() { $array = ['foo', 'bar']; @@ -30,9 +27,7 @@ public function itShouldSerializeTheInnerElements() $this->assertEquals(json_encode($array), json_encode($collection)); } - /** - * @test - */ + #[Test] public function itShouldBehaveAsAnArray() { $array = ['foo', 'baz'=>'bar']; @@ -51,9 +46,7 @@ public function itShouldBehaveAsAnArray() $this->assertFalse(isset($collection[99])); } - /** - * @test - */ + #[Test] public function itShouldBeCountable() { $array = ['foo', 'bar']; @@ -63,9 +56,7 @@ public function itShouldBeCountable() $this->assertEquals(2, count($collection)); } - /** - * @test - */ + #[Test] public function itShouldOfferAccessToInnerElementsByKey() { $array = ['foo' => 'bar']; @@ -86,9 +77,7 @@ public function itShouldOfferAccessToInnerElementsByKey() $this->assertNull($collection->remove('dummy')); } - /** - * @test - */ + #[Test] public function itShouldMergeCollections() { $array1 = ['foo' => 'bar']; diff --git a/tests/Batch/BatchTest.php b/tests/Batch/BatchTest.php index 190ff9ba..d3684a19 100644 --- a/tests/Batch/BatchTest.php +++ b/tests/Batch/BatchTest.php @@ -11,15 +11,15 @@ namespace League\Geotools\Tests\Batch; -use Cache\Adapter\PHPArray\ArrayCachePool; +use Psr\Cache\CacheItemPoolInterface; +use PHPUnit\Framework\Attributes\DataProvider; use Geocoder\Collection; use Geocoder\Provider\AbstractProvider; use Geocoder\Provider\Provider as ProviderInterface; use Geocoder\Query\GeocodeQuery; use Geocoder\Query\ReverseQuery; use League\Geotools\Batch\Batch; -use Psr\Cache\CacheItemPoolInterface; - +use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; /** * @author Antoine Corcy */ @@ -31,93 +31,73 @@ class BatchTest extends \League\Geotools\Tests\TestCase protected $data; protected $values; protected $coordinates; - protected function setup(): void { $this->data = array( 'latitude' => 48.8234055, 'longitude' => 2.3072664, ); - $this->providers = array( new MockProvider('provider1'), new MockProvider('provider2'), new MockProvider('provider3'), new MockProvider('provider4'), ); - foreach ($this->providers as $provider) { $this->providersName[] = $provider->getName(); } - $this->values = array( 'foo', 'bar', 'baz', 'qux', ); - $this->coordinates = array( $this->getMockCoordinateReturns(array(1, 2)), $this->getMockCoordinateReturns(array(1, 2)), $this->getMockCoordinateReturns(array(1, 2)), $this->getMockCoordinateReturns(array(1, 2)), ); - $this->geocoder = $this->getMockGeocoderReturns($this->providers); } - - /** - * @doesNotPerformAssertions - */ + #[DoesNotPerformAssertions] public function testConstructorShouldAcceptGeocoderInterface() { new TestableBatch($this->getStubGeocoder()); } - public function testConstructorShouldSetGeocoderInterface() { $batch = new TestableBatch($this->getStubGeocoder()); $geocoder = $batch->getGeocoder(); - $this->assertTrue(is_object($geocoder)); $this->assertInstanceOf('Geocoder\Geocoder', $geocoder); } - public function testGeocodeShouldReturnBatchInterface() { $batch = new TestableBatch($this->geocoder); $batchReturned = $batch->geocode('foo'); - $this->assertTrue(is_object($batchReturned)); $this->assertInstanceOf('League\Geotools\Batch\Batch', $batchReturned); $this->assertInstanceOf('League\Geotools\Batch\BatchInterface', $batchReturned); $this->assertSame($batch, $batchReturned); } - public function testGeocodeShouldMadeCorrectTasksArrayToCompute() { $batch = new TestableBatch($this->geocoder); $batch->geocode('foo'); $tasks = $batch->getTasks(); - $this->assertTrue(is_array($tasks)); $this->assertCount(count($this->providers), $tasks); } - public function testGeocodeShouldMadeCorrectTasksArrayToComputeWithManyValues() { $batch = new TestableBatch($this->geocoder); $batch->geocode($this->values); $tasks = $batch->getTasks(); - $this->assertTrue(is_array($tasks)); $this->assertCount(count($this->providers) * count($this->values), $tasks); } - - /** - * @dataProvider invalidValuesProvider - */ + #[DataProvider('invalidValuesProvider')] public function testGeocodeShouldThrowInvalidArgumentException($values) { $this->expectException(\League\Geotools\Exception\InvalidArgumentException::class); @@ -125,8 +105,7 @@ public function testGeocodeShouldThrowInvalidArgumentException($values) $batch = new TestableBatch($this->geocoder); $batch->geocode($values); } - - public function invalidValuesProvider() + public static function invalidValuesProvider() { return array( array(0), @@ -138,41 +117,32 @@ public function invalidValuesProvider() array(' ', ' '), ); } - public function testReverseShouldReturnBatchInterface() { $batch = new TestableBatch($this->geocoder); $batchReturned = $batch->reverse($this->getStubCoordinate()); - $this->assertTrue(is_object($batchReturned)); $this->assertInstanceOf('League\Geotools\Batch\Batch', $batchReturned); $this->assertInstanceOf('League\Geotools\Batch\BatchInterface', $batchReturned); $this->assertSame($batch, $batchReturned); } - public function testReverseShouldMadeCorrectTasksArrayToCompute() { $batch = new TestableBatch($this->geocoder); $batch->reverse($this->getStubCoordinate()); $tasks = $batch->getTasks(); - $this->assertTrue(is_array($tasks)); $this->assertCount(count($this->providers), $tasks); } - public function testReverseShouldMadeCorrectTasksArrayToComputeWithManyCoordinates() { $batch = new TestableBatch($this->geocoder); $batch->reverse($this->coordinates); $tasks = $batch->getTasks(); - $this->assertTrue(is_array($tasks)); $this->assertCount(count($this->providers) * count($this->coordinates), $tasks); } - - /** - * @dataProvider coordinatesProvider - */ + #[DataProvider('coordinatesProvider')] public function testReverseShouldThrowInvalidArgumentException($coordinates) { $this->expectException(\League\Geotools\Exception\InvalidArgumentException::class); @@ -180,8 +150,7 @@ public function testReverseShouldThrowInvalidArgumentException($coordinates) $batch = new TestableBatch($this->geocoder); $batch->reverse($coordinates); } - - public function coordinatesProvider() + public static function coordinatesProvider() { return array( array(0), @@ -193,13 +162,11 @@ public function coordinatesProvider() array(' ', ' '), ); } - public function testBatchGeocodeInSerie() { $geocoder = $this->getMockGeocoderReturns($this->providers, $this->data); $batch = new TestableBatch($geocoder); $resultComputedInSerie = $batch->geocode('foo')->serie(); - $this->assertCount(count($this->providers), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -208,7 +175,6 @@ public function testBatchGeocodeInSerie() $this->assertEquals($this->data['longitude'], $providerResult->getLongitude()); } } - public function testBatchGeocodeInSerieWithCache() { $geocoder = $this->getMockGeocoderReturns($this->providers, $this->data); @@ -217,7 +183,6 @@ public function testBatchGeocodeInSerieWithCache() ->geocode('foo') ->setCache($this->getMockCacheReturns($this->getMockGeocodedReturns($this->data))) ->serie(); - $this->assertCount(count($this->providers), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -226,13 +191,11 @@ public function testBatchGeocodeInSerieWithCache() $this->assertEquals($this->data['longitude'], $providerResult->getLongitude()); } } - public function testBatchGeocodeInSerieWithManyValues() { $geocoder = $this->getMockGeocoderReturns($this->providers, $this->data); $batch = new TestableBatch($geocoder); $resultComputedInSerie = $batch->geocode($this->values)->serie(); - $this->assertCount(count($this->providers) * count($this->values), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -241,7 +204,6 @@ public function testBatchGeocodeInSerieWithManyValues() $this->assertEquals($this->data['longitude'], $providerResult->getLongitude()); } } - public function testBatchGeocodeInSerieWithManyValuesWithCache() { $geocoder = $this->getMockGeocoderReturns($this->providers, $this->data); @@ -250,7 +212,6 @@ public function testBatchGeocodeInSerieWithManyValuesWithCache() ->geocode($this->values) ->setCache($this->getMockCacheReturns($this->getMockGeocodedReturns($this->data))) ->serie(); - $this->assertCount(count($this->providers) * count($this->values), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -259,13 +220,11 @@ public function testBatchGeocodeInSerieWithManyValuesWithCache() $this->assertEquals($this->data['longitude'], $providerResult->getLongitude()); } } - public function testBatchReverseGeocodingInSerie() { $geocoder = $this->getMockGeocoderReturns($this->providers, $this->data); $batch = new TestableBatch($geocoder); $resultComputedInSerie = $batch->reverse($this->getStubCoordinate($this->data['latitude'], $this->data['longitude']))->serie(); - $this->assertCount(count($this->providers), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -274,7 +233,6 @@ public function testBatchReverseGeocodingInSerie() $this->assertEquals($this->data['longitude'], $providerResult->getLongitude()); } } - public function testBatchReverseGeocodingInSerieWithCache() { $geocoder = $this->getMockGeocoderReturns($this->providers, $this->data); @@ -283,7 +241,6 @@ public function testBatchReverseGeocodingInSerieWithCache() ->reverse($this->getStubCoordinate()) ->setCache($this->getMockCacheReturns($this->getMockGeocodedReturns($this->data))) ->serie(); - $this->assertCount(count($this->providers), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -292,13 +249,11 @@ public function testBatchReverseGeocodingInSerieWithCache() $this->assertEquals($this->data['longitude'], $providerResult->getLongitude()); } } - public function testBatchReverseGeocodingInSerieWithManyCoordinates() { $geocoder = $this->getMockGeocoderReturns($this->providers, $this->data); $batch = new TestableBatch($geocoder); $resultComputedInSerie = $batch->reverse($this->coordinates)->serie(); - $this->assertCount(count($this->providers) * count($this->coordinates), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -307,7 +262,6 @@ public function testBatchReverseGeocodingInSerieWithManyCoordinates() $this->assertEquals($this->data['longitude'], $providerResult->getLongitude()); } } - public function testBatchReverseGeocodingInSerieWithManyCoordinatesWithCache() { $geocoder = $this->getMockGeocoderReturns($this->providers, $this->data); @@ -316,7 +270,6 @@ public function testBatchReverseGeocodingInSerieWithManyCoordinatesWithCache() ->reverse($this->coordinates) ->setCache($this->getMockCacheReturns($this->getMockGeocodedReturns($this->data))) ->serie(); - $this->assertCount(count($this->providers) * count($this->coordinates), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -325,13 +278,11 @@ public function testBatchReverseGeocodingInSerieWithManyCoordinatesWithCache() $this->assertEquals($this->data['longitude'], $providerResult->getLongitude()); } } - public function testBatchGeocodeInParallel() { $geocoder = $this->getMockGeocoderReturns($this->providers, $this->data); $batch = new TestableBatch($geocoder); $resultComputedInParallel = $batch->geocode('foo')->parallel(); - $this->assertCount(count($this->providers), $resultComputedInParallel); foreach ($resultComputedInParallel as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -340,7 +291,6 @@ public function testBatchGeocodeInParallel() $this->assertEquals($this->data['longitude'], $providerResult->getLongitude()); } } - public function testBatchGeocodeInParallelWithCache() { $geocoder = $this->getMockGeocoderReturns($this->providers, $this->data); @@ -349,7 +299,6 @@ public function testBatchGeocodeInParallelWithCache() ->geocode('foo') ->setCache($this->getMockCacheReturns($this->getMockGeocodedReturns($this->data))) ->parallel(); - $this->assertCount(count($this->providers), $resultComputedInParallel); foreach ($resultComputedInParallel as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -358,13 +307,11 @@ public function testBatchGeocodeInParallelWithCache() $this->assertEquals($this->data['longitude'], $providerResult->getLongitude()); } } - public function testBatchGeocodeInParallelWithManyValues() { $geocoder = $this->getMockGeocoderReturns($this->providers, $this->data); $batch = new TestableBatch($geocoder); $resultComputedInParallel = $batch->geocode($this->values)->parallel(); - $this->assertCount(count($this->providers) * count($this->values), $resultComputedInParallel); foreach ($resultComputedInParallel as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -373,7 +320,6 @@ public function testBatchGeocodeInParallelWithManyValues() $this->assertEquals($this->data['longitude'], $providerResult->getLongitude()); } } - public function testBatchGeocodeInParallelWithManyValuesWithCache() { $geocoder = $this->getMockGeocoderReturns($this->providers, $this->data); @@ -382,7 +328,6 @@ public function testBatchGeocodeInParallelWithManyValuesWithCache() ->geocode($this->values) ->setCache($this->getMockCacheReturns($this->getMockGeocodedReturns($this->data))) ->parallel(); - $this->assertCount(count($this->providers) * count($this->values), $resultComputedInParallel); foreach ($resultComputedInParallel as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -391,13 +336,11 @@ public function testBatchGeocodeInParallelWithManyValuesWithCache() $this->assertEquals($this->data['longitude'], $providerResult->getLongitude()); } } - public function testBatchReverseGeocodingInParallel() { $geocoder = $this->getMockGeocoderReturns($this->providers, $this->data); $batch = new TestableBatch($geocoder); $resultComputedInSerie = $batch->reverse($this->getStubCoordinate($this->data['latitude'], $this->data['longitude']))->parallel(); - $this->assertCount(count($this->providers), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -406,7 +349,6 @@ public function testBatchReverseGeocodingInParallel() $this->assertEquals($this->data['longitude'], $providerResult->getLongitude()); } } - public function testBatchReverseGeocodingInParallelWithCache() { $geocoder = $this->getMockGeocoderReturns($this->providers, $this->data); @@ -415,7 +357,6 @@ public function testBatchReverseGeocodingInParallelWithCache() ->reverse($this->getStubCoordinate()) ->setCache($this->getMockCacheReturns($this->getMockGeocodedReturns($this->data))) ->parallel(); - $this->assertCount(count($this->providers), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -424,13 +365,11 @@ public function testBatchReverseGeocodingInParallelWithCache() $this->assertEquals($this->data['longitude'], $providerResult->getLongitude()); } } - public function testBatchReverseGeocodingInParallelWithManyCoordinates() { $geocoder = $this->getMockGeocoderReturns($this->providers, $this->data); $batch = new TestableBatch($geocoder); $resultComputedInSerie = $batch->reverse($this->coordinates)->parallel(); - $this->assertCount(count($this->providers) * count($this->coordinates), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -439,7 +378,6 @@ public function testBatchReverseGeocodingInParallelWithManyCoordinates() $this->assertEquals($this->data['longitude'], $providerResult->getLongitude()); } } - public function testBatchReverseGeocodingInParallelWithManyCoordinatesWithCache() { $geocoder = $this->getMockGeocoderReturns($this->providers, $this->data); @@ -448,7 +386,6 @@ public function testBatchReverseGeocodingInParallelWithManyCoordinatesWithCache( ->reverse($this->coordinates) ->setCache($this->getMockCacheReturns($this->getMockGeocodedReturns($this->data))) ->parallel(); - $this->assertCount(count($this->providers) * count($this->coordinates), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -457,13 +394,11 @@ public function testBatchReverseGeocodingInParallelWithManyCoordinatesWithCache( $this->assertEquals($this->data['longitude'], $providerResult->getLongitude()); } } - public function testBatchGeocodeInSerieReturnNewGeocodedInstance() { $geocoder = $this->getMockGeocoderThrowException($this->providers); $batch = new TestableBatch($geocoder); $resultComputedInSerie = $batch->geocode('foo')->serie(); - $this->assertCount(count($this->providers), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -474,7 +409,6 @@ public function testBatchGeocodeInSerieReturnNewGeocodedInstance() $this->assertEquals('foo', $providerResult->getQuery()); } } - public function testBatchGeocodeInSerieReturnNewGeocodedInstanceWithCache() { $geocoder = $this->getMockGeocoderThrowException($this->providers); @@ -483,7 +417,6 @@ public function testBatchGeocodeInSerieReturnNewGeocodedInstanceWithCache() ->geocode('foo') ->setCache($this->getMockCacheReturns($this->getMockGeocodedReturns($this->data))) ->serie(); - $this->assertCount(count($this->providers), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -492,13 +425,11 @@ public function testBatchGeocodeInSerieReturnNewGeocodedInstanceWithCache() $this->assertEquals(2.3072664, $providerResult->getLongitude()); } } - public function testBatchGeocodeInParallelReturnNewGeocodedInstance() { $geocoder = $this->getMockGeocoderThrowException($this->providers); $batch = new TestableBatch($geocoder); $resultComputedInParallel = $batch->geocode('foo')->parallel(); - $this->assertCount(count($this->providers), $resultComputedInParallel); foreach ($resultComputedInParallel as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -509,7 +440,6 @@ public function testBatchGeocodeInParallelReturnNewGeocodedInstance() $this->assertEquals('foo', $providerResult->getQuery()); } } - public function testBatchGeocodeInParallelReturnNewGeocodedInstanceWithCache() { $geocoder = $this->getMockGeocoderThrowException($this->providers); @@ -518,7 +448,6 @@ public function testBatchGeocodeInParallelReturnNewGeocodedInstanceWithCache() ->geocode('foo') ->setCache($this->getMockCacheReturns($this->getMockGeocodedReturns($this->data))) ->parallel(); - $this->assertCount(count($this->providers), $resultComputedInParallel); foreach ($resultComputedInParallel as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -527,13 +456,11 @@ public function testBatchGeocodeInParallelReturnNewGeocodedInstanceWithCache() $this->assertEquals(2.3072664, $providerResult->getLongitude()); } } - public function testBatchGeocodeInSerieReturnNewGeocodedInstanceWithManyValues() { $geocoder = $this->getMockGeocoderThrowException($this->providers); $batch = new TestableBatch($geocoder); $resultComputedInSerie = $batch->geocode($this->values)->serie(); - $this->assertCount(count($this->providers) * count($this->values), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -544,7 +471,6 @@ public function testBatchGeocodeInSerieReturnNewGeocodedInstanceWithManyValues() $this->assertContains($providerResult->getQuery(), $this->values); } } - public function testBatchGeocodeInSerieReturnNewGeocodedInstanceWithManyValuesWithCache() { $geocoder = $this->getMockGeocoderThrowException($this->providers); @@ -553,7 +479,6 @@ public function testBatchGeocodeInSerieReturnNewGeocodedInstanceWithManyValuesWi ->geocode($this->values) ->setCache($this->getMockCacheReturns($this->getMockGeocodedReturns($this->data))) ->serie(); - $this->assertCount(count($this->providers) * count($this->values), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -562,13 +487,11 @@ public function testBatchGeocodeInSerieReturnNewGeocodedInstanceWithManyValuesWi $this->assertEquals(2.3072664, $providerResult->getLongitude()); } } - public function testBatchGeocodeInParallelReturnNewGeocodedInstanceWithManyValues() { $geocoder = $this->getMockGeocoderThrowException($this->providers); $batch = new TestableBatch($geocoder); $resultComputedInParallel = $batch->geocode($this->values)->parallel(); - $this->assertCount(count($this->providers) * count($this->values), $resultComputedInParallel); foreach ($resultComputedInParallel as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -579,7 +502,6 @@ public function testBatchGeocodeInParallelReturnNewGeocodedInstanceWithManyValue $this->assertContains($providerResult->getQuery(), $this->values); } } - public function testBatchGeocodeInParallelReturnNewGeocodedInstanceWithManyValuesWithCache() { $geocoder = $this->getMockGeocoderThrowException($this->providers); @@ -588,7 +510,6 @@ public function testBatchGeocodeInParallelReturnNewGeocodedInstanceWithManyValue ->geocode($this->values) ->setCache($this->getMockCacheReturns($this->getMockGeocodedReturns($this->data))) ->parallel(); - $this->assertCount(count($this->providers) * count($this->values), $resultComputedInParallel); foreach ($resultComputedInParallel as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -597,13 +518,11 @@ public function testBatchGeocodeInParallelReturnNewGeocodedInstanceWithManyValue $this->assertEquals(2.3072664, $providerResult->getLongitude()); } } - public function testBatchReverseGeocodingInSerieReturnNewGeocodedInstance() { $geocoder = $this->getMockGeocoderThrowException($this->providers); $batch = new TestableBatch($geocoder); $resultComputedInSerie = $batch->reverse($this->getMockCoordinateReturns(array(1, 2)))->serie(); - $this->assertCount(count($this->providers), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -614,7 +533,6 @@ public function testBatchReverseGeocodingInSerieReturnNewGeocodedInstance() $this->assertEquals($providerResult->getQuery(), '1, 2'); } } - public function testBatchReverseGeocodingInSerieReturnNewGeocodedInstanceWithCache() { $geocoder = $this->getMockGeocoderThrowException($this->providers); @@ -623,7 +541,6 @@ public function testBatchReverseGeocodingInSerieReturnNewGeocodedInstanceWithCac ->reverse($this->getMockCoordinateReturns(array(1, 2))) ->setCache($this->getMockCacheReturns($this->getMockGeocodedReturns($this->data))) ->serie(); - $this->assertCount(count($this->providers), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -632,13 +549,11 @@ public function testBatchReverseGeocodingInSerieReturnNewGeocodedInstanceWithCac $this->assertEquals(2.3072664, $providerResult->getLongitude()); } } - public function testBatchReverseGeocodingInParallelReturnNewGeocodedInstance() { $geocoder = $this->getMockGeocoderThrowException($this->providers); $batch = new TestableBatch($geocoder); $resultComputedInParallel = $batch->reverse($this->getMockCoordinateReturns(array(1, 2)))->parallel(); - $this->assertCount(count($this->providers), $resultComputedInParallel); foreach ($resultComputedInParallel as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -649,7 +564,6 @@ public function testBatchReverseGeocodingInParallelReturnNewGeocodedInstance() $this->assertEquals($providerResult->getQuery(), '1, 2'); } } - public function testBatchReverseGeocodingInParallelReturnNewGeocodedInstanceWithCache() { $geocoder = $this->getMockGeocoderThrowException($this->providers); @@ -658,7 +572,6 @@ public function testBatchReverseGeocodingInParallelReturnNewGeocodedInstanceWith ->reverse($this->getMockCoordinateReturns(array(1, 2))) ->setCache($this->getMockCacheReturns($this->getMockGeocodedReturns($this->data))) ->parallel(); - $this->assertCount(count($this->providers), $resultComputedInParallel); foreach ($resultComputedInParallel as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -667,13 +580,11 @@ public function testBatchReverseGeocodingInParallelReturnNewGeocodedInstanceWith $this->assertEquals(2.3072664, $providerResult->getLongitude()); } } - public function testBatchReverseGeocodingInSerieReturnNewGeocodedInstanceWithManyCoordinates() { $geocoder = $this->getMockGeocoderThrowException($this->providers); $batch = new TestableBatch($geocoder); $resultComputedInSerie = $batch->reverse($this->coordinates)->serie(); - $this->assertCount(count($this->providers) * count($this->coordinates), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -684,7 +595,6 @@ public function testBatchReverseGeocodingInSerieReturnNewGeocodedInstanceWithMan $this->assertEquals($providerResult->getQuery(), '1, 2'); } } - public function testBatchReverseGeocodingInSerieReturnNewGeocodedInstanceWithManyCoordinatesWithCache() { $geocoder = $this->getMockGeocoderThrowException($this->providers); @@ -693,7 +603,6 @@ public function testBatchReverseGeocodingInSerieReturnNewGeocodedInstanceWithMan ->reverse($this->coordinates) ->setCache($this->getMockCacheReturns($this->getMockGeocodedReturns($this->data))) ->serie(); - $this->assertCount(count($this->providers) * count($this->coordinates), $resultComputedInSerie); foreach ($resultComputedInSerie as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -702,13 +611,11 @@ public function testBatchReverseGeocodingInSerieReturnNewGeocodedInstanceWithMan $this->assertEquals(2.3072664, $providerResult->getLongitude()); } } - public function testBatchReverseGeocodingInParallelReturnNewGeocodedInstanceWithManyCoordinates() { $geocoder = $this->getMockGeocoderThrowException($this->providers); $batch = new TestableBatch($geocoder); $resultComputedInParallel = $batch->reverse($this->coordinates)->parallel(); - $this->assertCount(count($this->providers) * count($this->coordinates), $resultComputedInParallel); foreach ($resultComputedInParallel as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -719,7 +626,6 @@ public function testBatchReverseGeocodingInParallelReturnNewGeocodedInstanceWith $this->assertEquals($providerResult->getQuery(), '1, 2'); } } - public function testBatchReverseGeocodingInParallelReturnNewGeocodedInstanceWithManyCoordinatesWithCache() { $geocoder = $this->getMockGeocoderThrowException($this->providers); @@ -728,7 +634,6 @@ public function testBatchReverseGeocodingInParallelReturnNewGeocodedInstanceWith ->reverse($this->coordinates) ->setCache($this->getMockCacheReturns($this->getMockGeocodedReturns($this->data))) ->parallel(); - $this->assertCount(count($this->providers) * count($this->coordinates), $resultComputedInParallel); foreach ($resultComputedInParallel as $providerResult) { $this->assertTrue(is_object($providerResult)); @@ -737,7 +642,6 @@ public function testBatchReverseGeocodingInParallelReturnNewGeocodedInstanceWith $this->assertEquals(2.3072664, $providerResult->getLongitude()); } } - public function testSeriesShouldThrowException() { $this->expectException(\RuntimeException::class); @@ -755,13 +659,11 @@ function () { }, ))->geocode('foo')->serie(); } - public function testParallelShouldThrowException() { $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('booooooooooo!'); $called = 0; - $batch = new TestableBatch($this->geocoder); $batch->setTasks(array( function () { @@ -779,41 +681,32 @@ function () use (&$called) { return \React\Promise\resolve('bar'); }, ))->geocode('foo')->parallel(); - $this->assertSame(2, $called); } - public function testIsCachedShouldReturnBatchGeocoded() { $batch = new TestableBatch($this->geocoder); $cached = $batch->setCache($this->getMockCacheReturns($this->getStubBatchGeocoded()))->isCached('foo', 'bar'); - $this->assertTrue(is_object($cached)); $this->assertInstanceOf('League\Geotools\Batch\BatchGeocoded', $cached); } - public function testIsCachedShouldReturnFalse() { $batch = new TestableBatch($this->geocoder); $cached = $batch->isCached('foo', 'bar'); - $this->assertFalse($cached); } - public function testCacheShouldReturnBatchGeocoded() { $batch = new TestableBatch($this->geocoder); $caching = $batch->setCache($this->getMockCacheReturns('foo'))->cache($this->getStubBatchGeocoded()); - $this->assertTrue(is_object($caching)); $this->assertInstanceOf('League\Geotools\Batch\BatchGeocoded', $caching); } - public function testSetCacheShouldReturnBatchInterface() { $batch = new TestableBatch($this->geocoder); $batchWithCache = $batch->setCache(new ArrayCachePool()); - $this->assertTrue(is_object($batchWithCache)); $this->assertInstanceOf('League\Geotools\Batch\Batch', $batchWithCache); $this->assertInstanceOf('League\Geotools\Batch\BatchInterface', $batchWithCache); @@ -822,55 +715,72 @@ public function testSetCacheShouldReturnBatchInterface() $this->assertInstanceOf(CacheItemPoolInterface::class, $batchWithCache->getCache()); } } - class TestableBatch extends Batch { public function getGeocoder() { return $this->geocoder; } - public function getTasks() { return $this->tasks; } - public function setTasks(array $tasks) { $this->tasks = $tasks; - return $this; } - public function getCache() { return $this->cache; } } - +class ArrayCacheItem implements \Psr\Cache\CacheItemInterface +{ + private bool $hit = false; + private mixed $value = null; + public function __construct(private string $key) {} + public function getKey(): string { return $this->key; } + public function get(): mixed { return $this->value; } + public function isHit(): bool { return $this->hit; } + public function set(mixed $value): static { $this->value = $value; $this->hit = true; return $this; } + public function expiresAt(?\DateTimeInterface $expiration): static { return $this; } + public function expiresAfter(\DateInterval|int|null $time): static { return $this; } +} +class ArrayCachePool implements \Psr\Cache\CacheItemPoolInterface +{ + private array $items = []; + private array $deferred = []; + public function getItem(string $key): \Psr\Cache\CacheItemInterface + { + return $this->items[$key] ?? new ArrayCacheItem($key); + } + public function getItems(array $keys = []): iterable { return array_map(fn($k) => $this->getItem($k), $keys); } + public function hasItem(string $key): bool { return isset($this->items[$key]); } + public function clear(): bool { $this->items = []; return true; } + public function deleteItem(string $key): bool { unset($this->items[$key]); return true; } + public function deleteItems(array $keys): bool { foreach ($keys as $k) { $this->deleteItem($k); } return true; } + public function save(\Psr\Cache\CacheItemInterface $item): bool { $this->items[$item->getKey()] = $item; return true; } + public function saveDeferred(\Psr\Cache\CacheItemInterface $item): bool { $this->deferred[$item->getKey()] = $item; return true; } + public function commit(): bool { foreach ($this->deferred as $item) { $this->save($item); } $this->deferred = []; return true; } +} class MockProvider extends AbstractProvider implements ProviderInterface { protected $name; - public function __construct($name) { $this->name = $name; } - public function getName():string { return $this->name; } - public function geocodeQuery(GeocodeQuery $query): Collection { return new Collection([]); } - public function reverseQuery(ReverseQuery $query): Collection { return new Collection([]); } - - } diff --git a/tests/BoundingBox/BoundingBoxTest.php b/tests/BoundingBox/BoundingBoxTest.php index bcaf86b8..baf62f5f 100644 --- a/tests/BoundingBox/BoundingBoxTest.php +++ b/tests/BoundingBox/BoundingBoxTest.php @@ -11,12 +11,14 @@ namespace League\Geotools\Tests\BoundingBox; +use League\Geotools\Polygon\Polygon; +use PHPUnit\Framework\Attributes\DataProvider; use League\Geotools\BoundingBox\BoundingBox; use League\Geotools\Coordinate\Coordinate; use League\Geotools\Coordinate\Ellipsoid; use League\Geotools\Exception\InvalidArgumentException; -use League\Geotools\Polygon\Polygon; - +use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; +use PHPUnit\Framework\Attributes\Test; /** * @author Gabriel Bull */ @@ -26,13 +28,11 @@ class BoundingBoxTest extends \League\Geotools\Tests\TestCase * @var Polygon */ protected $polygon; - protected function setup(): void { $this->polygon = new Polygon; } - - public function polygonAndExpectedNorthWestAndSouthEastCoordinates() + public static function polygonAndExpectedNorthWestAndSouthEastCoordinates() { return array( array( @@ -49,46 +49,27 @@ public function polygonAndExpectedNorthWestAndSouthEastCoordinates() ), ); } - - /** - * @doesNotPerformAssertions - */ + #[DoesNotPerformAssertions] public function testConstructWithPolygon() { new BoundingBox(new Polygon); } - - - /** - * @doesNotPerformAssertions - */ + #[DoesNotPerformAssertions] public function testConstructWithCoordinate() { new BoundingBox(new Coordinate(array(0, 0))); } - - /** - * @doesNotPerformAssertions - */ + #[DoesNotPerformAssertions] public function testConstructWithNull() { new BoundingBox; } - public function testConstructWithInvalidArgument() { $this->expectException(\InvalidArgumentException::class); new BoundingBox('string'); } - - /** - * @dataProvider polygonAndExpectedNorthWestAndSouthEastCoordinates - * @param array $polygonCoordinates - * @param string $north - * @param string $east - * @param string $south - * @param string $west - */ + #[DataProvider('polygonAndExpectedNorthWestAndSouthEastCoordinates')] public function testPolygonBoundingBox($polygonCoordinates, $north, $east, $south, $west) { foreach ($polygonCoordinates as $coordinate) { @@ -96,7 +77,6 @@ public function testPolygonBoundingBox($polygonCoordinates, $north, $east, $sout $this->getMockCoordinateReturns($coordinate, Ellipsoid::createFromName(Ellipsoid::WGS84)) ); } - $this->assertEquals( $north, $this->polygon->getBoundingBox()->getNorth() @@ -114,39 +94,28 @@ public function testPolygonBoundingBox($polygonCoordinates, $north, $east, $sout $this->polygon->getBoundingBox()->getWest() ); } - - /** - * @test - */ + #[Test] public function itShouldThrowAnExceptionWhenEllipsoidsDontMatch() { $bb = new BoundingBox( new Polygon([new Coordinate([-1, -2], Ellipsoid::createFromName(Ellipsoid::AUSTRALIAN_NATIONAL))]) ); $polygon = new Polygon([new Coordinate([-1, -2], Ellipsoid::createFromName(Ellipsoid::WGS84))]); - $this->expectException('\InvalidArgumentException'); $bb->setPolygon($polygon); } - - /** - * @test - */ + #[Test] public function itShouldReturnThePolygonRectangleOfTheBoundingBox() { $bb = new BoundingBox(); - $this->assertNull($bb->getAsPolygon()); - $bb = new BoundingBox( new Polygon([ new Coordinate([-1, -2], Ellipsoid::createFromName(Ellipsoid::WGS84)), new Coordinate([1, 2], Ellipsoid::createFromName(Ellipsoid::WGS84)) ]) ); - $polygon = $bb->getAsPolygon(); - $expected = new Polygon([ new Coordinate([1, -2], Ellipsoid::createFromName(Ellipsoid::WGS84)), new Coordinate([1, 2], Ellipsoid::createFromName(Ellipsoid::WGS84)), @@ -154,13 +123,9 @@ public function itShouldReturnThePolygonRectangleOfTheBoundingBox() new Coordinate([-1, -2], Ellipsoid::createFromName(Ellipsoid::WGS84)), new Coordinate([1, -2], Ellipsoid::createFromName(Ellipsoid::WGS84)) ]); - $this->assertEquals($expected, $polygon); } - - /** - * @test - */ + #[Test] public function itShouldMergeBoundingBoxes() { $bb = new BoundingBox( @@ -169,16 +134,13 @@ public function itShouldMergeBoundingBoxes() new Coordinate([1, 2], Ellipsoid::createFromName(Ellipsoid::WGS84)) ]) ); - $bb2 = new BoundingBox( new Polygon([ new Coordinate([0, -3], Ellipsoid::createFromName(Ellipsoid::WGS84)), new Coordinate([2, 0], Ellipsoid::createFromName(Ellipsoid::WGS84)) ]) ); - $merged = $bb->merge($bb2); - $this->assertEquals(2, $merged->getNorth()); $this->assertEquals(-1, $merged->getSouth()); $this->assertEquals(2, $merged->getEast()); diff --git a/tests/CLI/Command/Convert/DMSTest.php b/tests/CLI/Command/Convert/DMSTest.php index 5bc55326..633732bc 100644 --- a/tests/CLI/Command/Convert/DMSTest.php +++ b/tests/CLI/Command/Convert/DMSTest.php @@ -27,7 +27,7 @@ class DMSTest extends \League\Geotools\Tests\TestCase protected function setup(): void { $this->application = new GeotoolsApplication(); - $this->application->add(new DMS); + $this->application->addCommand(new DMS); $this->command = $this->application->find('convert:dms'); diff --git a/tests/CLI/Command/Convert/DMTest.php b/tests/CLI/Command/Convert/DMTest.php index 6a838b98..b8e37fce 100644 --- a/tests/CLI/Command/Convert/DMTest.php +++ b/tests/CLI/Command/Convert/DMTest.php @@ -27,7 +27,7 @@ class DMTest extends \League\Geotools\Tests\TestCase protected function setup(): void { $this->application = new GeotoolsApplication(); - $this->application->add(new DM); + $this->application->addCommand(new DM); $this->command = $this->application->find('convert:dm'); diff --git a/tests/CLI/Command/Convert/UTMTest.php b/tests/CLI/Command/Convert/UTMTest.php index 022a1d52..5fe9c4fd 100644 --- a/tests/CLI/Command/Convert/UTMTest.php +++ b/tests/CLI/Command/Convert/UTMTest.php @@ -27,7 +27,7 @@ class UTMTest extends \League\Geotools\Tests\TestCase protected function setup(): void { $this->application = new GeotoolsApplication(); - $this->application->add(new UTM); + $this->application->addCommand(new UTM); $this->command = $this->application->find('convert:utm'); diff --git a/tests/CLI/Command/Distance/AllTest.php b/tests/CLI/Command/Distance/AllTest.php index 33aa7b40..d9a100bc 100644 --- a/tests/CLI/Command/Distance/AllTest.php +++ b/tests/CLI/Command/Distance/AllTest.php @@ -27,7 +27,7 @@ class AllTest extends \League\Geotools\Tests\TestCase protected function setup(): void { $this->application = new GeotoolsApplication(); - $this->application->add(new All); + $this->application->addCommand(new All); $this->command = $this->application->find('distance:all'); diff --git a/tests/CLI/Command/Distance/FlatTest.php b/tests/CLI/Command/Distance/FlatTest.php index 6f440d2a..513fcec1 100644 --- a/tests/CLI/Command/Distance/FlatTest.php +++ b/tests/CLI/Command/Distance/FlatTest.php @@ -27,7 +27,7 @@ class FlatTest extends \League\Geotools\Tests\TestCase protected function setup(): void { $this->application = new GeotoolsApplication(); - $this->application->add(new Flat); + $this->application->addCommand(new Flat); $this->command = $this->application->find('distance:flat'); diff --git a/tests/CLI/Command/Distance/GreatCircleTest.php b/tests/CLI/Command/Distance/GreatCircleTest.php index 7a1ff361..44dfad25 100644 --- a/tests/CLI/Command/Distance/GreatCircleTest.php +++ b/tests/CLI/Command/Distance/GreatCircleTest.php @@ -27,7 +27,7 @@ class GreatCircleTest extends \League\Geotools\Tests\TestCase protected function setup(): void { $this->application = new GeotoolsApplication(); - $this->application->add(new GreatCircle); + $this->application->addCommand(new GreatCircle); $this->command = $this->application->find('distance:great-circle'); diff --git a/tests/CLI/Command/Distance/HaversineTest.php b/tests/CLI/Command/Distance/HaversineTest.php index cae84575..27f584a2 100644 --- a/tests/CLI/Command/Distance/HaversineTest.php +++ b/tests/CLI/Command/Distance/HaversineTest.php @@ -27,7 +27,7 @@ class HaversineTest extends \League\Geotools\Tests\TestCase protected function setup(): void { $this->application = new GeotoolsApplication(); - $this->application->add(new Haversine); + $this->application->addCommand(new Haversine); $this->command = $this->application->find('distance:haversine'); diff --git a/tests/CLI/Command/Distance/VincentyTest.php b/tests/CLI/Command/Distance/VincentyTest.php index 55991440..7fa11af0 100644 --- a/tests/CLI/Command/Distance/VincentyTest.php +++ b/tests/CLI/Command/Distance/VincentyTest.php @@ -27,7 +27,7 @@ class VincentyTest extends \League\Geotools\Tests\TestCase protected function setup(): void { $this->application = new GeotoolsApplication(); - $this->application->add(new Vincenty); + $this->application->addCommand(new Vincenty); $this->command = $this->application->find('distance:vincenty'); diff --git a/tests/CLI/Command/Geocoder/GeocodeTest.php b/tests/CLI/Command/Geocoder/GeocodeTest.php index be4489a2..d0e9e321 100644 --- a/tests/CLI/Command/Geocoder/GeocodeTest.php +++ b/tests/CLI/Command/Geocoder/GeocodeTest.php @@ -27,7 +27,7 @@ class GeocodeTest extends \League\Geotools\Tests\TestCase protected function setup(): void { $this->application = new GeotoolsApplication(); - $this->application->add(new Geocode); + $this->application->addCommand(new Geocode); $this->command = $this->application->find('geocoder:geocode'); diff --git a/tests/CLI/Command/Geocoder/ReverseTest.php b/tests/CLI/Command/Geocoder/ReverseTest.php index b74c77f1..6feae405 100644 --- a/tests/CLI/Command/Geocoder/ReverseTest.php +++ b/tests/CLI/Command/Geocoder/ReverseTest.php @@ -27,7 +27,7 @@ class ReverseTest extends \League\Geotools\Tests\TestCase protected function setup(): void { $this->application = new GeotoolsApplication(); - $this->application->add(new Reverse); + $this->application->addCommand(new Reverse); $this->command = $this->application->find('geocoder:reverse'); diff --git a/tests/CLI/Command/Geohash/DecodeTest.php b/tests/CLI/Command/Geohash/DecodeTest.php index 3bca1265..83f11abd 100644 --- a/tests/CLI/Command/Geohash/DecodeTest.php +++ b/tests/CLI/Command/Geohash/DecodeTest.php @@ -27,7 +27,7 @@ class DecodeTest extends \League\Geotools\Tests\TestCase protected function setup(): void { $this->application = new GeotoolsApplication(); - $this->application->add(new Decode); + $this->application->addCommand(new Decode); $this->command = $this->application->find('geohash:decode'); diff --git a/tests/CLI/Command/Geohash/EncodeTest.php b/tests/CLI/Command/Geohash/EncodeTest.php index 782833a0..7c75fd80 100644 --- a/tests/CLI/Command/Geohash/EncodeTest.php +++ b/tests/CLI/Command/Geohash/EncodeTest.php @@ -28,7 +28,7 @@ class EncodeTest extends \League\Geotools\Tests\TestCase protected function setup(): void { $this->application = new GeotoolsApplication(); - $this->application->add(new Encode); + $this->application->addCommand(new Encode); $this->command = $this->application->find('geohash:encode'); diff --git a/tests/CLI/Command/Vertex/DestinationTest.php b/tests/CLI/Command/Vertex/DestinationTest.php index 4757ed01..1def0fb3 100644 --- a/tests/CLI/Command/Vertex/DestinationTest.php +++ b/tests/CLI/Command/Vertex/DestinationTest.php @@ -27,7 +27,7 @@ class DestinationTest extends \League\Geotools\Tests\TestCase protected function setup(): void { $this->application = new GeotoolsApplication(); - $this->application->add(new Destination); + $this->application->addCommand(new Destination); $this->command = $this->application->find('vertex:destination'); diff --git a/tests/CLI/Command/Vertex/FinalBearingTest.php b/tests/CLI/Command/Vertex/FinalBearingTest.php index 98a5f32c..6fe7ef29 100644 --- a/tests/CLI/Command/Vertex/FinalBearingTest.php +++ b/tests/CLI/Command/Vertex/FinalBearingTest.php @@ -27,7 +27,7 @@ class FinalBearingTest extends \League\Geotools\Tests\TestCase protected function setup(): void { $this->application = new GeotoolsApplication(); - $this->application->add(new FinalBearing); + $this->application->addCommand(new FinalBearing); $this->command = $this->application->find('vertex:final-bearing'); diff --git a/tests/CLI/Command/Vertex/FinalCardinalTest.php b/tests/CLI/Command/Vertex/FinalCardinalTest.php index 9dfa1b22..e773b5e0 100644 --- a/tests/CLI/Command/Vertex/FinalCardinalTest.php +++ b/tests/CLI/Command/Vertex/FinalCardinalTest.php @@ -27,7 +27,7 @@ class FinalCardinalTest extends \League\Geotools\Tests\TestCase protected function setup(): void { $this->application = new GeotoolsApplication(); - $this->application->add(new FinalCardinal); + $this->application->addCommand(new FinalCardinal); $this->command = $this->application->find('vertex:final-cardinal'); diff --git a/tests/CLI/Command/Vertex/InitialBearingTest.php b/tests/CLI/Command/Vertex/InitialBearingTest.php index 5d89eb8e..ce03346e 100644 --- a/tests/CLI/Command/Vertex/InitialBearingTest.php +++ b/tests/CLI/Command/Vertex/InitialBearingTest.php @@ -27,7 +27,7 @@ class InitialBearingTest extends \League\Geotools\Tests\TestCase protected function setup(): void { $this->application = new GeotoolsApplication(); - $this->application->add(new InitialBearing); + $this->application->addCommand(new InitialBearing); $this->command = $this->application->find('vertex:initial-bearing'); diff --git a/tests/CLI/Command/Vertex/InitialCardinalTest.php b/tests/CLI/Command/Vertex/InitialCardinalTest.php index 71e25eeb..bee2cfce 100644 --- a/tests/CLI/Command/Vertex/InitialCardinalTest.php +++ b/tests/CLI/Command/Vertex/InitialCardinalTest.php @@ -27,7 +27,7 @@ class InitialCardinalTest extends \League\Geotools\Tests\TestCase protected function setup(): void { $this->application = new GeotoolsApplication(); - $this->application->add(new InitialCardinal); + $this->application->addCommand(new InitialCardinal); $this->command = $this->application->find('vertex:initial-cardinal'); diff --git a/tests/CLI/Command/Vertex/MiddleTest.php b/tests/CLI/Command/Vertex/MiddleTest.php index 980ca23d..deaac4cf 100644 --- a/tests/CLI/Command/Vertex/MiddleTest.php +++ b/tests/CLI/Command/Vertex/MiddleTest.php @@ -27,7 +27,7 @@ class MiddleTest extends \League\Geotools\Tests\TestCase protected function setup(): void { $this->application = new GeotoolsApplication(); - $this->application->add(new Middle); + $this->application->addCommand(new Middle); $this->command = $this->application->find('vertex:middle'); diff --git a/tests/Convert/ConvertTest.php b/tests/Convert/ConvertTest.php index a2e8e679..55b6835b 100644 --- a/tests/Convert/ConvertTest.php +++ b/tests/Convert/ConvertTest.php @@ -11,44 +11,36 @@ namespace League\Geotools\Tests\Convert; -use League\Geotools\Convert\Convert; use League\Geotools\Coordinate\Coordinate; - +use PHPUnit\Framework\Attributes\DataProvider; +use League\Geotools\Convert\Convert; +use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; /** * @author Antoine Corcy */ class ConvertTest extends \League\Geotools\Tests\TestCase { - /** - * @doesNotPerformAssertions - */ + #[DoesNotPerformAssertions] public function testConstructorShouldAcceptCoordinateInterface() { new TestableConvert($this->getStubCoordinate()); } - public function testConstructorShouldSetCoordinateInterface() { $convert = new TestableConvert($this->getStubCoordinate()); $coordinates = $convert->getCoordinates(); - $this->assertTrue(is_object($coordinates)); $this->assertInstanceOf('League\Geotools\Coordinate\CoordinateInterface', $coordinates); } - - /** - * @dataProvider coordinatesToDMSProvider - */ + #[DataProvider('coordinatesToDMSProvider')] public function testToDegreesMinutesSeconds($coordinates, $format, $expectedResult) { $convert = new TestableConvert(new Coordinate($coordinates)); $converted = $convert->toDegreesMinutesSeconds($format); - $this->assertTrue(is_string($converted)); $this->assertSame($expectedResult, $converted); } - - public function coordinatesToDMSProvider() + public static function coordinatesToDMSProvider() { return array( array( @@ -83,20 +75,15 @@ public function coordinatesToDMSProvider() ), ); } - - /** - * @dataProvider coordinatesToDMProvider - */ + #[DataProvider('coordinatesToDMProvider')] public function testToDecimalMinutes($coordinates, $format, $expectedResult) { $convert = new TestableConvert(new Coordinate($coordinates)); $converted = $convert->toDecimalMinutes($format); - $this->assertTrue(is_string($converted)); $this->assertSame($expectedResult, $converted); } - - public function coordinatesToDMProvider() + public static function coordinatesToDMProvider() { return array( array( @@ -136,21 +123,15 @@ public function coordinatesToDMProvider() ), ); } - - - /** - * @dataProvider coordinatesToDegreeDecimalMinutesProvider - */ + #[DataProvider('coordinatesToDegreeDecimalMinutesProvider')] public function testToDegreeDecimalMinutes($coordinates, $expectedResult) { $convert = new TestableConvert(new Coordinate($coordinates)); $converted = $convert->toDegreeDecimalMinutes(); - $this->assertTrue(is_string($converted)); $this->assertSame($expectedResult, $converted); } - - public function coordinatesToDegreeDecimalMinutesProvider() + public static function coordinatesToDegreeDecimalMinutesProvider() { return array( array( @@ -171,20 +152,15 @@ public function coordinatesToDegreeDecimalMinutesProvider() ), ); } - - /** - * @dataProvider coordinatesToUTMProvider - */ + #[DataProvider('coordinatesToUTMProvider')] public function testToUniversalTransverseMercator($coordinates, $expectedResult) { $convert = new TestableConvert(new Coordinate($coordinates)); $converted = $convert->toUniversalTransverseMercator(); - $this->assertTrue(is_string($converted)); $this->assertSame($expectedResult, $converted); } - - public function coordinatesToUTMProvider() + public static function coordinatesToUTMProvider() { return array( array( @@ -248,7 +224,6 @@ public function coordinatesToUTMProvider() ); } } - class TestableConvert extends Convert { public function getCoordinates() diff --git a/tests/Coordinate/CoordinateTest.php b/tests/Coordinate/CoordinateTest.php index 5868d005..5cab4fc9 100644 --- a/tests/Coordinate/CoordinateTest.php +++ b/tests/Coordinate/CoordinateTest.php @@ -11,25 +11,23 @@ namespace League\Geotools\Tests\Coordinate; -use League\Geotools\Coordinate\Coordinate; use League\Geotools\Coordinate\Ellipsoid; - +use PHPUnit\Framework\Attributes\DataProvider; +use League\Geotools\Coordinate\Coordinate; +use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; /** * @author Antoine Corcy */ class CoordinateTest extends \League\Geotools\Tests\TestCase { - /** - * @dataProvider invalidCoordinatesProvider - */ + #[DataProvider('invalidCoordinatesProvider')] public function testConstructorWithInvalidCoordinatesShouldThrowAnException($coordinates) { $this->expectException(\League\Geotools\Exception\InvalidArgumentException::class); $this->expectExceptionMessage('It should be a string, an array or a class which implements Geocoder\Model\Address !'); new Coordinate($coordinates); } - - public function invalidCoordinatesProvider() + public static function invalidCoordinatesProvider() { return array( array(null), @@ -43,18 +41,14 @@ public function invalidCoordinatesProvider() ), ); } - - /** - * @dataProvider invalidStringCoordinatesProvider - */ + #[DataProvider('invalidStringCoordinatesProvider')] public function testConstructorWithInvalidStringCoordinatesShouldThrowAnException($coordinates) { $this->expectException(\League\Geotools\Exception\InvalidArgumentException::class); $this->expectExceptionMessage('It should be a valid and acceptable ways to write geographic coordinates !'); new Coordinate($coordinates); } - - public function invalidStringCoordinatesProvider() + public static function invalidStringCoordinatesProvider() { return array( array(''), @@ -69,19 +63,14 @@ public function invalidStringCoordinatesProvider() array('47°01\'36.3888\", 002°18\'26.1590\"'), ); } - - /** - * @dataProvider validCoordinatesAndExpectedCoordinatesProvider - */ + #[DataProvider('validCoordinatesAndExpectedCoordinatesProvider')] public function testConstructorWithValidCoordinatesShouldBeValid($coordinates, $expectedCoordinates) { $coordinate = new Coordinate($coordinates); - $this->assertSame($expectedCoordinates[0], $coordinate->getLatitude()); $this->assertSame($expectedCoordinates[1], $coordinate->getLongitude()); } - - public function validCoordinatesAndExpectedCoordinatesProvider() + public static function validCoordinatesAndExpectedCoordinatesProvider() { return array( array( @@ -174,28 +163,20 @@ public function validCoordinatesAndExpectedCoordinatesProvider() ), ); } - - /** - * @doesNotPerformAssertions - */ + #[DoesNotPerformAssertions] public function testConstructorWithAddressArgumentShouldBeValid() { new Coordinate($this->createEmptyAddress()); } - - /** - * @dataProvider resultsProvider - */ + #[DataProvider('resultsProvider')] public function testConstructorShouldReturnsLatitudeAndLongitude($result) { $geocoded = $this->createAddress($result); $coordinate = new Coordinate($geocoded); - $this->assertSame((string) $result['latitude'], $coordinate->getLatitude()); $this->assertSame((string) $result['longitude'], $coordinate->getLongitude()); } - - public function resultsProvider() + public static function resultsProvider() { return array( array( @@ -224,18 +205,13 @@ public function resultsProvider() ), ); } - - /** - * @dataProvider latitudesWithExpectedLatitudesProvider - */ + #[DataProvider('latitudesWithExpectedLatitudesProvider')] public function testNormalizeLatitude($latitude, $expectedLatitude) { $coordinate = new Coordinate($this->createEmptyAddress()); - $this->assertSame($expectedLatitude, $coordinate->normalizeLatitude($latitude)); } - - public function latitudesWithExpectedLatitudesProvider() + public static function latitudesWithExpectedLatitudesProvider() { return array( array('-180', '-90.0'), @@ -243,18 +219,13 @@ public function latitudesWithExpectedLatitudesProvider() array('180', '90.0'), ); } - - /** - * @dataProvider longitudesWithExpectedLatitudesProvider - */ + #[DataProvider('longitudesWithExpectedLatitudesProvider')] public function testNormalizeLongitude($longitude, $expectedLongitude) { $coordinate = new Coordinate($this->createEmptyAddress()); - $this->assertSame($expectedLongitude, $coordinate->normalizeLongitude($longitude)); } - - public function longitudesWithExpectedLatitudesProvider() + public static function longitudesWithExpectedLatitudesProvider() { return array( array(-500, '-140.0'), @@ -268,10 +239,7 @@ public function longitudesWithExpectedLatitudesProvider() array(500, '140.0'), ); } - - /** - * @dataProvider latitudesProvider - */ + #[DataProvider('latitudesProvider')] public function testSetLatitude($latitude) { $coordinate = new Coordinate($this->createEmptyAddress()); @@ -280,11 +248,9 @@ public function testSetLatitude($latitude) if (false === strpos($expected, '.')){ $expected .= '.0'; } - $this->assertSame($expected, $coordinate->getLatitude()); } - - public function latitudesProvider() + public static function latitudesProvider() { return array( array(1), @@ -297,10 +263,7 @@ public function latitudesProvider() array('-0.0001'), ); } - - /** - * @dataProvider longitudesProvider - */ + #[DataProvider('longitudesProvider')] public function testSetLongitude($longitude) { $coordinate = new Coordinate($this->createEmptyAddress()); @@ -309,11 +272,9 @@ public function testSetLongitude($longitude) if (false === strpos($expected, '.')){ $expected .= '.0'; } - $this->assertSame($expected, $coordinate->getLongitude()); } - - public function longitudesProvider() + public static function longitudesProvider() { return array( array(1), @@ -326,17 +287,14 @@ public function longitudesProvider() array('-0.0001'), ); } - public function testGetEllipsoid() { $WGS84 = Ellipsoid::createFromName(Ellipsoid::WGS84); $coordinate = new Coordinate($this->createEmptyAddress(), $WGS84); $ellipsoid = $coordinate->getEllipsoid(); - $this->assertTrue(is_object($ellipsoid)); $this->assertInstanceOf('League\Geotools\Coordinate\Ellipsoid', $ellipsoid); } - public function testCreateFromStringWithoutAString() { $this->expectException(\League\Geotools\Exception\InvalidArgumentException::class); @@ -344,7 +302,6 @@ public function testCreateFromStringWithoutAString() $coordinate = new Coordinate($this->createEmptyAddress()); $coordinate->setFromString(123); } - public function testCreateFromStringWithInvalidCoordinateString() { $this->expectException(\League\Geotools\Exception\InvalidArgumentException::class); @@ -352,42 +309,38 @@ public function testCreateFromStringWithInvalidCoordinateString() $coordinate = new Coordinate($this->createEmptyAddress()); $coordinate->setFromString('foo'); } - public function testCreateFromStringWithValidCoordinatesShouldBeValid() { $coordinate = new Coordinate($this->createEmptyAddress()); $coordinate->setFromString('40°26′47″N 079°58′36″W'); - $this->assertSame('40.4463888888889', $coordinate->getLatitude()); $this->assertSame('-79.9766666666667', $coordinate->getLongitude()); } - public function testSetCoordinateWithLocaleThatUsesDecimalPointAsDecimalSeparator() { + if (setlocale(LC_NUMERIC, 'en_US') === false) { + $this->markTestSkipped('Locale en_US is not available on this system.'); + } $this->setLocale(LC_NUMERIC, 'en_US'); - $latitude = "59.3293235"; $longitude = "18.0685808"; - $coordinate = new Coordinate($this->createEmptyAddress()); $coordinate->setLatitude($latitude); $coordinate->setLongitude($longitude); - $this->assertSame($latitude, $coordinate->getLatitude()); $this->assertSame($longitude, $coordinate->getLongitude()); } - public function testSetCoordinateWithLocaleThatUsesDecimalCommaAsDecimalSeparator() { + if (setlocale(LC_NUMERIC, 'sv_SE') === false) { + $this->markTestSkipped('Locale sv_SE is not available on this system.'); + } $this->setLocale(LC_NUMERIC, 'sv_SE'); - $latitude = "59.3293235"; $longitude = "18.0685808"; - $coordinate = new Coordinate($this->createEmptyAddress()); $coordinate->setLatitude($latitude); $coordinate->setLongitude($longitude); - $this->assertSame($latitude, $coordinate->getLatitude()); $this->assertSame($longitude, $coordinate->getLongitude()); } diff --git a/tests/Coordinate/EllipsoidTest.php b/tests/Coordinate/EllipsoidTest.php index d14c1936..1b19b452 100644 --- a/tests/Coordinate/EllipsoidTest.php +++ b/tests/Coordinate/EllipsoidTest.php @@ -12,23 +12,20 @@ namespace League\Geotools\Tests\Coordinate; use League\Geotools\Coordinate\Ellipsoid; - +use PHPUnit\Framework\Attributes\DataProvider; /** * @author Antoine Corcy */ class EllipsoidTest extends \League\Geotools\Tests\TestCase { - /** - * @dataProvider constructorArgumentsWhichThrowException - */ + #[DataProvider('constructorArgumentsWhichThrowException')] public function testConstructWithInverseFlatteningEqualsToZero($invF) { $this->expectException(\League\Geotools\Exception\InvalidArgumentException::class); $this->expectExceptionMessage('The inverse flattening cannot be negative or equal to zero !'); new Ellipsoid('foo', 'bar', $invF); } - - public function constructorArgumentsWhichThrowException() + public static function constructorArgumentsWhichThrowException() { return array( array(-123), @@ -41,22 +38,17 @@ public function constructorArgumentsWhichThrowException() array(null), ); } - - /** - * @dataProvider constructorArgumentsProvider - */ + #[DataProvider('constructorArgumentsProvider')] public function testConstructor($name, $a, $invF, $expected) { $ellipsoid = new Ellipsoid($name, $a, $invF); - $this->assertSame($expected[0], $ellipsoid->getName()); $this->assertSame($expected[1], $ellipsoid->getA()); $this->assertSame($expected[2], $ellipsoid->getB()); $this->assertSame($expected[3], $ellipsoid->getInvF()); $this->assertSame($expected[4], $ellipsoid->getArithmeticMeanRadius()); } - - public function constructorArgumentsProvider() + public static function constructorArgumentsProvider() { return array( array('name', 'a', 1, array('name', 0.0, 0.0, 1.0, 0.0)), @@ -64,25 +56,21 @@ public function constructorArgumentsProvider() array(123, 456, 789, array(123, 456.0, 455.4220532319391, 789.0, 455.80735107731306)), ); } - public function testCreateFromNameUnavailableEllipsoidThrowsException() { $this->expectException(\League\Geotools\Exception\InvalidArgumentException::class); $this->expectExceptionMessage('foo ellipsoid does not exist in selected reference ellipsoids !'); Ellipsoid::createFromName('foo'); } - public function testCreateFromNameEmptyNameThrowsException() { $this->expectException(\League\Geotools\Exception\InvalidArgumentException::class); $this->expectExceptionMessage('Please provide an ellipsoid name !'); Ellipsoid::createFromName(' '); } - public function testCreateFromName() { $ellipsoid = Ellipsoid::createFromName(Ellipsoid::WGS84); - $this->assertTrue(is_object($ellipsoid)); $this->assertInstanceOf('League\Geotools\Coordinate\Ellipsoid', $ellipsoid); $this->assertSame('WGS 84', $ellipsoid->getName()); @@ -91,18 +79,14 @@ public function testCreateFromName() $this->assertSame(298.257223563, $ellipsoid->getInvF()); $this->assertEqualsWithDelta(6371008.771415059, $ellipsoid->getArithmeticMeanRadius(), 0.0001, ''); } - - /** - * @dataProvider createFromArrayProvider - */ + #[DataProvider('createFromArrayProvider')] public function testCreateFromArrayThrowsException($newEllipsoid) { $this->expectException(\League\Geotools\Exception\InvalidArgumentException::class); $this->expectExceptionMessage('Ellipsoid arrays should contain `name`, `a` and `invF` keys !'); Ellipsoid::createFromArray($newEllipsoid); } - - public function createFromArrayProvider() + public static function createFromArrayProvider() { return array( array( @@ -130,7 +114,6 @@ public function createFromArrayProvider() ), ); } - public function testCreateFromArray() { $newEllipsoid = array( @@ -138,9 +121,7 @@ public function testCreateFromArray() 'a' => 6378136.0, 'invF' => 298.257223563, ); - $ellipsoid = Ellipsoid::createFromArray($newEllipsoid); - $this->assertTrue(is_object($ellipsoid)); $this->assertInstanceOf('League\Geotools\Coordinate\Ellipsoid', $ellipsoid); $this->assertSame('foo ellipsoid', $ellipsoid->getName()); @@ -149,8 +130,6 @@ public function testCreateFromArray() $this->assertSame(298.257223563, $ellipsoid->getInvF()); $this->assertEqualsWithDelta(6371007.7725327, $ellipsoid->getArithmeticMeanRadius(), 0.0001, ''); } - - public function testCoordinatesWithDifferentEllipsoids() { $this->expectException(\League\Geotools\Exception\NotMatchingEllipsoidException::class); @@ -161,10 +140,8 @@ public function testCoordinatesWithDifferentEllipsoids() 'a' => 123.0, 'invF' => 456.0 )); - $a = $this->getMockCoordinateReturns(array(1, 2), $WGS84); $b = $this->getMockCoordinateReturns(array(3, 4), $ANOTHER_ONE); - Ellipsoid::checkCoordinatesEllipsoid($a, $b); } } diff --git a/tests/Distance/DistanceTest.php b/tests/Distance/DistanceTest.php index 072151c4..1a16d1ee 100644 --- a/tests/Distance/DistanceTest.php +++ b/tests/Distance/DistanceTest.php @@ -11,9 +11,9 @@ namespace League\Geotools\Tests\Distance; -use League\Geotools\Coordinate\Ellipsoid; use League\Geotools\Distance\Distance; - +use PHPUnit\Framework\Attributes\DataProvider; +use League\Geotools\Coordinate\Ellipsoid; /** * @author Antoine Corcy */ @@ -24,7 +24,6 @@ class DistanceTest extends \League\Geotools\Tests\TestCase protected $to; protected $coordA; protected $coordB; - protected function setup(): void { $this->distance = new TestableDistance; @@ -33,87 +32,66 @@ protected function setup(): void $this->coordA = array(48.8234055, 2.3072664); $this->coordB = array(43.296482, 5.36978); } - public function testSetFromValueShouldBeACoordinateInterface() { $this->distance->setFrom($this->from); $from = $this->distance->getFrom(); - $this->assertTrue(is_object($from)); $this->assertInstanceOf('League\Geotools\Coordinate\CoordinateInterface', $from); } - public function testSetFromShouldReturnTheSameDistanceInstance() { $distance = $this->distance->setFrom($this->from); - $this->assertTrue(is_object($distance)); $this->assertInstanceOf('League\Geotools\Distance\Distance', $distance); $this->assertInstanceOf('League\Geotools\Distance\DistanceInterface', $distance); $this->assertSame($this->distance, $distance); } - public function testSetToValueShouldBeACoordinateInterface() { $this->distance->setTo($this->to); $to = $this->distance->getTo(); - $this->assertTrue(is_object($to)); $this->assertInstanceOf('League\Geotools\Coordinate\CoordinateInterface', $to); } - public function testSetToShouldReturnTheSameDistanceInstance() { $distance = $this->distance->setTo($this->to); - $this->assertTrue(is_object($distance)); $this->assertInstanceOf('League\Geotools\Distance\Distance', $distance); $this->assertInstanceOf('League\Geotools\Distance\DistanceInterface', $distance); $this->assertSame($this->distance, $distance); } - public function testIn() { $distance = $this->distance->in('foo'); - $this->assertSame('foo', $distance->getIn()); } - public function testInShouldReturnTheSameDistanceInstance() { $distance = $this->distance->in('foo'); - $this->assertTrue(is_object($distance)); $this->assertInstanceOf('League\Geotools\Distance\Distance', $distance); $this->assertInstanceOf('League\Geotools\Distance\DistanceInterface', $distance); $this->assertSame($this->distance, $distance); } - - /** - * @dataProvider ellipsoidInstanceAndExpectedResultProvider - */ + #[DataProvider('ellipsoidInstanceAndExpectedResultProvider')] public function testFlatDistance($ellipsoid, $result) { $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordB, $ellipsoid)); $this->assertEqualsWithDelta($result['flat']['m'], $this->distance->flat(), 0.00001, ''); - $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordB, $ellipsoid)); $this->assertEqualsWithDelta($result['flat']['km'], $this->distance->in('km')->flat(), 0.00001, ''); - $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordB, $ellipsoid)); $this->assertEqualsWithDelta($result['flat']['mi'], $this->distance->in('mi')->flat(), 0.00001, ''); - $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordB, $ellipsoid)); $this->assertEqualsWithDelta($result['flat']['ft'], $this->distance->in('ft')->flat(), 0.00001, ''); } - - /** - * @dataProvider ellipsoidInstanceAndExpectedResultProvider - */ + #[DataProvider('ellipsoidInstanceAndExpectedResultProvider')] public function testGreatCircleDistance($ellipsoid, $result) { $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); @@ -123,65 +101,49 @@ public function testGreatCircleDistance($ellipsoid, $result) $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordB, $ellipsoid)); $this->assertEqualsWithDelta($result['greatCircle']['m'], $this->distance->greatCircle(), 0.00001, ''); - $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordB, $ellipsoid)); $this->assertEqualsWithDelta($result['greatCircle']['km'], $this->distance->in('km')->greatCircle(), 0.00001, ''); - $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordB, $ellipsoid)); $this->assertEqualsWithDelta($result['greatCircle']['mi'], $this->distance->in('mi')->greatCircle(), 0.00001, ''); - $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordB, $ellipsoid)); $this->assertEqualsWithDelta($result['greatCircle']['ft'], $this->distance->in('ft')->greatCircle(), 0.00001, ''); } - - /** - * @dataProvider ellipsoidInstanceAndExpectedResultProvider - */ + #[DataProvider('ellipsoidInstanceAndExpectedResultProvider')] public function testHaversineDistance($ellipsoid, $result) { $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordB, $ellipsoid)); $this->assertEqualsWithDelta($result['haversine']['m'], $this->distance->haversine(), 0.00001, ''); - $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordB, $ellipsoid)); $this->assertEqualsWithDelta($result['haversine']['km'], $this->distance->in('km')->haversine(), 0.00001, ''); - $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordB, $ellipsoid)); $this->assertEqualsWithDelta($result['haversine']['mi'], $this->distance->in('mi')->haversine(), 0.00001, ''); - $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordB, $ellipsoid)); $this->assertEqualsWithDelta($result['haversine']['ft'], $this->distance->in('ft')->haversine(), 0.00001, ''); } - - /** - * @dataProvider ellipsoidInstanceAndExpectedResultProvider - */ + #[DataProvider('ellipsoidInstanceAndExpectedResultProvider')] public function testVincentyDistance($ellipsoid, $result) { $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordB, $ellipsoid)); $this->assertEqualsWithDelta($result['vincenty']['m'], $this->distance->vincenty(), 0.00001, ''); - $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordB, $ellipsoid)); $this->assertEqualsWithDelta($result['vincenty']['km'], $this->distance->in('km')->vincenty(), 0.00001, ''); - $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordB, $ellipsoid)); $this->assertEqualsWithDelta($result['vincenty']['mi'], $this->distance->in('mi')->vincenty(), 0.00001, ''); - $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordB, $ellipsoid)); $this->assertEqualsWithDelta($result['vincenty']['ft'], $this->distance->in('ft')->vincenty(), 0.00001, ''); } - - public function ellipsoidInstanceAndExpectedResultProvider() + public static function ellipsoidInstanceAndExpectedResultProvider() { return array( array( @@ -302,44 +264,35 @@ public function ellipsoidInstanceAndExpectedResultProvider() ), ); } - public function testVincentyDistanceCoIncidentPoint() { $ellipsoid = Ellipsoid::createFromName(Ellipsoid::WGS84); $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); - $this->assertSame(0.0, $this->distance->vincenty()); } - public function testFlatDistanceWithSameCoordinate() { $ellipsoid = Ellipsoid::createFromName(Ellipsoid::WGS84); $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); - $this->assertSame(0.0, $this->distance->flat()); } - public function testGreatCircleDistanceWithSameCoordinate() { $ellipsoid = Ellipsoid::createFromName(Ellipsoid::WGS84); $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); - $this->assertSame(0.0, $this->distance->greatCircle()); } - public function testHaversineDistanceWithSameCoordinate() { $ellipsoid = Ellipsoid::createFromName(Ellipsoid::WGS84); $this->distance->setFrom($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); $this->distance->setTo($this->getMockCoordinateReturns($this->coordA, $ellipsoid)); - $this->assertSame(0.0, $this->distance->haversine()); } } - class TestableDistance extends Distance { public function getIn() diff --git a/tests/Geohash/GeohashTest.php b/tests/Geohash/GeohashTest.php index 3784122c..6f35bd88 100644 --- a/tests/Geohash/GeohashTest.php +++ b/tests/Geohash/GeohashTest.php @@ -12,30 +12,25 @@ namespace League\Geotools\Tests\Geohash; use League\Geotools\Geohash\Geohash; - +use PHPUnit\Framework\Attributes\DataProvider; /** * @author Antoine Corcy */ class GeohashTest extends \League\Geotools\Tests\TestCase { protected $geohash; - protected function setup(): void { $this->geohash = new Geohash; } - - /** - * @dataProvider lengthsProvider - */ + #[DataProvider('lengthsProvider')] public function testEncodeShouldThrowException($length) { $this->expectException(\League\Geotools\Exception\InvalidArgumentException::class); $this->expectExceptionMessage('The length should be between 1 and 12.'); $this->geohash->encode($this->getStubCoordinate(), $length); } - - public function lengthsProvider() + public static function lengthsProvider() { return array( array(-1), @@ -48,28 +43,22 @@ public function lengthsProvider() array('13'), ); } - public function testEncodeShouldReturnTheSameGeohashInstance() { $encoded = $this->geohash->encode($this->getStubCoordinate()); - $this->assertTrue(is_object($encoded)); $this->assertInstanceOf('\League\Geotools\Geohash\Geohash', $encoded); $this->assertInstanceOf('\League\Geotools\Geohash\GeohashInterface', $encoded); $this->assertSame($this->geohash, $encoded); } - - /** - * @dataProvider invalidStringGeoHashesProvider - */ + #[DataProvider('invalidStringGeoHashesProvider')] public function testDecodeShouldThrowStringException($geohash) { $this->expectException(\League\Geotools\Exception\InvalidArgumentException::class); $this->expectExceptionMessage('The geo hash should be a string.'); $this->geohash->decode($geohash); } - - public function invalidStringGeoHashesProvider() + public static function invalidStringGeoHashesProvider() { return array( array(-1), @@ -78,18 +67,14 @@ public function invalidStringGeoHashesProvider() array(array()), ); } - - /** - * @dataProvider invalidRangeGeoHashesProvider - */ + #[DataProvider('invalidRangeGeoHashesProvider')] public function testDecodeShouldThrowRangeException($geohash) { $this->expectException(\League\Geotools\Exception\InvalidArgumentException::class); $this->expectExceptionMessage('The length of the geo hash should be between 1 and 12.'); $this->geohash->decode($geohash); } - - public function invalidRangeGeoHashesProvider() + public static function invalidRangeGeoHashesProvider() { return array( array(''), @@ -98,18 +83,14 @@ public function invalidRangeGeoHashesProvider() array('bcd04324kmnpz'), ); } - - /** - * @dataProvider invalidCharGeoHashesProvider - */ + #[DataProvider('invalidCharGeoHashesProvider')] public function testDecodeShouldThrowRuntimeException($geohash) { $this->expectException(\League\Geotools\Exception\RuntimeException::class); $this->expectExceptionMessage('This geo hash is invalid.'); $this->geohash->decode($geohash); } - - public function invalidCharGeoHashesProvider() + public static function invalidCharGeoHashesProvider() { return array( array(' '), @@ -121,50 +102,38 @@ public function invalidCharGeoHashesProvider() array('å'), ); } - public function testDecodeShouldReturnTheSameGeohashInstance() { $decoded = $this->geohash->decode('u09tu800gnqw'); - $this->assertTrue(is_object($decoded)); $this->assertInstanceOf('\League\Geotools\Geohash\Geohash', $decoded); $this->assertInstanceOf('\League\Geotools\Geohash\GeohashInterface', $decoded); $this->assertSame($this->geohash, $decoded); } - - /** - * @dataProvider coordinatesAndExpectedGeohashesAndBoundingBoxesProvider - */ + #[DataProvider('coordinatesAndExpectedGeohashesAndBoundingBoxesProvider')] public function testEncodedGetGeoHash($coordinate, $length, $expectedGeoHash) { $geohash = $this->geohash->encode($this->getMockCoordinateReturns($coordinate), $length)->getGeohash(); - $this->assertSame($length, strlen($geohash)); $this->assertSame($expectedGeoHash, $geohash); } - - /** - * @dataProvider coordinatesAndExpectedGeohashesAndBoundingBoxesProvider - */ + #[DataProvider('coordinatesAndExpectedGeohashesAndBoundingBoxesProvider')] public function testEncodedGetBoundingBox($coordinate, $length, $expectedGeoHash, $expectedBoundingBox) { $boundingBox = $this->geohash->encode($this->getMockCoordinateReturns($coordinate), $length)->getBoundingBox(); - $this->assertTrue(is_array($boundingBox)); $this->assertTrue(is_object($boundingBox[0])); $this->assertInstanceOf('\League\Geotools\Coordinate\Coordinate', $boundingBox[0]); $this->assertInstanceOf('\League\Geotools\Coordinate\CoordinateInterface', $boundingBox[0]); $this->assertEqualsWithDelta($expectedBoundingBox[0][0], $boundingBox[0]->getLatitude(), 0.1, ''); $this->assertEqualsWithDelta($expectedBoundingBox[0][1], $boundingBox[0]->getLongitude(), 0.1, ''); - $this->assertTrue(is_object($boundingBox[1])); $this->assertInstanceOf('\League\Geotools\Coordinate\Coordinate', $boundingBox[1]); $this->assertInstanceOf('\League\Geotools\Coordinate\CoordinateInterface', $boundingBox[1]); $this->assertEqualsWithDelta($expectedBoundingBox[1][0], $boundingBox[1]->getLatitude(), 0.1, ''); $this->assertEqualsWithDelta($expectedBoundingBox[1][1], $boundingBox[1]->getLongitude(), 0.1, ''); } - - public function coordinatesAndExpectedGeohashesAndBoundingBoxesProvider() + public static function coordinatesAndExpectedGeohashesAndBoundingBoxesProvider() { return array( array( @@ -196,43 +165,33 @@ public function coordinatesAndExpectedGeohashesAndBoundingBoxesProvider() ), ); } - - /** - * @dataProvider geohashesAndExpectedCoordinatesAndBoundingBoxesProvider - */ + #[DataProvider('geohashesAndExpectedCoordinatesAndBoundingBoxesProvider')] public function testDecodedGetCoordinate($geoHash, $expectedCoordinate) { $coordinate = $this->geohash->decode($geoHash)->getCoordinate(); - $this->assertTrue(is_object($coordinate)); $this->assertInstanceOf('\League\Geotools\Coordinate\Coordinate', $coordinate); $this->assertInstanceOf('\League\Geotools\Coordinate\CoordinateInterface', $coordinate); $this->assertEqualsWithDelta($expectedCoordinate[0], $coordinate->getLatitude(), 0.1, ''); $this->assertEqualsWithDelta($expectedCoordinate[1], $coordinate->getLongitude(), 0.1, ''); } - - /** - * @dataProvider geohashesAndExpectedCoordinatesAndBoundingBoxesProvider - */ + #[DataProvider('geohashesAndExpectedCoordinatesAndBoundingBoxesProvider')] public function testDecodedGetBoundingBox($geoHash, $expectedCoordinate, $expectedBoundingBox) { $boundingBox = $this->geohash->decode($geoHash)->getBoundingBox(); - $this->assertTrue(is_array($boundingBox)); $this->assertTrue(is_object($boundingBox[0])); $this->assertInstanceOf('\League\Geotools\Coordinate\Coordinate', $boundingBox[0]); $this->assertInstanceOf('\League\Geotools\Coordinate\CoordinateInterface', $boundingBox[0]); $this->assertEqualsWithDelta($expectedBoundingBox[0][0], $boundingBox[0]->getLatitude(), 0.1, ''); $this->assertEqualsWithDelta($expectedBoundingBox[0][1], $boundingBox[0]->getLongitude(), 0.1, ''); - $this->assertTrue(is_object($boundingBox[1])); $this->assertInstanceOf('\League\Geotools\Coordinate\Coordinate', $boundingBox[1]); $this->assertInstanceOf('\League\Geotools\Coordinate\CoordinateInterface', $boundingBox[1]); $this->assertEqualsWithDelta($expectedBoundingBox[1][0], $boundingBox[1]->getLatitude(), 0.1, ''); $this->assertEqualsWithDelta($expectedBoundingBox[1][1], $boundingBox[1]->getLongitude(), 0.1, ''); } - - public function geohashesAndExpectedCoordinatesAndBoundingBoxesProvider() + public static function geohashesAndExpectedCoordinatesAndBoundingBoxesProvider() { return array( array( @@ -261,18 +220,13 @@ public function geohashesAndExpectedCoordinatesAndBoundingBoxesProvider() ), ); } - - /** - * @dataProvider geohashesAndExpectedNeighborProvider - */ + #[DataProvider('geohashesAndExpectedNeighborProvider')] public function testGetNeighbors($geoHash, $expectedNeighbors) { $coordinate = $this->geohash->decode($geoHash); - $this->assertEquals($expectedNeighbors, $coordinate->getNeighbors(true)); } - - public function geohashesAndExpectedNeighborProvider() + public static function geohashesAndExpectedNeighborProvider() { return array( array( diff --git a/tests/GeometryCollectionTest.php b/tests/GeometryCollectionTest.php index 7d3ba67c..892a11a1 100644 --- a/tests/GeometryCollectionTest.php +++ b/tests/GeometryCollectionTest.php @@ -7,6 +7,7 @@ use League\Geotools\Coordinate\Ellipsoid; use League\Geotools\GeometryCollection; use League\Geotools\Polygon\Polygon; +use PHPUnit\Framework\Attributes\Test; class GeometryCollectionTest extends TestCase { @@ -20,9 +21,7 @@ public function setup(): void $this->secondGeometry = new Polygon(new CoordinateCollection([new Coordinate([2, 2])])); } - /** - * @test - */ + #[Test] public function itShouldHaveThePrecisionOfTheLessPreciseGeometryComponent() { $this->firstGeometry->setPrecision(5); @@ -34,9 +33,7 @@ public function itShouldHaveThePrecisionOfTheLessPreciseGeometryComponent() $this->assertEquals(10, $collection->getPrecision()); } - /** - * @test - */ + #[Test] public function itShouldReturnNullIfItHasNoGeometry() { $collection = new SimpleGeometryCollection(); @@ -44,9 +41,7 @@ public function itShouldReturnNullIfItHasNoGeometry() $this->assertNull($collection->getCoordinate()); } - /** - * @test - */ + #[Test] public function itShouldReturnTheCoordinateOfItsFirstGeometry() { $array = [$this->firstGeometry, $this->secondGeometry]; @@ -56,9 +51,7 @@ public function itShouldReturnTheCoordinateOfItsFirstGeometry() $this->assertEquals(new Coordinate([1, 1]), $collection->getCoordinate()); } - /** - * @test - */ + #[Test] public function itShouldReturnAnArrayOfAllTheCoordinatesOfItsGeometries() { $array = [$this->firstGeometry, $this->secondGeometry]; @@ -71,9 +64,7 @@ public function itShouldReturnAnArrayOfAllTheCoordinatesOfItsGeometries() ); } - /** - * @test - */ + #[Test] public function itShouldReturnTheMergedBoundingBoxOfAllItsGeometries() { $array = [$this->firstGeometry, $this->secondGeometry]; @@ -86,9 +77,7 @@ public function itShouldReturnTheMergedBoundingBoxOfAllItsGeometries() ); } - /** - * @test - */ + #[Test] public function itShouldReturnTheEllipsoidOfItsFirstGeometry() { $array = [$this->firstGeometry]; @@ -98,9 +87,7 @@ public function itShouldReturnTheEllipsoidOfItsFirstGeometry() $this->assertEquals($this->firstGeometry->getEllipsoid(), $collection->getEllipsoid()); } - /** - * @test - */ + #[Test] public function itShouldBehaveAsAnArrayOfGeometries() { $array = [$this->firstGeometry]; @@ -117,9 +104,7 @@ public function itShouldBehaveAsAnArrayOfGeometries() $this->assertFalse(isset($collection['test'])); } - /** - * @test - */ + #[Test] public function itShouldThrowAnExceptionWhenNotProvidedAGeometry() { $array = ['a']; @@ -129,9 +114,7 @@ public function itShouldThrowAnExceptionWhenNotProvidedAGeometry() $collection = new SimpleGeometryCollection($array); } - /** - * @test - */ + #[Test] public function itShouldThrowAnExceptionWhenProvidedAnInvalidGeometry() { $array = [$this->firstGeometry]; @@ -154,9 +137,7 @@ public function itShouldThrowAnExceptionWhenProvidedAnInvalidGeometry() $collection->add($secondGeometry); } - /** - * @test - */ + #[Test] public function itShouldBeCountable() { $array = [$this->secondGeometry]; @@ -166,9 +147,7 @@ public function itShouldBeCountable() $this->assertEquals(1, count($collection)); } - /** - * @test - */ + #[Test] public function itShouldOfferAccessToInnerElementsByKey() { $array = ['foo' => $this->firstGeometry]; @@ -189,9 +168,7 @@ public function itShouldOfferAccessToInnerElementsByKey() $this->assertNull($collection->remove('dummy')); } - /** - * @test - */ + #[Test] public function itShouldMergeCollections() { $array1 = ['foo' => $this->firstGeometry]; @@ -215,9 +192,7 @@ public function itShouldMergeCollections() $this->assertEquals(array_merge($array1, $array2), $mergedCollection->toArray()); } - /** - * @test - */ + #[Test] public function itShouldThorwAnExceptionWhenMergingDifferentTypesCollections() { $array1 = ['foo' => $this->firstGeometry]; diff --git a/tests/Polygon/MultiPolygonTest.php b/tests/Polygon/MultiPolygonTest.php index f01e9ffc..6b9f58e3 100644 --- a/tests/Polygon/MultiPolygonTest.php +++ b/tests/Polygon/MultiPolygonTest.php @@ -11,10 +11,10 @@ namespace League\Geotools\Tests\Polygon; +use League\Geotools\Polygon\Polygon; +use PHPUnit\Framework\Attributes\DataProvider; use League\Geotools\Coordinate\Coordinate; use League\Geotools\Polygon\MultiPolygon; -use League\Geotools\Polygon\Polygon; - /** * @author Gabriel Bull */ @@ -24,80 +24,62 @@ class MultiPolygonTest extends \League\Geotools\Tests\TestCase * @var Polygon */ protected $polygon; - protected function setup(): void { $this->polygon = new Polygon; } - public function testType() { $multiPolygon = new MultiPolygon(); - $this->assertEquals('MULTIPOLYGON', $multiPolygon->getGeometryType()); } - - public function polygonAndVertexCoordinate() + public static function polygonAndVertexCoordinate() { return array( array( - 'polygonCoordinates' => array( + array( array(48.9675969, 1.7440796), array(48.4711003, 2.5268555), array(48.9279131, 3.1448364), array(49.3895245, 2.6119995) ), - 'vertexCoordinate' => array(48.4711003, 2.5268555), + array(48.4711003, 2.5268555), ), ); } - - /** - * @dataProvider polygonAndVertexCoordinate - * @param array $polygonCoordinates - * @param array $vertexCoordinate - */ + #[DataProvider('polygonAndVertexCoordinate')] public function testPointOnVertex($polygonCoordinates, $vertexCoordinate) { $this->polygon->set($polygonCoordinates); - $multiPolygon = new MultiPolygon([$this->polygon]); - $this->assertTrue($this->polygon->pointOnVertex(new Coordinate($vertexCoordinate))); $this->assertTrue($multiPolygon->pointOnVertex(new Coordinate($vertexCoordinate))); } - - /** - * @dataProvider polygonAndVertexCoordinate - * @param array $polygonCoordinates - */ + #[DataProvider('polygonAndVertexCoordinate')] public function testPointNotOnVertex($polygonCoordinates) { $this->polygon->set($polygonCoordinates); - $multiPolygon = new MultiPolygon([$this->polygon]); - $this->assertFalse($this->polygon->pointOnVertex(new Coordinate(array(0, 0)))); $this->assertFalse($multiPolygon->pointOnVertex(new Coordinate(array(0, 0)))); } - - public function polygonAndPointOnBoundaryCoordinate() + public static function polygonAndPointOnBoundaryCoordinate() { return array( array( - 'polygonCoordinates' => array( + array( array(48.9675969, 1.7440796), array(48.4711003, 2.5268555), array(48.9279131, 3.1448364), array(49.3895245, 2.6119995) ), - 'pointOnBoundaryCoordinates' => array( + array( array(48.7193486, 2.13546755), array(48.6995067, 2.83584595), array(49.1587188, 2.87841795), array(49.1785607, 2.17803955), ), - 'pointNotOnBoundaryCoordinates' => array( + array( array(43.7193486, 2.13546755), array(45.6995067, 2.83584595), array(47.1587188, 2.87841795), @@ -106,101 +88,70 @@ public function polygonAndPointOnBoundaryCoordinate() ), ); } - - /** - * @dataProvider polygonAndPointOnBoundaryCoordinate - * @param array $polygonCoordinates - * @param array $pointOnBoundaryCoordinates - */ + #[DataProvider('polygonAndPointOnBoundaryCoordinate')] public function testPointOnBoundary($polygonCoordinates, $pointOnBoundaryCoordinates) { $this->polygon->set($polygonCoordinates); - $multiPolygon = new MultiPolygon([$this->polygon]); - foreach ($pointOnBoundaryCoordinates as $pointOnBoundaryCoordinate) { $this->assertTrue($this->polygon->pointOnBoundary(new Coordinate($pointOnBoundaryCoordinate))); $this->assertTrue($multiPolygon->pointOnBoundary(new Coordinate($pointOnBoundaryCoordinate))); } } - - /** - * @dataProvider polygonAndPointOnBoundaryCoordinate - * @param array $polygonCoordinates - * @param array $pointOnBoundaryCoordinates - * @param array $pointNotOnBoundaryCoordinates - */ + #[DataProvider('polygonAndPointOnBoundaryCoordinate')] public function testPointNotOnBoundary( $polygonCoordinates, $pointOnBoundaryCoordinates, $pointNotOnBoundaryCoordinates ) { $this->polygon->set($polygonCoordinates); - $multiPolygon = new MultiPolygon([$this->polygon]); - foreach ($pointNotOnBoundaryCoordinates as $pointNotOnBoundaryCoordinate) { $this->assertFalse($this->polygon->pointOnBoundary(new Coordinate($pointNotOnBoundaryCoordinate))); $this->assertFalse($multiPolygon->pointOnBoundary(new Coordinate($pointNotOnBoundaryCoordinate))); } } - - public function polygonAndPointInPolygonCoordinate() + public static function polygonAndPointInPolygonCoordinate() { return array( array( - 'polygonCoordinates' => array( + array( array(48.9675969, 1.7440796), array(48.4711003, 2.5268555), array(48.9279131, 3.1448364), array(49.3895245, 2.6119995) ), - 'pointInPolygonCoordinates' => array( + array( array(49.1785607, 2.4444580), array(49.1785607, 2.0000000), array(49.1785607, 1.7440796), array(48.9279131, 2.4444580), ), - 'pointNotInPolygonCoordinates' => array( + array( array(49.1785607, 5), array(50, 2.4444580), ) ), ); } - - /** - * @dataProvider polygonAndPointInPolygonCoordinate - * @param array $polygonCoordinates - * @param array $pointInPolygonCoordinates - */ + #[DataProvider('polygonAndPointInPolygonCoordinate')] public function testPointInPolygon($polygonCoordinates, $pointInPolygonCoordinates) { $this->polygon->set($polygonCoordinates); - $multiPolygon = new MultiPolygon([$this->polygon]); - foreach ($pointInPolygonCoordinates as $pointInPolygonCoordinate) { $this->assertTrue($this->polygon->pointInPolygon(new Coordinate($pointInPolygonCoordinate))); $this->assertTrue($multiPolygon->pointInPolygon(new Coordinate($pointInPolygonCoordinate))); } } - - /** - * @dataProvider polygonAndPointInPolygonCoordinate - * @param array $polygonCoordinates - * @param array $pointInPolygonCoordinates - * @param array $pointNotInPolygonCoordinates - */ + #[DataProvider('polygonAndPointInPolygonCoordinate')] public function testPointNotInPolygon( $polygonCoordinates, $pointInPolygonCoordinates, $pointNotInPolygonCoordinates ) { $this->polygon->set($polygonCoordinates); - $multiPolygon = new MultiPolygon([$this->polygon]); - foreach ($pointNotInPolygonCoordinates as $pointNotInPolygonCoordinate) { $this->assertFalse($this->polygon->pointInPolygon(new Coordinate($pointNotInPolygonCoordinate))); $this->assertFalse($multiPolygon->pointInPolygon(new Coordinate($pointNotInPolygonCoordinate))); diff --git a/tests/Polygon/PolygonTest.php b/tests/Polygon/PolygonTest.php index 421c19d4..04bcd4f1 100644 --- a/tests/Polygon/PolygonTest.php +++ b/tests/Polygon/PolygonTest.php @@ -11,9 +11,10 @@ namespace League\Geotools\Tests\Polygon; -use League\Geotools\Coordinate\Coordinate; use League\Geotools\Polygon\Polygon; - +use PHPUnit\Framework\Attributes\DataProvider; +use League\Geotools\Coordinate\Coordinate; +use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; /** * @author Gabriel Bull */ @@ -23,25 +24,21 @@ class PolygonTest extends \League\Geotools\Tests\TestCase * @var Polygon */ protected $polygon; - protected function setup(): void { $this->polygon = new Polygon; } - public function testCannotCreatePolygonWithString() { $this->expectException(\InvalidArgumentException::class); new Polygon('foo'); } - public function testCannotCreatePolygonWithInteger() { $this->expectException(\InvalidArgumentException::class); new Polygon(123); } - - public function polygonCoordinates() + public static function polygonCoordinates() { return array( array( @@ -54,85 +51,65 @@ public function polygonCoordinates() ), ); } - - /** - * @dataProvider polygonCoordinates - * @param array $polygonCoordinates - * @doesNotPerformAssertions - */ + #[DataProvider('polygonCoordinates')] + #[DoesNotPerformAssertions] public function testContructor($polygonCoordinates) { new Polygon($polygonCoordinates); } - - /** - * @dataProvider polygonCoordinates - * @param array $polygonCoordinates - */ + #[DataProvider('polygonCoordinates')] public function testArraySetterAndGetter($polygonCoordinates) { $this->polygon->set($polygonCoordinates); - $this->assertCount(4, $this->polygon); foreach ($polygonCoordinates as $key => $value) { $this->assertEquals($value[0], $this->polygon->get($key)->getLatitude()); $this->assertEquals($value[1], $this->polygon->get($key)->getLongitude()); } } - - public function polygonAndVertexCoordinate() + public static function polygonAndVertexCoordinate() { return array( array( - 'polygonCoordinates' => array( + array( array(48.9675969, 1.7440796), array(48.4711003, 2.5268555), array(48.9279131, 3.1448364), array(49.3895245, 2.6119995) ), - 'vertexCoordinate' => array(48.4711003, 2.5268555), + array(48.4711003, 2.5268555), ), ); } - - /** - * @dataProvider polygonAndVertexCoordinate - * @param array $polygonCoordinates - * @param array $vertexCoordinate - */ + #[DataProvider('polygonAndVertexCoordinate')] public function testPointOnVertex($polygonCoordinates, $vertexCoordinate) { $this->polygon->set($polygonCoordinates); $this->assertTrue($this->polygon->pointOnVertex(new Coordinate($vertexCoordinate))); } - - /** - * @dataProvider polygonAndVertexCoordinate - * @param array $polygonCoordinates - */ + #[DataProvider('polygonAndVertexCoordinate')] public function testPointNotOnVertex($polygonCoordinates) { $this->polygon->set($polygonCoordinates); $this->assertFalse($this->polygon->pointOnVertex(new Coordinate(array(0, 0)))); } - - public function polygonAndPointOnBoundaryCoordinate() + public static function polygonAndPointOnBoundaryCoordinate() { return array( array( - 'polygonCoordinates' => array( + array( array(48.9675969, 1.7440796), array(48.4711003, 2.5268555), array(48.9279131, 3.1448364), array(49.3895245, 2.6119995) ), - 'pointOnBoundaryCoordinates' => array( + array( array(48.7193486, 2.13546755), array(48.6995067, 2.83584595), array(49.1587188, 2.87841795), array(49.1785607, 2.17803955), ), - 'pointNotOnBoundaryCoordinates' => array( + array( array(43.7193486, 2.13546755), array(45.6995067, 2.83584595), array(47.1587188, 2.87841795), @@ -141,12 +118,7 @@ public function polygonAndPointOnBoundaryCoordinate() ), ); } - - /** - * @dataProvider polygonAndPointOnBoundaryCoordinate - * @param array $polygonCoordinates - * @param array $pointOnBoundaryCoordinates - */ + #[DataProvider('polygonAndPointOnBoundaryCoordinate')] public function testPointOnBoundary($polygonCoordinates, $pointOnBoundaryCoordinates) { $this->polygon->set($polygonCoordinates); @@ -154,13 +126,7 @@ public function testPointOnBoundary($polygonCoordinates, $pointOnBoundaryCoordin $this->assertTrue($this->polygon->pointOnBoundary(new Coordinate($pointOnBoundaryCoordinate))); } } - - /** - * @dataProvider polygonAndPointOnBoundaryCoordinate - * @param array $polygonCoordinates - * @param array $pointOnBoundaryCoordinates - * @param array $pointNotOnBoundaryCoordinates - */ + #[DataProvider('polygonAndPointOnBoundaryCoordinate')] public function testPointNotOnBoundary( $polygonCoordinates, $pointOnBoundaryCoordinates, @@ -171,36 +137,30 @@ public function testPointNotOnBoundary( $this->assertFalse($this->polygon->pointOnBoundary(new Coordinate($pointNotOnBoundaryCoordinate))); } } - - public function polygonAndPointInPolygonCoordinate() + public static function polygonAndPointInPolygonCoordinate() { return array( array( - 'polygonCoordinates' => array( + array( array(48.9675969, 1.7440796), array(48.4711003, 2.5268555), array(48.9279131, 3.1448364), array(49.3895245, 2.6119995) ), - 'pointInPolygonCoordinates' => array( + array( array(49.1785607, 2.4444580), array(49.1785607, 2.0000000), array(49.1785607, 1.7440796), array(48.9279131, 2.4444580), ), - 'pointNotInPolygonCoordinates' => array( + array( array(49.1785607, 5), array(50, 2.4444580), ) ), ); } - - /** - * @dataProvider polygonAndPointInPolygonCoordinate - * @param array $polygonCoordinates - * @param array $pointInPolygonCoordinates - */ + #[DataProvider('polygonAndPointInPolygonCoordinate')] public function testPointInPolygon($polygonCoordinates, $pointInPolygonCoordinates) { $this->polygon->set($polygonCoordinates); @@ -208,13 +168,7 @@ public function testPointInPolygon($polygonCoordinates, $pointInPolygonCoordinat $this->assertTrue($this->polygon->pointInPolygon(new Coordinate($pointInPolygonCoordinate))); } } - - /** - * @dataProvider polygonAndPointInPolygonCoordinate - * @param array $polygonCoordinates - * @param array $pointInPolygonCoordinates - * @param array $pointNotInPolygonCoordinates - */ + #[DataProvider('polygonAndPointInPolygonCoordinate')] public function testPointNotInPolygon( $polygonCoordinates, $pointInPolygonCoordinates, diff --git a/tests/TestCase.php b/tests/TestCase.php index ef8d9eed..9a96cc56 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -53,16 +53,16 @@ protected function getMockGeocoderReturns(array $providers, array $data = array( $mock = $this->createMock('\Geocoder\ProviderAggregator'); $mock ->method('getProviders') - ->will($this->returnValue($providers)); + ->willReturn($providers); $mock ->method('using') - ->will($this->returnSelf()); + ->willReturnSelf(); $mock ->method('geocode') - ->will($this->returnValue($addresses)); + ->willReturn($addresses); $mock ->method('reverse') - ->will($this->returnValue($addresses)); + ->willReturn($addresses); return $mock; } @@ -78,16 +78,16 @@ protected function getMockGeocoderThrowException(array $providers) $mock ->expects($this->once()) ->method('getProviders') - ->will($this->returnValue($providers)); + ->willReturn($providers); $mock ->method('using') - ->will($this->returnSelf()); + ->willReturnSelf(); $mock ->method('geocode') - ->will($this->throwException(new \Exception)); + ->willThrowException(new \Exception); $mock ->method('reverse') - ->will($this->throwException(new \Exception)); + ->willThrowException(new \Exception); return $mock; } @@ -122,16 +122,16 @@ protected function getMockCoordinateReturns(array $coordinate, ?Ellipsoid $ellip $mock = $this->createMock('\League\Geotools\Coordinate\CoordinateInterface'); $mock ->method('getLatitude') - ->will($this->returnValue($coordinate[0])); + ->willReturn($coordinate[0]); $mock ->method('getLongitude') - ->will($this->returnValue($coordinate[1])); + ->willReturn($coordinate[1]); if ($ellipsoid) { $mock ->expects($this->atLeastOnce()) ->method('getEllipsoid') - ->will($this->returnValue($ellipsoid)); + ->willReturn($ellipsoid); } return $mock; @@ -152,7 +152,7 @@ protected function getMockGeocoded($expects = null) $mock ->expects($expects) ->method('getCoordinates') - ->will($this->returnArgument(0)); + ->willReturnArgument(0); return $mock; } @@ -168,11 +168,11 @@ protected function getMockGeocodedReturns(array $coordinate) $mock ->expects($this->atLeastOnce()) ->method('getLatitude') - ->will($this->returnValue($coordinate['latitude'])); + ->willReturn($coordinate['latitude']); $mock ->expects($this->atLeastOnce()) ->method('getLongitude') - ->will($this->returnValue($coordinate['longitude'])); + ->willReturn($coordinate['longitude']); return $mock; } diff --git a/tests/Vertex/VertexTest.php b/tests/Vertex/VertexTest.php index 1c353f55..6e006298 100644 --- a/tests/Vertex/VertexTest.php +++ b/tests/Vertex/VertexTest.php @@ -11,10 +11,10 @@ namespace League\Geotools\Tests\Vertex; +use League\Geotools\Vertex\Vertex; +use PHPUnit\Framework\Attributes\DataProvider; use League\Geotools\Coordinate\Coordinate; use League\Geotools\Coordinate\Ellipsoid; -use League\Geotools\Vertex\Vertex; - /** * @author Antoine Corcy */ @@ -23,64 +23,50 @@ class VertexTest extends \League\Geotools\Tests\TestCase protected $vertex; protected $from; protected $to; - protected function setup(): void { $this->vertex = new Vertex; $this->from = $this->getStubCoordinate(); $this->to = $this->getStubCoordinate(); } - public function testSetFromValueShouldBeACoordinateInterface() { $this->vertex->setFrom($this->from); $from = $this->vertex->getFrom(); - $this->assertTrue(is_object($from)); $this->assertInstanceOf('League\Geotools\Coordinate\CoordinateInterface', $from); } - public function testSetFromShouldReturnTheSameVertexInstance() { $vertex = $this->vertex->setFrom($this->from); - $this->assertTrue(is_object($vertex)); $this->assertInstanceOf('League\Geotools\Vertex\Vertex', $vertex); $this->assertInstanceOf('League\Geotools\Vertex\VertexInterface', $vertex); $this->assertSame($this->vertex, $vertex); } - public function testSetToValueShouldBeACoordinateInterface() { $this->vertex->setTo($this->to); $to = $this->vertex->getTo(); - $this->assertTrue(is_object($to)); $this->assertInstanceOf('League\Geotools\Coordinate\CoordinateInterface', $to); } - public function testSetToShouldReturnTheSameVertexInstance() { $vertex = $this->vertex->setTo($this->to); - $this->assertTrue(is_object($vertex)); $this->assertInstanceOf('League\Geotools\Vertex\Vertex', $vertex); $this->assertInstanceOf('League\Geotools\Vertex\VertexInterface', $vertex); $this->assertSame($this->vertex, $vertex); } - - /** - * @dataProvider coordinatesAndExpectedDegreeForInitialBearingProvider - */ + #[DataProvider('coordinatesAndExpectedDegreeForInitialBearingProvider')] public function testInitialBearing($from, $to, $expectedDegree) { $this->vertex->setFrom($this->getMockCoordinateReturns($from)); $this->vertex->setTo($this->getMockCoordinateReturns($to)); - $this->assertEquals($expectedDegree, $this->vertex->initialBearing()); } - - public function coordinatesAndExpectedDegreeForInitialBearingProvider() + public static function coordinatesAndExpectedDegreeForInitialBearingProvider() { return array( array( @@ -110,19 +96,14 @@ public function coordinatesAndExpectedDegreeForInitialBearingProvider() ), ); } - - /** - * @dataProvider coordinatesAndExpectedDegreeForFinalBearingProvider - */ + #[DataProvider('coordinatesAndExpectedDegreeForFinalBearingProvider')] public function testFinalBearing($from, $to, $expectedDegree) { $this->vertex->setFrom($this->getMockCoordinateReturns($from)); $this->vertex->setTo($this->getMockCoordinateReturns($to)); - $this->assertEquals($expectedDegree, $this->vertex->finalBearing()); } - - public function coordinatesAndExpectedDegreeForFinalBearingProvider() + public static function coordinatesAndExpectedDegreeForFinalBearingProvider() { return array( array( @@ -152,19 +133,14 @@ public function coordinatesAndExpectedDegreeForFinalBearingProvider() ), ); } - - /** - * @dataProvider coordinatesAndExpectedInitialCardinalProvider - */ + #[DataProvider('coordinatesAndExpectedInitialCardinalProvider')] public function testInitialCardinal($from, $to, $expectedCardinal) { $this->vertex->setFrom($this->getMockCoordinateReturns($from)); $this->vertex->setTo($this->getMockCoordinateReturns($to)); - $this->assertEquals($expectedCardinal, $this->vertex->initialCardinal()); } - - public function coordinatesAndExpectedInitialCardinalProvider() + public static function coordinatesAndExpectedInitialCardinalProvider() { return array( array( @@ -194,19 +170,14 @@ public function coordinatesAndExpectedInitialCardinalProvider() ), ); } - - /** - * @dataProvider coordinatesAndExpectedFinalCardinalProvider - */ + #[DataProvider('coordinatesAndExpectedFinalCardinalProvider')] public function testFinalCardinal($from, $to, $expectedCardinal) { $this->vertex->setFrom($this->getMockCoordinateReturns($from)); $this->vertex->setTo($this->getMockCoordinateReturns($to)); - $this->assertEquals($expectedCardinal, $this->vertex->finalCardinal()); } - - public function coordinatesAndExpectedFinalCardinalProvider() + public static function coordinatesAndExpectedFinalCardinalProvider() { return array( array( @@ -236,49 +207,43 @@ public function coordinatesAndExpectedFinalCardinalProvider() ), ); } - - /** - * @dataProvider fromAndToCoordinatesAndExpectedMiddlePointProvider - */ + #[DataProvider('fromAndToCoordinatesAndExpectedMiddlePointProvider')] public function testMiddle($from, $to, $expectedMiddlePoint) { $this->vertex->setFrom($this->getMockCoordinateReturns($from)); $this->vertex->setTo($this->getMockCoordinateReturns($to)); $middlePoint = $this->vertex->middle(); - $this->assertTrue(is_object($middlePoint)); $this->assertInstanceOf('League\Geotools\Coordinate\Coordinate', $middlePoint); $this->assertInstanceOf('League\Geotools\Coordinate\CoordinateInterface', $middlePoint); $this->assertEquals($expectedMiddlePoint->getLatitude(), $middlePoint->getLatitude()); $this->assertEquals($expectedMiddlePoint->getLongitude(), $middlePoint->getLongitude()); } - - public function fromAndToCoordinatesAndExpectedMiddlePointProvider() + public static function fromAndToCoordinatesAndExpectedMiddlePointProvider() { return array( array( array(48.8234055, 2.3072664), array(43.296482, 5.36978), - $this->getMockCoordinateReturns(array(46.0701431258146, 3.9152401085931)) + new Coordinate([46.0701431258146, 3.9152401085931]) ), array( array('28.8234055', '1.3072664'), array('43.296482', '5.36978'), - $this->getMockCoordinateReturns(array(36.0769359371328, 3.1506401291113)) + new Coordinate([36.0769359371328, 3.1506401291113]) ), array( array(43.296482, 5.36978), array(48.8234055, 2.3072664), - $this->getMockCoordinateReturns(array('46.0701431258146', '3.9152401085931')) + new Coordinate([46.0701431258146, 3.9152401085931]) ), array( array(-13.296482, -5.36978), array(-38.8234055, -4.3072664), - $this->getMockCoordinateReturns(array(-26.0609038494776, -4.8973756901009)) + new Coordinate([-26.0609038494776, -4.8973756901009]) ), ); } - public function testMiddleShouldHaveTheSameEllipsoid() { $FOO = Ellipsoid::createFromArray(array( @@ -286,60 +251,51 @@ public function testMiddleShouldHaveTheSameEllipsoid() 'a' => 123.0, 'invF' => 456.0 )); - $this->vertex->setFrom($this->getMockCoordinateReturns(array(1, 2), $FOO)); $this->vertex->setTo($this->getMockCoordinateReturns(array(3, 4), $FOO)); - $this->assertSame($this->vertex->middle()->getEllipsoid(), $FOO); } - - /** - * @dataProvider fromAndBearingAndDistanceAndExpectedDestinationPoint - */ + #[DataProvider('fromAndBearingAndDistanceAndExpectedDestinationPoint')] public function testDestination($from, $bearing, $distance, $expectedDestinationPoint) { $WGS84 = Ellipsoid::createFromName(Ellipsoid::WGS84); - $this->vertex->setFrom($this->getMockCoordinateReturns($from, $WGS84)); $destinationPoint = $this->vertex->destination($bearing, $distance); - $this->assertTrue(is_object($destinationPoint)); $this->assertInstanceOf('League\Geotools\Coordinate\Coordinate', $destinationPoint); $this->assertInstanceOf('League\Geotools\Coordinate\CoordinateInterface', $destinationPoint); $this->assertEquals($expectedDestinationPoint->getLatitude(), $destinationPoint->getLatitude()); $this->assertEquals($expectedDestinationPoint->getLongitude(), $destinationPoint->getLongitude()); } - - public function fromAndBearingAndDistanceAndExpectedDestinationPoint() + public static function fromAndBearingAndDistanceAndExpectedDestinationPoint() { return array( array( array(48.8234055, 2.3072664), 180, 200000, - $this->getMockCoordinateReturns(array(47.026774931761, 2.3072664)) + new Coordinate([47.026774931761, 2.3072664]) ), array( array('28.8234055', '1.3072664'), 95, 500000, - $this->getMockCoordinateReturns(array(28.3366412434111, 6.3923708101944)) + new Coordinate([28.3366412434111, 6.3923708101944]) ), array( array(43.296482, 5.36978), 37, 3000, - $this->getMockCoordinateReturns(array('43.3180026306153', '5.3920718391258')) + new Coordinate([43.3180026306153, 5.3920718391258]) ), array( array(-13.296482, -5.36978), 166, 5000000, - $this->getMockCoordinateReturns(array(-56.0570895504523, 12.4434647018672)) + new Coordinate([-56.0570895504523, 12.4434647018672]) ), ); } - public function testDestinationShouldHaveTheSameEllipsoid() { $FOO = Ellipsoid::createFromArray(array( @@ -347,15 +303,10 @@ public function testDestinationShouldHaveTheSameEllipsoid() 'a' => 123.0, 'invF' => 456.0 )); - $this->vertex->setFrom($this->getMockCoordinateReturns(array(1, 2), $FOO)); - $this->assertSame($this->vertex->destination(123, 456)->getEllipsoid(), $FOO); } - - /** - * @dataProvider VertexCoordinatesAndExpectedSameLineStatusProvider - */ + #[DataProvider('VertexCoordinatesAndExpectedSameLineStatusProvider')] public function testSameLine($vertexCoordinatesFirst, $vertexCoordinatesSecond, $sameLineStatus) { $this->vertex->setFrom($this->getMockCoordinateReturns($vertexCoordinatesFirst['from'])); @@ -363,14 +314,12 @@ public function testSameLine($vertexCoordinatesFirst, $vertexCoordinatesSecond, $vertexToComp = new Vertex; $vertexToComp->setFrom($this->getMockCoordinateReturns($vertexCoordinatesSecond['from'])); $vertexToComp->setTo($this->getMockCoordinateReturns($vertexCoordinatesSecond['to'])); - $this->assertTrue(is_object($vertexToComp)); $this->assertInstanceOf('League\Geotools\Vertex\Vertex', $this->vertex); $this->assertInstanceOf('League\Geotools\Vertex\Vertex', $vertexToComp); $this->assertEquals($sameLineStatus, $this->vertex->isOnSameLine($vertexToComp)); } - - public function VertexCoordinatesAndExpectedSameLineStatusProvider() + public static function VertexCoordinatesAndExpectedSameLineStatusProvider() { return array( array( @@ -430,20 +379,15 @@ public function VertexCoordinatesAndExpectedSameLineStatusProvider() ), ); } - - /** - * @dataProvider VertexCoordinatesOriginalCoordinatesAndOtherOneProvider - */ + #[DataProvider('VertexCoordinatesOriginalCoordinatesAndOtherOneProvider')] public function testGetOtherCoordinate($vertexCoordinates, $oneCoordinate, $otherCoordinate) { $this->vertex->setFrom($vertexCoordinates['from']); $this->vertex->setTo($vertexCoordinates['to']); - $this->assertInstanceOf('League\Geotools\Vertex\Vertex', $this->vertex); $this->assertEquals($otherCoordinate, $this->vertex->getOtherCoordinate($oneCoordinate)); } - - public function VertexCoordinatesOriginalCoordinatesAndOtherOneProvider() + public static function VertexCoordinatesOriginalCoordinatesAndOtherOneProvider() { return array( array( @@ -472,25 +416,19 @@ public function VertexCoordinatesOriginalCoordinatesAndOtherOneProvider() ), ); } - - /** - * @dataProvider VertexCoordinatesAndExpectedDeterminantValueProvider - */ + #[DataProvider('VertexCoordinatesAndExpectedDeterminantValueProvider')] public function testGetDeterminant($vertexCoordinatesFirst, $vertexCoordinatesSecond, $determinantValue) { $this->vertex->setFrom($this->getMockCoordinateReturns($vertexCoordinatesFirst['from'])); $this->vertex->setTo($this->getMockCoordinateReturns($vertexCoordinatesFirst['to'])); - $vertexToComp = new Vertex; $vertexToComp->setFrom($this->getMockCoordinateReturns($vertexCoordinatesSecond['from'])); $vertexToComp->setTo($this->getMockCoordinateReturns($vertexCoordinatesSecond['to'])); - $this->assertInstanceOf('League\Geotools\Vertex\Vertex', $this->vertex); $this->assertInstanceOf('League\Geotools\Vertex\Vertex', $vertexToComp); $this->assertEquals($determinantValue, $this->vertex->getDeterminant($vertexToComp)); } - - public function VertexCoordinatesAndExpectedDeterminantValueProvider() + public static function VertexCoordinatesAndExpectedDeterminantValueProvider() { return array( array( @@ -539,5 +477,4 @@ public function VertexCoordinatesAndExpectedDeterminantValueProvider() ), ); } - }