diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 00000000..dd9a2b51
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,15 @@
+root = true
+
+[*]
+charset = utf-8
+end_of_line = lf
+indent_size = 4
+indent_style = space
+insert_final_newline = true
+trim_trailing_whitespace = true
+
+[*.md]
+trim_trailing_whitespace = false
+
+[*.{yml,yaml}]
+indent_size = 2
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 00000000..9e9519b3
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,19 @@
+# Path-based git attributes
+# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
+
+# Ignore all test and documentation with "export-ignore".
+/.github export-ignore
+/.gitattributes export-ignore
+/.gitignore export-ignore
+/phpunit.xml.dist export-ignore
+/art export-ignore
+/docs export-ignore
+/tests export-ignore
+/.editorconfig export-ignore
+/.php_cs.dist.php export-ignore
+/psalm.xml export-ignore
+/psalm.xml.dist export-ignore
+/testbench.yaml export-ignore
+/UPGRADING.md export-ignore
+/phpstan.neon.dist export-ignore
+/phpstan-baseline.neon export-ignore
diff --git a/.github/ISSUE_TEMPLATE/bug.yml b/.github/ISSUE_TEMPLATE/bug.yml
new file mode 100644
index 00000000..fe4cfe6d
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/bug.yml
@@ -0,0 +1,66 @@
+name: Bug Report
+description: Report an Issue or Bug with the Package
+title: "[Bug]: "
+labels: ["bug"]
+body:
+ - type: markdown
+ attributes:
+ value: |
+ We're sorry to hear you have a problem. Can you help us solve it by providing the following details.
+ - type: textarea
+ id: what-happened
+ attributes:
+ label: What happened?
+ description: What did you expect to happen?
+ placeholder: I cannot currently do X thing because when I do, it breaks X thing.
+ validations:
+ required: true
+ - type: textarea
+ id: how-to-reproduce
+ attributes:
+ label: How to reproduce the bug
+ description: How did this occur, please add any config values used and provide a set of reliable steps if possible.
+ placeholder: When I do X I see Y.
+ validations:
+ required: true
+ - type: input
+ id: package-version
+ attributes:
+ label: Package Version
+ description: What version of our Package are you running? Please be as specific as possible
+ placeholder: 2.0.0
+ validations:
+ required: true
+ - type: input
+ id: php-version
+ attributes:
+ label: PHP Version
+ description: What version of PHP are you running? Please be as specific as possible
+ placeholder: 8.2.0
+ validations:
+ required: true
+ - type: input
+ id: laravel-version
+ attributes:
+ label: Laravel Version
+ description: What version of Laravel are you running? Please be as specific as possible
+ placeholder: 9.0.0
+ validations:
+ required: true
+ - type: dropdown
+ id: operating-systems
+ attributes:
+ label: Which operating systems does with happen with?
+ description: You may select more than one.
+ multiple: true
+ options:
+ - macOS
+ - Windows
+ - Linux
+ - type: textarea
+ id: notes
+ attributes:
+ label: Notes
+ description: Use this field to provide any other notes that you feel might be relevant to the issue.
+ validations:
+ required: false
diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml
new file mode 100644
index 00000000..8b5da0ff
--- /dev/null
+++ b/.github/ISSUE_TEMPLATE/config.yml
@@ -0,0 +1,11 @@
+blank_issues_enabled: false
+contact_links:
+ - name: Ask a question
+ url: https://github.com/grimzy/laravel-mysql-spatial/discussions/new?category=q-a
+ about: Ask the community for help
+ - name: Request a feature
+ url: https://github.com/grimzy/laravel-mysql-spatial/discussions/new?category=ideas
+ about: Share ideas for new features
+ - name: Report a security issue
+ url: https://github.com/grimzy/laravel-mysql-spatial/security/policy
+ about: Learn how to notify us for sensitive bugs
diff --git a/.github/dependabot.yml b/.github/dependabot.yml
new file mode 100644
index 00000000..30c8a493
--- /dev/null
+++ b/.github/dependabot.yml
@@ -0,0 +1,12 @@
+# Please see the documentation for all configuration options:
+# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
+
+version: 2
+updates:
+
+ - package-ecosystem: "github-actions"
+ directory: "/"
+ schedule:
+ interval: "weekly"
+ labels:
+ - "dependencies"
\ No newline at end of file
diff --git a/.github/workflows/dependabot-auto-merge.yml b/.github/workflows/dependabot-auto-merge.yml
new file mode 100644
index 00000000..32f77541
--- /dev/null
+++ b/.github/workflows/dependabot-auto-merge.yml
@@ -0,0 +1,32 @@
+name: dependabot-auto-merge
+on: pull_request_target
+
+permissions:
+ pull-requests: write
+ contents: write
+
+jobs:
+ dependabot:
+ runs-on: ubuntu-latest
+ if: ${{ github.actor == 'dependabot[bot]' }}
+ steps:
+
+ - name: Dependabot metadata
+ id: metadata
+ uses: dependabot/fetch-metadata@v1.3.6
+ with:
+ github-token: "${{ secrets.GITHUB_TOKEN }}"
+
+ - name: Auto-merge Dependabot PRs for semver-minor updates
+ if: ${{steps.metadata.outputs.update-type == 'version-update:semver-minor'}}
+ run: gh pr merge --auto --merge "$PR_URL"
+ env:
+ PR_URL: ${{github.event.pull_request.html_url}}
+ GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
+
+ - name: Auto-merge Dependabot PRs for semver-patch updates
+ if: ${{steps.metadata.outputs.update-type == 'version-update:semver-patch'}}
+ run: gh pr merge --auto --merge "$PR_URL"
+ env:
+ PR_URL: ${{github.event.pull_request.html_url}}
+ GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
diff --git a/.github/workflows/fix-php-code-style-issues.yml b/.github/workflows/fix-php-code-style-issues.yml
new file mode 100644
index 00000000..d9314f2b
--- /dev/null
+++ b/.github/workflows/fix-php-code-style-issues.yml
@@ -0,0 +1,26 @@
+name: Fix PHP code style issues
+
+on:
+ push:
+ paths:
+ - "**.php"
+ pull_request:
+ branches: [master]
+
+jobs:
+ php-code-styling:
+ runs-on: ubuntu-latest
+
+ steps:
+ - name: Checkout code
+ uses: actions/checkout@v3
+ with:
+ ref: ${{ github.head_ref }}
+
+ - name: Fix PHP code style issues
+ uses: aglipanci/laravel-pint-action@2.1.0
+
+ - name: Commit changes
+ uses: stefanzweifel/git-auto-commit-action@v4
+ with:
+ commit_message: Fix styling
diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml
new file mode 100644
index 00000000..7d9a5987
--- /dev/null
+++ b/.github/workflows/phpstan.yml
@@ -0,0 +1,41 @@
+name: PHPStan
+
+on:
+ push:
+ paths:
+ - "**.php"
+ - "phpstan.neon.dist"
+ pull_request:
+ branches: [master]
+
+jobs:
+ phpstan:
+ name: phpstan
+ runs-on: ubuntu-latest
+
+ env:
+ DB_DATABASE: "laravel"
+ DB_HOST: "127.0.0.1"
+ DB_PORT: "3306"
+ DB_USERNAME: "root"
+ DB_PASSWORD: "root"
+
+ steps:
+ - name: Set up MySQL
+ run: |
+ sudo /etc/init.d/mysql start
+ mysql -e 'CREATE DATABASE laravel' -uroot -proot
+
+ - uses: actions/checkout@v3
+
+ - name: Setup PHP
+ uses: shivammathur/setup-php@v2
+ with:
+ php-version: "8.1"
+ coverage: none
+
+ - name: Install composer dependencies
+ uses: ramsey/composer-install@v2
+
+ - name: Run PHPStan
+ run: ./vendor/bin/phpstan --error-format=github
diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml
new file mode 100644
index 00000000..346f83e2
--- /dev/null
+++ b/.github/workflows/run-tests.yml
@@ -0,0 +1,56 @@
+name: PHP Tests
+
+on:
+ push:
+ branches: [master]
+ pull_request:
+ branches: [master]
+
+jobs:
+ test:
+ runs-on: ${{ matrix.os }}
+ strategy:
+ fail-fast: true
+ matrix:
+ os: [ubuntu-latest]
+ php: [8.2, 8.1]
+ laravel: [10.*]
+ stability: [prefer-lowest, prefer-stable]
+ include:
+ - laravel: 10.*
+ testbench: 8.*
+ carbon: ^2.63
+
+ name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
+
+ steps:
+ - name: Set up MySQL
+ run: |
+ sudo /etc/init.d/mysql start
+ mysql -e 'CREATE DATABASE laravel' -uroot -proot
+
+ - name: Checkout code
+ uses: actions/checkout@v3
+
+ - name: Setup PHP
+ uses: shivammathur/setup-php@v2
+ with:
+ php-version: ${{ matrix.php }}
+ extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo, mysql
+ coverage: xdebug
+
+ - name: Setup problem matchers
+ run: |
+ echo "::add-matcher::${{ runner.tool_cache }}/php.json"
+ echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
+
+ - name: Install dependencies
+ run: |
+ composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "nesbot/carbon:${{ matrix.carbon }}" --no-interaction --no-update
+ composer update --${{ matrix.stability }} --prefer-dist --no-interaction
+
+ - name: List Installed Dependencies
+ run: composer show -D
+
+ - name: Execute tests
+ run: vendor/bin/phpunit
diff --git a/.gitignore b/.gitignore
index 52370244..0bbeb929 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,13 @@
-.idea/
-vendor/
+.idea
+.phpunit.result.cache
+build
composer.lock
+coverage
+docs
+phpunit.xml
+phpstan.neon
+testbench.yaml
+vendor
_db/
-build/
\ No newline at end of file
+node_modules
+.phpunit.cache
diff --git a/.travis.yml b/.travis.yml
deleted file mode 100644
index f6a2a31f..00000000
--- a/.travis.yml
+++ /dev/null
@@ -1,33 +0,0 @@
-language: php
-
-php:
- - '7.3'
- - '7.4'
-
-env:
- - MYSQL_VERSION=8.0
-
-dist: trusty
-
-sudo: required
-
-services:
- - docker
-
-before_install:
- - echo "memory_limit=3G" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
- - sudo /etc/init.d/mysql stop
- - make start_db V=$MYSQL_VERSION
-
-install: composer install
-
-before_script:
- - mkdir -p build/logs
- - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- - chmod +x ./cc-test-reporter
- - ./cc-test-reporter before-build
-
-script: vendor/bin/phpunit --coverage-clover build/logs/clover.xml
-
-after_script: ./cc-test-reporter after-build --coverage-input-type clover --exit-code $TRAVIS_TEST_RESULT
-
diff --git a/Makefile b/Makefile
deleted file mode 100644
index 62db4599..00000000
--- a/Makefile
+++ /dev/null
@@ -1,32 +0,0 @@
-V=8.0
-DB_DIR=$(shell pwd)/_db-$(V)
-mV=10.3
-mDB_DIR=$(shell pwd)/_db-$(mV)
-
-start_db:
- @echo Starting MySQL $(V)
- docker run --rm -d --name spatial-mysql \
- -p 3306:3306 \
- -v $(DB_DIR):/var/lib/mysql \
- -e MYSQL_DATABASE=spatial_test \
- -e MYSQL_ALLOW_EMPTY_PASSWORD=yes \
- mysql:$(V) --character-set-server=utf8 --collation-server=utf8_general_ci --default-authentication-plugin=mysql_native_password
-
-start_db_maria:
- @echo Starting MariaDB $(mV)
- docker run --rm -d --name spatial-mysql \
- -p 3306:3306 \
- -v $(DB_DIR):/var/lib/mysql \
- -e MYSQL_DATABASE=spatial_test \
- -e MYSQL_ALLOW_EMPTY_PASSWORD=yes \
- mariadb:$(mV) --character-set-server=utf8 --collation-server=utf8_general_ci --default-authentication-plugin=mysql_native_password
-
-
-rm_db:
- docker stop spatial-mysql || true
- rm -Rf $(DB_DIR)
-
-refresh_db: rm_db start_db
-
-get_ip:
- @docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' spatial-mysql
\ No newline at end of file
diff --git a/composer.json b/composer.json
index 4451cb50..617444e5 100644
--- a/composer.json
+++ b/composer.json
@@ -1,10 +1,13 @@
{
"name": "grimzy/laravel-mysql-spatial",
"description": "MySQL spatial data types extension for Laravel.",
+ "homepage": "https://github.com/grimzy/laravel-mysql-spatial",
"scripts": {
- "test": "phpunit -c phpunit.xml.dist",
- "test:unit": "phpunit -c phpunit.xml.dist --testsuite unit",
- "test:integration": "phpunit -c phpunit.xml.dist --testsuite integration"
+ "post-autoload-dump": "@php ./vendor/bin/testbench package:discover --ansi",
+ "analyse": "vendor/bin/phpstan analyse",
+ "test": "vendor/bin/phpunit",
+ "test-coverage": "vendor/bin/phpunit --coverage",
+ "format": "vendor/bin/pint"
},
"type": "library",
"license": "MIT",
@@ -15,39 +18,53 @@
}
],
"require": {
- "php": ">=7.3",
- "ext-pdo": "*",
+ "php": "^8.1",
"ext-json": "*",
- "illuminate/database": "^8.0",
+ "ext-pdo": "*",
+ "doctrine/dbal": "^3.5",
"geo-io/wkb-parser": "^1.0",
+ "illuminate/contracts": "^10.0",
+ "illuminate/database": "^10.0",
"jmikola/geojson": "^1.0"
},
+ "conflict": {},
"require-dev": {
- "phpunit/phpunit": "~6.5",
- "laravel/laravel": "^8.0",
- "doctrine/dbal": "^2.5",
- "laravel/browser-kit-testing": "^2.0",
- "mockery/mockery": "^1.3"
+ "laravel/pint": "^1.6",
+ "laravel/laravel": "^10.0.4",
+ "laravel/browser-kit-testing": "^7.0",
+ "mockery/mockery": "^1.5.1",
+ "nunomaduro/collision": "^7.1.0",
+ "nunomaduro/larastan": "^2.5.1",
+ "orchestra/testbench": "^8.0.8",
+ "phpstan/extension-installer": "^1.2",
+ "phpstan/phpstan-deprecation-rules": "^1.1.2",
+ "phpstan/phpstan-phpunit": "^1.3.10",
+ "phpunit/phpunit": "^10.0.15"
},
"autoload": {
"psr-4": {
- "Grimzy\\LaravelMysqlSpatial\\": "src/"
+ "Grimzy\\LaravelMysqlSpatial\\": "src"
}
},
- "autoload-dev" : {
- "classmap" : [
- "tests/Unit",
- "tests/Integration"
- ]
+ "autoload-dev": {
+ "psr-4": {
+ "Grimzy\\LaravelMysqlSpatial\\Tests\\": "tests"
+ }
+ },
+ "config": {
+ "sort-packages": true,
+ "allow-plugins": {
+ "pestphp/pest-plugin": true,
+ "phpstan/extension-installer": true
+ }
},
"extra": {
- "branch-alias": {
- "dev-master": "4.0.x-dev"
- },
"laravel": {
"providers": [
"Grimzy\\LaravelMysqlSpatial\\SpatialServiceProvider"
]
}
- }
+ },
+ "minimum-stability": "stable",
+ "prefer-stable": true
}
diff --git a/docker-compose.yml b/docker-compose.yml
index fb41dd07..9aa3a874 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -1,10 +1,10 @@
version: '3.3'
services:
db:
- image: mysql:5.7
- ports:
- - "3306:3306"
- environment:
- MYSQL_DATABASE: 'spatial_test'
- MYSQL_ROOT_PASSWORD: ''
- MYSQL_ALLOW_EMPTY_PASSWORD: 1
\ No newline at end of file
+ image: mysql:8.0
+ ports:
+ - "33306:3306"
+ environment:
+ MYSQL_DATABASE: 'laravel'
+ MYSQL_ROOT_PASSWORD: 'root'
+ MYSQL_ALLOW_EMPTY_PASSWORD: 1
diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon
new file mode 100644
index 00000000..5338ba2c
--- /dev/null
+++ b/phpstan-baseline.neon
@@ -0,0 +1,62 @@
+parameters:
+ ignoreErrors:
+ -
+ message: "#^PHPDoc type Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryInterface of property Grimzy\\\\LaravelMysqlSpatial\\\\Eloquent\\\\SpatialExpression\\:\\:\\$value is not covariant with PHPDoc type float\\|int\\|string of overridden property Illuminate\\\\Database\\\\Query\\\\Expression\\:\\:\\$value\\.$#"
+ count: 1
+ path: src/Eloquent/SpatialExpression.php
+
+ -
+ message: "#^Call to function is_null\\(\\) with Illuminate\\\\Database\\\\Schema\\\\Grammars\\\\Grammar will always evaluate to false\\.$#"
+ count: 1
+ path: src/MysqlConnection.php
+
+ -
+ message: "#^Method Grimzy\\\\LaravelMysqlSpatial\\\\MysqlConnection\\:\\:getDefaultSchemaGrammar\\(\\) should return Illuminate\\\\Database\\\\Schema\\\\Grammars\\\\MySqlGrammar but returns Illuminate\\\\Database\\\\Grammar\\.$#"
+ count: 1
+ path: src/MysqlConnection.php
+
+ -
+ message: "#^Method Grimzy\\\\LaravelMysqlSpatial\\\\Schema\\\\Blueprint\\:\\:spatialIndex\\(\\) should return Illuminate\\\\Database\\\\Schema\\\\IndexDefinition but returns Illuminate\\\\Support\\\\Fluent\\.$#"
+ count: 1
+ path: src/Schema/Blueprint.php
+
+ -
+ message: "#^Access to an undefined property Illuminate\\\\Support\\\\Fluent\\:\\:\\$srid\\.$#"
+ count: 1
+ path: src/Schema/Grammars/MySqlGrammar.php
+
+ -
+ message: "#^Method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\Geometry\\:\\:fromWKT\\(\\) should return static\\(Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\Geometry\\) but returns Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryInterface\\.$#"
+ count: 1
+ path: src/Types/Geometry.php
+
+ -
+ message: "#^Unsafe usage of new static\\(\\)\\.$#"
+ count: 2
+ path: src/Types/GeometryCollection.php
+
+ -
+ message: "#^Unsafe usage of new static\\(\\)\\.$#"
+ count: 1
+ path: src/Types/LineString.php
+
+ -
+ message: "#^Unsafe usage of new static\\(\\)\\.$#"
+ count: 1
+ path: src/Types/MultiLineString.php
+
+ -
+ message: "#^Unsafe usage of new static\\(\\)\\.$#"
+ count: 1
+ path: src/Types/MultiPoint.php
+
+ -
+ message: "#^Unsafe usage of new static\\(\\)\\.$#"
+ count: 1
+ path: src/Types/MultiPolygon.php
+
+ -
+ message: "#^Unsafe usage of new static\\(\\)\\.$#"
+ count: 1
+ path: src/Types/Point.php
+
diff --git a/phpstan.neon.dist b/phpstan.neon.dist
new file mode 100644
index 00000000..4becbb0a
--- /dev/null
+++ b/phpstan.neon.dist
@@ -0,0 +1,11 @@
+includes:
+ - phpstan-baseline.neon
+
+parameters:
+ level: 6
+ paths:
+ - src
+ tmpDir: build/phpstan
+ checkModelProperties: true
+ checkMissingIterableValueType: false
+ checkGenericClassInNonGenericObjectType: false
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 1456a6ac..5d8dd7a1 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -1,45 +1,34 @@
-
-
-
- ./tests/Unit
-
-
- ./tests/Integration
-
-
-
-
-
-
-
-
- ./src
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
+
+
+
+ tests
+
+
+
+
+ ./src
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/pint.json b/pint.json
new file mode 100644
index 00000000..6aad9090
--- /dev/null
+++ b/pint.json
@@ -0,0 +1,6 @@
+{
+ "preset": "laravel",
+ "rules": {
+ "use_arrow_functions": true
+ }
+}
diff --git a/src/Connectors/ConnectionFactory.php b/src/Connectors/ConnectionFactory.php
index 223cea13..d8b14213 100644
--- a/src/Connectors/ConnectionFactory.php
+++ b/src/Connectors/ConnectionFactory.php
@@ -9,12 +9,10 @@
class ConnectionFactory extends IlluminateConnectionFactory
{
/**
- * @param string $driver
- * @param \Closure|PDO $connection
- * @param string $database
- * @param string $prefix
- * @param array $config
- *
+ * @param string $driver
+ * @param \Closure|PDO $connection
+ * @param string $database
+ * @param string $prefix
* @return \Illuminate\Database\ConnectionInterface
*/
protected function createConnection($driver, $connection, $database, $prefix = '', array $config = [])
diff --git a/src/Eloquent/Builder.php b/src/Eloquent/Builder.php
index 6230ed17..e31d2064 100755
--- a/src/Eloquent/Builder.php
+++ b/src/Eloquent/Builder.php
@@ -18,7 +18,7 @@ public function update(array $values)
return parent::update($values);
}
- protected function asWKT(GeometryInterface $geometry)
+ protected function asWKT(GeometryInterface $geometry): SpatialExpression
{
return new SpatialExpression($geometry);
}
diff --git a/src/Eloquent/SpatialExpression.php b/src/Eloquent/SpatialExpression.php
index 9224af0f..e4647524 100644
--- a/src/Eloquent/SpatialExpression.php
+++ b/src/Eloquent/SpatialExpression.php
@@ -2,22 +2,44 @@
namespace Grimzy\LaravelMysqlSpatial\Eloquent;
+use Grimzy\LaravelMysqlSpatial\Types\Geometry;
+use Grimzy\LaravelMysqlSpatial\Types\GeometryInterface;
+use Illuminate\Database\Grammar;
use Illuminate\Database\Query\Expression;
class SpatialExpression extends Expression
{
- public function getValue()
+ /**
+ * @var Geometry|GeometryInterface
+ */
+ protected $value;
+
+ /**
+ * @param Geometry|GeometryInterface $value
+ * @return void
+ */
+ public function __construct($value)
+ {
+ $this->value = $value;
+ }
+
+ public function getValue(Grammar $grammar)
{
return "ST_GeomFromText(?, ?, 'axis-order=long-lat')";
}
- public function getSpatialValue()
+ public function getSpatialValue(): string
{
return $this->value->toWkt();
}
- public function getSrid()
+ public function getSrid(): int
{
return $this->value->getSrid();
}
+
+ public function toWkt(): string
+ {
+ return $this->value->toWkt();
+ }
}
diff --git a/src/Eloquent/SpatialTrait.php b/src/Eloquent/SpatialTrait.php
index 5cc3f4b1..2674a87b 100755
--- a/src/Eloquent/SpatialTrait.php
+++ b/src/Eloquent/SpatialTrait.php
@@ -61,8 +61,7 @@ trait SpatialTrait
/**
* Create a new Eloquent query builder for the model.
*
- * @param \Illuminate\Database\Query\Builder $query
- *
+ * @param \Illuminate\Database\Query\Builder $query
* @return \Grimzy\LaravelMysqlSpatial\Eloquent\Builder
*/
public function newEloquentBuilder($query)
@@ -123,7 +122,7 @@ public function getSpatialFields()
public function isColumnAllowed($geometryColumn)
{
- if (!in_array($geometryColumn, $this->getSpatialFields())) {
+ if (! in_array($geometryColumn, $this->getSpatialFields())) {
throw new SpatialFieldsNotDefinedException();
}
@@ -163,7 +162,7 @@ public function scopeDistanceValue($query, $geometryColumn, $geometry)
$columns = $query->getQuery()->columns;
- if (!$columns) {
+ if (! $columns) {
$query->select('*');
}
@@ -206,7 +205,7 @@ public function scopeDistanceSphereValue($query, $geometryColumn, $geometry)
$columns = $query->getQuery()->columns;
- if (!$columns) {
+ if (! $columns) {
$query->select('*');
}
$query->selectRaw("st_distance_sphere(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat')) as distance", [
@@ -219,7 +218,7 @@ public function scopeComparison($query, $geometryColumn, $geometry, $relationshi
{
$this->isColumnAllowed($geometryColumn);
- if (!in_array($relationship, $this->stRelations)) {
+ if (! in_array($relationship, $this->stRelations)) {
throw new UnknownSpatialRelationFunction($relationship);
}
@@ -275,7 +274,7 @@ public function scopeOrderBySpatial($query, $geometryColumn, $geometry, $orderFu
{
$this->isColumnAllowed($geometryColumn);
- if (!in_array($orderFunction, $this->stOrderFunctions)) {
+ if (! in_array($orderFunction, $this->stOrderFunctions)) {
throw new UnknownSpatialFunctionException($orderFunction);
}
diff --git a/src/MysqlConnection.php b/src/MysqlConnection.php
index 38a2b1a4..44ce2675 100644
--- a/src/MysqlConnection.php
+++ b/src/MysqlConnection.php
@@ -26,7 +26,7 @@ public function __construct($pdo, $database = '', $tablePrefix = '', array $conf
'geometrycollection',
'geomcollection',
];
- $dbPlatform = $this->getDoctrineSchemaManager()->getDatabasePlatform();
+ $dbPlatform = $this->getDoctrineConnection()->getDatabasePlatform();
foreach ($geometries as $type) {
$dbPlatform->registerDoctrineTypeMapping($type, 'string');
}
@@ -35,20 +35,16 @@ public function __construct($pdo, $database = '', $tablePrefix = '', array $conf
/**
* Get the default schema grammar instance.
- *
- * @return \Illuminate\Database\Grammar
*/
- protected function getDefaultSchemaGrammar()
+ protected function getDefaultSchemaGrammar(): \Illuminate\Database\Grammar
{
return $this->withTablePrefix(new MySqlGrammar());
}
/**
* Get a schema builder instance for the connection.
- *
- * @return \Illuminate\Database\Schema\MySqlBuilder
*/
- public function getSchemaBuilder()
+ public function getSchemaBuilder(): \Illuminate\Database\Schema\MySqlBuilder
{
if (is_null($this->schemaGrammar)) {
$this->useDefaultSchemaGrammar();
diff --git a/src/Schema/Blueprint.php b/src/Schema/Blueprint.php
index 0a333f06..ff443dea 100644
--- a/src/Schema/Blueprint.php
+++ b/src/Schema/Blueprint.php
@@ -8,13 +8,8 @@ class Blueprint extends IlluminateBlueprint
{
/**
* Add a geometry column on the table.
- *
- * @param string $column
- * @param null|int $srid
- *
- * @return \Illuminate\Support\Fluent
*/
- public function geometry($column, $srid = null)
+ public function geometry($column, ?int $srid = null): \Illuminate\Support\Fluent
{
return $this->addColumn('geometry', $column, compact('srid'));
}
@@ -22,90 +17,57 @@ public function geometry($column, $srid = null)
/**
* Add a point column on the table.
*
- * @param string $column
- * @param null|int $srid
- *
- * @return \Illuminate\Support\Fluent
+ * @param ?int $srid
*/
- public function point($column, $srid = null)
+ public function point($column, $srid = null): \Illuminate\Support\Fluent
{
return $this->addColumn('point', $column, compact('srid'));
}
/**
* Add a linestring column on the table.
- *
- * @param string $column
- * @param null|int $srid
- *
- * @return \Illuminate\Support\Fluent
*/
- public function lineString($column, $srid = null)
+ public function lineString($column, ?int $srid = null): \Illuminate\Support\Fluent
{
return $this->addColumn('linestring', $column, compact('srid'));
}
/**
* Add a polygon column on the table.
- *
- * @param string $column
- * @param null|int $srid
- *
- * @return \Illuminate\Support\Fluent
*/
- public function polygon($column, $srid = null)
+ public function polygon($column, ?int $srid = null): \Illuminate\Support\Fluent
{
return $this->addColumn('polygon', $column, compact('srid'));
}
/**
* Add a multipoint column on the table.
- *
- * @param string $column
- * @param null|int $srid
- *
- * @return \Illuminate\Support\Fluent
*/
- public function multiPoint($column, $srid = null)
+ public function multiPoint($column, ?int $srid = null): \Illuminate\Support\Fluent
{
return $this->addColumn('multipoint', $column, compact('srid'));
}
/**
* Add a multilinestring column on the table.
- *
- * @param string $column
- * @param null|int $srid
- *
- * @return \Illuminate\Support\Fluent
*/
- public function multiLineString($column, $srid = null)
+ public function multiLineString($column, ?int $srid = null): \Illuminate\Support\Fluent
{
return $this->addColumn('multilinestring', $column, compact('srid'));
}
/**
* Add a multipolygon column on the table.
- *
- * @param string $column
- * @param null|int $srid
- *
- * @return \Illuminate\Support\Fluent
*/
- public function multiPolygon($column, $srid = null)
+ public function multiPolygon($column, ?int $srid = null): \Illuminate\Support\Fluent
{
return $this->addColumn('multipolygon', $column, compact('srid'));
}
/**
* Add a geometrycollection column on the table.
- *
- * @param string $column
- * @param null|int $srid
- *
- * @return \Illuminate\Support\Fluent
*/
- public function geometryCollection($column, $srid = null)
+ public function geometryCollection($column, ?int $srid = null): \Illuminate\Support\Fluent
{
return $this->addColumn('geometrycollection', $column, compact('srid'));
}
@@ -113,12 +75,10 @@ public function geometryCollection($column, $srid = null)
/**
* Specify a spatial index for the table.
*
- * @param string|array $columns
- * @param string $name
- *
- * @return \Illuminate\Support\Fluent
+ * @param string|array $columns
+ * @param string $name
*/
- public function spatialIndex($columns, $name = null)
+ public function spatialIndex($columns, $name = null): \Illuminate\Support\Fluent
{
return $this->indexCommand('spatial', $columns, $name);
}
@@ -126,11 +86,9 @@ public function spatialIndex($columns, $name = null)
/**
* Indicate that the given index should be dropped.
*
- * @param string|array $index
- *
- * @return \Illuminate\Support\Fluent
+ * @param string|array $index
*/
- public function dropSpatialIndex($index)
+ public function dropSpatialIndex($index): \Illuminate\Support\Fluent
{
return $this->dropIndexCommand('dropIndex', 'spatial', $index);
}
diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php
index baf8dc58..1aeb4165 100644
--- a/src/Schema/Builder.php
+++ b/src/Schema/Builder.php
@@ -10,9 +10,8 @@ class Builder extends MySqlBuilder
/**
* Create a new command set with a Closure.
*
- * @param string $table
- * @param Closure $callback
- *
+ * @param string $table
+ * @param Closure $callback
* @return Blueprint
*/
protected function createBlueprint($table, Closure $callback = null)
diff --git a/src/Schema/Grammars/MySqlGrammar.php b/src/Schema/Grammars/MySqlGrammar.php
index 9afe4513..a3332077 100644
--- a/src/Schema/Grammars/MySqlGrammar.php
+++ b/src/Schema/Grammars/MySqlGrammar.php
@@ -13,132 +13,92 @@ class MySqlGrammar extends IlluminateMySqlGrammar
public function __construct()
{
// Enable SRID as a column modifier
- if (!in_array(self::COLUMN_MODIFIER_SRID, $this->modifiers)) {
+ if (! in_array(self::COLUMN_MODIFIER_SRID, $this->modifiers)) {
$this->modifiers[] = self::COLUMN_MODIFIER_SRID;
}
}
/**
* Adds a statement to add a geometry column.
- *
- * @param Fluent $column
- *
- * @return string
*/
- public function typeGeometry(Fluent $column)
+ public function typeGeometry(Fluent $column): string
{
return 'GEOMETRY';
}
/**
* Adds a statement to add a point column.
- *
- * @param Fluent $column
- *
- * @return string
*/
- public function typePoint(Fluent $column)
+ public function typePoint(Fluent $column): string
{
return 'POINT';
}
/**
* Adds a statement to add a linestring column.
- *
- * @param Fluent $column
- *
- * @return string
*/
- public function typeLinestring(Fluent $column)
+ public function typeLinestring(Fluent $column): string
{
return 'LINESTRING';
}
/**
* Adds a statement to add a polygon column.
- *
- * @param Fluent $column
- *
- * @return string
*/
- public function typePolygon(Fluent $column)
+ public function typePolygon(Fluent $column): string
{
return 'POLYGON';
}
/**
* Adds a statement to add a multipoint column.
- *
- * @param Fluent $column
- *
- * @return string
*/
- public function typeMultipoint(Fluent $column)
+ public function typeMultipoint(Fluent $column): string
{
return 'MULTIPOINT';
}
/**
* Adds a statement to add a multilinestring column.
- *
- * @param Fluent $column
- *
- * @return string
*/
- public function typeMultilinestring(Fluent $column)
+ public function typeMultilinestring(Fluent $column): string
{
return 'MULTILINESTRING';
}
/**
* Adds a statement to add a multipolygon column.
- *
- * @param Fluent $column
- *
- * @return string
*/
- public function typeMultipolygon(Fluent $column)
+ public function typeMultipolygon(Fluent $column): string
{
return 'MULTIPOLYGON';
}
/**
* Adds a statement to add a geometrycollection column.
- *
- * @param Fluent $column
- *
- * @return string
*/
- public function typeGeometrycollection(Fluent $column)
+ public function typeGeometrycollection(Fluent $column): string
{
return 'GEOMETRYCOLLECTION';
}
/**
* Compile a spatial index key command.
- *
- * @param Blueprint $blueprint
- * @param Fluent $command
- *
- * @return string
*/
- public function compileSpatial(Blueprint $blueprint, Fluent $command)
+ public function compileSpatial(Blueprint $blueprint, Fluent $command): string
{
return $this->compileKey($blueprint, $command, 'spatial');
}
/**
* Get the SQL for a SRID column modifier.
- *
- * @param \Illuminate\Database\Schema\Blueprint $blueprint
- * @param Fluent $column
- *
- * @return string|null
*/
- protected function modifySrid(\Illuminate\Database\Schema\Blueprint $blueprint, Fluent $column)
+ protected function modifySrid(\Illuminate\Database\Schema\Blueprint $blueprint, Fluent $column): ?string
{
- if (!is_null($column->srid) && is_int($column->srid) && $column->srid > 0) {
+ if (! is_null($column->srid) && is_int($column->srid) && $column->srid > 0) {
return ' srid '.$column->srid;
}
+
+ return null;
}
}
diff --git a/src/SpatialServiceProvider.php b/src/SpatialServiceProvider.php
index 3b859f8e..d3933701 100644
--- a/src/SpatialServiceProvider.php
+++ b/src/SpatialServiceProvider.php
@@ -22,40 +22,36 @@ class SpatialServiceProvider extends DatabaseServiceProvider
{
/**
* Register the service provider.
- *
- * @return void
*/
- public function register()
+ public function register(): void
{
+ parent::register();
+
// The connection factory is used to create the actual connection instances on
// the database. We will inject the factory into the manager so that it may
// make the connections while they are actually needed and not of before.
- $this->app->singleton('db.factory', function ($app) {
- return new ConnectionFactory($app);
- });
+ $this->app->singleton('db.factory', fn ($app) => new ConnectionFactory($app));
// The database manager is used to resolve various connections, since multiple
// connections might be managed. It also implements the connection resolver
// interface which may be used by other components requiring connections.
- $this->app->singleton('db', function ($app) {
- return new DatabaseManager($app, $app['db.factory']);
- });
+ $this->app->singleton('db', fn ($app) => new DatabaseManager($app, $app['db.factory']));
if (class_exists(DoctrineType::class)) {
// Prevent geometry type fields from throwing a 'type not found' error when changing them
$geometries = [
- 'geometry' => Geometry::class,
- 'point' => Point::class,
- 'linestring' => LineString::class,
- 'polygon' => Polygon::class,
- 'multipoint' => MultiPoint::class,
- 'multilinestring' => MultiLineString::class,
- 'multipolygon' => MultiPolygon::class,
+ 'geometry' => Geometry::class,
+ 'point' => Point::class,
+ 'linestring' => LineString::class,
+ 'polygon' => Polygon::class,
+ 'multipoint' => MultiPoint::class,
+ 'multilinestring' => MultiLineString::class,
+ 'multipolygon' => MultiPolygon::class,
'geometrycollection' => GeometryCollection::class,
];
$typeNames = array_keys(DoctrineType::getTypesMap());
foreach ($geometries as $type => $class) {
- if (!in_array($type, $typeNames)) {
+ if (! in_array($type, $typeNames)) {
DoctrineType::addType($type, $class);
}
}
diff --git a/src/Types/Factory.php b/src/Types/Factory.php
index ed04ac2d..4f4d9d68 100755
--- a/src/Types/Factory.php
+++ b/src/Types/Factory.php
@@ -4,42 +4,74 @@
class Factory implements \GeoIO\Factory
{
- public function createPoint($dimension, array $coordinates, $srid = null)
+ /**
+ * @param mixed $dimension
+ * @param ?int $srid
+ */
+ public function createPoint($dimension, array $coordinates, $srid = 0): Point
{
return new Point($coordinates['y'], $coordinates['x'], $srid);
}
- public function createLineString($dimension, array $points, $srid = null)
+ /**
+ * @param mixed $dimension
+ * @param ?int $srid
+ */
+ public function createLineString($dimension, array $points, $srid = 0): LineString
{
return new LineString($points, $srid);
}
- public function createLinearRing($dimension, array $points, $srid = null)
+ /**
+ * @param mixed $dimension
+ * @param ?int $srid
+ */
+ public function createLinearRing($dimension, array $points, $srid = 0): LineString
{
return new LineString($points, $srid);
}
- public function createPolygon($dimension, array $lineStrings, $srid = null)
+ /**
+ * @param mixed $dimension
+ * @param ?int $srid
+ */
+ public function createPolygon($dimension, array $lineStrings, $srid = 0): Polygon
{
return new Polygon($lineStrings, $srid);
}
- public function createMultiPoint($dimension, array $points, $srid = null)
+ /**
+ * @param mixed $dimension
+ * @param ?int $srid
+ */
+ public function createMultiPoint($dimension, array $points, $srid = 0): MultiPoint
{
return new MultiPoint($points, $srid);
}
- public function createMultiLineString($dimension, array $lineStrings, $srid = null)
+ /**
+ * @param mixed $dimension
+ * @param ?int $srid
+ */
+ public function createMultiLineString($dimension, array $lineStrings, $srid = 0): MultiLineString
{
return new MultiLineString($lineStrings, $srid);
}
- public function createMultiPolygon($dimension, array $polygons, $srid = null)
+ /**
+ * @param mixed $dimension
+ * @param ?int $srid
+ */
+ public function createMultiPolygon($dimension, array $polygons, $srid = 0): MultiPolygon
{
return new MultiPolygon($polygons, $srid);
}
- public function createGeometryCollection($dimension, array $geometries, $srid = null)
+ /**
+ * @param mixed $dimension
+ * @param ?int $srid
+ */
+ public function createGeometryCollection($dimension, array $geometries, $srid = 0): GeometryCollection
{
return new GeometryCollection($geometries, $srid);
}
diff --git a/src/Types/Geometry.php b/src/Types/Geometry.php
index f840874c..81986387 100644
--- a/src/Types/Geometry.php
+++ b/src/Types/Geometry.php
@@ -3,13 +3,14 @@
namespace Grimzy\LaravelMysqlSpatial\Types;
use GeoIO\WKB\Parser\Parser;
+use GeoJson\Feature\Feature;
use GeoJson\GeoJson;
use Grimzy\LaravelMysqlSpatial\Exceptions\UnknownWKTTypeException;
use Illuminate\Contracts\Support\Jsonable;
abstract class Geometry implements GeometryInterface, Jsonable, \JsonSerializable
{
- protected static $wkb_types = [
+ protected static array$wkb_types = [
1 => Point::class,
2 => LineString::class,
3 => Polygon::class,
@@ -19,24 +20,24 @@ abstract class Geometry implements GeometryInterface, Jsonable, \JsonSerializabl
7 => GeometryCollection::class,
];
- protected $srid;
+ protected int $srid;
- public function __construct($srid = 0)
+ public function __construct(int $srid = 0)
{
- $this->srid = (int) $srid;
+ $this->srid = $srid;
}
- public function getSrid()
+ public function getSrid(): int
{
return $this->srid;
}
- public function setSrid($srid)
+ public function setSrid(int $srid): void
{
- $this->srid = (int) $srid;
+ $this->srid = $srid;
}
- public static function getWKTArgument($value)
+ public static function getWKTArgument(string $value): string
{
$left = strpos($value, '(');
$right = strrpos($value, ')');
@@ -44,32 +45,25 @@ public static function getWKTArgument($value)
return substr($value, $left + 1, $right - $left - 1);
}
- public static function getWKTClass($value)
+ /** @return class-string */
+ public static function getWKTClass(string $value): string
{
$left = strpos($value, '(');
$type = trim(substr($value, 0, $left));
- switch (strtoupper($type)) {
- case 'POINT':
- return Point::class;
- case 'LINESTRING':
- return LineString::class;
- case 'POLYGON':
- return Polygon::class;
- case 'MULTIPOINT':
- return MultiPoint::class;
- case 'MULTILINESTRING':
- return MultiLineString::class;
- case 'MULTIPOLYGON':
- return MultiPolygon::class;
- case 'GEOMETRYCOLLECTION':
- return GeometryCollection::class;
- default:
- throw new UnknownWKTTypeException('Type was '.$type);
- }
+ return match (strtoupper($type)) {
+ 'POINT' => Point::class,
+ 'LINESTRING' => LineString::class,
+ 'POLYGON' => Polygon::class,
+ 'MULTIPOINT' => MultiPoint::class,
+ 'MULTILINESTRING' => MultiLineString::class,
+ 'MULTIPOLYGON' => MultiPolygon::class,
+ 'GEOMETRYCOLLECTION' => GeometryCollection::class,
+ default => throw new UnknownWKTTypeException('Type was '.$type)
+ };
}
- public static function fromWKB($wkb)
+ public static function fromWKB(string $wkb): Geometry
{
$srid = substr($wkb, 0, 4);
$srid = unpack('L', $srid)[1];
@@ -87,14 +81,14 @@ public static function fromWKB($wkb)
return $parsed;
}
- public static function fromWKT($wkt, $srid = null)
+ public static function fromWKT(string $wkt, int $srid = 0): static
{
$wktArgument = static::getWKTArgument($wkt);
return static::fromString($wktArgument, $srid);
}
- public static function fromJson($geoJson)
+ public static function fromJson(string|GeoJson $geoJson): self
{
if (is_string($geoJson)) {
$geoJson = GeoJson::jsonUnserialize(json_decode($geoJson));
@@ -104,6 +98,7 @@ public static function fromJson($geoJson)
return GeometryCollection::fromJson($geoJson);
}
+ /** @var Feature $geoJson */
if ($geoJson->getType() === 'Feature') {
$geoJson = $geoJson->getGeometry();
}
diff --git a/src/Types/GeometryCollection.php b/src/Types/GeometryCollection.php
index 35f093f7..3a85a929 100755
--- a/src/Types/GeometryCollection.php
+++ b/src/Types/GeometryCollection.php
@@ -12,62 +12,63 @@
use InvalidArgumentException;
use IteratorAggregate;
-class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAccess, Arrayable, Countable
+/**
+ * @template G
+ *
+ * @implements GeometryInterface
+ */
+class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAccess, Arrayable, Countable, GeometryInterface
{
/**
* The minimum number of items required to create this collection.
- *
- * @var int
*/
- protected $minimumCollectionItems = 0;
+ protected int $minimumCollectionItems = 0;
/**
* The class of the items in the collection.
- *
- * @var string
*/
- protected $collectionItemType = GeometryInterface::class;
+ protected string $collectionItemType = GeometryInterface::class;
/**
* The items contained in the spatial collection.
*
- * @var GeometryInterface[]
+ * @var G[]
*/
- protected $items = [];
+ protected array $items = [];
/**
- * @param GeometryInterface[] $geometries
- * @param int $srid
+ * @param GeometryInterface[] $geometries
*
* @throws InvalidArgumentException
*/
- public function __construct(array $geometries, $srid = 0)
+ public function __construct(array $geometries, ?int $srid = 0)
{
- parent::__construct($srid);
+ parent::__construct((int) $srid);
$this->validateItems($geometries);
$this->items = $geometries;
}
- public function getGeometries()
+ /**
+ * @return G[]
+ */
+ public function getGeometries(): array
{
return $this->items;
}
- public function toWKT()
+ public function toWKT(): string
{
return sprintf('GEOMETRYCOLLECTION(%s)', (string) $this);
}
public function __toString()
{
- return implode(',', array_map(function (GeometryInterface $geometry) {
- return $geometry->toWKT();
- }, $this->items));
+ return implode(',', array_map(fn (GeometryInterface $geometry) => $geometry->toWKT(), $this->items));
}
- public static function fromString($wktArgument, $srid = 0)
+ public static function fromString(string $wktArgument, int $srid = 0): static
{
if (empty($wktArgument)) {
return new static([]);
@@ -87,22 +88,23 @@ public function toArray()
return $this->items;
}
- public function getIterator()
+ public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->items);
}
- public function offsetExists($offset)
+ public function offsetExists($offset): bool
{
return isset($this->items[$offset]);
}
+ #[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->offsetExists($offset) ? $this->items[$offset] : null;
}
- public function offsetSet($offset, $value)
+ public function offsetSet($offset, $value): void
{
$this->validateItemType($value);
@@ -113,23 +115,23 @@ public function offsetSet($offset, $value)
}
}
- public function offsetUnset($offset)
+ public function offsetUnset($offset): void
{
unset($this->items[$offset]);
}
- public function count()
+ public function count(): int
{
return count($this->items);
}
- public static function fromJson($geoJson)
+ public static function fromJson(string|GeoJson $geoJson): self
{
if (is_string($geoJson)) {
$geoJson = GeoJson::jsonUnserialize(json_decode($geoJson));
}
- if (!is_a($geoJson, FeatureCollection::class)) {
+ if (! is_a($geoJson, FeatureCollection::class)) {
throw new InvalidGeoJsonException('Expected '.FeatureCollection::class.', got '.get_class($geoJson));
}
@@ -143,9 +145,8 @@ public static function fromJson($geoJson)
/**
* Convert to GeoJson GeometryCollection that is jsonable to GeoJSON.
- *
- * @return \GeoJson\Geometry\GeometryCollection
*/
+ #[\ReturnTypeWillChange]
public function jsonSerialize()
{
$geometries = [];
@@ -158,10 +159,8 @@ public function jsonSerialize()
/**
* Checks whether the items are valid to create this collection.
- *
- * @param array $items
*/
- protected function validateItems(array $items)
+ protected function validateItems(array $items): void
{
$this->validateItemCount($items);
@@ -173,11 +172,11 @@ protected function validateItems(array $items)
/**
* Checks whether the array has enough items to generate a valid WKT.
*
- * @param array $items
+ * @param GeometryInterface[] $items
*
* @see $minimumCollectionItems
*/
- protected function validateItemCount(array $items)
+ protected function validateItemCount(array $items): void
{
if (count($items) < $this->minimumCollectionItems) {
$entries = $this->minimumCollectionItems === 1 ? 'entry' : 'entries';
@@ -194,13 +193,12 @@ protected function validateItemCount(array $items)
/**
* Checks the type of the items in the array.
*
- * @param $item
*
* @see $collectionItemType
*/
- protected function validateItemType($item)
+ protected function validateItemType(mixed $item): void
{
- if (!$item instanceof $this->collectionItemType) {
+ if (! $item instanceof $this->collectionItemType) {
throw new InvalidArgumentException(sprintf(
'%s must be a collection of %s',
get_class($this),
diff --git a/src/Types/GeometryInterface.php b/src/Types/GeometryInterface.php
index 4f0dd1ef..67669d67 100644
--- a/src/Types/GeometryInterface.php
+++ b/src/Types/GeometryInterface.php
@@ -2,15 +2,27 @@
namespace Grimzy\LaravelMysqlSpatial\Types;
+use GeoJson\GeoJson;
+
+/**
+ * @template T
+ */
interface GeometryInterface
{
- public function toWKT();
+ public function toWKT(): string;
- public static function fromWKT($wkt, $srid = 0);
+ public static function fromWKT(string $wkt, int $srid = 0): self;
public function __toString();
- public static function fromString($wktArgument, $srid = 0);
+ public static function fromString(string $wktArgument, int $srid = 0): self;
+
+ public static function fromJson(string|GeoJson $geoJson): self;
+
+ /**
+ * @return T
+ */
+ public function jsonSerialize();
- public static function fromJson($geoJson);
+ public function getSrid(): int;
}
diff --git a/src/Types/LineString.php b/src/Types/LineString.php
index 1cc4a410..dbc02525 100644
--- a/src/Types/LineString.php
+++ b/src/Types/LineString.php
@@ -10,29 +10,25 @@ class LineString extends PointCollection
{
/**
* The minimum number of items required to create this collection.
- *
- * @var int
*/
- protected $minimumCollectionItems = 2;
+ protected int $minimumCollectionItems = 2;
- public function toWKT()
+ public function toWKT(): string
{
return sprintf('LINESTRING(%s)', $this->toPairList());
}
- public static function fromWkt($wkt, $srid = 0)
+ public static function fromWKT(string $wkt, int $srid = 0): static
{
$wktArgument = Geometry::getWKTArgument($wkt);
return static::fromString($wktArgument, $srid);
}
- public static function fromString($wktArgument, $srid = 0)
+ public static function fromString(string $wktArgument, int $srid = 0): static
{
$pairs = explode(',', trim($wktArgument));
- $points = array_map(function ($pair) {
- return Point::fromPair($pair);
- }, $pairs);
+ $points = array_map(fn ($pair) => Point::fromPair($pair), $pairs);
return new static($points, $srid);
}
@@ -42,13 +38,13 @@ public function __toString()
return $this->toPairList();
}
- public static function fromJson($geoJson)
+ public static function fromJson(string|GeoJson $geoJson): self
{
if (is_string($geoJson)) {
$geoJson = GeoJson::jsonUnserialize(json_decode($geoJson));
}
- if (!is_a($geoJson, GeoJsonLineString::class)) {
+ if (! is_a($geoJson, GeoJsonLineString::class)) {
throw new InvalidGeoJsonException('Expected '.GeoJsonLineString::class.', got '.get_class($geoJson));
}
@@ -62,9 +58,8 @@ public static function fromJson($geoJson)
/**
* Convert to GeoJson LineString that is jsonable to GeoJSON.
- *
- * @return \GeoJson\Geometry\LineString
*/
+ #[\ReturnTypeWillChange]
public function jsonSerialize()
{
$points = [];
diff --git a/src/Types/MultiLineString.php b/src/Types/MultiLineString.php
index 62c4d576..57beea2a 100644
--- a/src/Types/MultiLineString.php
+++ b/src/Types/MultiLineString.php
@@ -6,63 +6,60 @@
use GeoJson\Geometry\MultiLineString as GeoJsonMultiLineString;
use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;
-class MultiLineString extends GeometryCollection
+/**
+ * @implements GeometryInterface
+ *
+ * @extends GeometryCollection
+ */
+class MultiLineString extends GeometryCollection implements GeometryInterface
{
/**
* The minimum number of items required to create this collection.
- *
- * @var int
*/
- protected $minimumCollectionItems = 1;
+ protected int $minimumCollectionItems = 1;
/**
* The class of the items in the collection.
- *
- * @var string
*/
- protected $collectionItemType = LineString::class;
+ protected string $collectionItemType = LineString::class;
- public function getLineStrings()
+ public function getLineStrings(): array
{
return $this->items;
}
- public function toWKT()
+ public function toWKT(): string
{
return sprintf('MULTILINESTRING(%s)', (string) $this);
}
- public static function fromString($wktArgument, $srid = 0)
+ public static function fromString(string $wktArgument, int $srid = 0): static
{
$str = preg_split('/\)\s*,\s*\(/', substr(trim($wktArgument), 1, -1));
- $lineStrings = array_map(function ($data) {
- return LineString::fromString($data);
- }, $str);
+ $lineStrings = array_map(fn ($data) => LineString::fromString($data), $str);
return new static($lineStrings, $srid);
}
public function __toString()
{
- return implode(',', array_map(function (LineString $lineString) {
- return sprintf('(%s)', (string) $lineString);
- }, $this->getLineStrings()));
+ return implode(',', array_map(fn (LineString $lineString) => sprintf('(%s)', (string) $lineString), $this->getLineStrings()));
}
- public function offsetSet($offset, $value)
+ public function offsetSet($offset, $value): void
{
$this->validateItemType($value);
parent::offsetSet($offset, $value);
}
- public static function fromJson($geoJson)
+ public static function fromJson(string|GeoJson $geoJson): self
{
if (is_string($geoJson)) {
$geoJson = GeoJson::jsonUnserialize(json_decode($geoJson));
}
- if (!is_a($geoJson, GeoJsonMultiLineString::class)) {
+ if (! is_a($geoJson, GeoJsonMultiLineString::class)) {
throw new InvalidGeoJsonException('Expected '.GeoJsonMultiLineString::class.', got '.get_class($geoJson));
}
@@ -80,9 +77,8 @@ public static function fromJson($geoJson)
/**
* Convert to GeoJson Point that is jsonable to GeoJSON.
- *
- * @return \GeoJson\Geometry\MultiLineString
*/
+ #[\ReturnTypeWillChange]
public function jsonSerialize()
{
$lineStrings = [];
diff --git a/src/Types/MultiPoint.php b/src/Types/MultiPoint.php
index 752967eb..f9bdbdea 100644
--- a/src/Types/MultiPoint.php
+++ b/src/Types/MultiPoint.php
@@ -6,53 +6,50 @@
use GeoJson\Geometry\MultiPoint as GeoJsonMultiPoint;
use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;
-class MultiPoint extends PointCollection
+/**
+ * @implements GeometryInterface
+ */
+class MultiPoint extends PointCollection implements GeometryInterface
{
/**
* The minimum number of items required to create this collection.
- *
- * @var int
*/
- protected $minimumCollectionItems = 1;
+ protected int $minimumCollectionItems = 1;
- public function toWKT()
+ public function toWKT(): string
{
return sprintf('MULTIPOINT(%s)', (string) $this);
}
- public static function fromWkt($wkt, $srid = 0)
+ public static function fromWKT(string $wkt, int $srid = 0): static
{
$wktArgument = Geometry::getWKTArgument($wkt);
return static::fromString($wktArgument, $srid);
}
- public static function fromString($wktArgument, $srid = 0)
+ public static function fromString(string $wktArgument, int $srid = 0): static
{
$matches = [];
preg_match_all('/\(\s*(\d+\s+\d+)\s*\)/', trim($wktArgument), $matches);
- $points = array_map(function ($pair) {
- return Point::fromPair($pair);
- }, $matches[1]);
+ $points = array_map(fn ($pair) => Point::fromPair($pair), $matches[1]);
return new static($points, $srid);
}
public function __toString()
{
- return implode(',', array_map(function (Point $point) {
- return sprintf('(%s)', $point->toPair());
- }, $this->items));
+ return implode(',', array_map(fn (Point $point) => sprintf('(%s)', $point->toPair()), $this->items));
}
- public static function fromJson($geoJson)
+ public static function fromJson(string|GeoJson $geoJson): self
{
if (is_string($geoJson)) {
$geoJson = GeoJson::jsonUnserialize(json_decode($geoJson));
}
- if (!is_a($geoJson, GeoJsonMultiPoint::class)) {
+ if (! is_a($geoJson, GeoJsonMultiPoint::class)) {
throw new InvalidGeoJsonException('Expected '.GeoJsonMultiPoint::class.', got '.get_class($geoJson));
}
@@ -66,8 +63,6 @@ public static function fromJson($geoJson)
/**
* Convert to GeoJson MultiPoint that is jsonable to GeoJSON.
- *
- * @return \GeoJson\Geometry\MultiPoint
*/
public function jsonSerialize()
{
diff --git a/src/Types/MultiPolygon.php b/src/Types/MultiPolygon.php
index cdea3a9c..dc666ca0 100644
--- a/src/Types/MultiPolygon.php
+++ b/src/Types/MultiPolygon.php
@@ -6,50 +6,45 @@
use GeoJson\Geometry\MultiPolygon as GeoJsonMultiPolygon;
use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;
-class MultiPolygon extends GeometryCollection
+/**
+ * @implements GeometryInterface
+ *
+ * @extends GeometryCollection
+ */
+class MultiPolygon extends GeometryCollection implements GeometryInterface
{
/**
* The minimum number of items required to create this collection.
- *
- * @var int
*/
- protected $minimumCollectionItems = 1;
+ protected int $minimumCollectionItems = 1;
/**
* The class of the items in the collection.
- *
- * @var string
*/
- protected $collectionItemType = Polygon::class;
+ protected string $collectionItemType = Polygon::class;
- public function toWKT()
+ public function toWKT(): string
{
return sprintf('MULTIPOLYGON(%s)', (string) $this);
}
public function __toString()
{
- return implode(',', array_map(function (Polygon $polygon) {
- return sprintf('(%s)', (string) $polygon);
- }, $this->items));
+ return implode(',', array_map(fn (Polygon $polygon) => sprintf('(%s)', (string) $polygon), $this->items));
}
- public static function fromString($wktArgument, $srid = 0)
+ public static function fromString(string $wktArgument, int $srid = 0): static
{
$parts = preg_split('/(\)\s*\)\s*,\s*\(\s*\()/', $wktArgument, -1, PREG_SPLIT_DELIM_CAPTURE);
$polygons = static::assembleParts($parts);
- return new static(array_map(function ($polygonString) {
- return Polygon::fromString($polygonString);
- }, $polygons), $srid);
+ return new static(array_map(fn ($polygonString) => Polygon::fromString($polygonString), $polygons), $srid);
}
/**
* Get the polygons that make up this MultiPolygon.
- *
- * @return array|Polygon[]
*/
- public function getPolygons()
+ public function getPolygons(): array
{
return $this->items;
}
@@ -66,19 +61,15 @@ public function getPolygons()
* "((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1))",
* "((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1))",
* "((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1))"
- *
- * @param array $parts
- *
- * @return array
*/
- protected static function assembleParts(array $parts)
+ protected static function assembleParts(array $parts): array
{
$polygons = [];
$count = count($parts);
for ($i = 0; $i < $count; $i++) {
if ($i % 2 !== 0) {
- list($end, $start) = explode(',', $parts[$i]);
+ [$end, $start] = explode(',', $parts[$i]);
$polygons[$i - 1] .= $end;
$polygons[++$i] = $start.$parts[$i];
} else {
@@ -89,20 +80,20 @@ protected static function assembleParts(array $parts)
return $polygons;
}
- public function offsetSet($offset, $value)
+ public function offsetSet($offset, $value): void
{
$this->validateItemType($value);
parent::offsetSet($offset, $value);
}
- public static function fromJson($geoJson)
+ public static function fromJson(string|GeoJson $geoJson): self
{
if (is_string($geoJson)) {
$geoJson = GeoJson::jsonUnserialize(json_decode($geoJson));
}
- if (!is_a($geoJson, GeoJsonMultiPolygon::class)) {
+ if (! is_a($geoJson, GeoJsonMultiPolygon::class)) {
throw new InvalidGeoJsonException('Expected '.GeoJsonMultiPolygon::class.', got '.get_class($geoJson));
}
@@ -124,9 +115,8 @@ public static function fromJson($geoJson)
/**
* Convert to GeoJson MultiPolygon that is jsonable to GeoJSON.
- *
- * @return \GeoJson\Geometry\MultiPolygon
*/
+ #[\ReturnTypeWillChange]
public function jsonSerialize()
{
$polygons = [];
diff --git a/src/Types/Point.php b/src/Types/Point.php
index d424ec5e..21c458c6 100644
--- a/src/Types/Point.php
+++ b/src/Types/Point.php
@@ -6,79 +6,81 @@
use GeoJson\Geometry\Point as GeoJsonPoint;
use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;
-class Point extends Geometry
+/**
+ * @implements GeometryInterface
+ */
+class Point extends Geometry implements GeometryInterface
{
- protected $lat;
+ protected float $lat;
- protected $lng;
+ protected float $lng;
- public function __construct($lat, $lng, $srid = 0)
+ public function __construct(float $lat, float $lng, ?int $srid = 0)
{
- parent::__construct($srid);
+ parent::__construct((int) $srid);
- $this->lat = (float) $lat;
- $this->lng = (float) $lng;
+ $this->lat = $lat;
+ $this->lng = $lng;
}
- public function getLat()
+ public function getLat(): float
{
return $this->lat;
}
- public function setLat($lat)
+ public function setLat(float $lat): void
{
- $this->lat = (float) $lat;
+ $this->lat = $lat;
}
- public function getLng()
+ public function getLng(): float
{
return $this->lng;
}
- public function setLng($lng)
+ public function setLng(float $lng): void
{
- $this->lng = (float) $lng;
+ $this->lng = $lng;
}
- public function toPair()
+ public function toPair(): string
{
return $this->getLng().' '.$this->getLat();
}
- public static function fromPair($pair, $srid = 0)
+ public static function fromPair(string $pair, int $srid = 0): static
{
- list($lng, $lat) = explode(' ', trim($pair, "\t\n\r \x0B()"));
+ [$lng, $lat] = explode(' ', trim($pair, "\t\n\r \x0B()"));
- return new static((float) $lat, (float) $lng, (int) $srid);
+ return new static((float) $lat, (float) $lng, $srid);
}
- public function toWKT()
+ public function toWKT(): string
{
return sprintf('POINT(%s)', (string) $this);
}
- public static function fromString($wktArgument, $srid = 0)
+ public static function fromString(string $wktArgument, int $srid = 0): self
{
return static::fromPair($wktArgument, $srid);
}
- public function __toString()
+ public function __toString(): string
{
return $this->getLng().' '.$this->getLat();
}
/**
* @param $geoJson \GeoJson\Feature\Feature|string
- *
* @return \Grimzy\LaravelMysqlSpatial\Types\Point
*/
- public static function fromJson($geoJson)
+ public static function fromJson(string|GeoJson $geoJson): self
{
if (is_string($geoJson)) {
$geoJson = GeoJson::jsonUnserialize(json_decode($geoJson));
}
- if (!is_a($geoJson, GeoJsonPoint::class)) {
+ if (! is_a($geoJson, GeoJsonPoint::class)) {
throw new InvalidGeoJsonException('Expected '.GeoJsonPoint::class.', got '.get_class($geoJson));
}
@@ -89,9 +91,8 @@ public static function fromJson($geoJson)
/**
* Convert to GeoJson Point that is jsonable to GeoJSON.
- *
- * @return \GeoJson\Geometry\Point
*/
+ #[\ReturnTypeWillChange]
public function jsonSerialize()
{
return new GeoJsonPoint([$this->getLng(), $this->getLat()]);
diff --git a/src/Types/PointCollection.php b/src/Types/PointCollection.php
index 30d1b8de..87232a1d 100755
--- a/src/Types/PointCollection.php
+++ b/src/Types/PointCollection.php
@@ -2,77 +2,30 @@
namespace Grimzy\LaravelMysqlSpatial\Types;
-use ArrayAccess;
-use InvalidArgumentException;
-
+/**
+ * @extends GeometryCollection
+ */
abstract class PointCollection extends GeometryCollection
{
/**
* The class of the items in the collection.
- *
- * @var string
*/
- protected $collectionItemType = Point::class;
+ protected string $collectionItemType = Point::class;
- public function toPairList()
+ public function toPairList(): string
{
- return implode(',', array_map(function (Point $point) {
- return $point->toPair();
- }, $this->items));
+ return implode(',', array_map(fn (Point $point): string => $point->toPair(), $this->items));
}
- public function offsetSet($offset, $value)
+ public function offsetSet($offset, $value): void
{
$this->validateItemType($value);
parent::offsetSet($offset, $value);
}
- /**
- * @return array|\Grimzy\LaravelMysqlSpatial\Types\Point[]
- */
- public function getPoints()
+ public function getPoints(): array
{
return $this->items;
}
-
- /**
- * @param \Grimzy\LaravelMysqlSpatial\Types\Point $point
- *
- * @deprecated 2.1.0 Use array_unshift($multipoint, $point); instead
- * @see array_unshift
- * @see ArrayAccess
- */
- public function prependPoint(Point $point)
- {
- array_unshift($this->items, $point);
- }
-
- /**
- * @param \Grimzy\LaravelMysqlSpatial\Types\Point $point
- *
- * @deprecated 2.1.0 Use $multipoint[] = $point; instead
- * @see ArrayAccess
- */
- public function appendPoint(Point $point)
- {
- $this->items[] = $point;
- }
-
- /**
- * @param $index
- * @param \Grimzy\LaravelMysqlSpatial\Types\Point $point
- *
- * @deprecated 2.1.0 Use array_splice($multipoint, $index, 0, [$point]); instead
- * @see array_splice
- * @see ArrayAccess
- */
- public function insertPoint($index, Point $point)
- {
- if (count($this->items) - 1 < $index) {
- throw new InvalidArgumentException('$index is greater than the size of the array');
- }
-
- array_splice($this->items, $index, 0, [$point]);
- }
}
diff --git a/src/Types/Polygon.php b/src/Types/Polygon.php
index 9c10cecc..7c3e4b60 100644
--- a/src/Types/Polygon.php
+++ b/src/Types/Polygon.php
@@ -6,20 +6,23 @@
use GeoJson\Geometry\Polygon as GeoJsonPolygon;
use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;
-class Polygon extends MultiLineString
+/**
+ * @implements GeometryInterface
+ */
+class Polygon extends MultiLineString implements GeometryInterface
{
- public function toWKT()
+ public function toWKT(): string
{
return sprintf('POLYGON(%s)', (string) $this);
}
- public static function fromJson($geoJson)
+ public static function fromJson(string|GeoJson $geoJson): self
{
if (is_string($geoJson)) {
$geoJson = GeoJson::jsonUnserialize(json_decode($geoJson));
}
- if (!is_a($geoJson, GeoJsonPolygon::class)) {
+ if (! is_a($geoJson, GeoJsonPolygon::class)) {
throw new InvalidGeoJsonException('Expected '.GeoJsonPolygon::class.', got '.get_class($geoJson));
}
@@ -37,9 +40,8 @@ public static function fromJson($geoJson)
/**
* Convert to GeoJson Polygon that is jsonable to GeoJSON.
- *
- * @return \GeoJson\Geometry\Polygon
*/
+ #[\ReturnTypeWillChange]
public function jsonSerialize()
{
$linearRings = [];
diff --git a/tests/Integration/Eloquent/TestModel.php b/tests/Integration/Eloquent/TestModel.php
new file mode 100644
index 00000000..1afd7f94
--- /dev/null
+++ b/tests/Integration/Eloquent/TestModel.php
@@ -0,0 +1,26 @@
+hasMany(TestRelatedModel::class);
+ }
+
+ public function testrelatedmodels2()
+ {
+ return $this->belongsToMany(TestRelatedModel::class);
+ }
+}
diff --git a/tests/Integration/Eloquent/TestRelatedModel.php b/tests/Integration/Eloquent/TestRelatedModel.php
new file mode 100644
index 00000000..f9610dce
--- /dev/null
+++ b/tests/Integration/Eloquent/TestRelatedModel.php
@@ -0,0 +1,16 @@
+belongsTo(TestModel::class);
+ }
+
+ public function testmodels()
+ {
+ return $this->belongsToMany(TestModel::class);
+ }
+}
diff --git a/tests/Integration/IntegrationBaseTestCase.php b/tests/Integration/IntegrationBaseCase.php
similarity index 76%
rename from tests/Integration/IntegrationBaseTestCase.php
rename to tests/Integration/IntegrationBaseCase.php
index 04634734..e9b5c752 100644
--- a/tests/Integration/IntegrationBaseTestCase.php
+++ b/tests/Integration/IntegrationBaseCase.php
@@ -1,20 +1,23 @@
register(SpatialServiceProvider::class);
@@ -41,29 +44,27 @@ public function createApplication()
/**
* Setup DB before each test.
- *
- * @return void
*/
- public function setUp()
+ public function setUp(): void
{
parent::setUp();
$this->after_fix = $this->isMySQL8AfterFix();
- $this->onMigrations(function ($migrationClass) {
- (new $migrationClass())->up();
- });
+ $this->artisan('migrate:fresh');
+
+ (new CreateTables)->up();
+ (new UpdateTables)->up();
//\DB::listen(function($sql) {
// var_dump($sql);
//});
}
- public function tearDown()
+ public function tearDown(): void
{
- $this->onMigrations(function ($migrationClass) {
- (new $migrationClass())->down();
- }, true);
+ (new UpdateTables)->down();
+ (new CreateTables)->down();
parent::tearDown();
}
@@ -71,7 +72,7 @@ public function tearDown()
// MySQL 8.0.4 fixed bug #26941370 and bug #88031
private function isMySQL8AfterFix()
{
- $results = DB::select(DB::raw('select version()'));
+ $results = DB::select(DB::raw('select version()')->getValue(DB::connection()->getQueryGrammar()));
$mysql_version = $results[0]->{'version()'};
return version_compare($mysql_version, '8.0.4', '>=');
@@ -90,7 +91,7 @@ protected function assertException($exceptionName, $exceptionMessage = null)
{
if (method_exists(parent::class, 'expectException')) {
parent::expectException($exceptionName);
- if (!is_null($exceptionMessage)) {
+ if (! is_null($exceptionMessage)) {
$this->expectExceptionMessage($exceptionMessage);
}
} else {
@@ -98,7 +99,7 @@ protected function assertException($exceptionName, $exceptionMessage = null)
}
}
- private function onMigrations(\Closure $closure, $reverse_sort = false)
+ private function onMigrations(Closure $closure, $reverse_sort = false)
{
$migrations = $this->migrations;
$reverse_sort ? rsort($migrations, SORT_STRING) : sort($migrations, SORT_STRING);
diff --git a/tests/Integration/MigrationTest.php b/tests/Integration/MigrationTest.php
index 6b740d0f..e7d909a1 100644
--- a/tests/Integration/MigrationTest.php
+++ b/tests/Integration/MigrationTest.php
@@ -1,12 +1,16 @@
charset = 'utf8mb4';
+ $table->collation = 'utf8mb4_unicode_ci';
+ $table->increments('id');
+ $table->geometryCollection('geometrycollection')->default(null)->nullable();
+ $table->lineString('linestring')->default(null)->nullable();
+ $table->multiLineString('multilinestring')->default(null)->nullable();
+ $table->multiPoint('multipoint')->default(null)->nullable();
+ $table->multiPolygon('multipolygon')->default(null)->nullable();
+ $table->point('point')->default(null)->nullable();
+ $table->polygon('polygon')->default(null)->nullable();
+ $table->timestamps();
+ });
+
Schema::create('geometry', function (Blueprint $table) {
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_unicode_ci';
@@ -50,13 +64,12 @@ public function up()
/**
* Reverse the migrations.
- *
- * @return void
*/
- public function down()
+ public function down(): void
{
Schema::drop('geometry');
Schema::drop('no_spatial_fields');
Schema::drop('with_srid');
+ Schema::drop('test_models');
}
}
diff --git a/tests/Integration/Migrations/UpdateTables.php b/tests/Integration/Migrations/UpdateTables.php
index 83915b81..9373c8d1 100644
--- a/tests/Integration/Migrations/UpdateTables.php
+++ b/tests/Integration/Migrations/UpdateTables.php
@@ -1,17 +1,17 @@
dropSpatialIndex(['location']); // either an array of column names or the index name
diff --git a/tests/Integration/Models/GeometryModel.php b/tests/Integration/Models/GeometryModel.php
index 854be5f1..8c585090 100644
--- a/tests/Integration/Models/GeometryModel.php
+++ b/tests/Integration/Models/GeometryModel.php
@@ -1,5 +1,7 @@
location)->get();
$this->assertCount(2, $a);
$this->assertEquals(0, $a[0]->distance);
- $this->assertEquals(1.4142135623, $a[1]->distance); // PHP floats' 11th+ digits don't matter
+ $this->assertEqualsWithDelta(1.4142135623, $a[1]->distance, 0.00000001); // PHP floats' 11th+ digits don't matter
}
public function testDistanceSphereValue()
@@ -230,9 +234,9 @@ public function testDistanceSphereValue()
$this->assertEquals(0, $a[0]->distance);
if ($this->after_fix) {
- $this->assertEquals(44.7414064842, $a[1]->distance); // PHP floats' 11th+ digits don't matter
+ $this->assertEqualsWithDelta(44.7414064842, $a[1]->distance, 0.00000001); // PHP floats' 11th+ digits don't matter
} else {
- $this->assertEquals(44.7414064845, $a[1]->distance); // PHP floats' 11th+ digits don't matter
+ $this->assertEqualsWithDelta(44.7414064845, $a[1]->distance, 0.00000001); // PHP floats' 11th+ digits don't matter
}
}
diff --git a/tests/Unit/Eloquent/SpatialTraitTest.php b/tests/Integration/SpatialTraitTest.php
similarity index 73%
rename from tests/Unit/Eloquent/SpatialTraitTest.php
rename to tests/Integration/SpatialTraitTest.php
index 8ece7d25..308a724f 100644
--- a/tests/Unit/Eloquent/SpatialTraitTest.php
+++ b/tests/Integration/SpatialTraitTest.php
@@ -1,35 +1,37 @@
model = new TestModel();
- $this->queries = &$this->model->getConnection()->getPdo()->queries;
+ DB::listen(function (QueryExecuted $query) {
+ $this->queries[] = $query->sql;
+ });
}
- public function tearDown()
+ public function tearDown(): void
{
- $this->model->getConnection()->getPdo()->resetQueries();
+ $this->queries = [];
}
public function testInsertUpdatePointHasCorrectSql()
@@ -40,7 +42,7 @@ public function testInsertUpdatePointHasCorrectSql()
$this->model->save();
$this->assertStringStartsWith('insert', $this->queries[0]);
- $this->assertContains('insert into `test_models` (`point`) values (ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $this->queries[0]);
+ $this->assertStringContainsString('insert into `test_models` (`point`) values (ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $this->queries[0]);
// TODO: assert bindings in query
$this->assertTrue($this->model->exists);
@@ -48,7 +50,7 @@ public function testInsertUpdatePointHasCorrectSql()
$this->model->save();
$this->assertStringStartsWith('update', $this->queries[1]);
- $this->assertContains('update `test_models` set `point` = ST_GeomFromText(?, ?, \'axis-order=long-lat\') where `id` = ?', $this->queries[1]);
+ $this->assertStringContainsString('update `test_models` set `point` = ST_GeomFromText(?, ?, \'axis-order=long-lat\') where `id` = ?', $this->queries[1]);
// TODO: assert bindings in query
}
@@ -63,7 +65,7 @@ public function testInsertUpdateLineStringHasCorrectSql()
$this->model->save();
$this->assertStringStartsWith('insert', $this->queries[0]);
- $this->assertContains('insert into `test_models` (`linestring`) values (ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $this->queries[0]);
+ $this->assertStringContainsString('insert into `test_models` (`linestring`) values (ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $this->queries[0]);
// TODO: assert bindings in query
$this->assertTrue($this->model->exists);
@@ -71,7 +73,7 @@ public function testInsertUpdateLineStringHasCorrectSql()
$this->model->save();
$this->assertStringStartsWith('update', $this->queries[1]);
- $this->assertContains('update `test_models` set `linestring` = ST_GeomFromText(?, ?, \'axis-order=long-lat\') where `id` = ?', $this->queries[1]);
+ $this->assertStringContainsString('update `test_models` set `linestring` = ST_GeomFromText(?, ?, \'axis-order=long-lat\') where `id` = ?', $this->queries[1]);
// TODO: assert bindings in query
}
@@ -79,25 +81,32 @@ public function testInsertUpdatePolygonHasCorrectSql()
{
$point1 = new Point(1, 2);
$point2 = new Point(2, 3);
- $linestring1 = new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point1, $point2]);
$point3 = new Point(3, 2);
$point4 = new Point(2, 1);
- $linestring2 = new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point3, $point4]);
+
+ $polygon = new \Grimzy\LaravelMysqlSpatial\Types\Polygon([
+ new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point1, $point2, $point3, $point4, $point1]),
+ ]);
$this->assertFalse($this->model->exists);
- $this->model->polygon = new \Grimzy\LaravelMysqlSpatial\Types\Polygon([$linestring1, $linestring2]);
+ $this->model->polygon = $polygon;
$this->model->save();
$this->assertStringStartsWith('insert', $this->queries[0]);
- $this->assertContains('insert into `test_models` (`polygon`) values (ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $this->queries[0]);
+ $this->assertStringContainsString('insert into `test_models` (`polygon`) values (ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $this->queries[0]);
// TODO: assert bindings in query
$this->assertTrue($this->model->exists);
- $this->model->polygon = new \Grimzy\LaravelMysqlSpatial\Types\Polygon([$linestring1, $linestring2]);
+ $polygon = new \Grimzy\LaravelMysqlSpatial\Types\Polygon([
+ new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point1, $point2, $point3, $point4, $point1]),
+ ]);
+
+ $this->model->polygon = $polygon;
$this->model->save();
+
$this->assertStringStartsWith('update', $this->queries[1]);
- $this->assertContains('update `test_models` set `polygon` = ST_GeomFromText(?, ?, \'axis-order=long-lat\') where `id` = ?', $this->queries[1]);
+ $this->assertStringContainsString('update `test_models` set `polygon` = ST_GeomFromText(?, ?, \'axis-order=long-lat\') where `id` = ?', $this->queries[1]);
// TODO: assert bindings in query
}
@@ -112,7 +121,7 @@ public function testInsertUpdateMultiPointHasCorrectSql()
$this->model->save();
$this->assertStringStartsWith('insert', $this->queries[0]);
- $this->assertContains('insert into `test_models` (`multipoint`) values (ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $this->queries[0]);
+ $this->assertStringContainsString('insert into `test_models` (`multipoint`) values (ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $this->queries[0]);
// TODO: assert bindings in query
$this->assertTrue($this->model->exists);
@@ -120,7 +129,7 @@ public function testInsertUpdateMultiPointHasCorrectSql()
$this->model->save();
$this->assertStringStartsWith('update', $this->queries[1]);
- $this->assertContains('update `test_models` set `multipoint` = ST_GeomFromText(?, ?, \'axis-order=long-lat\') where `id` = ?', $this->queries[1]);
+ $this->assertStringContainsString('update `test_models` set `multipoint` = ST_GeomFromText(?, ?, \'axis-order=long-lat\') where `id` = ?', $this->queries[1]);
// TODO: assert bindings in query
}
@@ -139,14 +148,14 @@ public function testInsertUpdateMultiLineStringHasCorrectSql()
$this->model->save();
$this->assertStringStartsWith('insert', $this->queries[0]);
- $this->assertContains('insert into `test_models` (`multilinestring`) values (ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $this->queries[0]);
+ $this->assertStringContainsString('insert into `test_models` (`multilinestring`) values (ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $this->queries[0]);
// TODO: assert bindings in query
$this->assertTrue($this->model->exists);
$this->model->multilinestring = new \Grimzy\LaravelMysqlSpatial\Types\MultiLineString([$linestring1, $linestring2]);
$this->model->save();
$this->assertStringStartsWith('update', $this->queries[1]);
- $this->assertContains('update `test_models` set `multilinestring` = ST_GeomFromText(?, ?, \'axis-order=long-lat\') where `id` = ?', $this->queries[1]);
+ $this->assertStringContainsString('update `test_models` set `multilinestring` = ST_GeomFromText(?, ?, \'axis-order=long-lat\') where `id` = ?', $this->queries[1]);
// TODO: assert bindings in query
}
@@ -154,19 +163,19 @@ public function testInsertUpdateMultiPolygonHasCorrectSql()
{
$point1 = new Point(1, 2);
$point2 = new Point(2, 3);
- $linestring1 = new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point1, $point2]);
$point3 = new Point(3, 2);
$point4 = new Point(2, 1);
- $linestring2 = new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point3, $point4]);
- $polygon1 = new \Grimzy\LaravelMysqlSpatial\Types\Polygon([$linestring1, $linestring2]);
+ $polygon1 = new \Grimzy\LaravelMysqlSpatial\Types\Polygon([
+ new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point1, $point2, $point3, $point1]),
+ ]);
$point5 = new Point(4, 5);
$point6 = new Point(5, 6);
- $linestring3 = new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point5, $point6]);
$point7 = new Point(6, 5);
$point8 = new Point(5, 4);
- $linestring4 = new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point7, $point8]);
- $polygon2 = new \Grimzy\LaravelMysqlSpatial\Types\Polygon([$linestring3, $linestring4]);
+ $polygon2 = new \Grimzy\LaravelMysqlSpatial\Types\Polygon([
+ new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point5, $point6, $point7, $point5]),
+ ]);
$this->assertFalse($this->model->exists);
@@ -174,14 +183,14 @@ public function testInsertUpdateMultiPolygonHasCorrectSql()
$this->model->save();
$this->assertStringStartsWith('insert', $this->queries[0]);
- $this->assertContains('insert into `test_models` (`multipolygon`) values (ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $this->queries[0]);
+ $this->assertStringContainsString('insert into `test_models` (`multipolygon`) values (ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $this->queries[0]);
// TODO: assert bindings in query
$this->assertTrue($this->model->exists);
$this->model->multipolygon = new \Grimzy\LaravelMysqlSpatial\Types\MultiPolygon([$polygon1, $polygon2]);
$this->model->save();
$this->assertStringStartsWith('update', $this->queries[1]);
- $this->assertContains('update `test_models` set `multipolygon` = ST_GeomFromText(?, ?, \'axis-order=long-lat\') where `id` = ?', $this->queries[1]);
+ $this->assertStringContainsString('update `test_models` set `multipolygon` = ST_GeomFromText(?, ?, \'axis-order=long-lat\') where `id` = ?', $this->queries[1]);
// TODO: assert bindings in query
}
@@ -198,14 +207,14 @@ public function testInsertUpdateGeometryCollectionHasCorrectSql()
$this->model->save();
$this->assertStringStartsWith('insert', $this->queries[0]);
- $this->assertContains('insert into `test_models` (`geometrycollection`) values (ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $this->queries[0]);
+ $this->assertStringContainsString('insert into `test_models` (`geometrycollection`) values (ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $this->queries[0]);
// TODO: assert bindings in query
$this->assertTrue($this->model->exists);
$this->model->geometrycollection = new \Grimzy\LaravelMysqlSpatial\Types\GeometryCollection([$point1, $linestring1]);
$this->model->save();
$this->assertStringStartsWith('update', $this->queries[1]);
- $this->assertContains('update `test_models` set `geometrycollection` = ST_GeomFromText(?, ?, \'axis-order=long-lat\') where `id` = ?', $this->queries[1]);
+ $this->assertStringContainsString('update `test_models` set `geometrycollection` = ST_GeomFromText(?, ?, \'axis-order=long-lat\') where `id` = ?', $this->queries[1]);
// TODO: assert bindings in query
}
@@ -219,10 +228,17 @@ public function testSettingRawAttributes()
public function testSpatialFieldsNotDefinedException()
{
- $model = new TestNoSpatialModel();
+ $model = new class extends Model
+ {
+ use SpatialTrait;
+ };
$this->assertException(
SpatialFieldsNotDefinedException::class,
- 'TestNoSpatialModel has to define $spatialFields'
+ 'TestNoSpatialModel'
+ );
+ $this->assertException(
+ SpatialFieldsNotDefinedException::class,
+ ' has to define $spatialFields'
);
$model->getSpatialFields();
}
@@ -303,7 +319,7 @@ public function testScopeDistanceValue()
$this->assertNotEmpty($bindings);
$this->assertEquals('*', $q->columns[0]);
$this->assertInstanceOf(\Illuminate\Database\Query\Expression::class, $q->columns[1]);
- $this->assertEquals('st_distance(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\')) as distance', $q->columns[1]->getValue());
+ $this->assertEquals('st_distance(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\')) as distance', $q->columns[1]->getValue(DB::connection()->getQueryGrammar()));
$this->assertEquals('POINT(2 1)', $bindings[0]);
}
@@ -319,7 +335,7 @@ public function testScopeDistanceValueWithSelect()
$this->assertNotEmpty($bindings);
$this->assertEquals('some_column', $q->columns[0]);
$this->assertInstanceOf(\Illuminate\Database\Query\Expression::class, $q->columns[1]);
- $this->assertEquals('st_distance(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\')) as distance', $q->columns[1]->getValue());
+ $this->assertEquals('st_distance(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\')) as distance', $q->columns[1]->getValue(DB::connection()->getQueryGrammar()));
$this->assertEquals('POINT(2 1)', $bindings[0]);
}
@@ -335,7 +351,7 @@ public function testScopeDistanceSphereValue()
$this->assertNotEmpty($bindings);
$this->assertEquals('*', $q->columns[0]);
$this->assertInstanceOf(\Illuminate\Database\Query\Expression::class, $q->columns[1]);
- $this->assertEquals('st_distance_sphere(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\')) as distance', $q->columns[1]->getValue());
+ $this->assertEquals('st_distance_sphere(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\')) as distance', $q->columns[1]->getValue(DB::connection()->getQueryGrammar()));
$this->assertEquals('POINT(2 1)', $bindings[0]);
}
@@ -351,7 +367,7 @@ public function testScopeDistanceSphereValueWithSelect()
$this->assertNotEmpty($bindings);
$this->assertEquals('some_column', $q->columns[0]);
$this->assertInstanceOf(\Illuminate\Database\Query\Expression::class, $q->columns[1]);
- $this->assertEquals('st_distance_sphere(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\')) as distance', $q->columns[1]->getValue());
+ $this->assertEquals('st_distance_sphere(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\')) as distance', $q->columns[1]->getValue(DB::connection()->getQueryGrammar()));
$this->assertEquals('POINT(2 1)', $bindings[0]);
}
@@ -379,7 +395,7 @@ public function testScopeComparison()
$this->assertNotEmpty($q->wheres);
$bindings = $q->getRawBindings()['where'];
$this->assertNotEmpty($bindings);
- $this->assertContains('st_within(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $q->wheres[0]['sql']);
+ $this->assertStringContainsString('st_within(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $q->wheres[0]['sql']);
$this->assertEquals('POLYGON((1 1,2 1),(2 1,2 2),(2 2,1 1))', $bindings[0]);
}
@@ -392,7 +408,7 @@ public function testScopeWithin()
$this->assertNotEmpty($q->wheres);
$bindings = $q->getRawBindings()['where'];
$this->assertNotEmpty($bindings);
- $this->assertContains('st_within(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $q->wheres[0]['sql']);
+ $this->assertStringContainsString('st_within(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $q->wheres[0]['sql']);
$this->assertEquals('POLYGON((1 1,2 1),(2 1,2 2),(2 2,1 1))', $bindings[0]);
}
@@ -405,7 +421,7 @@ public function testScopeCrosses()
$this->assertNotEmpty($q->wheres);
$bindings = $q->getRawBindings()['where'];
$this->assertNotEmpty($bindings);
- $this->assertContains('st_crosses(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $q->wheres[0]['sql']);
+ $this->assertStringContainsString('st_crosses(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $q->wheres[0]['sql']);
$this->assertEquals('POLYGON((1 1,2 1),(2 1,2 2),(2 2,1 1))', $bindings[0]);
}
@@ -418,7 +434,7 @@ public function testScopeContains()
$this->assertNotEmpty($q->wheres);
$bindings = $q->getRawBindings()['where'];
$this->assertNotEmpty($bindings);
- $this->assertContains('st_contains(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $q->wheres[0]['sql']);
+ $this->assertStringContainsString('st_contains(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $q->wheres[0]['sql']);
$this->assertEquals('POLYGON((1 1,2 1),(2 1,2 2),(2 2,1 1))', $bindings[0]);
}
@@ -431,7 +447,7 @@ public function testScopeDisjoint()
$this->assertNotEmpty($q->wheres);
$bindings = $q->getRawBindings()['where'];
$this->assertNotEmpty($bindings);
- $this->assertContains('st_disjoint(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $q->wheres[0]['sql']);
+ $this->assertStringContainsString('st_disjoint(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $q->wheres[0]['sql']);
$this->assertEquals('POLYGON((1 1,2 1),(2 1,2 2),(2 2,1 1))', $bindings[0]);
}
@@ -444,7 +460,7 @@ public function testScopeEquals()
$this->assertNotEmpty($q->wheres);
$bindings = $q->getRawBindings()['where'];
$this->assertNotEmpty($bindings);
- $this->assertContains('st_equals(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $q->wheres[0]['sql']);
+ $this->assertStringContainsString('st_equals(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $q->wheres[0]['sql']);
$this->assertEquals('POLYGON((1 1,2 1),(2 1,2 2),(2 2,1 1))', $bindings[0]);
}
@@ -457,7 +473,7 @@ public function testScopeIntersects()
$this->assertNotEmpty($q->wheres);
$bindings = $q->getRawBindings()['where'];
$this->assertNotEmpty($bindings);
- $this->assertContains('st_intersects(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $q->wheres[0]['sql']);
+ $this->assertStringContainsString('st_intersects(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $q->wheres[0]['sql']);
$this->assertEquals('POLYGON((1 1,2 1),(2 1,2 2),(2 2,1 1))', $bindings[0]);
}
@@ -470,7 +486,7 @@ public function testScopeOverlaps()
$this->assertNotEmpty($q->wheres);
$bindings = $q->getRawBindings()['where'];
$this->assertNotEmpty($bindings);
- $this->assertContains('st_overlaps(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $q->wheres[0]['sql']);
+ $this->assertStringContainsString('st_overlaps(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $q->wheres[0]['sql']);
$this->assertEquals('POLYGON((1 1,2 1),(2 1,2 2),(2 2,1 1))', $bindings[0]);
}
@@ -483,7 +499,7 @@ public function testScopeDoesTouch()
$this->assertNotEmpty($q->wheres);
$bindings = $q->getRawBindings()['where'];
$this->assertNotEmpty($bindings);
- $this->assertContains('st_touches(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $q->wheres[0]['sql']);
+ $this->assertStringContainsString('st_touches(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\'))', $q->wheres[0]['sql']);
$this->assertEquals('POLYGON((1 1,2 1),(2 1,2 2),(2 2,1 1))', $bindings[0]);
}
@@ -507,7 +523,7 @@ public function testScopeOrderByDistance()
$this->assertNotEmpty($q->orders);
$bindings = $q->getRawBindings()['order'];
$this->assertNotEmpty($bindings);
- $this->assertContains('st_distance(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\')) asc', $q->orders[0]['sql']);
+ $this->assertStringContainsString('st_distance(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\')) asc', $q->orders[0]['sql']);
$this->assertEquals('POINT(2 1)', $bindings[0]);
}
@@ -521,85 +537,7 @@ public function testScopeOrderByDistanceSphere()
$this->assertNotEmpty($q->orders);
$bindings = $q->getRawBindings()['order'];
$this->assertNotEmpty($bindings);
- $this->assertContains('st_distance_sphere(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\')) asc', $q->orders[0]['sql']);
+ $this->assertStringContainsString('st_distance_sphere(`point`, ST_GeomFromText(?, ?, \'axis-order=long-lat\')) asc', $q->orders[0]['sql']);
$this->assertEquals('POINT(2 1)', $bindings[0]);
}
}
-
-class TestModel extends Model
-{
- use \Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;
-
- protected $spatialFields = ['point']; // TODO: only required when fetching, not saving
-
- public $timestamps = false;
-
- public static $pdo;
-
- public static function resolveConnection($connection = null)
- {
- if (is_null(static::$pdo)) {
- static::$pdo = m::mock('TestPDO')->makePartial();
- }
-
- return new MysqlConnection(static::$pdo);
- }
-
- public function testrelatedmodels()
- {
- return $this->hasMany(TestRelatedModel::class);
- }
-
- public function testrelatedmodels2()
- {
- return $this->belongsToMany(TestRelatedModel::class);
- }
-}
-
-class TestRelatedModel extends TestModel
-{
- public function testmodel()
- {
- return $this->belongsTo(TestModel::class);
- }
-
- public function testmodels()
- {
- return $this->belongsToMany(TestModel::class);
- }
-}
-
-class TestNoSpatialModel extends Model
-{
- use \Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;
-}
-
-class TestPDO extends PDO
-{
- public $queries = [];
-
- public $counter = 1;
-
- public function prepare($statement, $driver_options = [])
- {
- $this->queries[] = $statement;
-
- $stmt = m::mock('PDOStatement');
- $stmt->shouldReceive('bindValue')->zeroOrMoreTimes();
- $stmt->shouldReceive('execute');
- $stmt->shouldReceive('fetchAll')->andReturn([['id' => 1, 'point' => 'POINT(1 2)']]);
- $stmt->shouldReceive('rowCount')->andReturn(1);
-
- return $stmt;
- }
-
- public function lastInsertId($name = null)
- {
- return $this->counter++;
- }
-
- public function resetQueries()
- {
- $this->queries = [];
- }
-}
diff --git a/tests/Integration/SridSpatialTest.php b/tests/Integration/SridSpatialTest.php
index f57c6cb2..69923ff8 100644
--- a/tests/Integration/SridSpatialTest.php
+++ b/tests/Integration/SridSpatialTest.php
@@ -1,5 +1,8 @@
location = new Point(1, 2);
$this->assertException(
- Illuminate\Database\QueryException::class,
- 'SQLSTATE[HY000]: General error: 3643 The SRID of the geometry '.
- 'does not match the SRID of the column \'location\'. The SRID '.
- 'of the geometry is 0, but the SRID of the column is 3857. '.
- 'Consider changing the SRID of the geometry or the SRID property '.
- 'of the column. (SQL: insert into `with_srid` (`location`) values '.
- '(ST_GeomFromText(POINT(2 1), 0, \'axis-order=long-lat\')))'
+ \Illuminate\Database\QueryException::class,
+ "SQLSTATE[HY000]: General error: 3643 The SRID of the geometry does not match the SRID of the column 'location'. The SRID of the geometry is 0, but the SRID of the column is 3857. Consider changing the SRID of the geometry or the SRID property of the column. (Connection: mysql, SQL: insert into `with_srid` (`location`) values (ST_GeomFromText(POINT(2 1), 0, 'axis-order=long-lat')))"
);
$geo->save();
}
diff --git a/tests/Unit/BaseTestCase.php b/tests/Unit/BaseTestCase.php
index 219f737d..23b7fb71 100644
--- a/tests/Unit/BaseTestCase.php
+++ b/tests/Unit/BaseTestCase.php
@@ -1,10 +1,13 @@
createMock(\PDO::class);
$factory = Mockery::mock(ConnectionFactory::class, [new Container()])->makePartial();
$factory->shouldAllowMockingProtectedMethods();
@@ -20,7 +23,7 @@ public function testMakeCallsCreateConnection()
public function testCreateConnectionDifferentDriver()
{
- $pdo = new PDOStub();
+ $pdo = $this->createMock(\PDO::class);
$factory = Mockery::mock(ConnectionFactory::class, [new Container()])->makePartial();
$factory->shouldAllowMockingProtectedMethods();
diff --git a/tests/Unit/Eloquent/BuilderTest.php b/tests/Unit/Eloquent/BuilderTest.php
index d9a12a55..ed4a5153 100644
--- a/tests/Unit/Eloquent/BuilderTest.php
+++ b/tests/Unit/Eloquent/BuilderTest.php
@@ -1,12 +1,12 @@
makePartial();
$grammar = Mockery::mock(MySqlGrammar::class)->makePartial();
@@ -32,7 +33,14 @@ protected function setUp()
->andReturn($this->queryBuilder);
$this->builder = new Builder($this->queryBuilder);
- $this->builder->setModel(new TestBuilderModel());
+ $this->builder->setModel(new class extends Model
+ {
+ use SpatialTrait;
+
+ public $timestamps = false;
+
+ protected $spatialFields = ['point', 'linestring', 'polygon'];
+ });
}
public function testUpdatePoint()
@@ -129,11 +137,3 @@ public function testUpdatePolygonWithSrid()
$this->assertSame(1, $result);
}
}
-
-class TestBuilderModel extends Model
-{
- use SpatialTrait;
-
- public $timestamps = false;
- protected $spatialFields = ['point', 'linestring', 'polygon'];
-}
diff --git a/tests/Unit/MysqlConnectionTest.php b/tests/Unit/MysqlConnectionTest.php
index cae970a2..3adc09e4 100644
--- a/tests/Unit/MysqlConnectionTest.php
+++ b/tests/Unit/MysqlConnectionTest.php
@@ -1,18 +1,19 @@
'mysql', 'prefix' => 'prefix', 'database' => 'database', 'name' => 'foo'];
- $this->mysqlConnection = new MysqlConnection(new PDOStub(), 'database', 'prefix', $mysqlConfig);
+ $this->mysqlConnection = new MysqlConnection($this->createMock(\PDO::class), 'database', 'prefix', $mysqlConfig);
}
public function testGetSchemaBuilder()
diff --git a/tests/Unit/Schema/BlueprintTest.php b/tests/Unit/Schema/BlueprintTest.php
index c22c9518..c176b0f5 100644
--- a/tests/Unit/Schema/BlueprintTest.php
+++ b/tests/Unit/Schema/BlueprintTest.php
@@ -1,20 +1,17 @@
assertEquals($expectedSql, $dropStatements[4]);
}
- /**
- * @return Connection
- */
- protected function getConnection()
+ protected function getConnection($connection = null, $table = null)
{
return Mockery::mock(MysqlConnection::class);
}
diff --git a/tests/Unit/Stubs/PDOStub.php b/tests/Unit/Stubs/PDOStub.php
deleted file mode 100644
index 99d2d806..00000000
--- a/tests/Unit/Stubs/PDOStub.php
+++ /dev/null
@@ -1,10 +0,0 @@
-getGeometryCollection();
- $this->assertInternalType('array', $geometryCollection->toArray());
+ $this->assertIsArray($geometryCollection->toArray());
}
public function testIteratorAggregate()
@@ -131,7 +135,7 @@ public function testInvalidGeoJsonException()
{
$this->assertException(
\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class,
- sprintf('Expected %s, got %s', GeoJson\Feature\FeatureCollection::class, GeoJson\Geometry\Point::class)
+ sprintf('Expected %s, got %s', \GeoJson\Feature\FeatureCollection::class, \GeoJson\Geometry\Point::class)
);
GeometryCollection::fromJson('{"type":"Point","coordinates":[3.4,1.2]}');
}
diff --git a/tests/Unit/Types/GeometryTest.php b/tests/Unit/Types/GeometryTest.php
index e51022d5..67913c9c 100644
--- a/tests/Unit/Types/GeometryTest.php
+++ b/tests/Unit/Types/GeometryTest.php
@@ -1,6 +1,9 @@
points = [new Point(0, 0), new Point(1, 1), new Point(2, 2)];
}
@@ -47,7 +50,7 @@ public function testInvalidGeoJsonException()
{
$this->assertException(
\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class,
- sprintf('Expected %s, got %s', \GeoJson\Geometry\LineString::class, GeoJson\Geometry\Point::class)
+ sprintf('Expected %s, got %s', \GeoJson\Geometry\LineString::class, \GeoJson\Geometry\Point::class)
);
LineString::fromJson('{"type":"Point","coordinates":[3.4,1.2]}');
}
diff --git a/tests/Unit/Types/MultiLineStringTest.php b/tests/Unit/Types/MultiLineStringTest.php
index 16477feb..e5112a79 100644
--- a/tests/Unit/Types/MultiLineStringTest.php
+++ b/tests/Unit/Types/MultiLineStringTest.php
@@ -1,8 +1,12 @@
assertException(
\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class,
- sprintf('Expected %s, got %s', GeoJson\Geometry\MultiLineString::class, GeoJson\Geometry\Point::class)
+ sprintf('Expected %s, got %s', \GeoJson\Geometry\MultiLineString::class, \GeoJson\Geometry\Point::class)
);
MultiLineString::fromJson('{"type":"Point","coordinates":[3.4,1.2]}');
}
diff --git a/tests/Unit/Types/MultiPointTest.php b/tests/Unit/Types/MultiPointTest.php
index a8a94f41..aacaf794 100644
--- a/tests/Unit/Types/MultiPointTest.php
+++ b/tests/Unit/Types/MultiPointTest.php
@@ -1,7 +1,11 @@
assertException(
\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class,
- sprintf('Expected %s, got %s', GeoJson\Geometry\MultiPoint::class, GeoJson\Geometry\Point::class)
+ sprintf('Expected %s, got %s', \GeoJson\Geometry\MultiPoint::class, \GeoJson\Geometry\Point::class)
);
MultiPoint::fromJson('{"type":"Point","coordinates":[3.4,1.2]}');
}
@@ -102,52 +106,4 @@ public function testArrayAccess()
);
$multipoint[] = 1;
}
-
- public function testDeprecatedPrependPoint()
- {
- $point1 = new Point(1, 1);
- $point2 = new Point(2, 2);
- $multipoint = new MultiPoint([$point1, $point2]);
-
- $point0 = new Point(0, 0);
- $multipoint->prependPoint($point0);
-
- $this->assertEquals($point0, $multipoint[0]);
- $this->assertEquals($point1, $multipoint[1]);
- $this->assertEquals($point2, $multipoint[2]);
- }
-
- public function testDeprecatedAppendPoint()
- {
- $point0 = new Point(0, 0);
- $point1 = new Point(1, 1);
- $multipoint = new MultiPoint([$point0, $point1]);
-
- $point2 = new Point(2, 2);
- $multipoint->appendPoint($point2);
-
- $this->assertEquals($point0, $multipoint[0]);
- $this->assertEquals($point1, $multipoint[1]);
- $this->assertEquals($point2, $multipoint[2]);
- }
-
- public function testDeprecatedInsertPoint()
- {
- $point1 = new Point(1, 1);
- $point3 = new Point(3, 3);
- $multipoint = new MultiPoint([$point1, $point3]);
-
- $point2 = new Point(2, 2);
- $multipoint->insertPoint(1, $point2);
-
- $this->assertEquals($point1, $multipoint[0]);
- $this->assertEquals($point2, $multipoint[1]);
- $this->assertEquals($point3, $multipoint[2]);
-
- $this->assertException(
- InvalidArgumentException::class,
- '$index is greater than the size of the array'
- );
- $multipoint->insertPoint(100, new Point(100, 100));
- }
}
diff --git a/tests/Unit/Types/MultiPolygonTest.php b/tests/Unit/Types/MultiPolygonTest.php
index 3e49d32d..9d29c8e3 100644
--- a/tests/Unit/Types/MultiPolygonTest.php
+++ b/tests/Unit/Types/MultiPolygonTest.php
@@ -1,9 +1,13 @@
assertException(
\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class,
- sprintf('Expected %s, got %s', GeoJson\Geometry\MultiPolygon::class, GeoJson\Geometry\Point::class)
+ sprintf('Expected %s, got %s', \GeoJson\Geometry\MultiPolygon::class, \GeoJson\Geometry\Point::class)
);
MultiPolygon::fromJson('{"type":"Point","coordinates":[3.4,1.2]}');
}
diff --git a/tests/Unit/Types/PointTest.php b/tests/Unit/Types/PointTest.php
index 518a8a56..07e1c2c9 100644
--- a/tests/Unit/Types/PointTest.php
+++ b/tests/Unit/Types/PointTest.php
@@ -1,5 +1,8 @@