From 450db4d668f230da8e1b1f1fe11e3b581f4df6ca Mon Sep 17 00:00:00 2001 From: Matan Yadaev Date: Thu, 29 Feb 2024 00:53:20 +0200 Subject: [PATCH 1/2] migration --- .github/workflows/{php-cs-fixer.yml => pint.yml} | 4 ++-- .php-cs-fixer.php | 13 ------------- composer.json | 5 ++--- 3 files changed, 4 insertions(+), 18 deletions(-) rename .github/workflows/{php-cs-fixer.yml => pint.yml} (78%) delete mode 100644 .php-cs-fixer.php diff --git a/.github/workflows/php-cs-fixer.yml b/.github/workflows/pint.yml similarity index 78% rename from .github/workflows/php-cs-fixer.yml rename to .github/workflows/pint.yml index 66cb0b0..5f431c8 100644 --- a/.github/workflows/php-cs-fixer.yml +++ b/.github/workflows/pint.yml @@ -8,7 +8,7 @@ on: jobs: php-cs-fixer: - name: PHP CS Fixer + name: Pint runs-on: ubuntu-latest @@ -26,4 +26,4 @@ jobs: run: composer install --prefer-dist --no-interaction - name: Run PHP CS Fixer - run: PHP_CS_FIXER_IGNORE_ENV=1 ./vendor/bin/php-cs-fixer fix --allow-risky=yes --diff --dry-run + run: ./vendor/bin/pint --test diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php deleted file mode 100644 index fcec260..0000000 --- a/.php-cs-fixer.php +++ /dev/null @@ -1,13 +0,0 @@ -setFinder( - PhpCsFixer\Finder::create() - ->in(__DIR__) - ) - ->setIndent(' ') - ->setRules([ - '@Laravel' => true, - '@Laravel:risky' => true, - ]) - ->setRiskyAllowed(true); diff --git a/composer.json b/composer.json index 2ca27cb..0048a5f 100644 --- a/composer.json +++ b/composer.json @@ -18,9 +18,8 @@ }, "require-dev": { "doctrine/dbal": "^3.5.3", - "friendsofphp/php-cs-fixer": "^3.14.3", - "jubeki/laravel-code-style": "^1.2", "larastan/larastan": "^1.0|^2.4", + "laravel/pint": "^1.14", "orchestra/testbench": "^8.0", "pestphp/pest": "^2.0", "pestphp/pest-plugin-laravel": "^2.0" @@ -36,7 +35,7 @@ } }, "scripts": { - "php-cs-fixer": "PHP_CS_FIXER_IGNORE_ENV=1 ./vendor/bin/php-cs-fixer fix --allow-risky=yes", + "pint": "./vendor/bin/pint", "phpstan": "./vendor/bin/phpstan analyse --memory-limit=2G", "pest:mysql": "DB_PORT=3307 ./vendor/bin/pest", "pest:postgres": "DB_CONNECTION=pgsql DB_PORT=5433 ./vendor/bin/pest", From 0c237a32191f0333fa5d8de6f71cee7da8cb509c Mon Sep 17 00:00:00 2001 From: Matan Yadaev Date: Thu, 29 Feb 2024 00:53:42 +0200 Subject: [PATCH 2/2] automated fixes --- .editorconfig | 2 +- src/AxisOrder.php | 50 +- src/Doctrine/GeographyType.php | 16 +- src/Doctrine/GeometryCollectionType.php | 16 +- src/Doctrine/GeometryType.php | 16 +- src/Doctrine/LineStringType.php | 16 +- src/Doctrine/MultiLineStringType.php | 16 +- src/Doctrine/MultiPointType.php | 16 +- src/Doctrine/MultiPolygonType.php | 16 +- src/Doctrine/PointType.php | 16 +- src/Doctrine/PolygonType.php | 16 +- src/EloquentSpatialServiceProvider.php | 50 +- src/Enums/Srid.php | 4 +- src/Factory.php | 84 +-- src/GeometryCast.php | 144 ++-- src/GeometryExpression.php | 18 +- src/Objects/Geometry.php | 362 +++++----- src/Objects/GeometryCollection.php | 303 ++++---- src/Objects/LineString.php | 28 +- src/Objects/MultiLineString.php | 65 +- src/Objects/MultiPoint.php | 28 +- src/Objects/MultiPolygon.php | 65 +- src/Objects/Point.php | 66 +- src/Objects/PointCollection.php | 23 +- src/Objects/Polygon.php | 28 +- src/Traits/HasSpatial.php | 586 +++++++-------- tests/DoctrineTypesTest.php | 86 +-- tests/Expectations.php | 8 +- tests/GeometryCastTest.php | 148 ++-- tests/HasSpatialTest.php | 674 +++++++++--------- tests/Objects/GeometryCollectionTest.php | 668 ++++++++--------- tests/Objects/GeometryTest.php | 148 ++-- tests/Objects/LineStringTest.php | 214 +++--- tests/Objects/MultiLineStringTest.php | 266 +++---- tests/Objects/MultiPointTest.php | 182 ++--- tests/Objects/MultiPolygonTest.php | 456 ++++++------ tests/Objects/PointTest.php | 104 +-- tests/Objects/PolygonTest.php | 400 +++++------ tests/TestCase.php | 29 +- tests/TestFactories/TestPlaceFactory.php | 22 +- tests/TestModels/TestPlace.php | 57 +- ..._00_00_000000_create_test_places_table.php | 48 +- 42 files changed, 2762 insertions(+), 2798 deletions(-) diff --git a/.editorconfig b/.editorconfig index 1f70ecd..d672f69 100644 --- a/.editorconfig +++ b/.editorconfig @@ -2,7 +2,7 @@ root = true [*] charset = utf-8 -indent_size = 2 +indent_size = 4 indent_style = space end_of_line = lf insert_final_newline = true diff --git a/src/AxisOrder.php b/src/AxisOrder.php index 030fb37..8926e77 100644 --- a/src/AxisOrder.php +++ b/src/AxisOrder.php @@ -11,37 +11,37 @@ /** @codeCoverageIgnore */ class AxisOrder { - public static function supported(ConnectionInterface $connection): bool - { - if (self::isMariaDb($connection)) { - return false; - } - - if (self::isMySql8OrAbove($connection)) { - return true; - } + public static function supported(ConnectionInterface $connection): bool + { + if (self::isMariaDb($connection)) { + return false; + } - return false; - } + if (self::isMySql8OrAbove($connection)) { + return true; + } - private static function isMariaDb(ConnectionInterface $connection): bool - { - if (! ($connection instanceof MySqlConnection)) { - return false; + return false; } - return $connection->isMaria(); - } + private static function isMariaDb(ConnectionInterface $connection): bool + { + if (! ($connection instanceof MySqlConnection)) { + return false; + } - private static function isMySql8OrAbove(ConnectionInterface $connection): bool - { - if (! ($connection instanceof MySqlConnection)) { - return false; + return $connection->isMaria(); } - /** @var string $version */ - $version = $connection->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION); + private static function isMySql8OrAbove(ConnectionInterface $connection): bool + { + if (! ($connection instanceof MySqlConnection)) { + return false; + } - return version_compare($version, '8.0.0', '>='); - } + /** @var string $version */ + $version = $connection->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION); + + return version_compare($version, '8.0.0', '>='); + } } diff --git a/src/Doctrine/GeographyType.php b/src/Doctrine/GeographyType.php index a65508a..035e315 100644 --- a/src/Doctrine/GeographyType.php +++ b/src/Doctrine/GeographyType.php @@ -10,13 +10,13 @@ /** @codeCoverageIgnore */ class GeographyType extends Type { - public function getSQLDeclaration(array $column, AbstractPlatform $platform): string - { - return 'geography'; - } + public function getSQLDeclaration(array $column, AbstractPlatform $platform): string + { + return 'geography'; + } - public function getName(): string - { - return 'geography'; - } + public function getName(): string + { + return 'geography'; + } } diff --git a/src/Doctrine/GeometryCollectionType.php b/src/Doctrine/GeometryCollectionType.php index 185c601..5ed9031 100644 --- a/src/Doctrine/GeometryCollectionType.php +++ b/src/Doctrine/GeometryCollectionType.php @@ -10,13 +10,13 @@ /** @codeCoverageIgnore */ class GeometryCollectionType extends Type { - public function getSQLDeclaration(array $column, AbstractPlatform $platform): string - { - return 'geometrycollection'; - } + public function getSQLDeclaration(array $column, AbstractPlatform $platform): string + { + return 'geometrycollection'; + } - public function getName(): string - { - return 'geometrycollection'; - } + public function getName(): string + { + return 'geometrycollection'; + } } diff --git a/src/Doctrine/GeometryType.php b/src/Doctrine/GeometryType.php index cd419ab..eedb51b 100644 --- a/src/Doctrine/GeometryType.php +++ b/src/Doctrine/GeometryType.php @@ -10,13 +10,13 @@ /** @codeCoverageIgnore */ class GeometryType extends Type { - public function getSQLDeclaration(array $column, AbstractPlatform $platform): string - { - return 'geometry'; - } + public function getSQLDeclaration(array $column, AbstractPlatform $platform): string + { + return 'geometry'; + } - public function getName(): string - { - return 'geometry'; - } + public function getName(): string + { + return 'geometry'; + } } diff --git a/src/Doctrine/LineStringType.php b/src/Doctrine/LineStringType.php index cb01883..bd1a25d 100644 --- a/src/Doctrine/LineStringType.php +++ b/src/Doctrine/LineStringType.php @@ -10,13 +10,13 @@ /** @codeCoverageIgnore */ class LineStringType extends Type { - public function getSQLDeclaration(array $column, AbstractPlatform $platform): string - { - return 'linestring'; - } + public function getSQLDeclaration(array $column, AbstractPlatform $platform): string + { + return 'linestring'; + } - public function getName(): string - { - return 'linestring'; - } + public function getName(): string + { + return 'linestring'; + } } diff --git a/src/Doctrine/MultiLineStringType.php b/src/Doctrine/MultiLineStringType.php index 7f84126..deda585 100644 --- a/src/Doctrine/MultiLineStringType.php +++ b/src/Doctrine/MultiLineStringType.php @@ -10,13 +10,13 @@ /** @codeCoverageIgnore */ class MultiLineStringType extends Type { - public function getSQLDeclaration(array $column, AbstractPlatform $platform): string - { - return 'multilinestring'; - } + public function getSQLDeclaration(array $column, AbstractPlatform $platform): string + { + return 'multilinestring'; + } - public function getName(): string - { - return 'multilinestring'; - } + public function getName(): string + { + return 'multilinestring'; + } } diff --git a/src/Doctrine/MultiPointType.php b/src/Doctrine/MultiPointType.php index 3418017..621299e 100644 --- a/src/Doctrine/MultiPointType.php +++ b/src/Doctrine/MultiPointType.php @@ -10,13 +10,13 @@ /** @codeCoverageIgnore */ class MultiPointType extends Type { - public function getSQLDeclaration(array $column, AbstractPlatform $platform): string - { - return 'multipoint'; - } + public function getSQLDeclaration(array $column, AbstractPlatform $platform): string + { + return 'multipoint'; + } - public function getName(): string - { - return 'multipoint'; - } + public function getName(): string + { + return 'multipoint'; + } } diff --git a/src/Doctrine/MultiPolygonType.php b/src/Doctrine/MultiPolygonType.php index 4d0931a..52491fa 100644 --- a/src/Doctrine/MultiPolygonType.php +++ b/src/Doctrine/MultiPolygonType.php @@ -10,13 +10,13 @@ /** @codeCoverageIgnore */ class MultiPolygonType extends Type { - public function getSQLDeclaration(array $column, AbstractPlatform $platform): string - { - return 'multipolygon'; - } + public function getSQLDeclaration(array $column, AbstractPlatform $platform): string + { + return 'multipolygon'; + } - public function getName(): string - { - return 'multipolygon'; - } + public function getName(): string + { + return 'multipolygon'; + } } diff --git a/src/Doctrine/PointType.php b/src/Doctrine/PointType.php index d358d5c..c577325 100644 --- a/src/Doctrine/PointType.php +++ b/src/Doctrine/PointType.php @@ -10,13 +10,13 @@ /** @codeCoverageIgnore */ class PointType extends Type { - public function getSQLDeclaration(array $column, AbstractPlatform $platform): string - { - return 'point'; - } + public function getSQLDeclaration(array $column, AbstractPlatform $platform): string + { + return 'point'; + } - public function getName(): string - { - return 'point'; - } + public function getName(): string + { + return 'point'; + } } diff --git a/src/Doctrine/PolygonType.php b/src/Doctrine/PolygonType.php index 63fdba2..0709274 100644 --- a/src/Doctrine/PolygonType.php +++ b/src/Doctrine/PolygonType.php @@ -10,13 +10,13 @@ /** @codeCoverageIgnore */ class PolygonType extends Type { - public function getSQLDeclaration(array $column, AbstractPlatform $platform): string - { - return 'polygon'; - } + public function getSQLDeclaration(array $column, AbstractPlatform $platform): string + { + return 'polygon'; + } - public function getName(): string - { - return 'polygon'; - } + public function getName(): string + { + return 'polygon'; + } } diff --git a/src/EloquentSpatialServiceProvider.php b/src/EloquentSpatialServiceProvider.php index a30bec0..e86975b 100644 --- a/src/EloquentSpatialServiceProvider.php +++ b/src/EloquentSpatialServiceProvider.php @@ -19,34 +19,34 @@ class EloquentSpatialServiceProvider extends DatabaseServiceProvider { - public function boot(): void - { - /** @var Connection $connection */ - $connection = DB::connection(); + public function boot(): void + { + /** @var Connection $connection */ + $connection = DB::connection(); - if ($connection->isDoctrineAvailable()) { - $this->registerDoctrineTypes($connection); + if ($connection->isDoctrineAvailable()) { + $this->registerDoctrineTypes($connection); + } } - } - protected function registerDoctrineTypes(Connection $connection): void - { - $geometries = [ - 'point' => PointType::class, - 'linestring' => LineStringType::class, - 'multipoint' => MultiPointType::class, - 'polygon' => PolygonType::class, - 'multilinestring' => MultiLineStringType::class, - 'multipolygon' => MultiPolygonType::class, - 'geometrycollection' => GeometryCollectionType::class, - 'geomcollection' => GeometryCollectionType::class, - 'geography' => GeographyType::class, - 'geometry' => GeometryType::class, - ]; + protected function registerDoctrineTypes(Connection $connection): void + { + $geometries = [ + 'point' => PointType::class, + 'linestring' => LineStringType::class, + 'multipoint' => MultiPointType::class, + 'polygon' => PolygonType::class, + 'multilinestring' => MultiLineStringType::class, + 'multipolygon' => MultiPolygonType::class, + 'geometrycollection' => GeometryCollectionType::class, + 'geomcollection' => GeometryCollectionType::class, + 'geography' => GeographyType::class, + 'geometry' => GeometryType::class, + ]; - foreach ($geometries as $type => $class) { - DB::registerDoctrineType($class, $type, $type); - $connection->registerDoctrineType($class, $type, $type); + foreach ($geometries as $type => $class) { + DB::registerDoctrineType($class, $type, $type); + $connection->registerDoctrineType($class, $type, $type); + } } - } } diff --git a/src/Enums/Srid.php b/src/Enums/Srid.php index d65c9c0..682b5b1 100644 --- a/src/Enums/Srid.php +++ b/src/Enums/Srid.php @@ -4,6 +4,6 @@ enum Srid: int { - case WGS84 = 4326; // https://epsg.org/crs_4326/WGS-84.html - case WEB_MERCATOR = 3857; //https://epsg.org/crs_3857/WGS-84-Pseudo-Mercator.html + case WGS84 = 4326; // https://epsg.org/crs_4326/WGS-84.html + case WEB_MERCATOR = 3857; //https://epsg.org/crs_3857/WGS-84-Pseudo-Mercator.html } diff --git a/src/Factory.php b/src/Factory.php index f946a03..49e0ef3 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -25,58 +25,58 @@ class Factory { - public static function parse(string $value): Geometry - { - try { - /** @var geoPHPGeometry|false $geoPHPGeometry */ - $geoPHPGeometry = geoPHP::load($value); - } finally { - if (! isset($geoPHPGeometry) || ! $geoPHPGeometry) { - throw new InvalidArgumentException('Invalid spatial value'); - } + public static function parse(string $value): Geometry + { + try { + /** @var geoPHPGeometry|false $geoPHPGeometry */ + $geoPHPGeometry = geoPHP::load($value); + } finally { + if (! isset($geoPHPGeometry) || ! $geoPHPGeometry) { + throw new InvalidArgumentException('Invalid spatial value'); + } + } + + return self::createFromGeometry($geoPHPGeometry); } - return self::createFromGeometry($geoPHPGeometry); - } + protected static function createFromGeometry(geoPHPGeometry $geometry): Geometry + { + $srid = is_int($geometry->getSRID()) ? $geometry->getSRID() : 0; - protected static function createFromGeometry(geoPHPGeometry $geometry): Geometry - { - $srid = is_int($geometry->getSRID()) ? $geometry->getSRID() : 0; + if ($geometry instanceof geoPHPPoint) { + if ($geometry->coords[0] === null || $geometry->coords[1] === null) { + throw new InvalidArgumentException('Invalid spatial value'); + } - if ($geometry instanceof geoPHPPoint) { - if ($geometry->coords[0] === null || $geometry->coords[1] === null) { - throw new InvalidArgumentException('Invalid spatial value'); - } + return new Point($geometry->coords[1], $geometry->coords[0], $srid); + } - return new Point($geometry->coords[1], $geometry->coords[0], $srid); - } + /** @var geoPHPGeometryCollection $geometry */ + $components = collect($geometry->components) + ->map(static function (geoPHPGeometry $geometryComponent): Geometry { + return self::createFromGeometry($geometryComponent); + }); - /** @var geoPHPGeometryCollection $geometry */ - $components = collect($geometry->components) - ->map(static function (geoPHPGeometry $geometryComponent): Geometry { - return self::createFromGeometry($geometryComponent); - }); + if ($geometry::class === geoPHPMultiPoint::class) { + return new MultiPoint($components, $srid); + } - if ($geometry::class === geoPHPMultiPoint::class) { - return new MultiPoint($components, $srid); - } + if ($geometry::class === geoPHPLineString::class) { + return new LineString($components, $srid); + } - if ($geometry::class === geoPHPLineString::class) { - return new LineString($components, $srid); - } + if ($geometry::class === geoPHPPolygon::class) { + return new Polygon($components, $srid); + } - if ($geometry::class === geoPHPPolygon::class) { - return new Polygon($components, $srid); - } + if ($geometry::class === geoPHPMultiLineString::class) { + return new MultiLineString($components, $srid); + } - if ($geometry::class === geoPHPMultiLineString::class) { - return new MultiLineString($components, $srid); - } + if ($geometry::class === geoPHPMultiPolygon::class) { + return new MultiPolygon($components, $srid); + } - if ($geometry::class === geoPHPMultiPolygon::class) { - return new MultiPolygon($components, $srid); + return new GeometryCollection($components, $srid); } - - return new GeometryCollection($components, $srid); - } } diff --git a/src/GeometryCast.php b/src/GeometryCast.php index dd4d665..a994059 100644 --- a/src/GeometryCast.php +++ b/src/GeometryCast.php @@ -13,90 +13,86 @@ class GeometryCast implements CastsAttributes { - /** @var class-string */ - private string $className; - - /** - * @param class-string $className - */ - public function __construct(string $className) - { - $this->className = $className; - } - - /** - * @param Model $model - * @param string $key - * @param string|ExpressionContract|null $value - * @param array $attributes - * @return Geometry|null - */ - public function get($model, string $key, $value, array $attributes): ?Geometry - { - if (! $value) { - return null; + /** @var class-string */ + private string $className; + + /** + * @param class-string $className + */ + public function __construct(string $className) + { + $this->className = $className; } - if ($value instanceof ExpressionContract) { - $wkt = $this->extractWktFromExpression($value, $model->getConnection()); - $srid = $this->extractSridFromExpression($value, $model->getConnection()); - - return $this->className::fromWkt($wkt, $srid); + /** + * @param Model $model + * @param string|ExpressionContract|null $value + * @param array $attributes + */ + public function get($model, string $key, $value, array $attributes): ?Geometry + { + if (! $value) { + return null; + } + + if ($value instanceof ExpressionContract) { + $wkt = $this->extractWktFromExpression($value, $model->getConnection()); + $srid = $this->extractSridFromExpression($value, $model->getConnection()); + + return $this->className::fromWkt($wkt, $srid); + } + + return $this->className::fromWkb($value); } - return $this->className::fromWkb($value); - } - - /** - * @param Model $model - * @param string $key - * @param Geometry|mixed|null $value - * @param array $attributes - * @return ExpressionContract|null - * - * @throws InvalidArgumentException - */ - public function set($model, string $key, $value, array $attributes): ExpressionContract|null - { - if (! $value) { - return null; + /** + * @param Model $model + * @param Geometry|mixed|null $value + * @param array $attributes + * + * @throws InvalidArgumentException + */ + public function set($model, string $key, $value, array $attributes): ?ExpressionContract + { + if (! $value) { + return null; + } + + if (is_array($value)) { + $value = Geometry::fromArray($value); + } + + if ($value instanceof ExpressionContract) { + return $value; + } + + if (! ($value instanceof $this->className)) { + $geometryType = is_object($value) ? $value::class : gettype($value); + throw new InvalidArgumentException( + sprintf('Expected %s, %s given.', static::class, $geometryType) + ); + } + + return $value->toSqlExpression($model->getConnection()); } - if (is_array($value)) { - $value = Geometry::fromArray($value); - } + private function extractWktFromExpression(ExpressionContract $expression, Connection $connection): string + { + $grammar = $connection->getQueryGrammar(); + $expressionValue = $expression->getValue($grammar); - if ($value instanceof ExpressionContract) { - return $value; - } + preg_match('/ST_GeomFromText\(\'(.+)\', .+(, .+)?\)/', (string) $expressionValue, $match); - if (! ($value instanceof $this->className)) { - $geometryType = is_object($value) ? $value::class : gettype($value); - throw new InvalidArgumentException( - sprintf('Expected %s, %s given.', static::class, $geometryType) - ); + return $match[1]; } - return $value->toSqlExpression($model->getConnection()); - } - - private function extractWktFromExpression(ExpressionContract $expression, Connection $connection): string - { - $grammar = $connection->getQueryGrammar(); - $expressionValue = $expression->getValue($grammar); + private function extractSridFromExpression(ExpressionContract $expression, Connection $connection): int + { + $grammar = $connection->getQueryGrammar(); + $expressionValue = $expression->getValue($grammar); - preg_match('/ST_GeomFromText\(\'(.+)\', .+(, .+)?\)/', (string) $expressionValue, $match); + preg_match('/ST_GeomFromText\(\'.+\', (.+)(, .+)?\)/', (string) $expressionValue, $match); - return $match[1]; - } - - private function extractSridFromExpression(ExpressionContract $expression, Connection $connection): int - { - $grammar = $connection->getQueryGrammar(); - $expressionValue = $expression->getValue($grammar); - - preg_match('/ST_GeomFromText\(\'.+\', (.+)(, .+)?\)/', (string) $expressionValue, $match); - - return (int) $match[1]; - } + return (int) $match[1]; + } } diff --git a/src/GeometryExpression.php b/src/GeometryExpression.php index d1d7ffd..2b4fed7 100644 --- a/src/GeometryExpression.php +++ b/src/GeometryExpression.php @@ -10,14 +10,14 @@ /** @codeCoverageIgnore */ class GeometryExpression { - public function __construct(readonly private string $expression) - { - } + public function __construct(readonly private string $expression) + { + } - public function normalize(ConnectionInterface $connection): string - { - return $connection instanceof PostgresConnection - ? $this->expression.'::geometry' - : $this->expression; - } + public function normalize(ConnectionInterface $connection): string + { + return $connection instanceof PostgresConnection + ? $this->expression.'::geometry' + : $this->expression; + } } diff --git a/src/Objects/Geometry.php b/src/Objects/Geometry.php index c09fa8a..864b8a8 100644 --- a/src/Objects/Geometry.php +++ b/src/Objects/Geometry.php @@ -24,210 +24,186 @@ use Stringable; use WKB as geoPHPWkb; -abstract class Geometry implements Castable, Arrayable, Jsonable, JsonSerializable, Stringable +abstract class Geometry implements Arrayable, Castable, Jsonable, JsonSerializable, Stringable { - use Macroable; - - public int $srid = 0; - - abstract public function toWkt(): string; - - abstract public function getWktData(): string; - - /** - * @return string - */ - public function __toString(): string - { - return $this->toWkt(); - } - - /** - * @param int $options - * @return string - * - * @throws JsonException - */ - public function toJson($options = 0): string - { - return json_encode($this, $options | JSON_THROW_ON_ERROR); - } - - public function toWkb(): string - { - $geoPHPGeometry = geoPHP::load($this->toJson()); - - $sridInBinary = pack('L', $this->srid); - - // @phpstan-ignore-next-line - $wkbWithoutSrid = (new geoPHPWkb)->write($geoPHPGeometry); - - return $sridInBinary.$wkbWithoutSrid; - } - - /** - * @param string $wkb - * @return static - */ - public static function fromWkb(string $wkb): static - { - if (ctype_xdigit($wkb)) { - // @codeCoverageIgnoreStart - $geometry = Factory::parse($wkb); - // @codeCoverageIgnoreEnd - } else { - $srid = substr($wkb, 0, 4); - // @phpstan-ignore-next-line - $srid = unpack('L', $srid)[1]; - - $wkb = substr($wkb, 4); - - $geometry = Factory::parse($wkb); - $geometry->srid = $srid; + use Macroable; + + public int $srid = 0; + + abstract public function toWkt(): string; + + abstract public function getWktData(): string; + + public function __toString(): string + { + return $this->toWkt(); } - if (! ($geometry instanceof static)) { - throw new InvalidArgumentException( - sprintf('Expected %s, %s given.', static::class, $geometry::class) - ); + /** + * @param int $options + * + * @throws JsonException + */ + public function toJson($options = 0): string + { + return json_encode($this, $options | JSON_THROW_ON_ERROR); } - return $geometry; - } - - /** - * @param string $wkt - * @param int $srid - * @return static - * - * @throws InvalidArgumentException - */ - public static function fromWkt(string $wkt, int|Srid $srid = 0): static - { - $geometry = Factory::parse($wkt); - $geometry->srid = $srid instanceof Srid ? $srid->value : $srid; - - if (! ($geometry instanceof static)) { - throw new InvalidArgumentException( - sprintf('Expected %s, %s given.', static::class, $geometry::class) - ); + public function toWkb(): string + { + $geoPHPGeometry = geoPHP::load($this->toJson()); + + $sridInBinary = pack('L', $this->srid); + + // @phpstan-ignore-next-line + $wkbWithoutSrid = (new geoPHPWkb)->write($geoPHPGeometry); + + return $sridInBinary.$wkbWithoutSrid; } - return $geometry; - } - - /** - * @param string $geoJson - * @param int $srid - * @return static - * - * @throws InvalidArgumentException - */ - public static function fromJson(string $geoJson, int|Srid $srid = 0): static - { - $geometry = Factory::parse($geoJson); - $geometry->srid = $srid instanceof Srid ? $srid->value : $srid; - - if (! ($geometry instanceof static)) { - throw new InvalidArgumentException( - sprintf('Expected %s, %s given.', static::class, $geometry::class) - ); + public static function fromWkb(string $wkb): static + { + if (ctype_xdigit($wkb)) { + // @codeCoverageIgnoreStart + $geometry = Factory::parse($wkb); + // @codeCoverageIgnoreEnd + } else { + $srid = substr($wkb, 0, 4); + // @phpstan-ignore-next-line + $srid = unpack('L', $srid)[1]; + + $wkb = substr($wkb, 4); + + $geometry = Factory::parse($wkb); + $geometry->srid = $srid; + } + + if (! ($geometry instanceof static)) { + throw new InvalidArgumentException( + sprintf('Expected %s, %s given.', static::class, $geometry::class) + ); + } + + return $geometry; } - return $geometry; - } - - /** - * @param array $geometry - * @return static - * - * @throws JsonException - */ - public static function fromArray(array $geometry): static - { - $geoJson = json_encode($geometry, JSON_THROW_ON_ERROR); - - return static::fromJson($geoJson); - } - - /** - * @return array - */ - public function jsonSerialize(): array - { - return $this->toArray(); - } - - /** - * @return array{type: string, coordinates: array} - */ - public function toArray(): array - { - return [ - 'type' => class_basename(static::class), - 'coordinates' => $this->getCoordinates(), - ]; - } - - /** - * @return string - * - * @throws JsonException - */ - public function toFeatureCollectionJson(): string - { - if (static::class === GeometryCollection::class) { - /** @var GeometryCollection $this */ - $geometries = $this->geometries; - } else { - $geometries = collect([$this]); + /** + * @throws InvalidArgumentException + */ + public static function fromWkt(string $wkt, int|Srid $srid = 0): static + { + $geometry = Factory::parse($wkt); + $geometry->srid = $srid instanceof Srid ? $srid->value : $srid; + + if (! ($geometry instanceof static)) { + throw new InvalidArgumentException( + sprintf('Expected %s, %s given.', static::class, $geometry::class) + ); + } + + return $geometry; } - $features = $geometries->map(static function (self $geometry): array { - return [ - 'type' => 'Feature', - 'properties' => [], - 'geometry' => $geometry->toArray(), - ]; - }); - - return json_encode( - [ - 'type' => 'FeatureCollection', - 'features' => $features, - ], - JSON_THROW_ON_ERROR - ); - } - - /** - * @return array - */ - abstract public function getCoordinates(): array; - - /** - * @param array $arguments - * @return CastsAttributes - */ - public static function castUsing(array $arguments): CastsAttributes - { - return new GeometryCast(static::class); - } - - /** - * @param ConnectionInterface $connection - * @return ExpressionContract - */ - public function toSqlExpression(ConnectionInterface $connection): ExpressionContract - { - $wkt = $this->toWkt(); - - if (! AxisOrder::supported($connection)) { - // @codeCoverageIgnoreStart - return DB::raw((new GeometryExpression("ST_GeomFromText('{$wkt}', {$this->srid})"))->normalize($connection)); - // @codeCoverageIgnoreEnd + /** + * @throws InvalidArgumentException + */ + public static function fromJson(string $geoJson, int|Srid $srid = 0): static + { + $geometry = Factory::parse($geoJson); + $geometry->srid = $srid instanceof Srid ? $srid->value : $srid; + + if (! ($geometry instanceof static)) { + throw new InvalidArgumentException( + sprintf('Expected %s, %s given.', static::class, $geometry::class) + ); + } + + return $geometry; } - return DB::raw((new GeometryExpression("ST_GeomFromText('{$wkt}', {$this->srid}, 'axis-order=long-lat')"))->normalize($connection)); - } + /** + * @param array $geometry + * + * @throws JsonException + */ + public static function fromArray(array $geometry): static + { + $geoJson = json_encode($geometry, JSON_THROW_ON_ERROR); + + return static::fromJson($geoJson); + } + + /** + * @return array + */ + public function jsonSerialize(): array + { + return $this->toArray(); + } + + /** + * @return array{type: string, coordinates: array} + */ + public function toArray(): array + { + return [ + 'type' => class_basename(static::class), + 'coordinates' => $this->getCoordinates(), + ]; + } + + /** + * @throws JsonException + */ + public function toFeatureCollectionJson(): string + { + if (static::class === GeometryCollection::class) { + /** @var GeometryCollection $this */ + $geometries = $this->geometries; + } else { + $geometries = collect([$this]); + } + + $features = $geometries->map(static function (self $geometry): array { + return [ + 'type' => 'Feature', + 'properties' => [], + 'geometry' => $geometry->toArray(), + ]; + }); + + return json_encode( + [ + 'type' => 'FeatureCollection', + 'features' => $features, + ], + JSON_THROW_ON_ERROR + ); + } + + /** + * @return array + */ + abstract public function getCoordinates(): array; + + /** + * @param array $arguments + */ + public static function castUsing(array $arguments): CastsAttributes + { + return new GeometryCast(static::class); + } + + public function toSqlExpression(ConnectionInterface $connection): ExpressionContract + { + $wkt = $this->toWkt(); + + if (! AxisOrder::supported($connection)) { + // @codeCoverageIgnoreStart + return DB::raw((new GeometryExpression("ST_GeomFromText('{$wkt}', {$this->srid})"))->normalize($connection)); + // @codeCoverageIgnoreEnd + } + + return DB::raw((new GeometryExpression("ST_GeomFromText('{$wkt}', {$this->srid}, 'axis-order=long-lat')"))->normalize($connection)); + } } diff --git a/src/Objects/GeometryCollection.php b/src/Objects/GeometryCollection.php index a46bf56..13b339b 100644 --- a/src/Objects/GeometryCollection.php +++ b/src/Objects/GeometryCollection.php @@ -12,163 +12,158 @@ class GeometryCollection extends Geometry implements ArrayAccess { - /** @var Collection */ - protected Collection $geometries; - - protected string $collectionOf = Geometry::class; - - protected int $minimumGeometries = 0; - - /** - * @param Collection|array $geometries - * @param int $srid - * - * @throws InvalidArgumentException - */ - public function __construct(Collection|array $geometries, int|Srid $srid = 0) - { - if (is_array($geometries)) { - $geometries = collect($geometries); + /** @var Collection */ + protected Collection $geometries; + + protected string $collectionOf = Geometry::class; + + protected int $minimumGeometries = 0; + + /** + * @param Collection|array $geometries + * + * @throws InvalidArgumentException + */ + public function __construct(Collection|array $geometries, int|Srid $srid = 0) + { + if (is_array($geometries)) { + $geometries = collect($geometries); + } + + $this->geometries = $geometries; + $this->srid = $srid instanceof Srid ? $srid->value : $srid; + + $this->validateGeometriesType(); + $this->validateGeometriesCount(); + } + + public function toWkt(): string + { + $wktData = $this->getWktData(); + + return "GEOMETRYCOLLECTION({$wktData})"; + } + + public function getWktData(): string + { + return $this->geometries + ->map(static function (Geometry $geometry): string { + return $geometry->toWkt(); + }) + ->join(', '); + } + + /** + * @return array + */ + public function getCoordinates(): array + { + return $this->geometries + ->map(static function (Geometry $geometry): array { + return $geometry->getCoordinates(); + }) + ->all(); + } + + /** + * @return array + */ + public function toArray(): array + { + if ($this->isExtended()) { + return parent::toArray(); + } + + return [ + 'type' => class_basename(static::class), + 'geometries' => $this->geometries->map(static function (Geometry $geometry): array { + return $geometry->toArray(); + }), + ]; + } + + /** + * @return Collection + */ + public function getGeometries(): Collection + { + return new Collection($this->geometries->all()); + } + + /** + * @param int $offset + */ + public function offsetExists($offset): bool + { + return isset($this->geometries[$offset]); + } + + /** + * @param int $offset + */ + public function offsetGet($offset): Geometry + { + // @phpstan-ignore-next-line + return $this->geometries[$offset]; + } + + /** + * @param int $offset + * @param Geometry $value + */ + public function offsetSet($offset, $value): void + { + $this->geometries[$offset] = $value; + $this->validateGeometriesType(); + } + + /** + * @param int $offset + */ + public function offsetUnset($offset): void + { + $this->geometries->splice($offset, 1); + $this->validateGeometriesCount(); + } + + /** + * @throws InvalidArgumentException + */ + protected function validateGeometriesCount(): void + { + $geometriesCount = $this->geometries->count(); + if ($geometriesCount < $this->minimumGeometries) { + throw new InvalidArgumentException( + sprintf( + '%s must contain at least %s %s', + static::class, + $this->minimumGeometries, + Str::plural('entries', $geometriesCount) + ) + ); + } } - $this->geometries = $geometries; - $this->srid = $srid instanceof Srid ? $srid->value : $srid; - - $this->validateGeometriesType(); - $this->validateGeometriesCount(); - } - - public function toWkt(): string - { - $wktData = $this->getWktData(); - - return "GEOMETRYCOLLECTION({$wktData})"; - } - - public function getWktData(): string - { - return $this->geometries - ->map(static function (Geometry $geometry): string { - return $geometry->toWkt(); - }) - ->join(', '); - } - - /** - * @return array - */ - public function getCoordinates(): array - { - return $this->geometries - ->map(static function (Geometry $geometry): array { - return $geometry->getCoordinates(); - }) - ->all(); - } - - /** - * @return array - */ - public function toArray(): array - { - if ($this->isExtended()) { - return parent::toArray(); + /** + * @throws InvalidArgumentException + */ + protected function validateGeometriesType(): void + { + $this->geometries->each(function (mixed $geometry): void { + /** @var mixed $geometry */ + if (! is_object($geometry) || ! ($geometry instanceof $this->collectionOf)) { + throw new InvalidArgumentException( + sprintf('%s must be a collection of %s', static::class, $this->collectionOf) + ); + } + }); } - return [ - 'type' => class_basename(static::class), - 'geometries' => $this->geometries->map(static function (Geometry $geometry): array { - return $geometry->toArray(); - }), - ]; - } - - /** - * @return Collection - */ - public function getGeometries(): Collection - { - return new Collection($this->geometries->all()); - } - - /** - * @param int $offset - * @return bool - */ - public function offsetExists($offset): bool - { - return isset($this->geometries[$offset]); - } - - /** - * @param int $offset - * @return Geometry - */ - public function offsetGet($offset): Geometry - { - // @phpstan-ignore-next-line - return $this->geometries[$offset]; - } - - /** - * @param int $offset - * @param Geometry $value - */ - public function offsetSet($offset, $value): void - { - $this->geometries[$offset] = $value; - $this->validateGeometriesType(); - } - - /** - * @param int $offset - */ - public function offsetUnset($offset): void - { - $this->geometries->splice($offset, 1); - $this->validateGeometriesCount(); - } - - /** - * @throws InvalidArgumentException - */ - protected function validateGeometriesCount(): void - { - $geometriesCount = $this->geometries->count(); - if ($geometriesCount < $this->minimumGeometries) { - throw new InvalidArgumentException( - sprintf( - '%s must contain at least %s %s', - static::class, - $this->minimumGeometries, - Str::plural('entries', $geometriesCount) - ) - ); + /** + * Checks whether the class is used directly or via a sub-class. + */ + private function isExtended(): bool + { + return is_subclass_of(static::class, self::class); } - } - - /** - * @throws InvalidArgumentException - */ - protected function validateGeometriesType(): void - { - $this->geometries->each(function (mixed $geometry): void { - /** @var mixed $geometry */ - if (! is_object($geometry) || ! ($geometry instanceof $this->collectionOf)) { - throw new InvalidArgumentException( - sprintf('%s must be a collection of %s', static::class, $this->collectionOf) - ); - } - }); - } - - /** - * Checks whether the class is used directly or via a sub-class. - * - * @return bool - */ - private function isExtended(): bool - { - return is_subclass_of(static::class, self::class); - } } diff --git a/src/Objects/LineString.php b/src/Objects/LineString.php index da6a8bc..d34b96f 100644 --- a/src/Objects/LineString.php +++ b/src/Objects/LineString.php @@ -6,21 +6,21 @@ class LineString extends PointCollection { - protected int $minimumGeometries = 2; + protected int $minimumGeometries = 2; - public function toWkt(): string - { - $wktData = $this->getWktData(); + public function toWkt(): string + { + $wktData = $this->getWktData(); - return "LINESTRING({$wktData})"; - } + return "LINESTRING({$wktData})"; + } - public function getWktData(): string - { - return $this->geometries - ->map(static function (Point $point): string { - return $point->getWktData(); - }) - ->join(', '); - } + public function getWktData(): string + { + return $this->geometries + ->map(static function (Point $point): string { + return $point->getWktData(); + }) + ->join(', '); + } } diff --git a/src/Objects/MultiLineString.php b/src/Objects/MultiLineString.php index c5ed380..1a370d9 100644 --- a/src/Objects/MultiLineString.php +++ b/src/Objects/MultiLineString.php @@ -17,37 +17,36 @@ */ class MultiLineString extends GeometryCollection { - protected string $collectionOf = LineString::class; - - protected int $minimumGeometries = 1; - - /** - * @param Collection|array $geometries - * @param int $srid - * - * @throws InvalidArgumentException - */ - public function __construct(Collection|array $geometries, int|Srid $srid = 0) - { - // @phpstan-ignore-next-line - parent::__construct($geometries, $this->srid = $srid instanceof Srid ? $srid->value : $srid); - } - - public function toWkt(): string - { - $wktData = $this->getWktData(); - - return "MULTILINESTRING({$wktData})"; - } - - public function getWktData(): string - { - return $this->geometries - ->map(static function (LineString $lineString): string { - $wktData = $lineString->getWktData(); - - return "({$wktData})"; - }) - ->join(', '); - } + protected string $collectionOf = LineString::class; + + protected int $minimumGeometries = 1; + + /** + * @param Collection|array $geometries + * + * @throws InvalidArgumentException + */ + public function __construct(Collection|array $geometries, int|Srid $srid = 0) + { + // @phpstan-ignore-next-line + parent::__construct($geometries, $this->srid = $srid instanceof Srid ? $srid->value : $srid); + } + + public function toWkt(): string + { + $wktData = $this->getWktData(); + + return "MULTILINESTRING({$wktData})"; + } + + public function getWktData(): string + { + return $this->geometries + ->map(static function (LineString $lineString): string { + $wktData = $lineString->getWktData(); + + return "({$wktData})"; + }) + ->join(', '); + } } diff --git a/src/Objects/MultiPoint.php b/src/Objects/MultiPoint.php index dec047b..7a783e2 100644 --- a/src/Objects/MultiPoint.php +++ b/src/Objects/MultiPoint.php @@ -6,21 +6,21 @@ class MultiPoint extends PointCollection { - protected int $minimumGeometries = 1; + protected int $minimumGeometries = 1; - public function toWkt(): string - { - $wktData = $this->getWktData(); + public function toWkt(): string + { + $wktData = $this->getWktData(); - return "MULTIPOINT({$wktData})"; - } + return "MULTIPOINT({$wktData})"; + } - public function getWktData(): string - { - return $this->geometries - ->map(static function (Point $point): string { - return $point->getWktData(); - }) - ->join(', '); - } + public function getWktData(): string + { + return $this->geometries + ->map(static function (Point $point): string { + return $point->getWktData(); + }) + ->join(', '); + } } diff --git a/src/Objects/MultiPolygon.php b/src/Objects/MultiPolygon.php index 66d5cd5..2a1f40a 100644 --- a/src/Objects/MultiPolygon.php +++ b/src/Objects/MultiPolygon.php @@ -17,37 +17,36 @@ */ class MultiPolygon extends GeometryCollection { - protected string $collectionOf = Polygon::class; - - protected int $minimumGeometries = 1; - - /** - * @param Collection|array $geometries - * @param int $srid - * - * @throws InvalidArgumentException - */ - public function __construct(Collection|array $geometries, int|Srid $srid = 0) - { - // @phpstan-ignore-next-line - parent::__construct($geometries, $this->srid = $srid instanceof Srid ? $srid->value : $srid); - } - - public function toWkt(): string - { - $wktData = $this->getWktData(); - - return "MULTIPOLYGON({$wktData})"; - } - - public function getWktData(): string - { - return $this->geometries - ->map(static function (Polygon $polygon): string { - $wktData = $polygon->getWktData(); - - return "({$wktData})"; - }) - ->join(', '); - } + protected string $collectionOf = Polygon::class; + + protected int $minimumGeometries = 1; + + /** + * @param Collection|array $geometries + * + * @throws InvalidArgumentException + */ + public function __construct(Collection|array $geometries, int|Srid $srid = 0) + { + // @phpstan-ignore-next-line + parent::__construct($geometries, $this->srid = $srid instanceof Srid ? $srid->value : $srid); + } + + public function toWkt(): string + { + $wktData = $this->getWktData(); + + return "MULTIPOLYGON({$wktData})"; + } + + public function getWktData(): string + { + return $this->geometries + ->map(static function (Polygon $polygon): string { + $wktData = $polygon->getWktData(); + + return "({$wktData})"; + }) + ->join(', '); + } } diff --git a/src/Objects/Point.php b/src/Objects/Point.php index da34591..187e633 100644 --- a/src/Objects/Point.php +++ b/src/Objects/Point.php @@ -8,37 +8,37 @@ class Point extends Geometry { - public float $latitude; - - public float $longitude; - - public function __construct(float $latitude, float $longitude, int|Srid $srid = 0) - { - $this->latitude = $latitude; - $this->longitude = $longitude; - $this->srid = $srid instanceof Srid ? $srid->value : $srid; - } - - public function toWkt(): string - { - $wktData = $this->getWktData(); - - return "POINT({$wktData})"; - } - - public function getWktData(): string - { - return "{$this->longitude} {$this->latitude}"; - } - - /** - * @return array{0: float, 1: float} - */ - public function getCoordinates(): array - { - return [ - $this->longitude, - $this->latitude, - ]; - } + public float $latitude; + + public float $longitude; + + public function __construct(float $latitude, float $longitude, int|Srid $srid = 0) + { + $this->latitude = $latitude; + $this->longitude = $longitude; + $this->srid = $srid instanceof Srid ? $srid->value : $srid; + } + + public function toWkt(): string + { + $wktData = $this->getWktData(); + + return "POINT({$wktData})"; + } + + public function getWktData(): string + { + return "{$this->longitude} {$this->latitude}"; + } + + /** + * @return array{0: float, 1: float} + */ + public function getCoordinates(): array + { + return [ + $this->longitude, + $this->latitude, + ]; + } } diff --git a/src/Objects/PointCollection.php b/src/Objects/PointCollection.php index 5190bb6..345449f 100644 --- a/src/Objects/PointCollection.php +++ b/src/Objects/PointCollection.php @@ -17,17 +17,16 @@ */ abstract class PointCollection extends GeometryCollection { - protected string $collectionOf = Point::class; + protected string $collectionOf = Point::class; - /** - * @param Collection|array $geometries - * @param int $srid - * - * @throws InvalidArgumentException - */ - public function __construct(Collection|array $geometries, int|Srid $srid = 0) - { - // @phpstan-ignore-next-line - parent::__construct($geometries, $this->srid = $srid instanceof Srid ? $srid->value : $srid); - } + /** + * @param Collection|array $geometries + * + * @throws InvalidArgumentException + */ + public function __construct(Collection|array $geometries, int|Srid $srid = 0) + { + // @phpstan-ignore-next-line + parent::__construct($geometries, $this->srid = $srid instanceof Srid ? $srid->value : $srid); + } } diff --git a/src/Objects/Polygon.php b/src/Objects/Polygon.php index 56b8220..218023a 100644 --- a/src/Objects/Polygon.php +++ b/src/Objects/Polygon.php @@ -6,21 +6,21 @@ class Polygon extends MultiLineString { - public function toWkt(): string - { - $wktData = $this->getWktData(); + public function toWkt(): string + { + $wktData = $this->getWktData(); - return "POLYGON({$wktData})"; - } + return "POLYGON({$wktData})"; + } - public function getWktData(): string - { - return $this->geometries - ->map(static function (LineString $lineString): string { - $wktData = $lineString->getWktData(); + public function getWktData(): string + { + return $this->geometries + ->map(static function (LineString $lineString): string { + $wktData = $lineString->getWktData(); - return "({$wktData})"; - }) - ->join(', '); - } + return "({$wktData})"; + }) + ->join(', '); + } } diff --git a/src/Traits/HasSpatial.php b/src/Traits/HasSpatial.php index 019915e..ad6ee75 100644 --- a/src/Traits/HasSpatial.php +++ b/src/Traits/HasSpatial.php @@ -11,325 +11,325 @@ trait HasSpatial { - public function scopeWithDistance( - Builder $query, - ExpressionContract|Geometry|string $column, - ExpressionContract|Geometry|string $geometryOrColumn, - string $alias = 'distance' - ): void { - if (! $query->getQuery()->columns) { - $query->select('*'); + public function scopeWithDistance( + Builder $query, + ExpressionContract|Geometry|string $column, + ExpressionContract|Geometry|string $geometryOrColumn, + string $alias = 'distance' + ): void { + if (! $query->getQuery()->columns) { + $query->select('*'); + } + + $query->selectRaw( + sprintf( + 'ST_DISTANCE(%s, %s) AS %s', + $this->toExpressionString($column), + $this->toExpressionString($geometryOrColumn), + $alias, + ) + ); } - $query->selectRaw( - sprintf( - 'ST_DISTANCE(%s, %s) AS %s', - $this->toExpressionString($column), - $this->toExpressionString($geometryOrColumn), - $alias, - ) - ); - } + public function scopeWhereDistance( + Builder $query, + ExpressionContract|Geometry|string $column, + ExpressionContract|Geometry|string $geometryOrColumn, + string $operator, + int|float $value + ): void { + $query->whereRaw( + sprintf( + 'ST_DISTANCE(%s, %s) %s ?', + $this->toExpressionString($column), + $this->toExpressionString($geometryOrColumn), + $operator, + ), + [$value], + ); + } - public function scopeWhereDistance( - Builder $query, - ExpressionContract|Geometry|string $column, - ExpressionContract|Geometry|string $geometryOrColumn, - string $operator, - int|float $value - ): void { - $query->whereRaw( - sprintf( - 'ST_DISTANCE(%s, %s) %s ?', - $this->toExpressionString($column), - $this->toExpressionString($geometryOrColumn), - $operator, - ), - [$value], - ); - } + public function scopeOrderByDistance( + Builder $query, + ExpressionContract|Geometry|string $column, + ExpressionContract|Geometry|string $geometryOrColumn, + string $direction = 'asc' + ): void { + $query->orderByRaw( + sprintf( + 'ST_DISTANCE(%s, %s) %s', + $this->toExpressionString($column), + $this->toExpressionString($geometryOrColumn), + $direction, + ) + ); + } - public function scopeOrderByDistance( - Builder $query, - ExpressionContract|Geometry|string $column, - ExpressionContract|Geometry|string $geometryOrColumn, - string $direction = 'asc' - ): void { - $query->orderByRaw( - sprintf( - 'ST_DISTANCE(%s, %s) %s', - $this->toExpressionString($column), - $this->toExpressionString($geometryOrColumn), - $direction, - ) - ); - } + public function scopeWithDistanceSphere( + Builder $query, + ExpressionContract|Geometry|string $column, + ExpressionContract|Geometry|string $geometryOrColumn, + string $alias = 'distance' + ): void { + if (! $query->getQuery()->columns) { + $query->select('*'); + } - public function scopeWithDistanceSphere( - Builder $query, - ExpressionContract|Geometry|string $column, - ExpressionContract|Geometry|string $geometryOrColumn, - string $alias = 'distance' - ): void { - if (! $query->getQuery()->columns) { - $query->select('*'); - } + // @codeCoverageIgnoreStart + $function = $this->getConnection() instanceof PostgresConnection + ? 'ST_DistanceSphere' + : 'ST_DISTANCE_SPHERE'; + // @codeCoverageIgnoreEnd - // @codeCoverageIgnoreStart - $function = $this->getConnection() instanceof PostgresConnection - ? 'ST_DistanceSphere' - : 'ST_DISTANCE_SPHERE'; - // @codeCoverageIgnoreEnd + $query->selectRaw( + sprintf( + '%s(%s, %s) AS %s', + $function, + $this->toExpressionString($column), + $this->toExpressionString($geometryOrColumn), + $alias, + ) + ); + } - $query->selectRaw( - sprintf( - '%s(%s, %s) AS %s', - $function, - $this->toExpressionString($column), - $this->toExpressionString($geometryOrColumn), - $alias, - ) - ); - } + public function scopeWhereDistanceSphere( + Builder $query, + ExpressionContract|Geometry|string $column, + ExpressionContract|Geometry|string $geometryOrColumn, + string $operator, + int|float $value + ): void { + // @codeCoverageIgnoreStart + $function = $this->getConnection() instanceof PostgresConnection + ? 'ST_DistanceSphere' + : 'ST_DISTANCE_SPHERE'; + // @codeCoverageIgnoreEnd - public function scopeWhereDistanceSphere( - Builder $query, - ExpressionContract|Geometry|string $column, - ExpressionContract|Geometry|string $geometryOrColumn, - string $operator, - int|float $value - ): void { - // @codeCoverageIgnoreStart - $function = $this->getConnection() instanceof PostgresConnection - ? 'ST_DistanceSphere' - : 'ST_DISTANCE_SPHERE'; - // @codeCoverageIgnoreEnd + $query->whereRaw( + sprintf( + '%s(%s, %s) %s ?', + $function, + $this->toExpressionString($column), + $this->toExpressionString($geometryOrColumn), + $operator, + ), + [$value], + ); + } - $query->whereRaw( - sprintf( - '%s(%s, %s) %s ?', - $function, - $this->toExpressionString($column), - $this->toExpressionString($geometryOrColumn), - $operator, - ), - [$value], - ); - } + public function scopeOrderByDistanceSphere( + Builder $query, + ExpressionContract|Geometry|string $column, + ExpressionContract|Geometry|string $geometryOrColumn, + string $direction = 'asc' + ): void { + // @codeCoverageIgnoreStart + $function = $this->getConnection() instanceof PostgresConnection + ? 'ST_DistanceSphere' + : 'ST_DISTANCE_SPHERE'; + // @codeCoverageIgnoreEnd - public function scopeOrderByDistanceSphere( - Builder $query, - ExpressionContract|Geometry|string $column, - ExpressionContract|Geometry|string $geometryOrColumn, - string $direction = 'asc' - ): void { - // @codeCoverageIgnoreStart - $function = $this->getConnection() instanceof PostgresConnection - ? 'ST_DistanceSphere' - : 'ST_DISTANCE_SPHERE'; - // @codeCoverageIgnoreEnd + $query->orderByRaw( + sprintf( + '%s(%s, %s) %s', + $function, + $this->toExpressionString($column), + $this->toExpressionString($geometryOrColumn), + $direction + ) + ); + } - $query->orderByRaw( - sprintf( - '%s(%s, %s) %s', - $function, - $this->toExpressionString($column), - $this->toExpressionString($geometryOrColumn), - $direction - ) - ); - } + public function scopeWhereWithin( + Builder $query, + ExpressionContract|Geometry|string $column, + ExpressionContract|Geometry|string $geometryOrColumn, + ): void { + $query->whereRaw( + sprintf( + 'ST_WITHIN(%s, %s)', + $this->toExpressionString($column), + $this->toExpressionString($geometryOrColumn), + ) + ); + } - public function scopeWhereWithin( - Builder $query, - ExpressionContract|Geometry|string $column, - ExpressionContract|Geometry|string $geometryOrColumn, - ): void { - $query->whereRaw( - sprintf( - 'ST_WITHIN(%s, %s)', - $this->toExpressionString($column), - $this->toExpressionString($geometryOrColumn), - ) - ); - } + public function scopeWhereNotWithin( + Builder $query, + ExpressionContract|Geometry|string $column, + ExpressionContract|Geometry|string $geometryOrColumn, + ): void { + $value = $this->getConnection() instanceof PostgresConnection ? 'false' : 0; - public function scopeWhereNotWithin( - Builder $query, - ExpressionContract|Geometry|string $column, - ExpressionContract|Geometry|string $geometryOrColumn, - ): void { - $value = $this->getConnection() instanceof PostgresConnection ? 'false' : 0; + $query->whereRaw( + sprintf( + 'ST_WITHIN(%s, %s) = %s', + $this->toExpressionString($column), + $this->toExpressionString($geometryOrColumn), + $value, + ) + ); + } - $query->whereRaw( - sprintf( - 'ST_WITHIN(%s, %s) = %s', - $this->toExpressionString($column), - $this->toExpressionString($geometryOrColumn), - $value, - ) - ); - } + public function scopeWhereContains( + Builder $query, + ExpressionContract|Geometry|string $column, + ExpressionContract|Geometry|string $geometryOrColumn, + ): void { + $query->whereRaw( + sprintf( + 'ST_CONTAINS(%s, %s)', + $this->toExpressionString($column), + $this->toExpressionString($geometryOrColumn), + ) + ); + } - public function scopeWhereContains( - Builder $query, - ExpressionContract|Geometry|string $column, - ExpressionContract|Geometry|string $geometryOrColumn, - ): void { - $query->whereRaw( - sprintf( - 'ST_CONTAINS(%s, %s)', - $this->toExpressionString($column), - $this->toExpressionString($geometryOrColumn), - ) - ); - } + public function scopeWhereNotContains( + Builder $query, + ExpressionContract|Geometry|string $column, + ExpressionContract|Geometry|string $geometryOrColumn, + ): void { + $value = $this->getConnection() instanceof PostgresConnection ? 'false' : 0; - public function scopeWhereNotContains( - Builder $query, - ExpressionContract|Geometry|string $column, - ExpressionContract|Geometry|string $geometryOrColumn, - ): void { - $value = $this->getConnection() instanceof PostgresConnection ? 'false' : 0; + $query->whereRaw( + sprintf( + 'ST_CONTAINS(%s, %s) = %s', + $this->toExpressionString($column), + $this->toExpressionString($geometryOrColumn), + $value, + ) + ); + } - $query->whereRaw( - sprintf( - 'ST_CONTAINS(%s, %s) = %s', - $this->toExpressionString($column), - $this->toExpressionString($geometryOrColumn), - $value, - ) - ); - } + public function scopeWhereTouches( + Builder $query, + ExpressionContract|Geometry|string $column, + ExpressionContract|Geometry|string $geometryOrColumn, + ): void { + $query->whereRaw( + sprintf( + 'ST_TOUCHES(%s, %s)', + $this->toExpressionString($column), + $this->toExpressionString($geometryOrColumn), + ) + ); + } - public function scopeWhereTouches( - Builder $query, - ExpressionContract|Geometry|string $column, - ExpressionContract|Geometry|string $geometryOrColumn, - ): void { - $query->whereRaw( - sprintf( - 'ST_TOUCHES(%s, %s)', - $this->toExpressionString($column), - $this->toExpressionString($geometryOrColumn), - ) - ); - } + public function scopeWhereIntersects( + Builder $query, + ExpressionContract|Geometry|string $column, + ExpressionContract|Geometry|string $geometryOrColumn, + ): void { + $query->whereRaw( + sprintf( + 'ST_INTERSECTS(%s, %s)', + $this->toExpressionString($column), + $this->toExpressionString($geometryOrColumn), + ) + ); + } - public function scopeWhereIntersects( - Builder $query, - ExpressionContract|Geometry|string $column, - ExpressionContract|Geometry|string $geometryOrColumn, - ): void { - $query->whereRaw( - sprintf( - 'ST_INTERSECTS(%s, %s)', - $this->toExpressionString($column), - $this->toExpressionString($geometryOrColumn), - ) - ); - } + public function scopeWhereCrosses( + Builder $query, + ExpressionContract|Geometry|string $column, + ExpressionContract|Geometry|string $geometryOrColumn, + ): void { + $query->whereRaw( + sprintf( + 'ST_CROSSES(%s, %s)', + $this->toExpressionString($column), + $this->toExpressionString($geometryOrColumn), + ) + ); + } - public function scopeWhereCrosses( - Builder $query, - ExpressionContract|Geometry|string $column, - ExpressionContract|Geometry|string $geometryOrColumn, - ): void { - $query->whereRaw( - sprintf( - 'ST_CROSSES(%s, %s)', - $this->toExpressionString($column), - $this->toExpressionString($geometryOrColumn), - ) - ); - } + public function scopeWhereDisjoint( + Builder $query, + ExpressionContract|Geometry|string $column, + ExpressionContract|Geometry|string $geometryOrColumn, + ): void { + $query->whereRaw( + sprintf( + 'ST_DISJOINT(%s, %s)', + $this->toExpressionString($column), + $this->toExpressionString($geometryOrColumn), + ) + ); + } - public function scopeWhereDisjoint( - Builder $query, - ExpressionContract|Geometry|string $column, - ExpressionContract|Geometry|string $geometryOrColumn, - ): void { - $query->whereRaw( - sprintf( - 'ST_DISJOINT(%s, %s)', - $this->toExpressionString($column), - $this->toExpressionString($geometryOrColumn), - ) - ); - } + public function scopeWhereOverlaps( + Builder $query, + ExpressionContract|Geometry|string $column, + ExpressionContract|Geometry|string $geometryOrColumn, + ): void { + $query->whereRaw( + sprintf( + 'ST_OVERLAPS(%s, %s)', + $this->toExpressionString($column), + $this->toExpressionString($geometryOrColumn), + ) + ); + } - public function scopeWhereOverlaps( - Builder $query, - ExpressionContract|Geometry|string $column, - ExpressionContract|Geometry|string $geometryOrColumn, - ): void { - $query->whereRaw( - sprintf( - 'ST_OVERLAPS(%s, %s)', - $this->toExpressionString($column), - $this->toExpressionString($geometryOrColumn), - ) - ); - } + public function scopeWhereEquals( + Builder $query, + ExpressionContract|Geometry|string $column, + ExpressionContract|Geometry|string $geometryOrColumn, + ): void { + $query->whereRaw( + sprintf( + 'ST_EQUALS(%s, %s)', + $this->toExpressionString($column), + $this->toExpressionString($geometryOrColumn), + ) + ); + } - public function scopeWhereEquals( - Builder $query, - ExpressionContract|Geometry|string $column, - ExpressionContract|Geometry|string $geometryOrColumn, - ): void { - $query->whereRaw( - sprintf( - 'ST_EQUALS(%s, %s)', - $this->toExpressionString($column), - $this->toExpressionString($geometryOrColumn), - ) - ); - } + public function scopeWhereSrid( + Builder $query, + ExpressionContract|Geometry|string $column, + string $operator, + int|float $value + ): void { + $query->whereRaw( + sprintf( + 'ST_SRID(%s) %s ?', + $this->toExpressionString($column), + $operator, + ), + [$value], + ); + } - public function scopeWhereSrid( - Builder $query, - ExpressionContract|Geometry|string $column, - string $operator, - int|float $value - ): void { - $query->whereRaw( - sprintf( - 'ST_SRID(%s) %s ?', - $this->toExpressionString($column), - $operator, - ), - [$value], - ); - } + public function scopeWithCentroid( + Builder $query, + ExpressionContract|Geometry|string $column, + string $alias = 'centroid', + ): void { + $query->selectRaw( + sprintf( + 'ST_CENTROID(%s) AS %s', + $this->toExpressionString($column), + $query->getGrammar()->wrap($alias), + ) + ); + } - public function scopeWithCentroid( - Builder $query, - ExpressionContract|Geometry|string $column, - string $alias = 'centroid', - ): void { - $query->selectRaw( - sprintf( - 'ST_CENTROID(%s) AS %s', - $this->toExpressionString($column), - $query->getGrammar()->wrap($alias), - ) - ); - } + protected function toExpressionString(ExpressionContract|Geometry|string $geometryOrColumnOrExpression): string + { + $grammar = $this->getGrammar(); - protected function toExpressionString(ExpressionContract|Geometry|string $geometryOrColumnOrExpression): string - { - $grammar = $this->getGrammar(); + if ($geometryOrColumnOrExpression instanceof ExpressionContract) { + $expression = $geometryOrColumnOrExpression; + } elseif ($geometryOrColumnOrExpression instanceof Geometry) { + $expression = DB::raw($geometryOrColumnOrExpression->toSqlExpression($this->getConnection())->getValue($grammar)); + } else { + $expression = DB::raw( + (new GeometryExpression($grammar->wrap($geometryOrColumnOrExpression)))->normalize($this->getConnection()) + ); + } - if ($geometryOrColumnOrExpression instanceof ExpressionContract) { - $expression = $geometryOrColumnOrExpression; - } elseif ($geometryOrColumnOrExpression instanceof Geometry) { - $expression = DB::raw($geometryOrColumnOrExpression->toSqlExpression($this->getConnection())->getValue($grammar)); - } else { - $expression = DB::raw( - (new GeometryExpression($grammar->wrap($geometryOrColumnOrExpression)))->normalize($this->getConnection()) - ); + return (string) $expression->getValue($grammar); } - - return (string) $expression->getValue($grammar); - } } diff --git a/tests/DoctrineTypesTest.php b/tests/DoctrineTypesTest.php index 8ce2676..2c89b08 100644 --- a/tests/DoctrineTypesTest.php +++ b/tests/DoctrineTypesTest.php @@ -14,52 +14,52 @@ /** @var array{column: string, postgresType: class-string, mySqlType: class-string} $typeClass */ $dataset = [ - [ - 'column' => 'point', - 'postgresType' => GeometryType::class, - 'mySqlType' => PointType::class, - ], - [ - 'column' => 'point_geography', - 'postgresType' => GeographyType::class, - 'mySqlType' => PointType::class, - ], - [ - 'column' => 'line_string', - 'postgresType' => GeometryType::class, - 'mySqlType' => LineStringType::class, - ], - [ - 'column' => 'multi_point', - 'postgresType' => GeometryType::class, - 'mySqlType' => MultiPointType::class, - ], - [ - 'column' => 'polygon', - 'postgresType' => GeometryType::class, - 'mySqlType' => PolygonType::class, - ], - [ - 'column' => 'multi_line_string', - 'postgresType' => GeometryType::class, - 'mySqlType' => MultiLineStringType::class, - ], - [ - 'column' => 'multi_polygon', - 'postgresType' => GeometryType::class, - 'mySqlType' => MultiPolygonType::class, - ], - [ - 'column' => 'geometry_collection', - 'postgresType' => GeometryType::class, - 'mySqlType' => GeometryCollectionType::class, - ], + [ + 'column' => 'point', + 'postgresType' => GeometryType::class, + 'mySqlType' => PointType::class, + ], + [ + 'column' => 'point_geography', + 'postgresType' => GeographyType::class, + 'mySqlType' => PointType::class, + ], + [ + 'column' => 'line_string', + 'postgresType' => GeometryType::class, + 'mySqlType' => LineStringType::class, + ], + [ + 'column' => 'multi_point', + 'postgresType' => GeometryType::class, + 'mySqlType' => MultiPointType::class, + ], + [ + 'column' => 'polygon', + 'postgresType' => GeometryType::class, + 'mySqlType' => PolygonType::class, + ], + [ + 'column' => 'multi_line_string', + 'postgresType' => GeometryType::class, + 'mySqlType' => MultiLineStringType::class, + ], + [ + 'column' => 'multi_polygon', + 'postgresType' => GeometryType::class, + 'mySqlType' => MultiPolygonType::class, + ], + [ + 'column' => 'geometry_collection', + 'postgresType' => GeometryType::class, + 'mySqlType' => GeometryCollectionType::class, + ], ]; it('uses custom Doctrine types for spatial columns', function ($column, $postgresType, $mySqlType): void { - $doctrineSchemaManager = DB::connection()->getDoctrineSchemaManager(); + $doctrineSchemaManager = DB::connection()->getDoctrineSchemaManager(); - $columns = $doctrineSchemaManager->listTableColumns('test_places'); + $columns = $doctrineSchemaManager->listTableColumns('test_places'); - expect($columns[$column]->getType())->toBeInstanceOfOnPostgres($postgresType)->toBeInstanceOfOnMysql($mySqlType); + expect($columns[$column]->getType())->toBeInstanceOfOnPostgres($postgresType)->toBeInstanceOfOnMysql($mySqlType); })->with($dataset); diff --git a/tests/Expectations.php b/tests/Expectations.php index 1620bdd..dd30163 100644 --- a/tests/Expectations.php +++ b/tests/Expectations.php @@ -4,17 +4,17 @@ use Illuminate\Support\Facades\DB; expect()->extend('toBeOnPostgres', function (mixed $value) { - return $this->when(DB::connection() instanceof PostgresConnection, fn () => $this->toBe($value)); + return $this->when(DB::connection() instanceof PostgresConnection, fn () => $this->toBe($value)); }); expect()->extend('toBeOnMysql', function (mixed $value) { - return $this->when(! (DB::connection() instanceof PostgresConnection), fn () => $this->toBe($value)); + return $this->when(! (DB::connection() instanceof PostgresConnection), fn () => $this->toBe($value)); }); expect()->extend('toBeInstanceOfOnPostgres', function (string $type) { - return $this->when(DB::connection() instanceof PostgresConnection, fn () => $this->toBeInstanceOf($type)); + return $this->when(DB::connection() instanceof PostgresConnection, fn () => $this->toBeInstanceOf($type)); }); expect()->extend('toBeInstanceOfOnMysql', function (string $type) { - return $this->when(! (DB::connection() instanceof PostgresConnection), fn () => $this->toBeInstanceOf($type)); + return $this->when(! (DB::connection() instanceof PostgresConnection), fn () => $this->toBeInstanceOf($type)); }); diff --git a/tests/GeometryCastTest.php b/tests/GeometryCastTest.php index 070ce4d..4073d14 100644 --- a/tests/GeometryCastTest.php +++ b/tests/GeometryCastTest.php @@ -8,121 +8,121 @@ use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace; it('creates a model record with null geometry', function (): void { - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['point' => null]); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['point' => null]); - expect($testPlace->point)->toBeNull(); + expect($testPlace->point)->toBeNull(); }); it('updates a model record', function (): void { - $point = new Point(0, 180); - $point2 = new Point(0, 0); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['point' => $point]); + $point = new Point(0, 180); + $point2 = new Point(0, 0); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['point' => $point]); - $testPlace->update(['point' => $point2]); + $testPlace->update(['point' => $point2]); - expect($testPlace->point)->not->toEqual($point); - expect($testPlace->point)->toEqual($point2); + expect($testPlace->point)->not->toEqual($point); + expect($testPlace->point)->toEqual($point2); }); it('updates a model record with expression', function (): void { - $point = new Point(0, 180); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['point' => $point]); - $pointFromAttributes = $testPlace->getAttributes()['point']; - - expect(function () use ($testPlace, $pointFromAttributes): void { - $testPlace->update(['point' => $pointFromAttributes]); - })->not->toThrow(InvalidArgumentException::class); + $point = new Point(0, 180); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['point' => $point]); + $pointFromAttributes = $testPlace->getAttributes()['point']; + + expect(function () use ($testPlace, $pointFromAttributes): void { + $testPlace->update(['point' => $pointFromAttributes]); + })->not->toThrow(InvalidArgumentException::class); }); it('updates a model record with null geometry', function (): void { - $point = new Point(0, 180); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['point' => $point]); + $point = new Point(0, 180); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['point' => $point]); - $testPlace->update(['point' => null]); + $testPlace->update(['point' => null]); - expect($testPlace->point)->toBeNull(); + expect($testPlace->point)->toBeNull(); }); it('gets original geometry field', function (): void { - $point = new Point(0, 180, Srid::WGS84->value); - $point2 = new Point(0, 0, Srid::WGS84->value); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['point' => $point]); + $point = new Point(0, 180, Srid::WGS84->value); + $point2 = new Point(0, 0, Srid::WGS84->value); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['point' => $point]); - $testPlace->point = $point2; + $testPlace->point = $point2; - expect($testPlace->getOriginal('point'))->toEqual($point); - expect($testPlace->point)->not->toEqual($point); - expect($testPlace->point)->toEqual($point2); + expect($testPlace->getOriginal('point'))->toEqual($point); + expect($testPlace->point)->not->toEqual($point); + expect($testPlace->point)->toEqual($point2); }); it('serializes a model record to array with geometry', function (): void { - $point = new Point(0, 180); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['point' => $point]); + $point = new Point(0, 180); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['point' => $point]); - $serialized = $testPlace->toArray(); + $serialized = $testPlace->toArray(); - $expectedArray = $point->toArray(); - expect($serialized['point'])->toEqual($expectedArray); + $expectedArray = $point->toArray(); + expect($serialized['point'])->toEqual($expectedArray); }); it('serializes a model record to json with geometry', function (): void { - $point = new Point(0, 180); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['point' => $point]); + $point = new Point(0, 180); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['point' => $point]); - $serialized = $testPlace->toJson(); + $serialized = $testPlace->toJson(); - // @phpstan-ignore-next-line - $json = json_encode(json_decode($serialized, true)['point']); - $expectedJson = $point->toJson(); - expect($json)->toBe($expectedJson); + // @phpstan-ignore-next-line + $json = json_encode(json_decode($serialized, true)['point']); + $expectedJson = $point->toJson(); + expect($json)->toBe($expectedJson); }); it('throws exception when cast serializing incorrect geometry object', function (): void { - expect(function (): void { - TestPlace::factory()->make([ - 'point' => new LineString([ - new Point(0, 180), - new Point(1, 179), - ]), - ]); - })->toThrow(InvalidArgumentException::class); + expect(function (): void { + TestPlace::factory()->make([ + 'point' => new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ]); + })->toThrow(InvalidArgumentException::class); }); it('throws exception when cast serializing non-geometry object', function (): void { - expect(function (): void { - TestPlace::factory()->make([ - 'point' => 'not-a-point-object', - ]); - })->toThrow(InvalidArgumentException::class); + expect(function (): void { + TestPlace::factory()->make([ + 'point' => 'not-a-point-object', + ]); + })->toThrow(InvalidArgumentException::class); }); it('throws exception when cast deserializing incorrect geometry object', function (): void { - TestPlace::insert(array_merge(TestPlace::factory()->definition(), [ - 'point_with_line_string_cast' => DB::raw( - (new GeometryExpression('POINT(0, 180)'))->normalize(DB::connection()) - ), - ])); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::firstOrFail(); - - expect(function () use ($testPlace): void { - $testPlace->getAttribute('point_with_line_string_cast'); - })->toThrow(InvalidArgumentException::class); + TestPlace::insert(array_merge(TestPlace::factory()->definition(), [ + 'point_with_line_string_cast' => DB::raw( + (new GeometryExpression('POINT(0, 180)'))->normalize(DB::connection()) + ), + ])); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::firstOrFail(); + + expect(function () use ($testPlace): void { + $testPlace->getAttribute('point_with_line_string_cast'); + })->toThrow(InvalidArgumentException::class); }); it('creates a model record with geometry from geo json array', function (): void { - $point = new Point(0, 180); - $pointGeoJsonArray = $point->toArray(); + $point = new Point(0, 180); + $pointGeoJsonArray = $point->toArray(); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->make(['point' => $pointGeoJsonArray]); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->make(['point' => $pointGeoJsonArray]); - expect($testPlace->point)->toEqual($point); + expect($testPlace->point)->toEqual($point); }); diff --git a/tests/HasSpatialTest.php b/tests/HasSpatialTest.php index 0e69fd8..1a967a2 100644 --- a/tests/HasSpatialTest.php +++ b/tests/HasSpatialTest.php @@ -10,470 +10,470 @@ use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace; it('calculates distance', function (): void { - TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); + TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); - /** @var TestPlace $testPlaceWithDistance */ - $testPlaceWithDistance = TestPlace::query() - ->withDistance('point', new Point(1, 1, Srid::WGS84->value)) - ->firstOrFail(); + /** @var TestPlace $testPlaceWithDistance */ + $testPlaceWithDistance = TestPlace::query() + ->withDistance('point', new Point(1, 1, Srid::WGS84->value)) + ->firstOrFail(); - expect($testPlaceWithDistance->distance)->toBe(156897.79947260793); + expect($testPlaceWithDistance->distance)->toBe(156897.79947260793); })->skip(fn () => ! AxisOrder::supported(DB::connection())); it('calculates distance - without axis-order', function (): void { - TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); + TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); - /** @var TestPlace $testPlaceWithDistance */ - $testPlaceWithDistance = TestPlace::query() - ->withDistance('point', new Point(1, 1, Srid::WGS84->value)) - ->firstOrFail(); + /** @var TestPlace $testPlaceWithDistance */ + $testPlaceWithDistance = TestPlace::query() + ->withDistance('point', new Point(1, 1, Srid::WGS84->value)) + ->firstOrFail(); - expect($testPlaceWithDistance->distance)->toBe(1.4142135623730951); -})->skip(fn () => AxisOrder::supported(DB::connection())); + expect($testPlaceWithDistance->distance)->toBe(1.4142135623730951); +})->skip(fn () => AxisOrder::supported(DB::connection())); it('calculates distance with alias', function (): void { - TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); + TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); - /** @var TestPlace $testPlaceWithDistance */ - $testPlaceWithDistance = TestPlace::query() - ->withDistance('point', new Point(1, 1, Srid::WGS84->value), 'distance_in_meters') - ->firstOrFail(); + /** @var TestPlace $testPlaceWithDistance */ + $testPlaceWithDistance = TestPlace::query() + ->withDistance('point', new Point(1, 1, Srid::WGS84->value), 'distance_in_meters') + ->firstOrFail(); - expect($testPlaceWithDistance->distance_in_meters)->toBe(156897.79947260793); + expect($testPlaceWithDistance->distance_in_meters)->toBe(156897.79947260793); })->skip(fn () => ! AxisOrder::supported(DB::connection())); it('calculates distance with alias - without axis-order', function (): void { - TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); + TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); - /** @var TestPlace $testPlaceWithDistance */ - $testPlaceWithDistance = TestPlace::query() - ->withDistance('point', new Point(1, 1, Srid::WGS84->value), 'distance_in_meters') - ->firstOrFail(); + /** @var TestPlace $testPlaceWithDistance */ + $testPlaceWithDistance = TestPlace::query() + ->withDistance('point', new Point(1, 1, Srid::WGS84->value), 'distance_in_meters') + ->firstOrFail(); - expect($testPlaceWithDistance->distance_in_meters)->toBe(1.4142135623730951); -})->skip(fn () => AxisOrder::supported(DB::connection())); + expect($testPlaceWithDistance->distance_in_meters)->toBe(1.4142135623730951); +})->skip(fn () => AxisOrder::supported(DB::connection())); it('filters by distance', function (): void { - $pointWithinDistance = new Point(0, 0, Srid::WGS84->value); - $pointNotWithinDistance = new Point(50, 50, Srid::WGS84->value); - TestPlace::factory()->create(['point' => $pointWithinDistance]); - TestPlace::factory()->create(['point' => $pointNotWithinDistance]); - - /** @var TestPlace[] $testPlacesWithinDistance */ - $testPlacesWithinDistance = TestPlace::query() - ->whereDistance('point', new Point(1, 1, Srid::WGS84->value), '<', 200_000) - ->get(); - - expect($testPlacesWithinDistance)->toHaveCount(1); - expect($testPlacesWithinDistance[0]->point)->toEqual($pointWithinDistance); + $pointWithinDistance = new Point(0, 0, Srid::WGS84->value); + $pointNotWithinDistance = new Point(50, 50, Srid::WGS84->value); + TestPlace::factory()->create(['point' => $pointWithinDistance]); + TestPlace::factory()->create(['point' => $pointNotWithinDistance]); + + /** @var TestPlace[] $testPlacesWithinDistance */ + $testPlacesWithinDistance = TestPlace::query() + ->whereDistance('point', new Point(1, 1, Srid::WGS84->value), '<', 200_000) + ->get(); + + expect($testPlacesWithinDistance)->toHaveCount(1); + expect($testPlacesWithinDistance[0]->point)->toEqual($pointWithinDistance); })->skip(fn () => ! AxisOrder::supported(DB::connection())); it('filters by distance - without axis-order', function (): void { - $pointWithinDistance = new Point(0, 0, Srid::WGS84->value); - $pointNotWithinDistance = new Point(50, 50, Srid::WGS84->value); - TestPlace::factory()->create(['point' => $pointWithinDistance]); - TestPlace::factory()->create(['point' => $pointNotWithinDistance]); + $pointWithinDistance = new Point(0, 0, Srid::WGS84->value); + $pointNotWithinDistance = new Point(50, 50, Srid::WGS84->value); + TestPlace::factory()->create(['point' => $pointWithinDistance]); + TestPlace::factory()->create(['point' => $pointNotWithinDistance]); - /** @var TestPlace[] $testPlacesWithinDistance */ - $testPlacesWithinDistance = TestPlace::query() - ->whereDistance('point', new Point(1, 1, Srid::WGS84->value), '<', 2) - ->get(); + /** @var TestPlace[] $testPlacesWithinDistance */ + $testPlacesWithinDistance = TestPlace::query() + ->whereDistance('point', new Point(1, 1, Srid::WGS84->value), '<', 2) + ->get(); - expect($testPlacesWithinDistance)->toHaveCount(1); - expect($testPlacesWithinDistance[0]->point)->toEqual($pointWithinDistance); -})->skip(fn () => AxisOrder::supported(DB::connection())); + expect($testPlacesWithinDistance)->toHaveCount(1); + expect($testPlacesWithinDistance[0]->point)->toEqual($pointWithinDistance); +})->skip(fn () => AxisOrder::supported(DB::connection())); it('orders by distance ASC', function (): void { - $closerTestPlace = TestPlace::factory()->create(['point' => new Point(1, 1, Srid::WGS84->value)]); - $fartherTestPlace = TestPlace::factory()->create(['point' => new Point(2, 2, Srid::WGS84->value)]); + $closerTestPlace = TestPlace::factory()->create(['point' => new Point(1, 1, Srid::WGS84->value)]); + $fartherTestPlace = TestPlace::factory()->create(['point' => new Point(2, 2, Srid::WGS84->value)]); - /** @var TestPlace[] $testPlacesOrderedByDistance */ - $testPlacesOrderedByDistance = TestPlace::query() - ->orderByDistance('point', new Point(0, 0, Srid::WGS84->value)) - ->get(); + /** @var TestPlace[] $testPlacesOrderedByDistance */ + $testPlacesOrderedByDistance = TestPlace::query() + ->orderByDistance('point', new Point(0, 0, Srid::WGS84->value)) + ->get(); - expect($testPlacesOrderedByDistance[0]->id)->toBe($closerTestPlace->id); - expect($testPlacesOrderedByDistance[1]->id)->toBe($fartherTestPlace->id); + expect($testPlacesOrderedByDistance[0]->id)->toBe($closerTestPlace->id); + expect($testPlacesOrderedByDistance[1]->id)->toBe($fartherTestPlace->id); }); it('orders by distance DESC', function (): void { - $closerTestPlace = TestPlace::factory()->create(['point' => new Point(1, 1, Srid::WGS84->value)]); - $fartherTestPlace = TestPlace::factory()->create(['point' => new Point(2, 2, Srid::WGS84->value)]); + $closerTestPlace = TestPlace::factory()->create(['point' => new Point(1, 1, Srid::WGS84->value)]); + $fartherTestPlace = TestPlace::factory()->create(['point' => new Point(2, 2, Srid::WGS84->value)]); - /** @var TestPlace[] $testPlacesOrderedByDistance */ - $testPlacesOrderedByDistance = TestPlace::query() - ->orderByDistance('point', new Point(0, 0, Srid::WGS84->value), 'desc') - ->get(); + /** @var TestPlace[] $testPlacesOrderedByDistance */ + $testPlacesOrderedByDistance = TestPlace::query() + ->orderByDistance('point', new Point(0, 0, Srid::WGS84->value), 'desc') + ->get(); - expect($testPlacesOrderedByDistance[1]->id)->toBe($closerTestPlace->id); - expect($testPlacesOrderedByDistance[0]->id)->toBe($fartherTestPlace->id); + expect($testPlacesOrderedByDistance[1]->id)->toBe($closerTestPlace->id); + expect($testPlacesOrderedByDistance[0]->id)->toBe($fartherTestPlace->id); }); it('calculates distance sphere', function (): void { - TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); + TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); - /** @var TestPlace $testPlaceWithDistance */ - $testPlaceWithDistance = TestPlace::query() - ->withDistanceSphere('point', new Point(1, 1, Srid::WGS84->value)) - ->firstOrFail(); + /** @var TestPlace $testPlaceWithDistance */ + $testPlaceWithDistance = TestPlace::query() + ->withDistanceSphere('point', new Point(1, 1, Srid::WGS84->value)) + ->firstOrFail(); - expect($testPlaceWithDistance->distance)->toBe(157249.59776850493); -})->skip(fn () =>! AxisOrder::supported(DB::connection())); + expect($testPlaceWithDistance->distance)->toBe(157249.59776850493); +})->skip(fn () => ! AxisOrder::supported(DB::connection())); it('calculates distance sphere - without axis-order', function (): void { - TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); + TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); - /** @var TestPlace $testPlaceWithDistance */ - $testPlaceWithDistance = TestPlace::query() - ->withDistanceSphere('point', new Point(1, 1, Srid::WGS84->value)) - ->firstOrFail(); + /** @var TestPlace $testPlaceWithDistance */ + $testPlaceWithDistance = TestPlace::query() + ->withDistanceSphere('point', new Point(1, 1, Srid::WGS84->value)) + ->firstOrFail(); - expect($testPlaceWithDistance->distance)->toBeOnPostgres(157249.59776851); - expect($testPlaceWithDistance->distance)->toBeOnMysql(157249.0357231545); -})->skip(fn () => AxisOrder::supported(DB::connection())); + expect($testPlaceWithDistance->distance)->toBeOnPostgres(157249.59776851); + expect($testPlaceWithDistance->distance)->toBeOnMysql(157249.0357231545); +})->skip(fn () => AxisOrder::supported(DB::connection())); it('calculates distance sphere with alias', function (): void { - TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); + TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); - /** @var TestPlace $testPlaceWithDistance */ - $testPlaceWithDistance = TestPlace::query() - ->withDistanceSphere('point', new Point(1, 1, Srid::WGS84->value), 'distance_in_meters') - ->firstOrFail(); + /** @var TestPlace $testPlaceWithDistance */ + $testPlaceWithDistance = TestPlace::query() + ->withDistanceSphere('point', new Point(1, 1, Srid::WGS84->value), 'distance_in_meters') + ->firstOrFail(); - expect($testPlaceWithDistance->distance_in_meters)->toBe(157249.59776850493); + expect($testPlaceWithDistance->distance_in_meters)->toBe(157249.59776850493); })->skip(fn () => ! AxisOrder::supported(DB::connection())); it('calculates distance sphere with alias - without axis-order', function (): void { - TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); + TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); - /** @var TestPlace $testPlaceWithDistance */ - $testPlaceWithDistance = TestPlace::query() - ->withDistanceSphere('point', new Point(1, 1, Srid::WGS84->value), 'distance_in_meters') - ->firstOrFail(); + /** @var TestPlace $testPlaceWithDistance */ + $testPlaceWithDistance = TestPlace::query() + ->withDistanceSphere('point', new Point(1, 1, Srid::WGS84->value), 'distance_in_meters') + ->firstOrFail(); - expect($testPlaceWithDistance->distance_in_meters)->toBeOnPostgres(157249.59776851); - expect($testPlaceWithDistance->distance_in_meters)->toBeOnMysql(157249.0357231545); -})->skip(fn () => AxisOrder::supported(DB::connection())); + expect($testPlaceWithDistance->distance_in_meters)->toBeOnPostgres(157249.59776851); + expect($testPlaceWithDistance->distance_in_meters)->toBeOnMysql(157249.0357231545); +})->skip(fn () => AxisOrder::supported(DB::connection())); it('filters distance sphere', function (): void { - $pointWithinDistance = new Point(0, 0, Srid::WGS84->value); - $pointNotWithinDistance = new Point(50, 50, Srid::WGS84->value); - TestPlace::factory()->create(['point' => $pointWithinDistance]); - TestPlace::factory()->create(['point' => $pointNotWithinDistance]); - - /** @var TestPlace[] $testPlacesWithinDistance */ - $testPlacesWithinDistance = TestPlace::query() - ->whereDistanceSphere('point', new Point(1, 1, Srid::WGS84->value), '<', 200000) - ->get(); - - expect($testPlacesWithinDistance)->toHaveCount(1); - expect($testPlacesWithinDistance[0]->point)->toEqual($pointWithinDistance); + $pointWithinDistance = new Point(0, 0, Srid::WGS84->value); + $pointNotWithinDistance = new Point(50, 50, Srid::WGS84->value); + TestPlace::factory()->create(['point' => $pointWithinDistance]); + TestPlace::factory()->create(['point' => $pointNotWithinDistance]); + + /** @var TestPlace[] $testPlacesWithinDistance */ + $testPlacesWithinDistance = TestPlace::query() + ->whereDistanceSphere('point', new Point(1, 1, Srid::WGS84->value), '<', 200000) + ->get(); + + expect($testPlacesWithinDistance)->toHaveCount(1); + expect($testPlacesWithinDistance[0]->point)->toEqual($pointWithinDistance); }); it('orders by distance sphere ASC', function (): void { - $closerTestPlace = TestPlace::factory()->create(['point' => new Point(1, 1, Srid::WGS84->value)]); - $fartherTestPlace = TestPlace::factory()->create(['point' => new Point(2, 2, Srid::WGS84->value)]); + $closerTestPlace = TestPlace::factory()->create(['point' => new Point(1, 1, Srid::WGS84->value)]); + $fartherTestPlace = TestPlace::factory()->create(['point' => new Point(2, 2, Srid::WGS84->value)]); - /** @var TestPlace[] $testPlacesOrderedByDistance */ - $testPlacesOrderedByDistance = TestPlace::query() - ->orderByDistanceSphere('point', new Point(0, 0, Srid::WGS84->value)) - ->get(); + /** @var TestPlace[] $testPlacesOrderedByDistance */ + $testPlacesOrderedByDistance = TestPlace::query() + ->orderByDistanceSphere('point', new Point(0, 0, Srid::WGS84->value)) + ->get(); - expect($testPlacesOrderedByDistance[0]->id)->toBe($closerTestPlace->id); - expect($testPlacesOrderedByDistance[1]->id)->toBe($fartherTestPlace->id); + expect($testPlacesOrderedByDistance[0]->id)->toBe($closerTestPlace->id); + expect($testPlacesOrderedByDistance[1]->id)->toBe($fartherTestPlace->id); }); it('orders by distance sphere DESC', function (): void { - $closerTestPlace = TestPlace::factory()->create(['point' => new Point(1, 1, Srid::WGS84->value)]); - $fartherTestPlace = TestPlace::factory()->create(['point' => new Point(2, 2, Srid::WGS84->value)]); + $closerTestPlace = TestPlace::factory()->create(['point' => new Point(1, 1, Srid::WGS84->value)]); + $fartherTestPlace = TestPlace::factory()->create(['point' => new Point(2, 2, Srid::WGS84->value)]); - /** @var TestPlace[] $testPlacesOrderedByDistance */ - $testPlacesOrderedByDistance = TestPlace::query() - ->orderByDistanceSphere('point', new Point(0, 0, Srid::WGS84->value), 'desc') - ->get(); + /** @var TestPlace[] $testPlacesOrderedByDistance */ + $testPlacesOrderedByDistance = TestPlace::query() + ->orderByDistanceSphere('point', new Point(0, 0, Srid::WGS84->value), 'desc') + ->get(); - expect($testPlacesOrderedByDistance[1]->id)->toBe($closerTestPlace->id); - expect($testPlacesOrderedByDistance[0]->id)->toBe($fartherTestPlace->id); + expect($testPlacesOrderedByDistance[1]->id)->toBe($closerTestPlace->id); + expect($testPlacesOrderedByDistance[0]->id)->toBe($fartherTestPlace->id); }); it('filters by within', function (): void { - $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}', Srid::WGS84->value); - $pointWithinPolygon = new Point(0, 0, Srid::WGS84->value); - $pointOutsidePolygon = new Point(50, 50, Srid::WGS84->value); - TestPlace::factory()->create(['point' => $pointWithinPolygon]); - TestPlace::factory()->create(['point' => $pointOutsidePolygon]); - - /** @var TestPlace[] $testPlacesWithinPolygon */ - $testPlacesWithinPolygon = TestPlace::query() - ->whereWithin('point', $polygon) - ->get(); - - expect($testPlacesWithinPolygon)->toHaveCount(1); - expect($testPlacesWithinPolygon[0]->point)->toEqual($pointWithinPolygon); + $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}', Srid::WGS84->value); + $pointWithinPolygon = new Point(0, 0, Srid::WGS84->value); + $pointOutsidePolygon = new Point(50, 50, Srid::WGS84->value); + TestPlace::factory()->create(['point' => $pointWithinPolygon]); + TestPlace::factory()->create(['point' => $pointOutsidePolygon]); + + /** @var TestPlace[] $testPlacesWithinPolygon */ + $testPlacesWithinPolygon = TestPlace::query() + ->whereWithin('point', $polygon) + ->get(); + + expect($testPlacesWithinPolygon)->toHaveCount(1); + expect($testPlacesWithinPolygon[0]->point)->toEqual($pointWithinPolygon); }); it('filters by not within', function (): void { - $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}', Srid::WGS84->value); - $pointWithinPolygon = new Point(0, 0, Srid::WGS84->value); - $pointOutsidePolygon = new Point(50, 50, Srid::WGS84->value); - TestPlace::factory()->create(['point' => $pointWithinPolygon]); - TestPlace::factory()->create(['point' => $pointOutsidePolygon]); - - /** @var TestPlace[] $testPlacesNotWithinPolygon */ - $testPlacesNotWithinPolygon = TestPlace::query() - ->whereNotWithin('point', $polygon) - ->get(); - - expect($testPlacesNotWithinPolygon)->toHaveCount(1); - expect($testPlacesNotWithinPolygon[0]->point)->toEqual($pointOutsidePolygon); + $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}', Srid::WGS84->value); + $pointWithinPolygon = new Point(0, 0, Srid::WGS84->value); + $pointOutsidePolygon = new Point(50, 50, Srid::WGS84->value); + TestPlace::factory()->create(['point' => $pointWithinPolygon]); + TestPlace::factory()->create(['point' => $pointOutsidePolygon]); + + /** @var TestPlace[] $testPlacesNotWithinPolygon */ + $testPlacesNotWithinPolygon = TestPlace::query() + ->whereNotWithin('point', $polygon) + ->get(); + + expect($testPlacesNotWithinPolygon)->toHaveCount(1); + expect($testPlacesNotWithinPolygon[0]->point)->toEqual($pointOutsidePolygon); }); it('filters by contains', function (): void { - $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}', Srid::WGS84->value); - $pointWithinPolygon = new Point(0, 0, Srid::WGS84->value); - $pointOutsidePolygon = new Point(50, 50, Srid::WGS84->value); - TestPlace::factory()->create(['polygon' => $polygon]); - - $testPlace = TestPlace::query() - ->whereContains('polygon', $pointWithinPolygon) - ->first(); - $testPlace2 = TestPlace::query() - ->whereContains('polygon', $pointOutsidePolygon) - ->first(); - - expect($testPlace)->not->toBeNull(); - expect($testPlace2)->toBeNull(); + $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}', Srid::WGS84->value); + $pointWithinPolygon = new Point(0, 0, Srid::WGS84->value); + $pointOutsidePolygon = new Point(50, 50, Srid::WGS84->value); + TestPlace::factory()->create(['polygon' => $polygon]); + + $testPlace = TestPlace::query() + ->whereContains('polygon', $pointWithinPolygon) + ->first(); + $testPlace2 = TestPlace::query() + ->whereContains('polygon', $pointOutsidePolygon) + ->first(); + + expect($testPlace)->not->toBeNull(); + expect($testPlace2)->toBeNull(); }); it('filters by not contains', function (): void { - $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}', Srid::WGS84->value); - $pointWithinPolygon = new Point(0, 0, Srid::WGS84->value); - $pointOutsidePolygon = new Point(50, 50, Srid::WGS84->value); - TestPlace::factory()->create(['polygon' => $polygon]); - - $testPlace = TestPlace::query() - ->whereNotContains('polygon', $pointWithinPolygon) - ->first(); - $testPlace2 = TestPlace::query() - ->whereNotContains('polygon', $pointOutsidePolygon) - ->first(); - - expect($testPlace)->toBeNull(); - expect($testPlace2)->not->toBeNull(); + $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}', Srid::WGS84->value); + $pointWithinPolygon = new Point(0, 0, Srid::WGS84->value); + $pointOutsidePolygon = new Point(50, 50, Srid::WGS84->value); + TestPlace::factory()->create(['polygon' => $polygon]); + + $testPlace = TestPlace::query() + ->whereNotContains('polygon', $pointWithinPolygon) + ->first(); + $testPlace2 = TestPlace::query() + ->whereNotContains('polygon', $pointOutsidePolygon) + ->first(); + + expect($testPlace)->toBeNull(); + expect($testPlace2)->not->toBeNull(); }); it('filters by touches', function (): void { - $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[0,-1],[0,0],[-1,0],[-1,-1]]]}', Srid::WGS84->value); - $pointTouchesPolygon = new Point(0, 0, Srid::WGS84->value); - $pointNotTouchesPolygon = new Point(50, 50, Srid::WGS84->value); - TestPlace::factory()->create(['point' => $pointTouchesPolygon]); - TestPlace::factory()->create(['point' => $pointNotTouchesPolygon]); - - /** @var TestPlace[] $testPlacesTouchPolygon */ - $testPlacesTouchPolygon = TestPlace::query() - ->whereTouches('point', $polygon) - ->get(); - - expect($testPlacesTouchPolygon)->toHaveCount(1); - expect($testPlacesTouchPolygon[0]->point)->toEqual($pointTouchesPolygon); + $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[0,-1],[0,0],[-1,0],[-1,-1]]]}', Srid::WGS84->value); + $pointTouchesPolygon = new Point(0, 0, Srid::WGS84->value); + $pointNotTouchesPolygon = new Point(50, 50, Srid::WGS84->value); + TestPlace::factory()->create(['point' => $pointTouchesPolygon]); + TestPlace::factory()->create(['point' => $pointNotTouchesPolygon]); + + /** @var TestPlace[] $testPlacesTouchPolygon */ + $testPlacesTouchPolygon = TestPlace::query() + ->whereTouches('point', $polygon) + ->get(); + + expect($testPlacesTouchPolygon)->toHaveCount(1); + expect($testPlacesTouchPolygon[0]->point)->toEqual($pointTouchesPolygon); }); it('filters by intersects', function (): void { - $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}', Srid::WGS84->value); - $pointIntersectsPolygon = new Point(0, 0, Srid::WGS84->value); - $pointNotIntersectsPolygon = new Point(50, 50, Srid::WGS84->value); - TestPlace::factory()->create(['point' => $pointIntersectsPolygon]); - TestPlace::factory()->create(['point' => $pointNotIntersectsPolygon]); - - /** @var TestPlace[] $testPlacesInterestPolygon */ - $testPlacesInterestPolygon = TestPlace::query() - ->whereIntersects('point', $polygon) - ->get(); - - expect($testPlacesInterestPolygon)->toHaveCount(1); - expect($testPlacesInterestPolygon[0]->point)->toEqual($pointIntersectsPolygon); + $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}', Srid::WGS84->value); + $pointIntersectsPolygon = new Point(0, 0, Srid::WGS84->value); + $pointNotIntersectsPolygon = new Point(50, 50, Srid::WGS84->value); + TestPlace::factory()->create(['point' => $pointIntersectsPolygon]); + TestPlace::factory()->create(['point' => $pointNotIntersectsPolygon]); + + /** @var TestPlace[] $testPlacesInterestPolygon */ + $testPlacesInterestPolygon = TestPlace::query() + ->whereIntersects('point', $polygon) + ->get(); + + expect($testPlacesInterestPolygon)->toHaveCount(1); + expect($testPlacesInterestPolygon[0]->point)->toEqual($pointIntersectsPolygon); }); it('filters by crosses', function (): void { - $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}', Srid::WGS84->value); - $lineStringCrossesPolygon = LineString::fromJson('{"type":"LineString","coordinates":[[0,0],[2,0]]}', Srid::WGS84->value); - $lineStringNotCrossesPolygon = LineString::fromJson('{"type":"LineString","coordinates":[[50,50],[52,50]]}', Srid::WGS84->value); - TestPlace::factory()->create(['line_string' => $lineStringCrossesPolygon]); - TestPlace::factory()->create(['line_string' => $lineStringNotCrossesPolygon]); - - /** @var TestPlace[] $testPlacesCrossPolygon */ - $testPlacesCrossPolygon = TestPlace::query() - ->whereCrosses('line_string', $polygon) - ->get(); - - expect($testPlacesCrossPolygon)->toHaveCount(1); - expect($testPlacesCrossPolygon[0]->line_string)->toEqual($lineStringCrossesPolygon); + $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}', Srid::WGS84->value); + $lineStringCrossesPolygon = LineString::fromJson('{"type":"LineString","coordinates":[[0,0],[2,0]]}', Srid::WGS84->value); + $lineStringNotCrossesPolygon = LineString::fromJson('{"type":"LineString","coordinates":[[50,50],[52,50]]}', Srid::WGS84->value); + TestPlace::factory()->create(['line_string' => $lineStringCrossesPolygon]); + TestPlace::factory()->create(['line_string' => $lineStringNotCrossesPolygon]); + + /** @var TestPlace[] $testPlacesCrossPolygon */ + $testPlacesCrossPolygon = TestPlace::query() + ->whereCrosses('line_string', $polygon) + ->get(); + + expect($testPlacesCrossPolygon)->toHaveCount(1); + expect($testPlacesCrossPolygon[0]->line_string)->toEqual($lineStringCrossesPolygon); }); it('filters by disjoint', function (): void { - $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[-0.5,-1],[-0.5,-0.5],[-1,-0.5],[-1,-1]]]}', Srid::WGS84->value); - $pointDisjointsPolygon = new Point(0, 0, Srid::WGS84->value); - $pointNotDisjointsPolygon = new Point(-1, -1, Srid::WGS84->value); - TestPlace::factory()->create(['point' => $pointDisjointsPolygon]); - TestPlace::factory()->create(['point' => $pointNotDisjointsPolygon]); - - /** @var TestPlace[] $testPlacesDisjointPolygon */ - $testPlacesDisjointPolygon = TestPlace::query() - ->whereDisjoint('point', $polygon) - ->get(); - - expect($testPlacesDisjointPolygon)->toHaveCount(1); - expect($testPlacesDisjointPolygon[0]->point)->toEqual($pointDisjointsPolygon); + $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[-0.5,-1],[-0.5,-0.5],[-1,-0.5],[-1,-1]]]}', Srid::WGS84->value); + $pointDisjointsPolygon = new Point(0, 0, Srid::WGS84->value); + $pointNotDisjointsPolygon = new Point(-1, -1, Srid::WGS84->value); + TestPlace::factory()->create(['point' => $pointDisjointsPolygon]); + TestPlace::factory()->create(['point' => $pointNotDisjointsPolygon]); + + /** @var TestPlace[] $testPlacesDisjointPolygon */ + $testPlacesDisjointPolygon = TestPlace::query() + ->whereDisjoint('point', $polygon) + ->get(); + + expect($testPlacesDisjointPolygon)->toHaveCount(1); + expect($testPlacesDisjointPolygon[0]->point)->toEqual($pointDisjointsPolygon); }); it('filters by overlaps', function (): void { - $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-0.75,-0.75],[1,-1],[1,1],[-1,1],[-0.75,-0.75]]]}', Srid::WGS84->value); - $overlappingPolygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[-0.5,-1],[-0.5,-0.5],[-1,-0.5],[-1,-1]]]}', Srid::WGS84->value); - $notOverlappingPolygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-10,-10],[-5,-10],[-5,-5],[-10,-5],[-10,-10]]]}', Srid::WGS84->value); - TestPlace::factory()->create(['polygon' => $overlappingPolygon]); - TestPlace::factory()->create(['polygon' => $notOverlappingPolygon]); - - /** @var TestPlace[] $overlappingTestPlaces */ - $overlappingTestPlaces = TestPlace::query() - ->whereOverlaps('polygon', $polygon) - ->get(); - - expect($overlappingTestPlaces)->toHaveCount(1); - expect($overlappingTestPlaces[0]->polygon)->toEqual($overlappingPolygon); + $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-0.75,-0.75],[1,-1],[1,1],[-1,1],[-0.75,-0.75]]]}', Srid::WGS84->value); + $overlappingPolygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[-0.5,-1],[-0.5,-0.5],[-1,-0.5],[-1,-1]]]}', Srid::WGS84->value); + $notOverlappingPolygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-10,-10],[-5,-10],[-5,-5],[-10,-5],[-10,-10]]]}', Srid::WGS84->value); + TestPlace::factory()->create(['polygon' => $overlappingPolygon]); + TestPlace::factory()->create(['polygon' => $notOverlappingPolygon]); + + /** @var TestPlace[] $overlappingTestPlaces */ + $overlappingTestPlaces = TestPlace::query() + ->whereOverlaps('polygon', $polygon) + ->get(); + + expect($overlappingTestPlaces)->toHaveCount(1); + expect($overlappingTestPlaces[0]->polygon)->toEqual($overlappingPolygon); }); it('filters by equals', function (): void { - $point1 = new Point(0, 0, Srid::WGS84->value); - $point2 = new Point(50, 50, Srid::WGS84->value); - TestPlace::factory()->create(['point' => $point1]); - TestPlace::factory()->create(['point' => $point2]); - - /** @var TestPlace[] $testPlaces */ - $testPlaces = TestPlace::query() - ->whereEquals('point', $point1) - ->get(); - - expect($testPlaces)->toHaveCount(1); - expect($testPlaces[0]->point)->toEqual($point1); + $point1 = new Point(0, 0, Srid::WGS84->value); + $point2 = new Point(50, 50, Srid::WGS84->value); + TestPlace::factory()->create(['point' => $point1]); + TestPlace::factory()->create(['point' => $point2]); + + /** @var TestPlace[] $testPlaces */ + $testPlaces = TestPlace::query() + ->whereEquals('point', $point1) + ->get(); + + expect($testPlaces)->toHaveCount(1); + expect($testPlaces[0]->point)->toEqual($point1); }); it('filters by SRID', function (): void { - $point1 = new Point(0, 0, Srid::WGS84->value); - $point2 = new Point(50, 50, 0); - TestPlace::factory()->create(['point' => $point1]); - TestPlace::factory()->create(['point' => $point2]); - - /** @var TestPlace[] $testPlaces */ - $testPlaces = TestPlace::query() - ->whereSrid('point', '=', Srid::WGS84->value) - ->get(); - - expect($testPlaces)->toHaveCount(1); - expect($testPlaces[0]->point)->toEqual($point1); + $point1 = new Point(0, 0, Srid::WGS84->value); + $point2 = new Point(50, 50, 0); + TestPlace::factory()->create(['point' => $point1]); + TestPlace::factory()->create(['point' => $point2]); + + /** @var TestPlace[] $testPlaces */ + $testPlaces = TestPlace::query() + ->whereSrid('point', '=', Srid::WGS84->value) + ->get(); + + expect($testPlaces)->toHaveCount(1); + expect($testPlaces[0]->point)->toEqual($point1); }); it('calculates geometry centroid', function (): void { - // Arrange - $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}'); - TestPlace::factory()->create(['polygon' => $polygon]); - - // Act - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::query() - ->withCentroid('polygon') - ->withCasts(['centroid' => Point::class]) - ->firstOrFail(); - - // Assert - $expectedCentroid = new Point(0, 0); - expect($testPlace->centroid)->toEqual($expectedCentroid); + // Arrange + $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}'); + TestPlace::factory()->create(['polygon' => $polygon]); + + // Act + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::query() + ->withCentroid('polygon') + ->withCasts(['centroid' => Point::class]) + ->firstOrFail(); + + // Assert + $expectedCentroid = new Point(0, 0); + expect($testPlace->centroid)->toEqual($expectedCentroid); }); it('calculates geometry centroid with alias', function (): void { - // Arrange - $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}'); - TestPlace::factory()->create(['polygon' => $polygon]); - - // Act - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::query() - ->withCentroid('polygon', 'centroid_alias') - ->withCasts(['centroid_alias' => Point::class]) - ->firstOrFail(); - - // Assert - $expectedCentroid = new Point(0, 0); - expect($testPlace->centroid_alias)->toEqual($expectedCentroid); + // Arrange + $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}'); + TestPlace::factory()->create(['polygon' => $polygon]); + + // Act + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::query() + ->withCentroid('polygon', 'centroid_alias') + ->withCasts(['centroid_alias' => Point::class]) + ->firstOrFail(); + + // Assert + $expectedCentroid = new Point(0, 0); + expect($testPlace->centroid_alias)->toEqual($expectedCentroid); }); it('uses spatial function with column', function (): void { - TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); + TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); - /** @var TestPlace $testPlaceWithDistance */ - $testPlaceWithDistance = TestPlace::query() - ->withDistance('point', 'point') - ->firstOrFail(); + /** @var TestPlace $testPlaceWithDistance */ + $testPlaceWithDistance = TestPlace::query() + ->withDistance('point', 'point') + ->firstOrFail(); - expect($testPlaceWithDistance->distance)->toBe(0.0); + expect($testPlaceWithDistance->distance)->toBe(0.0); }); it('uses spatial function with column that contains table name', function (): void { - TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); + TestPlace::factory()->create(['point' => new Point(0, 0, Srid::WGS84->value)]); - /** @var TestPlace $testPlaceWithDistance */ - $testPlaceWithDistance = TestPlace::query() - ->withDistance('test_places.point', 'test_places.point') - ->firstOrFail(); + /** @var TestPlace $testPlaceWithDistance */ + $testPlaceWithDistance = TestPlace::query() + ->withDistance('test_places.point', 'test_places.point') + ->firstOrFail(); - expect($testPlaceWithDistance->distance)->toBe(0.0); + expect($testPlaceWithDistance->distance)->toBe(0.0); }); it('uses spatial function with expression', function (): void { - $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}'); - TestPlace::factory()->create([ - 'polygon' => $polygon, - 'longitude' => 0, - 'latitude' => 0, - ]); - $expression = DB::raw((new GeometryExpression('POINT(longitude, latitude)'))->normalize(DB::connection())); - $expression2 = DB::raw((new GeometryExpression('polygon'))->normalize(DB::connection())); - - /** @var TestPlace $testPlaceWithDistance */ - $testPlaceWithDistance = TestPlace::query() - ->whereWithin($expression, $expression2) - ->firstOrFail(); - - expect($testPlaceWithDistance)->not()->toBeNull(); + $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}'); + TestPlace::factory()->create([ + 'polygon' => $polygon, + 'longitude' => 0, + 'latitude' => 0, + ]); + $expression = DB::raw((new GeometryExpression('POINT(longitude, latitude)'))->normalize(DB::connection())); + $expression2 = DB::raw((new GeometryExpression('polygon'))->normalize(DB::connection())); + + /** @var TestPlace $testPlaceWithDistance */ + $testPlaceWithDistance = TestPlace::query() + ->whereWithin($expression, $expression2) + ->firstOrFail(); + + expect($testPlaceWithDistance)->not()->toBeNull(); }); it('toExpressionString can handle a Expression input', function (): void { - $spatialBuilder = new TestPlace(); - $toExpressionStringMethod = (new ReflectionClass($spatialBuilder))->getMethod('toExpressionString'); + $spatialBuilder = new TestPlace(); + $toExpressionStringMethod = (new ReflectionClass($spatialBuilder))->getMethod('toExpressionString'); - $result = $toExpressionStringMethod->invoke($spatialBuilder, DB::raw('POINT(longitude, latitude)')); + $result = $toExpressionStringMethod->invoke($spatialBuilder, DB::raw('POINT(longitude, latitude)')); - expect($result)->toBe('POINT(longitude, latitude)'); + expect($result)->toBe('POINT(longitude, latitude)'); }); it('toExpressionString can handle a Geometry input', function (): void { - $testPlace = new TestPlace(); - $toExpressionStringMethod = (new ReflectionClass($testPlace))->getMethod('toExpressionString'); - $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}'); + $testPlace = new TestPlace(); + $toExpressionStringMethod = (new ReflectionClass($testPlace))->getMethod('toExpressionString'); + $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}'); - $result = $toExpressionStringMethod->invoke($testPlace, $polygon); + $result = $toExpressionStringMethod->invoke($testPlace, $polygon); - $grammar = $testPlace->getGrammar(); - $connection = $testPlace->getConnection(); - $sqlSerializedPolygon = $polygon->toSqlExpression($connection)->getValue($grammar); - expect($result)->toBe($sqlSerializedPolygon); + $grammar = $testPlace->getGrammar(); + $connection = $testPlace->getConnection(); + $sqlSerializedPolygon = $polygon->toSqlExpression($connection)->getValue($grammar); + expect($result)->toBe($sqlSerializedPolygon); }); it('toExpressionString can handle a string input', function (): void { - $spatialBuilder = new TestPlace(); - $toExpressionStringMethod = (new ReflectionClass($spatialBuilder))->getMethod('toExpressionString'); + $spatialBuilder = new TestPlace(); + $toExpressionStringMethod = (new ReflectionClass($spatialBuilder))->getMethod('toExpressionString'); - $result = $toExpressionStringMethod->invoke($spatialBuilder, 'test_places.point'); + $result = $toExpressionStringMethod->invoke($spatialBuilder, 'test_places.point'); - expect($result)->toBeOnPostgres('"test_places"."point"::geometry'); - expect($result)->toBeOnMysql('`test_places`.`point`'); + expect($result)->toBeOnPostgres('"test_places"."point"::geometry'); + expect($result)->toBeOnMysql('`test_places`.`point`'); }); diff --git a/tests/Objects/GeometryCollectionTest.php b/tests/Objects/GeometryCollectionTest.php index 0bea357..2af8ead 100644 --- a/tests/Objects/GeometryCollectionTest.php +++ b/tests/Objects/GeometryCollectionTest.php @@ -9,402 +9,402 @@ use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace; it('creates a model record with geometry collection', function (): void { - $geometryCollection = new GeometryCollection([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - new Point(0, 180), - ]); - - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['geometry_collection' => $geometryCollection]); - - expect($testPlace->geometry_collection)->toBeInstanceOf(GeometryCollection::class); - expect($testPlace->geometry_collection)->toEqual($geometryCollection); + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ]); + + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['geometry_collection' => $geometryCollection]); + + expect($testPlace->geometry_collection)->toBeInstanceOf(GeometryCollection::class); + expect($testPlace->geometry_collection)->toEqual($geometryCollection); }); it('creates a model record with geometry collection with SRID integer', function (): void { - $geometryCollection = new GeometryCollection([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - new Point(0, 180), - ], Srid::WGS84->value); - - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['geometry_collection' => $geometryCollection]); - - expect($testPlace->geometry_collection->srid)->toBe(Srid::WGS84->value); + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ], Srid::WGS84->value); + + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['geometry_collection' => $geometryCollection]); + + expect($testPlace->geometry_collection->srid)->toBe(Srid::WGS84->value); }); it('creates a model record with geometry collection with SRID enum', function (): void { - $geometryCollection = new GeometryCollection([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - new Point(0, 180), - ], Srid::WGS84); - - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['geometry_collection' => $geometryCollection]); - - expect($testPlace->geometry_collection->srid)->toBe(Srid::WGS84->value); + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ], Srid::WGS84); + + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['geometry_collection' => $geometryCollection]); + + expect($testPlace->geometry_collection->srid)->toBe(Srid::WGS84->value); }); it('creates geometry collection from JSON', function (): void { - $geometryCollection = new GeometryCollection([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - new Point(0, 180), - ]); - - $geometryCollectionFromJson = GeometryCollection::fromJson('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}'); - - expect($geometryCollectionFromJson)->toEqual($geometryCollection); + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ]); + + $geometryCollectionFromJson = GeometryCollection::fromJson('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}'); + + expect($geometryCollectionFromJson)->toEqual($geometryCollection); }); it('creates geometry collection with SRID from JSON', function (): void { - $geometryCollection = new GeometryCollection([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - new Point(0, 180), - ], Srid::WGS84->value); - - $geometryCollectionFromJson = GeometryCollection::fromJson('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}', Srid::WGS84->value); - - expect($geometryCollectionFromJson)->toEqual($geometryCollection); + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ], Srid::WGS84->value); + + $geometryCollectionFromJson = GeometryCollection::fromJson('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}', Srid::WGS84->value); + + expect($geometryCollectionFromJson)->toEqual($geometryCollection); }); it('creates geometry collection from feature collection JSON', function (): void { - $geometryCollection = new GeometryCollection([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - new Point(0, 180), - ]); - - $geometryCollectionFromFeatureCollectionJson = GeometryCollection::fromJson('{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}},{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[180,0]}}]}'); - - expect($geometryCollectionFromFeatureCollectionJson)->toEqual($geometryCollection); + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ]); + + $geometryCollectionFromFeatureCollectionJson = GeometryCollection::fromJson('{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}},{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[180,0]}}]}'); + + expect($geometryCollectionFromFeatureCollectionJson)->toEqual($geometryCollection); }); it('generates geometry collection JSON', function (): void { - $geometryCollection = new GeometryCollection([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - new Point(0, 180), - ]); - - $json = $geometryCollection->toJson(); - - $expectedJson = '{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}'; - expect($json)->toBe($expectedJson); + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ]); + + $json = $geometryCollection->toJson(); + + $expectedJson = '{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}'; + expect($json)->toBe($expectedJson); }); it('generates geometry collection feature collection JSON', function (): void { - $geometryCollection = new GeometryCollection([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - new Point(0, 180), - ]); - - $featureCollectionJson = $geometryCollection->toFeatureCollectionJson(); - - $expectedFeatureCollectionJson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}},{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[180,0]}}]}'; - expect($featureCollectionJson)->toBe($expectedFeatureCollectionJson); + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ]); + + $featureCollectionJson = $geometryCollection->toFeatureCollectionJson(); + + $expectedFeatureCollectionJson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}},{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[180,0]}}]}'; + expect($featureCollectionJson)->toBe($expectedFeatureCollectionJson); }); it('creates geometry collection from WKT', function (): void { - $geometryCollection = new GeometryCollection([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - new Point(0, 180), - ]); - - $geometryCollectionFromWkt = GeometryCollection::fromWkt('GEOMETRYCOLLECTION(POLYGON((180 0, 179 1, 178 2, 177 3, 180 0)), POINT(180 0))'); - - expect($geometryCollectionFromWkt)->toEqual($geometryCollection); + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ]); + + $geometryCollectionFromWkt = GeometryCollection::fromWkt('GEOMETRYCOLLECTION(POLYGON((180 0, 179 1, 178 2, 177 3, 180 0)), POINT(180 0))'); + + expect($geometryCollectionFromWkt)->toEqual($geometryCollection); }); it('creates geometry collection with SRID from WKT', function (): void { - $geometryCollection = new GeometryCollection([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - new Point(0, 180), - ], Srid::WGS84->value); - - $geometryCollectionFromWkt = GeometryCollection::fromWkt('GEOMETRYCOLLECTION(POLYGON((180 0, 179 1, 178 2, 177 3, 180 0)), POINT(180 0))', Srid::WGS84->value); - - expect($geometryCollectionFromWkt)->toEqual($geometryCollection); + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ], Srid::WGS84->value); + + $geometryCollectionFromWkt = GeometryCollection::fromWkt('GEOMETRYCOLLECTION(POLYGON((180 0, 179 1, 178 2, 177 3, 180 0)), POINT(180 0))', Srid::WGS84->value); + + expect($geometryCollectionFromWkt)->toEqual($geometryCollection); }); it('generates geometry collection WKT', function (): void { - $geometryCollection = new GeometryCollection([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - new Point(0, 180), - ]); - - $wkt = $geometryCollection->toWkt(); - - $expectedWkt = 'GEOMETRYCOLLECTION(POLYGON((180 0, 179 1, 178 2, 177 3, 180 0)), POINT(180 0))'; - expect($wkt)->toBe($expectedWkt); + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ]); + + $wkt = $geometryCollection->toWkt(); + + $expectedWkt = 'GEOMETRYCOLLECTION(POLYGON((180 0, 179 1, 178 2, 177 3, 180 0)), POINT(180 0))'; + expect($wkt)->toBe($expectedWkt); }); it('creates geometry collection from WKB', function (): void { - $geometryCollection = new GeometryCollection([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - new Point(0, 180), - ]); - - $geometryCollectionFromWkb = GeometryCollection::fromWkb($geometryCollection->toWkb()); - - expect($geometryCollectionFromWkb)->toEqual($geometryCollection); + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ]); + + $geometryCollectionFromWkb = GeometryCollection::fromWkb($geometryCollection->toWkb()); + + expect($geometryCollectionFromWkb)->toEqual($geometryCollection); }); it('creates geometry collection with SRID from WKB', function (): void { - $geometryCollection = new GeometryCollection([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - new Point(0, 180), - ], Srid::WGS84->value); - - $geometryCollectionFromWkb = GeometryCollection::fromWkb($geometryCollection->toWkb()); - - expect($geometryCollectionFromWkb)->toEqual($geometryCollection); + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ], Srid::WGS84->value); + + $geometryCollectionFromWkb = GeometryCollection::fromWkb($geometryCollection->toWkb()); + + expect($geometryCollectionFromWkb)->toEqual($geometryCollection); }); it('does not throw exception when geometry collection has no geometries', function (): void { - $geometryCollection = new GeometryCollection([]); + $geometryCollection = new GeometryCollection([]); - expect($geometryCollection->getGeometries())->toHaveCount(0); + expect($geometryCollection->getGeometries())->toHaveCount(0); }); it('unsets geometry collection item', function (): void { - $point = new Point(0, 180); - $geometryCollection = new GeometryCollection([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - $point, - ]); - - unset($geometryCollection[0]); - - expect($geometryCollection[0])->toBe($point); - expect($geometryCollection->getGeometries())->toHaveCount(1); + $point = new Point(0, 180); + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + $point, + ]); + + unset($geometryCollection[0]); + + expect($geometryCollection[0])->toBe($point); + expect($geometryCollection->getGeometries())->toHaveCount(1); }); it('throws exception when unsetting geometry collection item below minimum', function (): void { - $polygon = new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]); - - expect(function () use ($polygon): void { - unset($polygon[0]); - })->toThrow(InvalidArgumentException::class); + $polygon = new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]); + + expect(function () use ($polygon): void { + unset($polygon[0]); + })->toThrow(InvalidArgumentException::class); }); it('checks if geometry collection item is exists', function (): void { - $geometryCollection = new GeometryCollection([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - new Point(0, 180), - ]); - - $firstItemExists = isset($geometryCollection[0]); - $secondItemExists = isset($geometryCollection[1]); - $thirdItemExists = isset($geometryCollection[2]); - - expect($firstItemExists)->toBeTrue(); - expect($secondItemExists)->toBeTrue(); - expect($thirdItemExists)->toBeFalse(); + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ]); + + $firstItemExists = isset($geometryCollection[0]); + $secondItemExists = isset($geometryCollection[1]); + $thirdItemExists = isset($geometryCollection[2]); + + expect($firstItemExists)->toBeTrue(); + expect($secondItemExists)->toBeTrue(); + expect($thirdItemExists)->toBeFalse(); }); it('sets item to geometry collection', function (): void { - $geometryCollection = new GeometryCollection([ - new Polygon([ - new LineString([ + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ]); + $lineString = new LineString([ new Point(0, 180), new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - new Point(0, 180), - ]); - $lineString = new LineString([ - new Point(0, 180), - new Point(1, 179), - ]); - - $geometryCollection[2] = $lineString; - - expect($geometryCollection[2])->toBe($lineString); + ]); + + $geometryCollection[2] = $lineString; + + expect($geometryCollection[2])->toBe($lineString); }); it('throws exception when setting invalid item to geometry collection', function (): void { - $polygon = new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]); - - expect(function () use ($polygon): void { - // @phpstan-ignore-next-line - $polygon[1] = new Point(0, 180); - })->toThrow(InvalidArgumentException::class); + $polygon = new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]); + + expect(function () use ($polygon): void { + // @phpstan-ignore-next-line + $polygon[1] = new Point(0, 180); + })->toThrow(InvalidArgumentException::class); }); it('casts a GeometryCollection to a string', function (): void { - $geometryCollection = new GeometryCollection([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - new Point(0, 180), - ]); - - expect($geometryCollection->__toString())->toEqual('GEOMETRYCOLLECTION(POLYGON((180 0, 179 1, 178 2, 177 3, 180 0)), POINT(180 0))'); + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ]); + + expect($geometryCollection->__toString())->toEqual('GEOMETRYCOLLECTION(POLYGON((180 0, 179 1, 178 2, 177 3, 180 0)), POINT(180 0))'); }); it('adds a macro toGeometryCollection', function (): void { - Geometry::macro('getName', function (): string { - /** @var Geometry $this */ - // @phpstan-ignore-next-line - return class_basename($this); - }); + Geometry::macro('getName', function (): string { + /** @var Geometry $this */ + // @phpstan-ignore-next-line + return class_basename($this); + }); + + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ]); - $geometryCollection = new GeometryCollection([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - new Point(0, 180), - ]); - - // @phpstan-ignore-next-line - expect($geometryCollection->getName())->toBe('GeometryCollection'); + // @phpstan-ignore-next-line + expect($geometryCollection->getName())->toBe('GeometryCollection'); }); diff --git a/tests/Objects/GeometryTest.php b/tests/Objects/GeometryTest.php index 976b778..d157f4b 100644 --- a/tests/Objects/GeometryTest.php +++ b/tests/Objects/GeometryTest.php @@ -12,126 +12,126 @@ use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace; it('throws exception when generating geometry from other geometry WKB', function (): void { - expect(function (): void { - $pointWkb = (new Point(0, 180))->toWkb(); + expect(function (): void { + $pointWkb = (new Point(0, 180))->toWkb(); - LineString::fromWkb($pointWkb); - })->toThrow(InvalidArgumentException::class); + LineString::fromWkb($pointWkb); + })->toThrow(InvalidArgumentException::class); }); it('throws exception when generating geometry with invalid latitude', function (): void { - expect(function (): void { - $point = (new Point(91, 0, Srid::WGS84->value)); - TestPlace::factory()->create(['point' => $point]); - })->toThrow(QueryException::class); + expect(function (): void { + $point = (new Point(91, 0, Srid::WGS84->value)); + TestPlace::factory()->create(['point' => $point]); + })->toThrow(QueryException::class); })->skip(fn () => ! AxisOrder::supported(DB::connection())); it('throws exception when generating geometry with invalid latitude - without axis-order', function (): void { - expect(function (): void { - $point = (new Point(91, 0, Srid::WGS84->value)); - TestPlace::factory()->create(['point' => $point]); - - TestPlace::query() - ->withDistanceSphere('point', new Point(1, 1, Srid::WGS84->value)) - ->firstOrFail(); - })->toThrow(QueryException::class); + expect(function (): void { + $point = (new Point(91, 0, Srid::WGS84->value)); + TestPlace::factory()->create(['point' => $point]); + + TestPlace::query() + ->withDistanceSphere('point', new Point(1, 1, Srid::WGS84->value)) + ->firstOrFail(); + })->toThrow(QueryException::class); })->skip(fn () => AxisOrder::supported(DB::connection()) || DB::connection() instanceof PostgresConnection); it('throws exception when generating geometry with invalid longitude', function (): void { - expect(function (): void { - $point = (new Point(0, 181, Srid::WGS84->value)); - TestPlace::factory()->create(['point' => $point]); - })->toThrow(QueryException::class); + expect(function (): void { + $point = (new Point(0, 181, Srid::WGS84->value)); + TestPlace::factory()->create(['point' => $point]); + })->toThrow(QueryException::class); })->skip(fn () => ! AxisOrder::supported(DB::connection())); it('throws exception when generating geometry with invalid longitude - without axis-order', function (): void { - expect(function (): void { - $point = (new Point(0, 181, Srid::WGS84->value)); - TestPlace::factory()->create(['point' => $point]); - - TestPlace::query() - ->withDistanceSphere('point', new Point(1, 1, Srid::WGS84->value)) - ->firstOrFail(); - })->toThrow(QueryException::class); + expect(function (): void { + $point = (new Point(0, 181, Srid::WGS84->value)); + TestPlace::factory()->create(['point' => $point]); + + TestPlace::query() + ->withDistanceSphere('point', new Point(1, 1, Srid::WGS84->value)) + ->firstOrFail(); + })->toThrow(QueryException::class); })->skip(fn () => AxisOrder::supported(DB::connection()) || DB::connection() instanceof PostgresConnection); it('throws exception when generating geometry from other geometry WKT', function (): void { - expect(function (): void { - $pointWkt = 'POINT(180 0)'; + expect(function (): void { + $pointWkt = 'POINT(180 0)'; - LineString::fromWkt($pointWkt); - })->toThrow(InvalidArgumentException::class); + LineString::fromWkt($pointWkt); + })->toThrow(InvalidArgumentException::class); }); it('throws exception when generating geometry from non-JSON', function (): void { - expect(function (): void { - Point::fromJson('invalid-value'); - })->toThrow(InvalidArgumentException::class); + expect(function (): void { + Point::fromJson('invalid-value'); + })->toThrow(InvalidArgumentException::class); }); it('throws exception when generating geometry from empty JSON', function (): void { - expect(function (): void { - Point::fromJson('{}'); - })->toThrow(InvalidArgumentException::class); + expect(function (): void { + Point::fromJson('{}'); + })->toThrow(InvalidArgumentException::class); }); it('throws exception when generating geometry from other geometry JSON', function (): void { - expect(function (): void { - $pointJson = '{"type":"Point","coordinates":[0,180]}'; + expect(function (): void { + $pointJson = '{"type":"Point","coordinates":[0,180]}'; - LineString::fromJson($pointJson); - })->toThrow(InvalidArgumentException::class); + LineString::fromJson($pointJson); + })->toThrow(InvalidArgumentException::class); }); it('creates an SQL expression from a geometry', function (): void { - $point = new Point(0, 180, Srid::WGS84->value); + $point = new Point(0, 180, Srid::WGS84->value); - $expression = $point->toSqlExpression(DB::connection()); + $expression = $point->toSqlExpression(DB::connection()); - $grammar = DB::getQueryGrammar(); - $expressionValue = $expression->getValue($grammar); - expect($expressionValue)->toEqual("ST_GeomFromText('POINT(180 0)', 4326, 'axis-order=long-lat')"); + $grammar = DB::getQueryGrammar(); + $expressionValue = $expression->getValue($grammar); + expect($expressionValue)->toEqual("ST_GeomFromText('POINT(180 0)', 4326, 'axis-order=long-lat')"); })->skip(fn () => ! AxisOrder::supported(DB::connection())); it('creates an SQL expression from a geometry - without axis-order', function (): void { - $point = new Point(0, 180, Srid::WGS84->value); + $point = new Point(0, 180, Srid::WGS84->value); - $expression = $point->toSqlExpression(DB::connection()); + $expression = $point->toSqlExpression(DB::connection()); - $grammar = DB::getQueryGrammar(); - $expressionValue = $expression->getValue($grammar); - expect($expressionValue)->toEqual( - (new GeometryExpression("ST_GeomFromText('POINT(180 0)', 4326)"))->normalize(DB::connection()) - ); + $grammar = DB::getQueryGrammar(); + $expressionValue = $expression->getValue($grammar); + expect($expressionValue)->toEqual( + (new GeometryExpression("ST_GeomFromText('POINT(180 0)', 4326)"))->normalize(DB::connection()) + ); })->skip(fn () => AxisOrder::supported(DB::connection())); it('creates a geometry object from a geo json array', function (): void { - $point = new Point(0, 180); - $pointGeoJsonArray = $point->toArray(); + $point = new Point(0, 180); + $pointGeoJsonArray = $point->toArray(); - $geometryCollectionFromArray = Point::fromArray($pointGeoJsonArray); + $geometryCollectionFromArray = Point::fromArray($pointGeoJsonArray); - expect($geometryCollectionFromArray)->toEqual($point); + expect($geometryCollectionFromArray)->toEqual($point); }); it('throws exception when creating a geometry object from an invalid geo json array', function (): void { - $invalidPointGeoJsonArray = [ - 'type' => 'InvalidGeometryType', - 'coordinates' => [0, 180], - ]; - - expect(function () use ($invalidPointGeoJsonArray): void { - Geometry::fromArray($invalidPointGeoJsonArray); - })->toThrow(InvalidArgumentException::class); + $invalidPointGeoJsonArray = [ + 'type' => 'InvalidGeometryType', + 'coordinates' => [0, 180], + ]; + + expect(function () use ($invalidPointGeoJsonArray): void { + Geometry::fromArray($invalidPointGeoJsonArray); + })->toThrow(InvalidArgumentException::class); }); it('throws exception when creating a geometry object from another geometry geo json array', function (): void { - $pointGeoJsonArray = [ - 'type' => 'Point', - 'coordinates' => [0, 180], - ]; - - expect(function () use ($pointGeoJsonArray): void { - LineString::fromArray($pointGeoJsonArray); - })->toThrow(InvalidArgumentException::class); + $pointGeoJsonArray = [ + 'type' => 'Point', + 'coordinates' => [0, 180], + ]; + + expect(function () use ($pointGeoJsonArray): void { + LineString::fromArray($pointGeoJsonArray); + })->toThrow(InvalidArgumentException::class); }); diff --git a/tests/Objects/LineStringTest.php b/tests/Objects/LineStringTest.php index 3256ec2..2ba5b93 100644 --- a/tests/Objects/LineStringTest.php +++ b/tests/Objects/LineStringTest.php @@ -8,182 +8,182 @@ use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace; it('creates a model record with line string', function (): void { - $lineString = new LineString([ - new Point(0, 180), - new Point(1, 179), - ]); + $lineString = new LineString([ + new Point(0, 180), + new Point(1, 179), + ]); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['line_string' => $lineString]); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['line_string' => $lineString]); - expect($testPlace->line_string)->toBeInstanceOf(LineString::class); - expect($testPlace->line_string)->toEqual($lineString); + expect($testPlace->line_string)->toBeInstanceOf(LineString::class); + expect($testPlace->line_string)->toEqual($lineString); }); it('creates a model record with line string with SRID integer', function (): void { - $lineString = new LineString([ - new Point(0, 180), - new Point(1, 179), - ], Srid::WGS84->value); + $lineString = new LineString([ + new Point(0, 180), + new Point(1, 179), + ], Srid::WGS84->value); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['line_string' => $lineString]); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['line_string' => $lineString]); - expect($testPlace->line_string->srid)->toBe(Srid::WGS84->value); + expect($testPlace->line_string->srid)->toBe(Srid::WGS84->value); }); it('creates a model record with line string with SRID enum', function (): void { - $lineString = new LineString([ - new Point(0, 180), - new Point(1, 179), - ], Srid::WGS84); + $lineString = new LineString([ + new Point(0, 180), + new Point(1, 179), + ], Srid::WGS84); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['line_string' => $lineString]); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['line_string' => $lineString]); - expect($testPlace->line_string->srid)->toBe(Srid::WGS84->value); + expect($testPlace->line_string->srid)->toBe(Srid::WGS84->value); }); it('creates line string from JSON', function (): void { - $lineString = new LineString([ - new Point(0, 180), - new Point(1, 179), - ]); + $lineString = new LineString([ + new Point(0, 180), + new Point(1, 179), + ]); - $lineStringFromJson = LineString::fromJson('{"type":"LineString","coordinates":[[180,0],[179,1]]}'); + $lineStringFromJson = LineString::fromJson('{"type":"LineString","coordinates":[[180,0],[179,1]]}'); - expect($lineStringFromJson)->toEqual($lineString); + expect($lineStringFromJson)->toEqual($lineString); }); it('creates line string with SRID from JSON', function (): void { - $lineString = new LineString([ - new Point(0, 180), - new Point(1, 179), - ], Srid::WGS84->value); + $lineString = new LineString([ + new Point(0, 180), + new Point(1, 179), + ], Srid::WGS84->value); - $lineStringFromJson = LineString::fromJson('{"type":"LineString","coordinates":[[180,0],[179,1]]}', Srid::WGS84->value); + $lineStringFromJson = LineString::fromJson('{"type":"LineString","coordinates":[[180,0],[179,1]]}', Srid::WGS84->value); - expect($lineStringFromJson)->toEqual($lineString); + expect($lineStringFromJson)->toEqual($lineString); }); it('generates line string JSON', function (): void { - $lineString = new LineString([ - new Point(0, 180), - new Point(1, 179), - ]); + $lineString = new LineString([ + new Point(0, 180), + new Point(1, 179), + ]); - $json = $lineString->toJson(); + $json = $lineString->toJson(); - $expectedJson = '{"type":"LineString","coordinates":[[180,0],[179,1]]}'; - expect($json)->toBe($expectedJson); + $expectedJson = '{"type":"LineString","coordinates":[[180,0],[179,1]]}'; + expect($json)->toBe($expectedJson); }); it('generates line string feature collection JSON', function (): void { - $lineString = new LineString([ - new Point(0, 180), - new Point(1, 179), - ]); + $lineString = new LineString([ + new Point(0, 180), + new Point(1, 179), + ]); - $featureCollectionJson = $lineString->toFeatureCollectionJson(); + $featureCollectionJson = $lineString->toFeatureCollectionJson(); - $expectedFeatureCollectionJson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"LineString","coordinates":[[180,0],[179,1]]}}]}'; - expect($featureCollectionJson)->toBe($expectedFeatureCollectionJson); + $expectedFeatureCollectionJson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"LineString","coordinates":[[180,0],[179,1]]}}]}'; + expect($featureCollectionJson)->toBe($expectedFeatureCollectionJson); }); it('creates line string from WKT', function (): void { - $lineString = new LineString([ - new Point(0, 180), - new Point(1, 179), - ]); + $lineString = new LineString([ + new Point(0, 180), + new Point(1, 179), + ]); - $lineStringFromWkt = LineString::fromWkt('LINESTRING(180 0, 179 1)'); + $lineStringFromWkt = LineString::fromWkt('LINESTRING(180 0, 179 1)'); - expect($lineStringFromWkt)->toEqual($lineString); + expect($lineStringFromWkt)->toEqual($lineString); }); it('creates line string with SRID from WKT', function (): void { - $lineString = new LineString([ - new Point(0, 180), - new Point(1, 179), - ], Srid::WGS84->value); + $lineString = new LineString([ + new Point(0, 180), + new Point(1, 179), + ], Srid::WGS84->value); - $lineStringFromWkt = LineString::fromWkt('LINESTRING(180 0, 179 1)', Srid::WGS84->value); + $lineStringFromWkt = LineString::fromWkt('LINESTRING(180 0, 179 1)', Srid::WGS84->value); - expect($lineStringFromWkt)->toEqual($lineString); + expect($lineStringFromWkt)->toEqual($lineString); }); it('generates line string WKT', function (): void { - $lineString = new LineString([ - new Point(0, 180), - new Point(1, 179), - ]); + $lineString = new LineString([ + new Point(0, 180), + new Point(1, 179), + ]); - $wkt = $lineString->toWkt(); + $wkt = $lineString->toWkt(); - $expectedWkt = 'LINESTRING(180 0, 179 1)'; - expect($wkt)->toBe($expectedWkt); + $expectedWkt = 'LINESTRING(180 0, 179 1)'; + expect($wkt)->toBe($expectedWkt); }); it('creates line string from WKB', function (): void { - $lineString = new LineString([ - new Point(0, 180), - new Point(1, 179), - ]); + $lineString = new LineString([ + new Point(0, 180), + new Point(1, 179), + ]); - $lineStringFromWkb = LineString::fromWkb($lineString->toWkb()); + $lineStringFromWkb = LineString::fromWkb($lineString->toWkb()); - expect($lineStringFromWkb)->toEqual($lineString); + expect($lineStringFromWkb)->toEqual($lineString); }); it('creates line string with SRID from WKB', function (): void { - $lineString = new LineString([ - new Point(0, 180), - new Point(1, 179), - ], Srid::WGS84->value); + $lineString = new LineString([ + new Point(0, 180), + new Point(1, 179), + ], Srid::WGS84->value); - $lineStringFromWkb = LineString::fromWkb($lineString->toWkb()); + $lineStringFromWkb = LineString::fromWkb($lineString->toWkb()); - expect($lineStringFromWkb)->toEqual($lineString); + expect($lineStringFromWkb)->toEqual($lineString); }); it('throws exception when line string has less than two points', function (): void { - expect(function (): void { - new LineString([ - new Point(0, 180), - ]); - })->toThrow(InvalidArgumentException::class); + expect(function (): void { + new LineString([ + new Point(0, 180), + ]); + })->toThrow(InvalidArgumentException::class); }); it('throws exception when creating line string from incorrect geometry', function (): void { - expect(function (): void { - // @phpstan-ignore-next-line - new LineString([ - Polygon::fromJson('{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}'), - ]); - })->toThrow(InvalidArgumentException::class); + expect(function (): void { + // @phpstan-ignore-next-line + new LineString([ + Polygon::fromJson('{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}'), + ]); + })->toThrow(InvalidArgumentException::class); }); it('casts a LineString to a string', function (): void { - $lineString = new LineString([ - new Point(0, 180), - new Point(1, 179), - ]); + $lineString = new LineString([ + new Point(0, 180), + new Point(1, 179), + ]); - expect($lineString->__toString())->toEqual('LINESTRING(180 0, 179 1)'); + expect($lineString->__toString())->toEqual('LINESTRING(180 0, 179 1)'); }); it('adds a macro toLineString', function (): void { - Geometry::macro('getName', function (): string { - /** @var Geometry $this */ - // @phpstan-ignore-next-line - return class_basename($this); - }); - - $lineString = new LineString([ - new Point(0, 180), - new Point(1, 179), - ]); + Geometry::macro('getName', function (): string { + /** @var Geometry $this */ + // @phpstan-ignore-next-line + return class_basename($this); + }); + + $lineString = new LineString([ + new Point(0, 180), + new Point(1, 179), + ]); - // @phpstan-ignore-next-line - expect($lineString->getName())->toBe('LineString'); + // @phpstan-ignore-next-line + expect($lineString->getName())->toBe('LineString'); }); diff --git a/tests/Objects/MultiLineStringTest.php b/tests/Objects/MultiLineStringTest.php index c16d810..5e48b7e 100644 --- a/tests/Objects/MultiLineStringTest.php +++ b/tests/Objects/MultiLineStringTest.php @@ -8,208 +8,208 @@ use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace; it('creates a model record with multi line string', function (): void { - $multiLineString = new MultiLineString([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - ]), - ]); + $multiLineString = new MultiLineString([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ]); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['multi_line_string' => $multiLineString]); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['multi_line_string' => $multiLineString]); - expect($testPlace->multi_line_string)->toBeInstanceOf(MultiLineString::class); - expect($testPlace->multi_line_string)->toEqual($multiLineString); + expect($testPlace->multi_line_string)->toBeInstanceOf(MultiLineString::class); + expect($testPlace->multi_line_string)->toEqual($multiLineString); }); it('creates a model record with multi line string with SRID integer', function (): void { - $multiLineString = new MultiLineString([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - ]), - ], Srid::WGS84->value); + $multiLineString = new MultiLineString([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ], Srid::WGS84->value); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['multi_line_string' => $multiLineString]); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['multi_line_string' => $multiLineString]); - expect($testPlace->multi_line_string->srid)->toBe(Srid::WGS84->value); + expect($testPlace->multi_line_string->srid)->toBe(Srid::WGS84->value); }); it('creates a model record with multi line string with SRID enum', function (): void { - $multiLineString = new MultiLineString([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - ]), - ], Srid::WGS84); + $multiLineString = new MultiLineString([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ], Srid::WGS84); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['multi_line_string' => $multiLineString]); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['multi_line_string' => $multiLineString]); - expect($testPlace->multi_line_string->srid)->toBe(Srid::WGS84->value); + expect($testPlace->multi_line_string->srid)->toBe(Srid::WGS84->value); }); it('creates multi line string from JSON', function (): void { - $multiLineString = new MultiLineString([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - ]), - ]); + $multiLineString = new MultiLineString([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ]); - $multiLineStringFromJson = MultiLineString::fromJson('{"type":"MultiLineString","coordinates":[[[180,0],[179,1]]]}'); + $multiLineStringFromJson = MultiLineString::fromJson('{"type":"MultiLineString","coordinates":[[[180,0],[179,1]]]}'); - expect($multiLineStringFromJson)->toEqual($multiLineString); + expect($multiLineStringFromJson)->toEqual($multiLineString); }); it('creates multi line string with SRID from JSON', function (): void { - $multiLineString = new MultiLineString([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - ]), - ], Srid::WGS84->value); + $multiLineString = new MultiLineString([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ], Srid::WGS84->value); - $multiLineStringFromJson = MultiLineString::fromJson('{"type":"MultiLineString","coordinates":[[[180,0],[179,1]]]}', Srid::WGS84->value); + $multiLineStringFromJson = MultiLineString::fromJson('{"type":"MultiLineString","coordinates":[[[180,0],[179,1]]]}', Srid::WGS84->value); - expect($multiLineStringFromJson)->toEqual($multiLineString); + expect($multiLineStringFromJson)->toEqual($multiLineString); }); it('generates multi line string JSON', function (): void { - $multiLineString = new MultiLineString([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - ]), - ]); + $multiLineString = new MultiLineString([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ]); - $json = $multiLineString->toJson(); + $json = $multiLineString->toJson(); - $expectedJson = '{"type":"MultiLineString","coordinates":[[[180,0],[179,1]]]}'; - expect($json)->toBe($expectedJson); + $expectedJson = '{"type":"MultiLineString","coordinates":[[[180,0],[179,1]]]}'; + expect($json)->toBe($expectedJson); }); it('generates multi line string feature collection JSON', function (): void { - $multiLineString = new MultiLineString([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - ]), - ]); + $multiLineString = new MultiLineString([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ]); - $featureCollectionJson = $multiLineString->toFeatureCollectionJson(); + $featureCollectionJson = $multiLineString->toFeatureCollectionJson(); - $expectedFeatureCollectionJson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"MultiLineString","coordinates":[[[180,0],[179,1]]]}}]}'; - expect($featureCollectionJson)->toBe($expectedFeatureCollectionJson); + $expectedFeatureCollectionJson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"MultiLineString","coordinates":[[[180,0],[179,1]]]}}]}'; + expect($featureCollectionJson)->toBe($expectedFeatureCollectionJson); }); it('creates multi line string from WKT', function (): void { - $multiLineString = new MultiLineString([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - ]), - ]); + $multiLineString = new MultiLineString([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ]); - $multiLineStringFromWkt = MultiLineString::fromWkt('MULTILINESTRING((180 0, 179 1))'); + $multiLineStringFromWkt = MultiLineString::fromWkt('MULTILINESTRING((180 0, 179 1))'); - expect($multiLineStringFromWkt)->toEqual($multiLineString); + expect($multiLineStringFromWkt)->toEqual($multiLineString); }); it('creates multi line string with SRID from WKT', function (): void { - $multiLineString = new MultiLineString([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - ]), - ], Srid::WGS84->value); + $multiLineString = new MultiLineString([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ], Srid::WGS84->value); - $multiLineStringFromWkt = MultiLineString::fromWkt('MULTILINESTRING((180 0, 179 1))', Srid::WGS84->value); + $multiLineStringFromWkt = MultiLineString::fromWkt('MULTILINESTRING((180 0, 179 1))', Srid::WGS84->value); - expect($multiLineStringFromWkt)->toEqual($multiLineString); + expect($multiLineStringFromWkt)->toEqual($multiLineString); }); it('generates multi line string WKT', function (): void { - $multiLineString = new MultiLineString([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - ]), - ]); + $multiLineString = new MultiLineString([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ]); - $wkt = $multiLineString->toWkt(); + $wkt = $multiLineString->toWkt(); - $expectedWkt = 'MULTILINESTRING((180 0, 179 1))'; - expect($wkt)->toBe($expectedWkt); + $expectedWkt = 'MULTILINESTRING((180 0, 179 1))'; + expect($wkt)->toBe($expectedWkt); }); it('creates multi line string from WKB', function (): void { - $multiLineString = new MultiLineString([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - ]), - ]); + $multiLineString = new MultiLineString([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ]); - $multiLineStringFromWkb = MultiLineString::fromWkb($multiLineString->toWkb()); + $multiLineStringFromWkb = MultiLineString::fromWkb($multiLineString->toWkb()); - expect($multiLineStringFromWkb)->toEqual($multiLineString); + expect($multiLineStringFromWkb)->toEqual($multiLineString); }); it('creates multi line string with SRID from WKB', function (): void { - $multiLineString = new MultiLineString([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - ]), - ], Srid::WGS84->value); + $multiLineString = new MultiLineString([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ], Srid::WGS84->value); - $multiLineStringFromWkb = MultiLineString::fromWkb($multiLineString->toWkb()); + $multiLineStringFromWkb = MultiLineString::fromWkb($multiLineString->toWkb()); - expect($multiLineStringFromWkb)->toEqual($multiLineString); + expect($multiLineStringFromWkb)->toEqual($multiLineString); }); it('throws exception when multi line string has no line strings', function (): void { - expect(function (): void { - new MultiLineString([]); - })->toThrow(InvalidArgumentException::class); + expect(function (): void { + new MultiLineString([]); + })->toThrow(InvalidArgumentException::class); }); it('throws exception when creating multi line string from incorrect geometry', function (): void { - expect(function (): void { - // @phpstan-ignore-next-line - new MultiLineString([ - new Point(0, 0), - ]); - })->toThrow(InvalidArgumentException::class); + expect(function (): void { + // @phpstan-ignore-next-line + new MultiLineString([ + new Point(0, 0), + ]); + })->toThrow(InvalidArgumentException::class); }); it('casts a MultiLineString to a string', function (): void { - $multiLineString = new MultiLineString([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - ]), - ]); + $multiLineString = new MultiLineString([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ]); - expect($multiLineString->__toString())->toEqual('MULTILINESTRING((180 0, 179 1))'); + expect($multiLineString->__toString())->toEqual('MULTILINESTRING((180 0, 179 1))'); }); it('adds a macro toMultiLineString', function (): void { - Geometry::macro('getName', function (): string { - /** @var Geometry $this */ + Geometry::macro('getName', function (): string { + /** @var Geometry $this */ + // @phpstan-ignore-next-line + return class_basename($this); + }); + + $multiLineString = new MultiLineString([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ]); + // @phpstan-ignore-next-line - return class_basename($this); - }); - - $multiLineString = new MultiLineString([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - ]), - ]); - - // @phpstan-ignore-next-line - expect($multiLineString->getName())->toBe('MultiLineString'); + expect($multiLineString->getName())->toBe('MultiLineString'); }); diff --git a/tests/Objects/MultiPointTest.php b/tests/Objects/MultiPointTest.php index 1bc1add..05069e9 100644 --- a/tests/Objects/MultiPointTest.php +++ b/tests/Objects/MultiPointTest.php @@ -8,166 +8,166 @@ use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace; it('creates a model record with multi point', function (): void { - $multiPoint = new MultiPoint([ - new Point(0, 180), - ]); + $multiPoint = new MultiPoint([ + new Point(0, 180), + ]); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['multi_point' => $multiPoint]); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['multi_point' => $multiPoint]); - expect($testPlace->multi_point)->toBeInstanceOf(MultiPoint::class); - expect($testPlace->multi_point)->toEqual($multiPoint); + expect($testPlace->multi_point)->toBeInstanceOf(MultiPoint::class); + expect($testPlace->multi_point)->toEqual($multiPoint); }); it('creates a model record with multi point with SRID integer', function (): void { - $multiPoint = new MultiPoint([ - new Point(0, 180), - ], Srid::WGS84->value); + $multiPoint = new MultiPoint([ + new Point(0, 180), + ], Srid::WGS84->value); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['multi_point' => $multiPoint]); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['multi_point' => $multiPoint]); - expect($testPlace->multi_point->srid)->toBe(Srid::WGS84->value); + expect($testPlace->multi_point->srid)->toBe(Srid::WGS84->value); }); it('creates a model record with multi point with SRID enum', function (): void { - $multiPoint = new MultiPoint([ - new Point(0, 180), - ], Srid::WGS84); + $multiPoint = new MultiPoint([ + new Point(0, 180), + ], Srid::WGS84); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['multi_point' => $multiPoint]); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['multi_point' => $multiPoint]); - expect($testPlace->multi_point->srid)->toBe(Srid::WGS84->value); + expect($testPlace->multi_point->srid)->toBe(Srid::WGS84->value); }); it('creates multi point from JSON', function (): void { - $multiPoint = new MultiPoint([ - new Point(0, 180), - ]); + $multiPoint = new MultiPoint([ + new Point(0, 180), + ]); - $multiPointFromJson = MultiPoint::fromJson('{"type":"MultiPoint","coordinates":[[180,0]]}'); + $multiPointFromJson = MultiPoint::fromJson('{"type":"MultiPoint","coordinates":[[180,0]]}'); - expect($multiPointFromJson)->toEqual($multiPoint); + expect($multiPointFromJson)->toEqual($multiPoint); }); it('creates multi point with SRID from JSON', function (): void { - $multiPoint = new MultiPoint([ - new Point(0, 180), - ], Srid::WGS84->value); + $multiPoint = new MultiPoint([ + new Point(0, 180), + ], Srid::WGS84->value); - $multiPointFromJson = MultiPoint::fromJson('{"type":"MultiPoint","coordinates":[[180,0]]}', Srid::WGS84->value); + $multiPointFromJson = MultiPoint::fromJson('{"type":"MultiPoint","coordinates":[[180,0]]}', Srid::WGS84->value); - expect($multiPointFromJson)->toEqual($multiPoint); + expect($multiPointFromJson)->toEqual($multiPoint); }); it('generates multi point JSON', function (): void { - $multiPoint = new MultiPoint([ - new Point(0, 180), - ]); + $multiPoint = new MultiPoint([ + new Point(0, 180), + ]); - $json = $multiPoint->toJson(); + $json = $multiPoint->toJson(); - $expectedJson = '{"type":"MultiPoint","coordinates":[[180,0]]}'; - expect($json)->toBe($expectedJson); + $expectedJson = '{"type":"MultiPoint","coordinates":[[180,0]]}'; + expect($json)->toBe($expectedJson); }); it('generates multi point feature collection JSON', function (): void { - $multiPoint = new MultiPoint([ - new Point(0, 180), - ]); + $multiPoint = new MultiPoint([ + new Point(0, 180), + ]); - $multiPointFeatureCollectionJson = $multiPoint->toFeatureCollectionJson(); + $multiPointFeatureCollectionJson = $multiPoint->toFeatureCollectionJson(); - $expectedFeatureCollectionJson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"MultiPoint","coordinates":[[180,0]]}}]}'; - expect($multiPointFeatureCollectionJson)->toBe($expectedFeatureCollectionJson); + $expectedFeatureCollectionJson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"MultiPoint","coordinates":[[180,0]]}}]}'; + expect($multiPointFeatureCollectionJson)->toBe($expectedFeatureCollectionJson); }); it('creates multi point from WKT', function (): void { - $multiPoint = new MultiPoint([ - new Point(0, 180), - ]); + $multiPoint = new MultiPoint([ + new Point(0, 180), + ]); - $multiPointFromWkt = MultiPoint::fromWkt('MULTIPOINT(180 0)'); + $multiPointFromWkt = MultiPoint::fromWkt('MULTIPOINT(180 0)'); - expect($multiPointFromWkt)->toEqual($multiPoint); + expect($multiPointFromWkt)->toEqual($multiPoint); }); it('creates multi point with SRID from WKT', function (): void { - $multiPoint = new MultiPoint([ - new Point(0, 180), - ], Srid::WGS84->value); + $multiPoint = new MultiPoint([ + new Point(0, 180), + ], Srid::WGS84->value); - $multiPointFromWkt = MultiPoint::fromWkt('MULTIPOINT(180 0)', Srid::WGS84->value); + $multiPointFromWkt = MultiPoint::fromWkt('MULTIPOINT(180 0)', Srid::WGS84->value); - expect($multiPointFromWkt)->toEqual($multiPoint); + expect($multiPointFromWkt)->toEqual($multiPoint); }); it('generates multi point WKT', function (): void { - $multiPoint = new MultiPoint([ - new Point(0, 180), - ]); + $multiPoint = new MultiPoint([ + new Point(0, 180), + ]); - $wkt = $multiPoint->toWkt(); + $wkt = $multiPoint->toWkt(); - $expectedWkt = 'MULTIPOINT(180 0)'; - expect($wkt)->toBe($expectedWkt); + $expectedWkt = 'MULTIPOINT(180 0)'; + expect($wkt)->toBe($expectedWkt); }); it('creates multi point from WKB', function (): void { - $multiPoint = new MultiPoint([ - new Point(0, 180), - ]); + $multiPoint = new MultiPoint([ + new Point(0, 180), + ]); - $multiPointFromWkb = MultiPoint::fromWkb($multiPoint->toWkb()); + $multiPointFromWkb = MultiPoint::fromWkb($multiPoint->toWkb()); - expect($multiPointFromWkb)->toEqual($multiPoint); + expect($multiPointFromWkb)->toEqual($multiPoint); }); it('creates multi point with SRID from WKB', function (): void { - $multiPoint = new MultiPoint([ - new Point(0, 180), - ], Srid::WGS84->value); + $multiPoint = new MultiPoint([ + new Point(0, 180), + ], Srid::WGS84->value); - $multiPointFromWkb = MultiPoint::fromWkb($multiPoint->toWkb()); + $multiPointFromWkb = MultiPoint::fromWkb($multiPoint->toWkb()); - expect($multiPointFromWkb)->toEqual($multiPoint); + expect($multiPointFromWkb)->toEqual($multiPoint); }); it('throws exception when multi point has no points', function (): void { - expect(function (): void { - new MultiPoint([]); - })->toThrow(InvalidArgumentException::class); + expect(function (): void { + new MultiPoint([]); + })->toThrow(InvalidArgumentException::class); }); it('throws exception when creating multi point from incorrect geometry', function (): void { - expect(function (): void { - // @phpstan-ignore-next-line - new MultiPoint([ - Polygon::fromJson('{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}'), - ]); - })->toThrow(InvalidArgumentException::class); + expect(function (): void { + // @phpstan-ignore-next-line + new MultiPoint([ + Polygon::fromJson('{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}'), + ]); + })->toThrow(InvalidArgumentException::class); }); it('casts a MultiPoint to a string', function (): void { - $multiPoint = new MultiPoint([ - new Point(0, 180), - ]); + $multiPoint = new MultiPoint([ + new Point(0, 180), + ]); - expect($multiPoint->__toString())->toEqual('MULTIPOINT(180 0)'); + expect($multiPoint->__toString())->toEqual('MULTIPOINT(180 0)'); }); it('adds a macro toMultiPoint', function (): void { - Geometry::macro('getName', function (): string { - /** @var Geometry $this */ - // @phpstan-ignore-next-line - return class_basename($this); - }); - - $multiPoint = new MultiPoint([ - new Point(0, 180), - ]); + Geometry::macro('getName', function (): string { + /** @var Geometry $this */ + // @phpstan-ignore-next-line + return class_basename($this); + }); + + $multiPoint = new MultiPoint([ + new Point(0, 180), + ]); - // @phpstan-ignore-next-line - expect($multiPoint->getName())->toBe('MultiPoint'); + // @phpstan-ignore-next-line + expect($multiPoint->getName())->toBe('MultiPoint'); }); diff --git a/tests/Objects/MultiPolygonTest.php b/tests/Objects/MultiPolygonTest.php index 1bc104b..d85ae6a 100644 --- a/tests/Objects/MultiPolygonTest.php +++ b/tests/Objects/MultiPolygonTest.php @@ -9,278 +9,278 @@ use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace; it('creates a model record with multi polygon', function (): void { - $multiPolygon = new MultiPolygon([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - ]); - - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['multi_polygon' => $multiPolygon]); - - expect($testPlace->multi_polygon)->toBeInstanceOf(MultiPolygon::class); - expect($testPlace->multi_polygon)->toEqual($multiPolygon); + $multiPolygon = new MultiPolygon([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + ]); + + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['multi_polygon' => $multiPolygon]); + + expect($testPlace->multi_polygon)->toBeInstanceOf(MultiPolygon::class); + expect($testPlace->multi_polygon)->toEqual($multiPolygon); }); it('creates a model record with multi polygon with SRID integer', function (): void { - $multiPolygon = new MultiPolygon([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - ], Srid::WGS84->value); - - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['multi_polygon' => $multiPolygon]); - - expect($testPlace->multi_polygon->srid)->toBe(Srid::WGS84->value); + $multiPolygon = new MultiPolygon([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + ], Srid::WGS84->value); + + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['multi_polygon' => $multiPolygon]); + + expect($testPlace->multi_polygon->srid)->toBe(Srid::WGS84->value); }); it('creates a model record with multi polygon with SRID enum', function (): void { - $multiPolygon = new MultiPolygon([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - ], Srid::WGS84); - - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['multi_polygon' => $multiPolygon]); - - expect($testPlace->multi_polygon->srid)->toBe(Srid::WGS84->value); + $multiPolygon = new MultiPolygon([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + ], Srid::WGS84); + + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['multi_polygon' => $multiPolygon]); + + expect($testPlace->multi_polygon->srid)->toBe(Srid::WGS84->value); }); it('creates multi polygon from JSON', function (): void { - $multiPolygon = new MultiPolygon([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - ]); - - $multiPolygonFromJson = MultiPolygon::fromJson('{"type":"MultiPolygon","coordinates":[[[[180,0],[179,1],[178,2],[177,3],[180,0]]]]}'); - - expect($multiPolygonFromJson)->toEqual($multiPolygon); + $multiPolygon = new MultiPolygon([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + ]); + + $multiPolygonFromJson = MultiPolygon::fromJson('{"type":"MultiPolygon","coordinates":[[[[180,0],[179,1],[178,2],[177,3],[180,0]]]]}'); + + expect($multiPolygonFromJson)->toEqual($multiPolygon); }); it('creates multi polygon with SRID from JSON', function (): void { - $multiPolygon = new MultiPolygon([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - ], Srid::WGS84->value); - - $multiPolygonFromJson = MultiPolygon::fromJson('{"type":"MultiPolygon","coordinates":[[[[180,0],[179,1],[178,2],[177,3],[180,0]]]]}', Srid::WGS84->value); - - expect($multiPolygonFromJson)->toEqual($multiPolygon); + $multiPolygon = new MultiPolygon([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + ], Srid::WGS84->value); + + $multiPolygonFromJson = MultiPolygon::fromJson('{"type":"MultiPolygon","coordinates":[[[[180,0],[179,1],[178,2],[177,3],[180,0]]]]}', Srid::WGS84->value); + + expect($multiPolygonFromJson)->toEqual($multiPolygon); }); it('generates multi polygon JSON', function (): void { - $multiPolygon = new MultiPolygon([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - ]); - - $json = $multiPolygon->toJson(); - - $expectedJson = '{"type":"MultiPolygon","coordinates":[[[[180,0],[179,1],[178,2],[177,3],[180,0]]]]}'; - expect($json)->toBe($expectedJson); + $multiPolygon = new MultiPolygon([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + ]); + + $json = $multiPolygon->toJson(); + + $expectedJson = '{"type":"MultiPolygon","coordinates":[[[[180,0],[179,1],[178,2],[177,3],[180,0]]]]}'; + expect($json)->toBe($expectedJson); }); it('generates multi polygon feature collection JSON', function (): void { - $multiPolygon = new MultiPolygon([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - ]); - - $featureCollectionJson = $multiPolygon->toFeatureCollectionJson(); - - $expectedFeatureCollectionJson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"MultiPolygon","coordinates":[[[[180,0],[179,1],[178,2],[177,3],[180,0]]]]}}]}'; - expect($featureCollectionJson)->toBe($expectedFeatureCollectionJson); + $multiPolygon = new MultiPolygon([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + ]); + + $featureCollectionJson = $multiPolygon->toFeatureCollectionJson(); + + $expectedFeatureCollectionJson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"MultiPolygon","coordinates":[[[[180,0],[179,1],[178,2],[177,3],[180,0]]]]}}]}'; + expect($featureCollectionJson)->toBe($expectedFeatureCollectionJson); }); it('creates multi polygon from WKT', function (): void { - $multiPolygon = new MultiPolygon([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - ]); - - $multiPolygonFromWkt = MultiPolygon::fromWkt('MULTIPOLYGON(((180 0, 179 1, 178 2, 177 3, 180 0)))'); - - expect($multiPolygonFromWkt)->toEqual($multiPolygon); + $multiPolygon = new MultiPolygon([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + ]); + + $multiPolygonFromWkt = MultiPolygon::fromWkt('MULTIPOLYGON(((180 0, 179 1, 178 2, 177 3, 180 0)))'); + + expect($multiPolygonFromWkt)->toEqual($multiPolygon); }); it('creates multi polygon with SRID from WKT', function (): void { - $multiPolygon = new MultiPolygon([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - ], Srid::WGS84->value); - - $multiPolygonFromWkt = MultiPolygon::fromWkt('MULTIPOLYGON(((180 0, 179 1, 178 2, 177 3, 180 0)))', Srid::WGS84->value); - - expect($multiPolygonFromWkt)->toEqual($multiPolygon); + $multiPolygon = new MultiPolygon([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + ], Srid::WGS84->value); + + $multiPolygonFromWkt = MultiPolygon::fromWkt('MULTIPOLYGON(((180 0, 179 1, 178 2, 177 3, 180 0)))', Srid::WGS84->value); + + expect($multiPolygonFromWkt)->toEqual($multiPolygon); }); it('generates multi polygon WKT', function (): void { - $multiPolygon = new MultiPolygon([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - ]); - - $wkt = $multiPolygon->toWkt(); - - $expectedWkt = 'MULTIPOLYGON(((180 0, 179 1, 178 2, 177 3, 180 0)))'; - expect($wkt)->toBe($expectedWkt); + $multiPolygon = new MultiPolygon([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + ]); + + $wkt = $multiPolygon->toWkt(); + + $expectedWkt = 'MULTIPOLYGON(((180 0, 179 1, 178 2, 177 3, 180 0)))'; + expect($wkt)->toBe($expectedWkt); }); it('creates multi polygon from WKB', function (): void { - $multiPolygon = new MultiPolygon([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - ]); - - $multiPolygonFromWkb = MultiPolygon::fromWkb($multiPolygon->toWkb()); - - expect($multiPolygonFromWkb)->toEqual($multiPolygon); + $multiPolygon = new MultiPolygon([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + ]); + + $multiPolygonFromWkb = MultiPolygon::fromWkb($multiPolygon->toWkb()); + + expect($multiPolygonFromWkb)->toEqual($multiPolygon); }); it('creates multi polygon with SRID from WKB', function (): void { - $multiPolygon = new MultiPolygon([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - ], Srid::WGS84->value); - - $multiPolygonFromWkb = MultiPolygon::fromWkb($multiPolygon->toWkb()); - - expect($multiPolygonFromWkb)->toEqual($multiPolygon); + $multiPolygon = new MultiPolygon([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + ], Srid::WGS84->value); + + $multiPolygonFromWkb = MultiPolygon::fromWkb($multiPolygon->toWkb()); + + expect($multiPolygonFromWkb)->toEqual($multiPolygon); }); it('throws exception when multi polygon has no polygons', function (): void { - expect(function (): void { - new MultiPolygon([]); - })->toThrow(InvalidArgumentException::class); + expect(function (): void { + new MultiPolygon([]); + })->toThrow(InvalidArgumentException::class); }); it('throws exception when creating multi polygon from incorrect geometry', function (): void { - expect(function (): void { - // @phpstan-ignore-next-line - new MultiPolygon([ - new Point(0, 0), - ]); - })->toThrow(InvalidArgumentException::class); + expect(function (): void { + // @phpstan-ignore-next-line + new MultiPolygon([ + new Point(0, 0), + ]); + })->toThrow(InvalidArgumentException::class); }); it('casts a MultiPolygon to a string', function (): void { - $multiPolygon = new MultiPolygon([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - ]); - - expect($multiPolygon->__toString())->toEqual('MULTIPOLYGON(((180 0, 179 1, 178 2, 177 3, 180 0)))'); + $multiPolygon = new MultiPolygon([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + ]); + + expect($multiPolygon->__toString())->toEqual('MULTIPOLYGON(((180 0, 179 1, 178 2, 177 3, 180 0)))'); }); it('adds a macro toMultiPolygon', function (): void { - Geometry::macro('getName', function (): string { - /** @var Geometry $this */ + Geometry::macro('getName', function (): string { + /** @var Geometry $this */ + // @phpstan-ignore-next-line + return class_basename($this); + }); + + $multiPolygon = new MultiPolygon([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + ]); + // @phpstan-ignore-next-line - return class_basename($this); - }); - - $multiPolygon = new MultiPolygon([ - new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]), - ]); - - // @phpstan-ignore-next-line - expect($multiPolygon->getName())->toBe('MultiPolygon'); + expect($multiPolygon->getName())->toBe('MultiPolygon'); }); diff --git a/tests/Objects/PointTest.php b/tests/Objects/PointTest.php index bf9e5d4..69e5281 100644 --- a/tests/Objects/PointTest.php +++ b/tests/Objects/PointTest.php @@ -6,120 +6,120 @@ use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace; it('creates a model record with point', function (): void { - $point = new Point(0, 180); + $point = new Point(0, 180); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['point' => $point]); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['point' => $point]); - expect($testPlace->point)->toBeInstanceOf(Point::class); - expect($testPlace->point)->toEqual($point); + expect($testPlace->point)->toBeInstanceOf(Point::class); + expect($testPlace->point)->toEqual($point); }); it('creates a model record with point with SRID integer', function (): void { - $point = new Point(0, 180, Srid::WGS84->value); + $point = new Point(0, 180, Srid::WGS84->value); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['point' => $point]); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['point' => $point]); - expect($testPlace->point->srid)->toBe(Srid::WGS84->value); + expect($testPlace->point->srid)->toBe(Srid::WGS84->value); }); it('creates a model record with point with SRID enum', function (): void { - $point = new Point(0, 180, Srid::WGS84); + $point = new Point(0, 180, Srid::WGS84); - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['point' => $point]); + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['point' => $point]); - expect($testPlace->point->srid)->toBe(Srid::WGS84->value); + expect($testPlace->point->srid)->toBe(Srid::WGS84->value); }); it('creates point from JSON', function (): void { - $point = new Point(0, 180); + $point = new Point(0, 180); - $pointFromJson = Point::fromJson('{"type":"Point","coordinates":[180,0]}'); + $pointFromJson = Point::fromJson('{"type":"Point","coordinates":[180,0]}'); - expect($pointFromJson)->toEqual($point); + expect($pointFromJson)->toEqual($point); }); it('creates point with SRID from JSON', function (): void { - $point = new Point(0, 180, Srid::WGS84->value); + $point = new Point(0, 180, Srid::WGS84->value); - $pointFromJson = Point::fromJson('{"type":"Point","coordinates":[180,0]}', Srid::WGS84->value); + $pointFromJson = Point::fromJson('{"type":"Point","coordinates":[180,0]}', Srid::WGS84->value); - expect($pointFromJson)->toEqual($point); + expect($pointFromJson)->toEqual($point); }); it('generates point JSON', function (): void { - $point = new Point(0, 180); + $point = new Point(0, 180); - $json = $point->toJson(); + $json = $point->toJson(); - $expectedJson = '{"type":"Point","coordinates":[180,0]}'; - expect($json)->toBe($expectedJson); + $expectedJson = '{"type":"Point","coordinates":[180,0]}'; + expect($json)->toBe($expectedJson); }); it('throws exception when creating point from invalid JSON', function (): void { - expect(function (): void { - Point::fromJson('{"type":"Point","coordinates":[]}'); - })->toThrow(InvalidArgumentException::class); + expect(function (): void { + Point::fromJson('{"type":"Point","coordinates":[]}'); + })->toThrow(InvalidArgumentException::class); }); it('creates point from WKT', function (): void { - $point = new Point(0, 180); + $point = new Point(0, 180); - $pointFromWkt = Point::fromWkt('POINT(180 0)'); + $pointFromWkt = Point::fromWkt('POINT(180 0)'); - expect($pointFromWkt)->toEqual($point); + expect($pointFromWkt)->toEqual($point); }); it('creates point with SRID from WKT', function (): void { - $point = new Point(0, 180, Srid::WGS84->value); + $point = new Point(0, 180, Srid::WGS84->value); - $pointFromWkt = Point::fromWkt('POINT(180 0)', Srid::WGS84->value); + $pointFromWkt = Point::fromWkt('POINT(180 0)', Srid::WGS84->value); - expect($pointFromWkt)->toEqual($point); + expect($pointFromWkt)->toEqual($point); }); it('generates point WKT', function (): void { - $point = new Point(0, 180); + $point = new Point(0, 180); - $wkt = $point->toWkt(); + $wkt = $point->toWkt(); - $expectedWkt = 'POINT(180 0)'; - expect($wkt)->toBe($expectedWkt); + $expectedWkt = 'POINT(180 0)'; + expect($wkt)->toBe($expectedWkt); }); it('creates point from WKB', function (): void { - $point = new Point(0, 180); + $point = new Point(0, 180); - $pointFromWkb = Point::fromWkb($point->toWkb()); + $pointFromWkb = Point::fromWkb($point->toWkb()); - expect($pointFromWkb)->toEqual($point); + expect($pointFromWkb)->toEqual($point); }); it('creates point with SRID from WKB', function (): void { - $point = new Point(0, 180, Srid::WGS84->value); + $point = new Point(0, 180, Srid::WGS84->value); - $pointFromWkb = Point::fromWkb($point->toWkb()); + $pointFromWkb = Point::fromWkb($point->toWkb()); - expect($pointFromWkb)->toEqual($point); + expect($pointFromWkb)->toEqual($point); }); it('casts a Point to a string', function (): void { - $point = new Point(0, 180, Srid::WGS84->value); + $point = new Point(0, 180, Srid::WGS84->value); - expect($point->__toString())->toEqual('POINT(180 0)'); + expect($point->__toString())->toEqual('POINT(180 0)'); }); it('adds a macro toPoint', function (): void { - Geometry::macro('getName', function (): string { - /** @var Geometry $this */ - // @phpstan-ignore-next-line - return class_basename($this); - }); + Geometry::macro('getName', function (): string { + /** @var Geometry $this */ + // @phpstan-ignore-next-line + return class_basename($this); + }); - $point = new Point(0, 180, Srid::WGS84->value); + $point = new Point(0, 180, Srid::WGS84->value); - // @phpstan-ignore-next-line - expect($point->getName())->toBe('Point'); + // @phpstan-ignore-next-line + expect($point->getName())->toBe('Point'); }); diff --git a/tests/Objects/PolygonTest.php b/tests/Objects/PolygonTest.php index 65c77d7..df356d8 100644 --- a/tests/Objects/PolygonTest.php +++ b/tests/Objects/PolygonTest.php @@ -8,250 +8,250 @@ use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace; it('creates a model record with polygon', function (): void { - $polygon = new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]); - - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['polygon' => $polygon]); - - expect($testPlace->polygon)->toBeInstanceOf(Polygon::class); - expect($testPlace->polygon)->toEqual($polygon); + $polygon = new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]); + + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['polygon' => $polygon]); + + expect($testPlace->polygon)->toBeInstanceOf(Polygon::class); + expect($testPlace->polygon)->toEqual($polygon); }); it('creates a model record with polygon with SRID integer', function (): void { - $polygon = new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ], Srid::WGS84->value); - - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['polygon' => $polygon]); - - expect($testPlace->polygon->srid)->toBe(Srid::WGS84->value); + $polygon = new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ], Srid::WGS84->value); + + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['polygon' => $polygon]); + + expect($testPlace->polygon->srid)->toBe(Srid::WGS84->value); }); it('creates a model record with polygon with SRID enum', function (): void { - $polygon = new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ], Srid::WGS84); - - /** @var TestPlace $testPlace */ - $testPlace = TestPlace::factory()->create(['polygon' => $polygon]); - - expect($testPlace->polygon->srid)->toBe(Srid::WGS84->value); + $polygon = new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ], Srid::WGS84); + + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['polygon' => $polygon]); + + expect($testPlace->polygon->srid)->toBe(Srid::WGS84->value); }); it('creates polygon from JSON', function (): void { - $polygon = new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]); - - $polygonFromJson = Polygon::fromJson('{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}'); - - expect($polygonFromJson)->toEqual($polygon); + $polygon = new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]); + + $polygonFromJson = Polygon::fromJson('{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}'); + + expect($polygonFromJson)->toEqual($polygon); }); it('creates polygon with SRID from JSON', function (): void { - $polygon = new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ], Srid::WGS84->value); - - $polygonFromJson = Polygon::fromJson('{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}', Srid::WGS84->value); - - expect($polygonFromJson)->toEqual($polygon); + $polygon = new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ], Srid::WGS84->value); + + $polygonFromJson = Polygon::fromJson('{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}', Srid::WGS84->value); + + expect($polygonFromJson)->toEqual($polygon); }); it('generates polygon JSON', function (): void { - $polygon = new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]); - - $json = $polygon->toJson(); - - $expectedJson = '{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}'; - expect($json)->toBe($expectedJson); + $polygon = new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]); + + $json = $polygon->toJson(); + + $expectedJson = '{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}'; + expect($json)->toBe($expectedJson); }); it('generates polygon feature collection JSON', function (): void { - $polygon = new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]); - - $featureCollectionJson = $polygon->toFeatureCollectionJson(); - - $expectedFeatureCollectionJson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}}]}'; - expect($featureCollectionJson)->toBe($expectedFeatureCollectionJson); + $polygon = new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]); + + $featureCollectionJson = $polygon->toFeatureCollectionJson(); + + $expectedFeatureCollectionJson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}}]}'; + expect($featureCollectionJson)->toBe($expectedFeatureCollectionJson); }); it('creates polygon from WKT', function (): void { - $polygon = new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]); - - $polygonFromWkt = Polygon::fromWkt('POLYGON((180 0, 179 1, 178 2, 177 3, 180 0))'); - - expect($polygonFromWkt)->toEqual($polygon); + $polygon = new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]); + + $polygonFromWkt = Polygon::fromWkt('POLYGON((180 0, 179 1, 178 2, 177 3, 180 0))'); + + expect($polygonFromWkt)->toEqual($polygon); }); it('creates polygon with SRID from WKT', function (): void { - $polygon = new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ], Srid::WGS84->value); - - $polygonFromWkt = Polygon::fromWkt('POLYGON((180 0, 179 1, 178 2, 177 3, 180 0))', Srid::WGS84->value); - - expect($polygonFromWkt)->toEqual($polygon); + $polygon = new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ], Srid::WGS84->value); + + $polygonFromWkt = Polygon::fromWkt('POLYGON((180 0, 179 1, 178 2, 177 3, 180 0))', Srid::WGS84->value); + + expect($polygonFromWkt)->toEqual($polygon); }); it('generates polygon WKT', function (): void { - $polygon = new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]); - - $wkt = $polygon->toWkt(); - - $expectedWkt = 'POLYGON((180 0, 179 1, 178 2, 177 3, 180 0))'; - expect($wkt)->toBe($expectedWkt); + $polygon = new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]); + + $wkt = $polygon->toWkt(); + + $expectedWkt = 'POLYGON((180 0, 179 1, 178 2, 177 3, 180 0))'; + expect($wkt)->toBe($expectedWkt); }); it('creates polygon from WKB', function (): void { - $polygon = new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]); - - $polygonFromWkb = Polygon::fromWkb($polygon->toWkb()); - - expect($polygonFromWkb)->toEqual($polygon); + $polygon = new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]); + + $polygonFromWkb = Polygon::fromWkb($polygon->toWkb()); + + expect($polygonFromWkb)->toEqual($polygon); }); it('creates polygon with SRID from WKB', function (): void { - $polygon = new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ], Srid::WGS84->value); - - $polygonFromWkb = Polygon::fromWkb($polygon->toWkb()); - - expect($polygonFromWkb)->toEqual($polygon); + $polygon = new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ], Srid::WGS84->value); + + $polygonFromWkb = Polygon::fromWkb($polygon->toWkb()); + + expect($polygonFromWkb)->toEqual($polygon); }); it('throws exception when polygon has no line strings', function (): void { - expect(function (): void { - new Polygon([]); - })->toThrow(InvalidArgumentException::class); + expect(function (): void { + new Polygon([]); + })->toThrow(InvalidArgumentException::class); }); it('throws exception when creating polygon from incorrect geometry', function (): void { - expect(function (): void { - // @phpstan-ignore-next-line - new Polygon([ - new Point(0, 0), - ]); - })->toThrow(InvalidArgumentException::class); + expect(function (): void { + // @phpstan-ignore-next-line + new Polygon([ + new Point(0, 0), + ]); + })->toThrow(InvalidArgumentException::class); }); it('casts a Polygon to a string', function (): void { - $polygon = new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]); - - expect($polygon->__toString())->toEqual('POLYGON((180 0, 179 1, 178 2, 177 3, 180 0))'); + $polygon = new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]); + + expect($polygon->__toString())->toEqual('POLYGON((180 0, 179 1, 178 2, 177 3, 180 0))'); }); it('adds a macro toPolygon', function (): void { - Geometry::macro('getName', function (): string { - /** @var Geometry $this */ + Geometry::macro('getName', function (): string { + /** @var Geometry $this */ + // @phpstan-ignore-next-line + return class_basename($this); + }); + + $polygon = new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]); + // @phpstan-ignore-next-line - return class_basename($this); - }); - - $polygon = new Polygon([ - new LineString([ - new Point(0, 180), - new Point(1, 179), - new Point(2, 178), - new Point(3, 177), - new Point(0, 180), - ]), - ]); - - // @phpstan-ignore-next-line - expect($polygon->getName())->toBe('Polygon'); + expect($polygon->getName())->toBe('Polygon'); }); diff --git a/tests/TestCase.php b/tests/TestCase.php index 5bc3d9d..5005ceb 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -8,21 +8,20 @@ class TestCase extends Orchestra { - protected function setUp(): void - { - parent::setUp(); + protected function setUp(): void + { + parent::setUp(); - $this->loadMigrationsFrom(__DIR__.'/database/migrations'); - } + $this->loadMigrationsFrom(__DIR__.'/database/migrations'); + } - /** - * @param $app - * @return class-string[] - */ - protected function getPackageProviders($app): array - { - return [ - EloquentSpatialServiceProvider::class, - ]; - } + /** + * @return class-string[] + */ + protected function getPackageProviders($app): array + { + return [ + EloquentSpatialServiceProvider::class, + ]; + } } diff --git a/tests/TestFactories/TestPlaceFactory.php b/tests/TestFactories/TestPlaceFactory.php index b111e14..b43090d 100644 --- a/tests/TestFactories/TestPlaceFactory.php +++ b/tests/TestFactories/TestPlaceFactory.php @@ -10,16 +10,16 @@ */ class TestPlaceFactory extends Factory { - protected $model = TestPlace::class; + protected $model = TestPlace::class; - /** - * @return array - */ - public function definition(): array - { - return [ - 'name' => $this->faker->streetName(), - 'address' => $this->faker->address(), - ]; - } + /** + * @return array + */ + public function definition(): array + { + return [ + 'name' => $this->faker->streetName(), + 'address' => $this->faker->address(), + ]; + } } diff --git a/tests/TestModels/TestPlace.php b/tests/TestModels/TestPlace.php index 57392b7..e9dc0d1 100644 --- a/tests/TestModels/TestPlace.php +++ b/tests/TestModels/TestPlace.php @@ -26,39 +26,40 @@ * @property float|null $distance_in_meters * @property Point|null $centroid * @property Point|null $centroid_alias + * * @mixin Model */ class TestPlace extends Model { - use HasFactory, HasSpatial; + use HasFactory, HasSpatial; - protected $fillable = [ - 'address', - 'point', - 'multi_point', - 'line_string', - 'multi_line_string', - 'polygon', - 'multi_polygon', - 'geometry_collection', - 'point_with_line_string_cast', - ]; + protected $fillable = [ + 'address', + 'point', + 'multi_point', + 'line_string', + 'multi_line_string', + 'polygon', + 'multi_polygon', + 'geometry_collection', + 'point_with_line_string_cast', + ]; - protected $casts = [ - 'point' => Point::class, - 'multi_point' => MultiPoint::class, - 'line_string' => LineString::class, - 'multi_line_string' => MultiLineString::class, - 'polygon' => Polygon::class, - 'multi_polygon' => MultiPolygon::class, - 'geometry_collection' => GeometryCollection::class, - 'point_with_line_string_cast' => LineString::class, - 'distance' => 'float', - 'distance_in_meters' => 'float', - ]; + protected $casts = [ + 'point' => Point::class, + 'multi_point' => MultiPoint::class, + 'line_string' => LineString::class, + 'multi_line_string' => MultiLineString::class, + 'polygon' => Polygon::class, + 'multi_polygon' => MultiPolygon::class, + 'geometry_collection' => GeometryCollection::class, + 'point_with_line_string_cast' => LineString::class, + 'distance' => 'float', + 'distance_in_meters' => 'float', + ]; - protected static function newFactory(): TestPlaceFactory - { - return new TestPlaceFactory; - } + protected static function newFactory(): TestPlaceFactory + { + return new TestPlaceFactory; + } } diff --git a/tests/database/migrations/0000_00_00_000000_create_test_places_table.php b/tests/database/migrations/0000_00_00_000000_create_test_places_table.php index a52e708..1bc60d8 100644 --- a/tests/database/migrations/0000_00_00_000000_create_test_places_table.php +++ b/tests/database/migrations/0000_00_00_000000_create_test_places_table.php @@ -6,29 +6,29 @@ class CreateTestPlacesTable extends Migration { - public function up(): void - { - Schema::create('test_places', function (Blueprint $table): void { - $table->id(); - $table->timestamps(); - $table->string('name'); - $table->string('address'); - $table->point('point')->isGeometry()->nullable(); - $table->multiPoint('multi_point')->isGeometry()->nullable(); - $table->lineString('line_string')->isGeometry()->nullable(); - $table->multiLineString('multi_line_string')->isGeometry()->nullable(); - $table->polygon('polygon')->isGeometry()->nullable(); - $table->multiPolygon('multi_polygon')->isGeometry()->nullable(); - $table->geometryCollection('geometry_collection')->isGeometry()->nullable(); - $table->point('point_with_line_string_cast')->isGeometry()->nullable(); - $table->point('point_geography')->nullable(); - $table->decimal('longitude')->nullable(); - $table->decimal('latitude')->nullable(); - }); - } + public function up(): void + { + Schema::create('test_places', function (Blueprint $table): void { + $table->id(); + $table->timestamps(); + $table->string('name'); + $table->string('address'); + $table->point('point')->isGeometry()->nullable(); + $table->multiPoint('multi_point')->isGeometry()->nullable(); + $table->lineString('line_string')->isGeometry()->nullable(); + $table->multiLineString('multi_line_string')->isGeometry()->nullable(); + $table->polygon('polygon')->isGeometry()->nullable(); + $table->multiPolygon('multi_polygon')->isGeometry()->nullable(); + $table->geometryCollection('geometry_collection')->isGeometry()->nullable(); + $table->point('point_with_line_string_cast')->isGeometry()->nullable(); + $table->point('point_geography')->nullable(); + $table->decimal('longitude')->nullable(); + $table->decimal('latitude')->nullable(); + }); + } - public function down(): void - { - Schema::dropIfExists('test_places'); - } + public function down(): void + { + Schema::dropIfExists('test_places'); + } }