From 991d3691044f3ed160367d08d443385cbd4ded43 Mon Sep 17 00:00:00 2001 From: Linus Metzler Date: Fri, 17 Feb 2023 11:40:03 +0100 Subject: [PATCH 01/10] configure GitHub actions etc. --- .editorconfig | 15 +++++ .gitattributes | 19 ++++++ .github/ISSUE_TEMPLATE/bug.yml | 66 +++++++++++++++++++ .github/ISSUE_TEMPLATE/config.yml | 11 ++++ .github/dependabot.yml | 12 ++++ .github/workflows/dependabot-auto-merge.yml | 32 +++++++++ .../workflows/fix-php-code-style-issues.yml | 24 +++++++ .github/workflows/phpstan.yml | 26 ++++++++ .github/workflows/run-tests.yml | 55 ++++++++++++++++ .gitignore | 13 +++- .travis.yml | 33 ---------- composer.json | 55 +++++++++++----- phpstan-baseline.neon | 0 phpstan.neon.dist | 12 ++++ phpunit.xml.dist | 42 +++++++----- tests/Integration/IntegrationBaseTestCase.php | 4 +- tests/TestCase.php | 36 ++++++++++ tests/Unit/BaseTestCase.php | 2 +- tests/Unit/Eloquent/BuilderTest.php | 2 +- tests/Unit/Eloquent/SpatialTraitTest.php | 4 +- tests/Unit/MysqlConnectionTest.php | 2 +- tests/Unit/Schema/BlueprintTest.php | 2 +- tests/Unit/Types/LineStringTest.php | 2 +- tests/Unit/Types/PolygonTest.php | 2 +- 24 files changed, 391 insertions(+), 80 deletions(-) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .github/ISSUE_TEMPLATE/bug.yml create mode 100644 .github/ISSUE_TEMPLATE/config.yml create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/dependabot-auto-merge.yml create mode 100644 .github/workflows/fix-php-code-style-issues.yml create mode 100644 .github/workflows/phpstan.yml create mode 100644 .github/workflows/run-tests.yml delete mode 100644 .travis.yml create mode 100644 phpstan-baseline.neon create mode 100644 phpstan.neon.dist create mode 100644 tests/TestCase.php 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..39a77e7b --- /dev/null +++ b/.github/workflows/fix-php-code-style-issues.yml @@ -0,0 +1,24 @@ +name: Fix PHP code style issues + +on: + push: + paths: + - '**.php' + +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..9d41c0cf --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,26 @@ +name: PHPStan + +on: + push: + paths: + - '**.php' + - 'phpstan.neon.dist' + +jobs: + phpstan: + name: phpstan + runs-on: ubuntu-latest + steps: + - 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..e3a47550 --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -0,0 +1,55 @@ +name: run-tests + +on: + push: + branches: [master] + pull_request: + branches: [master] + +jobs: + test: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: true + matrix: + os: [ubuntu-latest, windows-latest] + php: [8.2, 8.1] + laravel: [9.*] + stability: [prefer-lowest, prefer-stable] + include: + - laravel: 9.* + testbench: 7.* + carbon: ^2.63 + + name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} + + steps: + - uses: mirromutth/mysql-action@v1.1 + with: + mysql version: "8.0" + + - 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 + coverage: none + + - 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..f3bf0d33 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,12 @@ -.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 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/composer.json b/composer.json index 4451cb50..a0e69d54 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,55 @@ } ], "require": { - "php": ">=7.3", + "php": "^8.1", "ext-pdo": "*", "ext-json": "*", - "illuminate/database": "^8.0", + "illuminate/contracts": "^9.0", + "illuminate/database": "^9.0", "geo-io/wkb-parser": "^1.0", "jmikola/geojson": "^1.0" }, "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.0", + "laravel/laravel": "^9.0", + "laravel/browser-kit-testing": "^6.0", + "mockery/mockery": "^1.3", + "nunomaduro/collision": "^6.0", + "nunomaduro/larastan": "^2.0.1", + "orchestra/testbench": "^7.0", + "phpstan/extension-installer": "^1.1", + "phpstan/phpstan-deprecation-rules": "^1.0", + "phpstan/phpstan-phpunit": "^1.0", + "phpunit/phpunit": "^9.5" }, "autoload": { "psr-4": { - "Grimzy\\LaravelMysqlSpatial\\": "src/" + "Grimzy\\LaravelMysqlSpatial\\": "src" } }, - "autoload-dev" : { - "classmap" : [ + "autoload-dev": { + "classmap": [ "tests/Unit", "tests/Integration" - ] + ], + "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": "dev", + "prefer-stable": true } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 00000000..e69de29b diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 00000000..9914a5e1 --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,12 @@ +includes: + - phpstan-baseline.neon + +parameters: + level: 4 + paths: + - src + tmpDir: build/phpstan + checkOctaneCompatibility: true + checkModelProperties: true + checkMissingIterableValueType: false + diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 1456a6ac..114eeaa7 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,13 +1,18 @@ - + ./tests/Unit @@ -21,13 +26,18 @@ file="vendor/mockery/mockery/library/Mockery/Adapter/Phpunit/TestListener.php"> - - + + ./src - - + + + + + + + - + @@ -36,10 +46,10 @@ - + - \ No newline at end of file + diff --git a/tests/Integration/IntegrationBaseTestCase.php b/tests/Integration/IntegrationBaseTestCase.php index 04634734..b3e193f3 100644 --- a/tests/Integration/IntegrationBaseTestCase.php +++ b/tests/Integration/IntegrationBaseTestCase.php @@ -44,7 +44,7 @@ public function createApplication() * * @return void */ - public function setUp() + public function setUp():void { parent::setUp(); @@ -59,7 +59,7 @@ public function setUp() //}); } - public function tearDown() + public function tearDown():void { $this->onMigrations(function ($migrationClass) { (new $migrationClass())->down(); diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 00000000..f1ae9897 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,36 @@ + 'Grimzy\\LaravelMysqlSpatial\\Database\\Factories\\'.class_basename($modelName).'Factory' + ); + } + + protected function getPackageProviders($app) + { + return [ + LaravelMysqlSpatialServiceProvider::class, + ]; + } + + public function getEnvironmentSetUp($app) + { + config()->set('database.default', 'testing'); + + /* + $migration = include __DIR__.'/../database/migrations/create_laravel-mysql-spatial_table.php.stub'; + $migration->up(); + */ + } +} diff --git a/tests/Unit/BaseTestCase.php b/tests/Unit/BaseTestCase.php index 219f737d..0322a5c9 100644 --- a/tests/Unit/BaseTestCase.php +++ b/tests/Unit/BaseTestCase.php @@ -4,7 +4,7 @@ abstract class BaseTestCase extends TestCase { - public function tearDown() + public function tearDown():void { Mockery::close(); } diff --git a/tests/Unit/Eloquent/BuilderTest.php b/tests/Unit/Eloquent/BuilderTest.php index d9a12a55..e9aa45e7 100644 --- a/tests/Unit/Eloquent/BuilderTest.php +++ b/tests/Unit/Eloquent/BuilderTest.php @@ -20,7 +20,7 @@ class BuilderTest extends BaseTestCase protected $builder; protected $queryBuilder; - protected function setUp() + protected function setUp():void { $connection = Mockery::mock(MysqlConnection::class)->makePartial(); $grammar = Mockery::mock(MySqlGrammar::class)->makePartial(); diff --git a/tests/Unit/Eloquent/SpatialTraitTest.php b/tests/Unit/Eloquent/SpatialTraitTest.php index 8ece7d25..1c161892 100644 --- a/tests/Unit/Eloquent/SpatialTraitTest.php +++ b/tests/Unit/Eloquent/SpatialTraitTest.php @@ -21,13 +21,13 @@ class SpatialTraitTest extends BaseTestCase */ protected $queries; - public function setUp() + public function setUp():void { $this->model = new TestModel(); $this->queries = &$this->model->getConnection()->getPdo()->queries; } - public function tearDown() + public function tearDown():void { $this->model->getConnection()->getPdo()->resetQueries(); } diff --git a/tests/Unit/MysqlConnectionTest.php b/tests/Unit/MysqlConnectionTest.php index cae970a2..70dd5e9c 100644 --- a/tests/Unit/MysqlConnectionTest.php +++ b/tests/Unit/MysqlConnectionTest.php @@ -9,7 +9,7 @@ class MysqlConnectionTest extends TestCase { private $mysqlConnection; - protected function setUp() + protected function setUp():void { $mysqlConfig = ['driver' => 'mysql', 'prefix' => 'prefix', 'database' => 'database', 'name' => 'foo']; $this->mysqlConnection = new MysqlConnection(new PDOStub(), 'database', 'prefix', $mysqlConfig); diff --git a/tests/Unit/Schema/BlueprintTest.php b/tests/Unit/Schema/BlueprintTest.php index c22c9518..8e78962b 100644 --- a/tests/Unit/Schema/BlueprintTest.php +++ b/tests/Unit/Schema/BlueprintTest.php @@ -14,7 +14,7 @@ class BlueprintTest extends BaseTestCase */ protected $blueprint; - public function setUp() + public function setUp():void { parent::setUp(); diff --git a/tests/Unit/Types/LineStringTest.php b/tests/Unit/Types/LineStringTest.php index ce5713d2..ba1ad9e8 100644 --- a/tests/Unit/Types/LineStringTest.php +++ b/tests/Unit/Types/LineStringTest.php @@ -7,7 +7,7 @@ class LineStringTest extends BaseTestCase { private $points; - protected function setUp() + protected function setUp():void { $this->points = [new Point(0, 0), new Point(1, 1), new Point(2, 2)]; } diff --git a/tests/Unit/Types/PolygonTest.php b/tests/Unit/Types/PolygonTest.php index aaab437b..f9326496 100644 --- a/tests/Unit/Types/PolygonTest.php +++ b/tests/Unit/Types/PolygonTest.php @@ -8,7 +8,7 @@ class PolygonTest extends BaseTestCase { private $polygon; - protected function setUp() + protected function setUp():void { $collection = new LineString( [ From a0c6526587188968189e5fa2241bfe06ac801c53 Mon Sep 17 00:00:00 2001 From: limenet Date: Fri, 17 Feb 2023 10:40:34 +0000 Subject: [PATCH 02/10] Fix styling --- src/Connectors/ConnectionFactory.php | 10 ++-- src/Eloquent/SpatialTrait.php | 13 +++-- src/Schema/Blueprint.php | 48 ++++++++----------- src/Schema/Builder.php | 5 +- src/Schema/Grammars/MySqlGrammar.php | 16 +------ src/SpatialServiceProvider.php | 16 +++---- src/Types/GeometryCollection.php | 12 ++--- src/Types/LineString.php | 2 +- src/Types/MultiLineString.php | 2 +- src/Types/MultiPoint.php | 2 +- src/Types/MultiPolygon.php | 5 +- src/Types/Point.php | 5 +- src/Types/PointCollection.php | 7 ++- src/Types/Polygon.php | 2 +- tests/Integration/IntegrationBaseTestCase.php | 11 ++--- tests/TestCase.php | 2 +- tests/Unit/BaseTestCase.php | 2 +- tests/Unit/Eloquent/BuilderTest.php | 4 +- tests/Unit/Eloquent/SpatialTraitTest.php | 4 +- tests/Unit/MysqlConnectionTest.php | 2 +- tests/Unit/Schema/BlueprintTest.php | 2 +- tests/Unit/Types/LineStringTest.php | 2 +- tests/Unit/Types/PolygonTest.php | 2 +- 23 files changed, 72 insertions(+), 104 deletions(-) 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/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/Schema/Blueprint.php b/src/Schema/Blueprint.php index 0a333f06..cd09b047 100644 --- a/src/Schema/Blueprint.php +++ b/src/Schema/Blueprint.php @@ -9,9 +9,8 @@ class Blueprint extends IlluminateBlueprint /** * Add a geometry column on the table. * - * @param string $column - * @param null|int $srid - * + * @param string $column + * @param null|int $srid * @return \Illuminate\Support\Fluent */ public function geometry($column, $srid = null) @@ -22,9 +21,8 @@ public function geometry($column, $srid = null) /** * Add a point column on the table. * - * @param string $column - * @param null|int $srid - * + * @param string $column + * @param null|int $srid * @return \Illuminate\Support\Fluent */ public function point($column, $srid = null) @@ -35,9 +33,8 @@ public function point($column, $srid = null) /** * Add a linestring column on the table. * - * @param string $column - * @param null|int $srid - * + * @param string $column + * @param null|int $srid * @return \Illuminate\Support\Fluent */ public function lineString($column, $srid = null) @@ -48,9 +45,8 @@ public function lineString($column, $srid = null) /** * Add a polygon column on the table. * - * @param string $column - * @param null|int $srid - * + * @param string $column + * @param null|int $srid * @return \Illuminate\Support\Fluent */ public function polygon($column, $srid = null) @@ -61,9 +57,8 @@ public function polygon($column, $srid = null) /** * Add a multipoint column on the table. * - * @param string $column - * @param null|int $srid - * + * @param string $column + * @param null|int $srid * @return \Illuminate\Support\Fluent */ public function multiPoint($column, $srid = null) @@ -74,9 +69,8 @@ public function multiPoint($column, $srid = null) /** * Add a multilinestring column on the table. * - * @param string $column - * @param null|int $srid - * + * @param string $column + * @param null|int $srid * @return \Illuminate\Support\Fluent */ public function multiLineString($column, $srid = null) @@ -87,9 +81,8 @@ public function multiLineString($column, $srid = null) /** * Add a multipolygon column on the table. * - * @param string $column - * @param null|int $srid - * + * @param string $column + * @param null|int $srid * @return \Illuminate\Support\Fluent */ public function multiPolygon($column, $srid = null) @@ -100,9 +93,8 @@ public function multiPolygon($column, $srid = null) /** * Add a geometrycollection column on the table. * - * @param string $column - * @param null|int $srid - * + * @param string $column + * @param null|int $srid * @return \Illuminate\Support\Fluent */ public function geometryCollection($column, $srid = null) @@ -113,9 +105,8 @@ public function geometryCollection($column, $srid = null) /** * Specify a spatial index for the table. * - * @param string|array $columns - * @param string $name - * + * @param string|array $columns + * @param string $name * @return \Illuminate\Support\Fluent */ public function spatialIndex($columns, $name = null) @@ -126,8 +117,7 @@ public function spatialIndex($columns, $name = null) /** * Indicate that the given index should be dropped. * - * @param string|array $index - * + * @param string|array $index * @return \Illuminate\Support\Fluent */ public function dropSpatialIndex($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..5ead6db6 100644 --- a/src/Schema/Grammars/MySqlGrammar.php +++ b/src/Schema/Grammars/MySqlGrammar.php @@ -13,7 +13,7 @@ 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; } } @@ -21,7 +21,6 @@ public function __construct() /** * Adds a statement to add a geometry column. * - * @param Fluent $column * * @return string */ @@ -33,7 +32,6 @@ public function typeGeometry(Fluent $column) /** * Adds a statement to add a point column. * - * @param Fluent $column * * @return string */ @@ -45,7 +43,6 @@ public function typePoint(Fluent $column) /** * Adds a statement to add a linestring column. * - * @param Fluent $column * * @return string */ @@ -57,7 +54,6 @@ public function typeLinestring(Fluent $column) /** * Adds a statement to add a polygon column. * - * @param Fluent $column * * @return string */ @@ -69,7 +65,6 @@ public function typePolygon(Fluent $column) /** * Adds a statement to add a multipoint column. * - * @param Fluent $column * * @return string */ @@ -81,7 +76,6 @@ public function typeMultipoint(Fluent $column) /** * Adds a statement to add a multilinestring column. * - * @param Fluent $column * * @return string */ @@ -93,7 +87,6 @@ public function typeMultilinestring(Fluent $column) /** * Adds a statement to add a multipolygon column. * - * @param Fluent $column * * @return string */ @@ -105,7 +98,6 @@ public function typeMultipolygon(Fluent $column) /** * Adds a statement to add a geometrycollection column. * - * @param Fluent $column * * @return string */ @@ -117,8 +109,6 @@ public function typeGeometrycollection(Fluent $column) /** * Compile a spatial index key command. * - * @param Blueprint $blueprint - * @param Fluent $command * * @return string */ @@ -130,14 +120,12 @@ public function compileSpatial(Blueprint $blueprint, Fluent $command) /** * 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) { - 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; } } diff --git a/src/SpatialServiceProvider.php b/src/SpatialServiceProvider.php index 3b859f8e..b2008b5a 100644 --- a/src/SpatialServiceProvider.php +++ b/src/SpatialServiceProvider.php @@ -44,18 +44,18 @@ public function register() 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/GeometryCollection.php b/src/Types/GeometryCollection.php index 35f093f7..8e701524 100755 --- a/src/Types/GeometryCollection.php +++ b/src/Types/GeometryCollection.php @@ -36,8 +36,8 @@ class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAcc protected $items = []; /** - * @param GeometryInterface[] $geometries - * @param int $srid + * @param GeometryInterface[] $geometries + * @param int $srid * * @throws InvalidArgumentException */ @@ -129,7 +129,7 @@ public static function fromJson($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)); } @@ -158,8 +158,6 @@ public function jsonSerialize() /** * Checks whether the items are valid to create this collection. - * - * @param array $items */ protected function validateItems(array $items) { @@ -173,7 +171,6 @@ protected function validateItems(array $items) /** * Checks whether the array has enough items to generate a valid WKT. * - * @param array $items * * @see $minimumCollectionItems */ @@ -194,13 +191,12 @@ protected function validateItemCount(array $items) /** * Checks the type of the items in the array. * - * @param $item * * @see $collectionItemType */ protected function validateItemType($item) { - 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/LineString.php b/src/Types/LineString.php index 1cc4a410..d05ac4e8 100644 --- a/src/Types/LineString.php +++ b/src/Types/LineString.php @@ -48,7 +48,7 @@ public static function fromJson($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)); } diff --git a/src/Types/MultiLineString.php b/src/Types/MultiLineString.php index 62c4d576..f3401fbd 100644 --- a/src/Types/MultiLineString.php +++ b/src/Types/MultiLineString.php @@ -62,7 +62,7 @@ public static function fromJson($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)); } diff --git a/src/Types/MultiPoint.php b/src/Types/MultiPoint.php index 752967eb..171cd896 100644 --- a/src/Types/MultiPoint.php +++ b/src/Types/MultiPoint.php @@ -52,7 +52,7 @@ public static function fromJson($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)); } diff --git a/src/Types/MultiPolygon.php b/src/Types/MultiPolygon.php index cdea3a9c..ac2e88fe 100644 --- a/src/Types/MultiPolygon.php +++ b/src/Types/MultiPolygon.php @@ -67,7 +67,6 @@ public function getPolygons() * "((-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 */ @@ -78,7 +77,7 @@ protected static function assembleParts(array $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 { @@ -102,7 +101,7 @@ public static function fromJson($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)); } diff --git a/src/Types/Point.php b/src/Types/Point.php index d424ec5e..1a82f2fb 100644 --- a/src/Types/Point.php +++ b/src/Types/Point.php @@ -47,7 +47,7 @@ public function toPair() public static function fromPair($pair, $srid = 0) { - 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); } @@ -69,7 +69,6 @@ public function __toString() /** * @param $geoJson \GeoJson\Feature\Feature|string - * * @return \Grimzy\LaravelMysqlSpatial\Types\Point */ public static function fromJson($geoJson) @@ -78,7 +77,7 @@ public static function fromJson($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)); } diff --git a/src/Types/PointCollection.php b/src/Types/PointCollection.php index 30d1b8de..b3bde354 100755 --- a/src/Types/PointCollection.php +++ b/src/Types/PointCollection.php @@ -37,7 +37,7 @@ public function getPoints() } /** - * @param \Grimzy\LaravelMysqlSpatial\Types\Point $point + * @param \Grimzy\LaravelMysqlSpatial\Types\Point $point * * @deprecated 2.1.0 Use array_unshift($multipoint, $point); instead * @see array_unshift @@ -49,7 +49,7 @@ public function prependPoint(Point $point) } /** - * @param \Grimzy\LaravelMysqlSpatial\Types\Point $point + * @param \Grimzy\LaravelMysqlSpatial\Types\Point $point * * @deprecated 2.1.0 Use $multipoint[] = $point; instead * @see ArrayAccess @@ -60,8 +60,7 @@ public function appendPoint(Point $point) } /** - * @param $index - * @param \Grimzy\LaravelMysqlSpatial\Types\Point $point + * @param \Grimzy\LaravelMysqlSpatial\Types\Point $point * * @deprecated 2.1.0 Use array_splice($multipoint, $index, 0, [$point]); instead * @see array_splice diff --git a/src/Types/Polygon.php b/src/Types/Polygon.php index 9c10cecc..7dd0c4ab 100644 --- a/src/Types/Polygon.php +++ b/src/Types/Polygon.php @@ -19,7 +19,7 @@ public static function fromJson($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)); } diff --git a/tests/Integration/IntegrationBaseTestCase.php b/tests/Integration/IntegrationBaseTestCase.php index b3e193f3..108fa487 100644 --- a/tests/Integration/IntegrationBaseTestCase.php +++ b/tests/Integration/IntegrationBaseTestCase.php @@ -7,6 +7,7 @@ abstract class IntegrationBaseTestCase extends BaseTestCase { protected $after_fix = false; + protected $migrations = []; /** @@ -41,10 +42,8 @@ public function createApplication() /** * Setup DB before each test. - * - * @return void */ - public function setUp():void + public function setUp(): void { parent::setUp(); @@ -59,7 +58,7 @@ public function setUp():void //}); } - public function tearDown():void + public function tearDown(): void { $this->onMigrations(function ($migrationClass) { (new $migrationClass())->down(); @@ -90,7 +89,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 +97,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/TestCase.php b/tests/TestCase.php index f1ae9897..2631978e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,9 +2,9 @@ namespace Grimzy\LaravelMysqlSpatial\Tests; +use Grimzy\LaravelMysqlSpatial\LaravelMysqlSpatialServiceProvider; use Illuminate\Database\Eloquent\Factories\Factory; use Orchestra\Testbench\TestCase as Orchestra; -use Grimzy\LaravelMysqlSpatial\LaravelMysqlSpatialServiceProvider; class TestCase extends Orchestra { diff --git a/tests/Unit/BaseTestCase.php b/tests/Unit/BaseTestCase.php index 0322a5c9..215690ef 100644 --- a/tests/Unit/BaseTestCase.php +++ b/tests/Unit/BaseTestCase.php @@ -4,7 +4,7 @@ abstract class BaseTestCase extends TestCase { - public function tearDown():void + public function tearDown(): void { Mockery::close(); } diff --git a/tests/Unit/Eloquent/BuilderTest.php b/tests/Unit/Eloquent/BuilderTest.php index e9aa45e7..26dbac63 100644 --- a/tests/Unit/Eloquent/BuilderTest.php +++ b/tests/Unit/Eloquent/BuilderTest.php @@ -18,9 +18,10 @@ class BuilderTest extends BaseTestCase { protected $builder; + protected $queryBuilder; - protected function setUp():void + protected function setUp(): void { $connection = Mockery::mock(MysqlConnection::class)->makePartial(); $grammar = Mockery::mock(MySqlGrammar::class)->makePartial(); @@ -135,5 +136,6 @@ class TestBuilderModel extends Model use SpatialTrait; public $timestamps = false; + protected $spatialFields = ['point', 'linestring', 'polygon']; } diff --git a/tests/Unit/Eloquent/SpatialTraitTest.php b/tests/Unit/Eloquent/SpatialTraitTest.php index 1c161892..dad13c99 100644 --- a/tests/Unit/Eloquent/SpatialTraitTest.php +++ b/tests/Unit/Eloquent/SpatialTraitTest.php @@ -21,13 +21,13 @@ class SpatialTraitTest extends BaseTestCase */ protected $queries; - public function setUp():void + public function setUp(): void { $this->model = new TestModel(); $this->queries = &$this->model->getConnection()->getPdo()->queries; } - public function tearDown():void + public function tearDown(): void { $this->model->getConnection()->getPdo()->resetQueries(); } diff --git a/tests/Unit/MysqlConnectionTest.php b/tests/Unit/MysqlConnectionTest.php index 70dd5e9c..c80b14fe 100644 --- a/tests/Unit/MysqlConnectionTest.php +++ b/tests/Unit/MysqlConnectionTest.php @@ -9,7 +9,7 @@ class MysqlConnectionTest extends TestCase { private $mysqlConnection; - protected function setUp():void + protected function setUp(): void { $mysqlConfig = ['driver' => 'mysql', 'prefix' => 'prefix', 'database' => 'database', 'name' => 'foo']; $this->mysqlConnection = new MysqlConnection(new PDOStub(), 'database', 'prefix', $mysqlConfig); diff --git a/tests/Unit/Schema/BlueprintTest.php b/tests/Unit/Schema/BlueprintTest.php index 8e78962b..457adab1 100644 --- a/tests/Unit/Schema/BlueprintTest.php +++ b/tests/Unit/Schema/BlueprintTest.php @@ -14,7 +14,7 @@ class BlueprintTest extends BaseTestCase */ protected $blueprint; - public function setUp():void + public function setUp(): void { parent::setUp(); diff --git a/tests/Unit/Types/LineStringTest.php b/tests/Unit/Types/LineStringTest.php index ba1ad9e8..0262dea0 100644 --- a/tests/Unit/Types/LineStringTest.php +++ b/tests/Unit/Types/LineStringTest.php @@ -7,7 +7,7 @@ class LineStringTest extends BaseTestCase { private $points; - protected function setUp():void + protected function setUp(): void { $this->points = [new Point(0, 0), new Point(1, 1), new Point(2, 2)]; } diff --git a/tests/Unit/Types/PolygonTest.php b/tests/Unit/Types/PolygonTest.php index f9326496..b572f121 100644 --- a/tests/Unit/Types/PolygonTest.php +++ b/tests/Unit/Types/PolygonTest.php @@ -8,7 +8,7 @@ class PolygonTest extends BaseTestCase { private $polygon; - protected function setUp():void + protected function setUp(): void { $collection = new LineString( [ From c699a7a4d2a5e4a63671ce7152a82b1076f2c278 Mon Sep 17 00:00:00 2001 From: Linus Metzler Date: Fri, 17 Feb 2023 11:42:10 +0100 Subject: [PATCH 03/10] configure mysql --- .github/workflows/run-tests.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index e3a47550..e9743902 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -27,6 +27,8 @@ jobs: - uses: mirromutth/mysql-action@v1.1 with: mysql version: "8.0" + mysql user: "user" + mysql password: "password" - name: Checkout code uses: actions/checkout@v3 From 266a036a406363aaf2c811e0980ac6f5af14ec4b Mon Sep 17 00:00:00 2001 From: Linus Metzler Date: Fri, 17 Feb 2023 11:42:54 +0100 Subject: [PATCH 04/10] remove windows --- .github/workflows/run-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index e9743902..25375f6d 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -12,7 +12,7 @@ jobs: strategy: fail-fast: true matrix: - os: [ubuntu-latest, windows-latest] + os: [ubuntu-latest] php: [8.2, 8.1] laravel: [9.*] stability: [prefer-lowest, prefer-stable] From 1db05ca2cd4072980a507174c946c798c2c4f430 Mon Sep 17 00:00:00 2001 From: Linus Metzler Date: Fri, 17 Feb 2023 11:45:30 +0100 Subject: [PATCH 05/10] fix tests change creds doctrine/dbal mysql creds bump mysql mysql ext change mysql sleep run against PRs sleep mysql call parent use mysql 8 use delta comparison use delta disable fail-fast cleanup dbal conflict fail fast --- .../workflows/fix-php-code-style-issues.yml | 4 +- .github/workflows/phpstan.yml | 8 +-- .github/workflows/run-tests.yml | 11 ++-- Makefile | 32 ------------ composer.json | 30 ++++++----- docker-compose.yml | 14 +++--- phpunit.xml.dist | 4 +- src/SpatialServiceProvider.php | 2 + tests/Integration/SpatialTest.php | 6 +-- tests/TestCase.php | 36 ------------- tests/Unit/Eloquent/SpatialTraitTest.php | 50 +++++++++---------- tests/Unit/Types/GeometryCollectionTest.php | 2 +- 12 files changed, 70 insertions(+), 129 deletions(-) delete mode 100644 Makefile delete mode 100644 tests/TestCase.php diff --git a/.github/workflows/fix-php-code-style-issues.yml b/.github/workflows/fix-php-code-style-issues.yml index 39a77e7b..d9314f2b 100644 --- a/.github/workflows/fix-php-code-style-issues.yml +++ b/.github/workflows/fix-php-code-style-issues.yml @@ -3,7 +3,9 @@ name: Fix PHP code style issues on: push: paths: - - '**.php' + - "**.php" + pull_request: + branches: [master] jobs: php-code-styling: diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index 9d41c0cf..204a6766 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -3,8 +3,10 @@ name: PHPStan on: push: paths: - - '**.php' - - 'phpstan.neon.dist' + - "**.php" + - "phpstan.neon.dist" + pull_request: + branches: [master] jobs: phpstan: @@ -16,7 +18,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.1' + php-version: "8.1" coverage: none - name: Install composer dependencies diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 25375f6d..b58dbb1a 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -24,11 +24,10 @@ jobs: name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} steps: - - uses: mirromutth/mysql-action@v1.1 - with: - mysql version: "8.0" - mysql user: "user" - mysql password: "password" + - 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 @@ -37,7 +36,7 @@ jobs: 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 + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo, mysql coverage: none - name: Setup problem matchers 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 a0e69d54..1ba35930 100644 --- a/composer.json +++ b/composer.json @@ -19,25 +19,29 @@ ], "require": { "php": "^8.1", - "ext-pdo": "*", "ext-json": "*", + "ext-pdo": "*", + "doctrine/dbal": "^2.5", + "geo-io/wkb-parser": "^1.0", "illuminate/contracts": "^9.0", "illuminate/database": "^9.0", - "geo-io/wkb-parser": "^1.0", "jmikola/geojson": "^1.0" }, + "conflict": { + "doctrine/dbal": "2.13.*" + }, "require-dev": { - "laravel/pint": "^1.0", - "laravel/laravel": "^9.0", - "laravel/browser-kit-testing": "^6.0", - "mockery/mockery": "^1.3", - "nunomaduro/collision": "^6.0", - "nunomaduro/larastan": "^2.0.1", - "orchestra/testbench": "^7.0", - "phpstan/extension-installer": "^1.1", - "phpstan/phpstan-deprecation-rules": "^1.0", - "phpstan/phpstan-phpunit": "^1.0", - "phpunit/phpunit": "^9.5" + "laravel/pint": "^1.5", + "laravel/laravel": "^9.5.2", + "laravel/browser-kit-testing": "^6.4", + "mockery/mockery": "^1.5.1", + "nunomaduro/collision": "^6.4", + "nunomaduro/larastan": "^2.4.1", + "orchestra/testbench": "^7.22", + "phpstan/extension-installer": "^1.2", + "phpstan/phpstan-deprecation-rules": "^1.1.1", + "phpstan/phpstan-phpunit": "^1.3.4", + "phpunit/phpunit": "^9.6.3" }, "autoload": { "psr-4": { 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/phpunit.xml.dist b/phpunit.xml.dist index 114eeaa7..4491de92 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -46,10 +46,10 @@ - + - + diff --git a/src/SpatialServiceProvider.php b/src/SpatialServiceProvider.php index b2008b5a..dff5fbcb 100644 --- a/src/SpatialServiceProvider.php +++ b/src/SpatialServiceProvider.php @@ -27,6 +27,8 @@ class SpatialServiceProvider extends DatabaseServiceProvider */ public function register() { + 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. diff --git a/tests/Integration/SpatialTest.php b/tests/Integration/SpatialTest.php index fd767f62..c79ed21f 100644 --- a/tests/Integration/SpatialTest.php +++ b/tests/Integration/SpatialTest.php @@ -212,7 +212,7 @@ public function testDistanceValue() $a = GeometryModel::distanceValue('location', $loc1->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 +230,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/TestCase.php b/tests/TestCase.php deleted file mode 100644 index 2631978e..00000000 --- a/tests/TestCase.php +++ /dev/null @@ -1,36 +0,0 @@ - 'Grimzy\\LaravelMysqlSpatial\\Database\\Factories\\'.class_basename($modelName).'Factory' - ); - } - - protected function getPackageProviders($app) - { - return [ - LaravelMysqlSpatialServiceProvider::class, - ]; - } - - public function getEnvironmentSetUp($app) - { - config()->set('database.default', 'testing'); - - /* - $migration = include __DIR__.'/../database/migrations/create_laravel-mysql-spatial_table.php.stub'; - $migration->up(); - */ - } -} diff --git a/tests/Unit/Eloquent/SpatialTraitTest.php b/tests/Unit/Eloquent/SpatialTraitTest.php index dad13c99..f7b30213 100644 --- a/tests/Unit/Eloquent/SpatialTraitTest.php +++ b/tests/Unit/Eloquent/SpatialTraitTest.php @@ -40,7 +40,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 +48,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 +63,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 +71,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 } @@ -90,14 +90,14 @@ public function testInsertUpdatePolygonHasCorrectSql() $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]); $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 +112,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 +120,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 +139,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 } @@ -174,14 +174,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 +198,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 } @@ -379,7 +379,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 +392,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 +405,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 +418,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 +431,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 +444,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 +457,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 +470,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 +483,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 +507,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,7 +521,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]); } } diff --git a/tests/Unit/Types/GeometryCollectionTest.php b/tests/Unit/Types/GeometryCollectionTest.php index a0d6f016..11acad08 100644 --- a/tests/Unit/Types/GeometryCollectionTest.php +++ b/tests/Unit/Types/GeometryCollectionTest.php @@ -71,7 +71,7 @@ public function testToArray() { $geometryCollection = $this->getGeometryCollection(); - $this->assertInternalType('array', $geometryCollection->toArray()); + $this->assertIsArray($geometryCollection->toArray()); } public function testIteratorAggregate() From 158c481359ef88da331dbf6f57270c24aa86ee73 Mon Sep 17 00:00:00 2001 From: Linus Metzler Date: Fri, 17 Feb 2023 12:42:40 +0100 Subject: [PATCH 06/10] phpstan phpstan Fix styling Fix styling Fix styling --- .github/workflows/phpstan.yml | 13 ++ phpstan-baseline.neon | 116 ++++++++++++++++++ phpstan.neon.dist | 5 +- pint.json | 6 + src/Eloquent/Builder.php | 2 +- src/Eloquent/SpatialExpression.php | 4 +- src/MysqlConnection.php | 8 +- src/Schema/Blueprint.php | 54 ++------ src/Schema/Grammars/MySqlGrammar.php | 52 ++------ src/SpatialServiceProvider.php | 12 +- src/Types/Factory.php | 48 ++++++-- src/Types/Geometry.php | 55 ++++----- src/Types/GeometryCollection.php | 49 ++++---- src/Types/GeometryInterface.php | 12 +- src/Types/LineString.php | 17 ++- src/Types/MultiLineString.php | 27 ++-- src/Types/MultiPoint.php | 24 ++-- src/Types/MultiPolygon.php | 32 ++--- src/Types/Point.php | 39 +++--- src/Types/PointCollection.php | 56 +-------- src/Types/Polygon.php | 5 +- tests/Integration/IntegrationBaseTestCase.php | 2 +- tests/Integration/Migrations/CreateTables.php | 8 +- tests/Integration/Migrations/UpdateTables.php | 8 +- tests/Unit/Eloquent/SpatialTraitTest.php | 12 +- tests/Unit/Schema/BlueprintTest.php | 5 +- .../Unit/Schema/Grammars/MySqlGrammarTest.php | 6 +- tests/Unit/Types/MultiPointTest.php | 48 -------- 28 files changed, 340 insertions(+), 385 deletions(-) create mode 100644 pint.json diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index 204a6766..7d9a5987 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -12,7 +12,20 @@ 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 diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index e69de29b..3388ea2e 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -0,0 +1,116 @@ +parameters: + ignoreErrors: + - + 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: "#^Call to an undefined method GeoJson\\\\GeoJson\\:\\:getGeometry\\(\\)\\.$#" + count: 1 + path: src/Types/Geometry.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: "#^Method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryInterface\\:\\:jsonSerialize\\(\\) has no return type specified\\.$#" + count: 1 + path: src/Types/GeometryInterface.php + + - + message: "#^Return type \\(GeoJson\\\\Geometry\\\\LineString\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\LineString\\:\\:jsonSerialize\\(\\) should be compatible with return type \\(GeoJson\\\\Geometry\\\\GeometryCollection\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryCollection\\:\\:jsonSerialize\\(\\)$#" + count: 1 + path: src/Types/LineString.php + + - + message: "#^Unsafe usage of new static\\(\\)\\.$#" + count: 1 + path: src/Types/LineString.php + + - + message: "#^Return type \\(GeoJson\\\\Geometry\\\\MultiLineString\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\MultiLineString\\:\\:jsonSerialize\\(\\) should be compatible with return type \\(GeoJson\\\\Geometry\\\\GeometryCollection\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryCollection\\:\\:jsonSerialize\\(\\)$#" + count: 1 + path: src/Types/MultiLineString.php + + - + message: "#^Unsafe usage of new static\\(\\)\\.$#" + count: 1 + path: src/Types/MultiLineString.php + + - + message: "#^Parameter \\#1 \\$callback of function array_map expects \\(callable\\(Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryInterface\\)\\: mixed\\)\\|null, Closure\\(Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\Point\\)\\: non\\-falsy\\-string given\\.$#" + count: 1 + path: src/Types/MultiPoint.php + + - + message: "#^Return type \\(GeoJson\\\\Geometry\\\\MultiPoint\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\MultiPoint\\:\\:jsonSerialize\\(\\) should be compatible with return type \\(GeoJson\\\\Geometry\\\\GeometryCollection\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryCollection\\:\\:jsonSerialize\\(\\)$#" + count: 1 + path: src/Types/MultiPoint.php + + - + message: "#^Unsafe usage of new static\\(\\)\\.$#" + count: 1 + path: src/Types/MultiPoint.php + + - + message: "#^Method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\MultiPolygon\\:\\:getPolygons\\(\\) should return array\\ but returns array\\\\.$#" + count: 1 + path: src/Types/MultiPolygon.php + + - + message: "#^Parameter \\#1 \\$callback of function array_map expects \\(callable\\(Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryInterface\\)\\: mixed\\)\\|null, Closure\\(Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\Polygon\\)\\: non\\-falsy\\-string given\\.$#" + count: 1 + path: src/Types/MultiPolygon.php + + - + message: "#^Return type \\(GeoJson\\\\Geometry\\\\MultiPolygon\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\MultiPolygon\\:\\:jsonSerialize\\(\\) should be compatible with return type \\(GeoJson\\\\Geometry\\\\GeometryCollection\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryCollection\\:\\:jsonSerialize\\(\\)$#" + count: 1 + path: src/Types/MultiPolygon.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 + + - + message: "#^Method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\PointCollection\\:\\:getPoints\\(\\) should return array\\ but returns array\\\\.$#" + count: 1 + path: src/Types/PointCollection.php + + - + message: "#^Parameter \\#1 \\$callback of function array_map expects \\(callable\\(Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryInterface\\)\\: mixed\\)\\|null, Closure\\(Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\Point\\)\\: string given\\.$#" + count: 1 + path: src/Types/PointCollection.php + + - + message: "#^Return type \\(GeoJson\\\\Geometry\\\\Polygon\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\Polygon\\:\\:jsonSerialize\\(\\) should be compatible with return type \\(GeoJson\\\\Geometry\\\\MultiLineString\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\MultiLineString\\:\\:jsonSerialize\\(\\)$#" + count: 1 + path: src/Types/Polygon.php diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 9914a5e1..4becbb0a 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,11 +2,10 @@ includes: - phpstan-baseline.neon parameters: - level: 4 + level: 6 paths: - src tmpDir: build/phpstan - checkOctaneCompatibility: true checkModelProperties: true checkMissingIterableValueType: false - + checkGenericClassInNonGenericObjectType: false 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/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..35a1d0a5 100644 --- a/src/Eloquent/SpatialExpression.php +++ b/src/Eloquent/SpatialExpression.php @@ -11,12 +11,12 @@ public function getValue() 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(); } diff --git a/src/MysqlConnection.php b/src/MysqlConnection.php index 38a2b1a4..e484a3ab 100644 --- a/src/MysqlConnection.php +++ b/src/MysqlConnection.php @@ -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 cd09b047..ff443dea 100644 --- a/src/Schema/Blueprint.php +++ b/src/Schema/Blueprint.php @@ -8,12 +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')); } @@ -21,83 +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')); } @@ -107,9 +77,8 @@ public function geometryCollection($column, $srid = null) * * @param string|array $columns * @param string $name - * @return \Illuminate\Support\Fluent */ - public function spatialIndex($columns, $name = null) + public function spatialIndex($columns, $name = null): \Illuminate\Support\Fluent { return $this->indexCommand('spatial', $columns, $name); } @@ -118,9 +87,8 @@ public function spatialIndex($columns, $name = null) * Indicate that the given index should be dropped. * * @param string|array $index - * @return \Illuminate\Support\Fluent */ - public function dropSpatialIndex($index) + public function dropSpatialIndex($index): \Illuminate\Support\Fluent { return $this->dropIndexCommand('dropIndex', 'spatial', $index); } diff --git a/src/Schema/Grammars/MySqlGrammar.php b/src/Schema/Grammars/MySqlGrammar.php index 5ead6db6..a3332077 100644 --- a/src/Schema/Grammars/MySqlGrammar.php +++ b/src/Schema/Grammars/MySqlGrammar.php @@ -20,113 +20,85 @@ public function __construct() /** * Adds a statement to add a geometry column. - * - * - * @return string */ - public function typeGeometry(Fluent $column) + public function typeGeometry(Fluent $column): string { return 'GEOMETRY'; } /** * Adds a statement to add a point column. - * - * - * @return string */ - public function typePoint(Fluent $column) + public function typePoint(Fluent $column): string { return 'POINT'; } /** * Adds a statement to add a linestring column. - * - * - * @return string */ - public function typeLinestring(Fluent $column) + public function typeLinestring(Fluent $column): string { return 'LINESTRING'; } /** * Adds a statement to add a polygon column. - * - * - * @return string */ - public function typePolygon(Fluent $column) + public function typePolygon(Fluent $column): string { return 'POLYGON'; } /** * Adds a statement to add a multipoint column. - * - * - * @return string */ - public function typeMultipoint(Fluent $column) + public function typeMultipoint(Fluent $column): string { return 'MULTIPOINT'; } /** * Adds a statement to add a multilinestring column. - * - * - * @return string */ - public function typeMultilinestring(Fluent $column) + public function typeMultilinestring(Fluent $column): string { return 'MULTILINESTRING'; } /** * Adds a statement to add a multipolygon column. - * - * - * @return string */ - public function typeMultipolygon(Fluent $column) + public function typeMultipolygon(Fluent $column): string { return 'MULTIPOLYGON'; } /** * Adds a statement to add a geometrycollection column. - * - * - * @return string */ - public function typeGeometrycollection(Fluent $column) + public function typeGeometrycollection(Fluent $column): string { return 'GEOMETRYCOLLECTION'; } /** * Compile a spatial index key 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. - * - * - * @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) { return ' srid '.$column->srid; } + + return null; } } diff --git a/src/SpatialServiceProvider.php b/src/SpatialServiceProvider.php index dff5fbcb..d3933701 100644 --- a/src/SpatialServiceProvider.php +++ b/src/SpatialServiceProvider.php @@ -22,26 +22,20 @@ 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 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..f256e377 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)); @@ -105,6 +99,7 @@ public static function fromJson($geoJson) } if ($geoJson->getType() === 'Feature') { + /** @var Feature $geoJson */ $geoJson = $geoJson->getGeometry(); } diff --git a/src/Types/GeometryCollection.php b/src/Types/GeometryCollection.php index 8e701524..f4339f73 100755 --- a/src/Types/GeometryCollection.php +++ b/src/Types/GeometryCollection.php @@ -16,58 +16,54 @@ class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAcc { /** * 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[] */ - protected $items = []; + protected array $items = []; /** * @param GeometryInterface[] $geometries - * @param int $srid * * @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 GeometryInterface[] + */ + 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 +83,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,17 +110,17 @@ 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)); @@ -146,6 +143,7 @@ public static function fromJson($geoJson) * * @return \GeoJson\Geometry\GeometryCollection */ + #[\ReturnTypeWillChange] public function jsonSerialize() { $geometries = []; @@ -159,7 +157,7 @@ public function jsonSerialize() /** * Checks whether the items are valid to create this collection. */ - protected function validateItems(array $items) + protected function validateItems(array $items): void { $this->validateItemCount($items); @@ -171,10 +169,11 @@ protected function validateItems(array $items) /** * Checks whether the array has enough items to generate a valid WKT. * + * @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,7 +193,7 @@ protected function validateItemCount(array $items) * * @see $collectionItemType */ - protected function validateItemType($item) + protected function validateItemType(mixed $item): void { if (! $item instanceof $this->collectionItemType) { throw new InvalidArgumentException(sprintf( diff --git a/src/Types/GeometryInterface.php b/src/Types/GeometryInterface.php index 4f0dd1ef..ae2388ee 100644 --- a/src/Types/GeometryInterface.php +++ b/src/Types/GeometryInterface.php @@ -2,15 +2,19 @@ namespace Grimzy\LaravelMysqlSpatial\Types; +use GeoJson\GeoJson; + 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; - public static function fromJson($geoJson); + public function jsonSerialize(); } diff --git a/src/Types/LineString.php b/src/Types/LineString.php index d05ac4e8..4922d9e8 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,7 +38,7 @@ 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)); @@ -65,6 +61,7 @@ public static function fromJson($geoJson) * * @return \GeoJson\Geometry\LineString */ + #[\ReturnTypeWillChange] public function jsonSerialize() { $points = []; diff --git a/src/Types/MultiLineString.php b/src/Types/MultiLineString.php index f3401fbd..3374bdea 100644 --- a/src/Types/MultiLineString.php +++ b/src/Types/MultiLineString.php @@ -10,53 +10,45 @@ class MultiLineString extends GeometryCollection { /** * 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)); @@ -83,6 +75,7 @@ public static function fromJson($geoJson) * * @return \GeoJson\Geometry\MultiLineString */ + #[\ReturnTypeWillChange] public function jsonSerialize() { $lineStrings = []; diff --git a/src/Types/MultiPoint.php b/src/Types/MultiPoint.php index 171cd896..d886623f 100644 --- a/src/Types/MultiPoint.php +++ b/src/Types/MultiPoint.php @@ -10,43 +10,37 @@ class MultiPoint extends PointCollection { /** * 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)); @@ -66,10 +60,8 @@ public static function fromJson($geoJson) /** * Convert to GeoJson MultiPoint that is jsonable to GeoJSON. - * - * @return \GeoJson\Geometry\MultiPoint */ - public function jsonSerialize() + public function jsonSerialize(): GeoJsonMultiPoint { $points = []; foreach ($this->items as $point) { diff --git a/src/Types/MultiPolygon.php b/src/Types/MultiPolygon.php index ac2e88fe..01cfccb2 100644 --- a/src/Types/MultiPolygon.php +++ b/src/Types/MultiPolygon.php @@ -10,38 +10,30 @@ class MultiPolygon extends GeometryCollection { /** * 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); } /** @@ -49,7 +41,7 @@ public static function fromString($wktArgument, $srid = 0) * * @return array|Polygon[] */ - public function getPolygons() + public function getPolygons(): array { return $this->items; } @@ -66,11 +58,8 @@ 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))" - * - * - * @return array */ - protected static function assembleParts(array $parts) + protected static function assembleParts(array $parts): array { $polygons = []; $count = count($parts); @@ -88,14 +77,14 @@ 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)); @@ -126,6 +115,7 @@ public static function fromJson($geoJson) * * @return \GeoJson\Geometry\MultiPolygon */ + #[\ReturnTypeWillChange] public function jsonSerialize() { $polygons = []; diff --git a/src/Types/Point.php b/src/Types/Point.php index 1a82f2fb..4bfcb997 100644 --- a/src/Types/Point.php +++ b/src/Types/Point.php @@ -8,61 +8,61 @@ class Point extends Geometry { - 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 { [$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(); } @@ -71,7 +71,7 @@ public function __toString() * @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)); @@ -91,6 +91,7 @@ public static function fromJson($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 b3bde354..2395de24 100755 --- a/src/Types/PointCollection.php +++ b/src/Types/PointCollection.php @@ -2,26 +2,19 @@ namespace Grimzy\LaravelMysqlSpatial\Types; -use ArrayAccess; -use InvalidArgumentException; - 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); @@ -31,47 +24,8 @@ public function 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 \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 7dd0c4ab..dafc91a3 100644 --- a/src/Types/Polygon.php +++ b/src/Types/Polygon.php @@ -8,12 +8,12 @@ class Polygon extends MultiLineString { - 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)); @@ -40,6 +40,7 @@ public static function fromJson($geoJson) * * @return \GeoJson\Geometry\Polygon */ + #[\ReturnTypeWillChange] public function jsonSerialize() { $linearRings = []; diff --git a/tests/Integration/IntegrationBaseTestCase.php b/tests/Integration/IntegrationBaseTestCase.php index 108fa487..15f2cbe6 100644 --- a/tests/Integration/IntegrationBaseTestCase.php +++ b/tests/Integration/IntegrationBaseTestCase.php @@ -15,7 +15,7 @@ abstract class IntegrationBaseTestCase extends BaseTestCase * * @return \Illuminate\Foundation\Application */ - public function createApplication() + public function createApplication(): Illuminate\Foundation\Application { $app = require __DIR__.'/../../vendor/laravel/laravel/bootstrap/app.php'; $app->register(SpatialServiceProvider::class); diff --git a/tests/Integration/Migrations/CreateTables.php b/tests/Integration/Migrations/CreateTables.php index fdff4f58..08a0c181 100644 --- a/tests/Integration/Migrations/CreateTables.php +++ b/tests/Integration/Migrations/CreateTables.php @@ -8,10 +8,8 @@ class CreateLocationTable extends Migration { /** * Run the migrations. - * - * @return void */ - public function up() + public function up(): void { Schema::create('geometry', function (Blueprint $table) { $table->charset = 'utf8mb4'; @@ -50,10 +48,8 @@ public function up() /** * Reverse the migrations. - * - * @return void */ - public function down() + public function down(): void { Schema::drop('geometry'); Schema::drop('no_spatial_fields'); diff --git a/tests/Integration/Migrations/UpdateTables.php b/tests/Integration/Migrations/UpdateTables.php index 83915b81..3f2715de 100644 --- a/tests/Integration/Migrations/UpdateTables.php +++ b/tests/Integration/Migrations/UpdateTables.php @@ -8,10 +8,8 @@ class UpdateLocationTable extends Migration { /** * Run the migrations. - * - * @return void */ - public function up() + public function up(): void { // MySQL < 5.7.5: table has to be MyISAM \DB::statement('ALTER TABLE geometry ENGINE = MyISAM'); @@ -36,10 +34,8 @@ public function up() /** * Reverse the migrations. - * - * @return void */ - public function down() + public function down(): void { Schema::table('geometry', function (Blueprint $table) { $table->dropSpatialIndex(['location']); // either an array of column names or the index name diff --git a/tests/Unit/Eloquent/SpatialTraitTest.php b/tests/Unit/Eloquent/SpatialTraitTest.php index f7b30213..d754e983 100644 --- a/tests/Unit/Eloquent/SpatialTraitTest.php +++ b/tests/Unit/Eloquent/SpatialTraitTest.php @@ -11,15 +11,9 @@ class SpatialTraitTest extends BaseTestCase { use MockeryPHPUnitIntegration; - /** - * @var TestModel - */ - protected $model; - - /** - * @var array - */ - protected $queries; + protected TestModel $model; + + protected array $queries; public function setUp(): void { diff --git a/tests/Unit/Schema/BlueprintTest.php b/tests/Unit/Schema/BlueprintTest.php index 457adab1..6f6ee3e9 100644 --- a/tests/Unit/Schema/BlueprintTest.php +++ b/tests/Unit/Schema/BlueprintTest.php @@ -9,10 +9,7 @@ class BlueprintTest extends BaseTestCase { - /** - * @var \Grimzy\LaravelMysqlSpatial\Schema\Blueprint - */ - protected $blueprint; + protected \Grimzy\LaravelMysqlSpatial\Schema\Blueprint $blueprint; public function setUp(): void { diff --git a/tests/Unit/Schema/Grammars/MySqlGrammarTest.php b/tests/Unit/Schema/Grammars/MySqlGrammarTest.php index 66ba7641..8bc3a21d 100644 --- a/tests/Unit/Schema/Grammars/MySqlGrammarTest.php +++ b/tests/Unit/Schema/Grammars/MySqlGrammarTest.php @@ -3,6 +3,7 @@ use Grimzy\LaravelMysqlSpatial\MysqlConnection; use Grimzy\LaravelMysqlSpatial\Schema\Blueprint; use Grimzy\LaravelMysqlSpatial\Schema\Grammars\MySqlGrammar; +use Illuminate\Database\Connection; class MySqlGrammarBaseTest extends BaseTestCase { @@ -186,10 +187,7 @@ public function testAddRemoveSpatialIndex() $this->assertEquals($expectedSql, $dropStatements[4]); } - /** - * @return Connection - */ - protected function getConnection() + protected function getConnection(): Connection { return Mockery::mock(MysqlConnection::class); } diff --git a/tests/Unit/Types/MultiPointTest.php b/tests/Unit/Types/MultiPointTest.php index a8a94f41..245e0770 100644 --- a/tests/Unit/Types/MultiPointTest.php +++ b/tests/Unit/Types/MultiPointTest.php @@ -102,52 +102,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)); - } } From 8032ce80c9d165a80f9bc7ea9588f17436dbcf12 Mon Sep 17 00:00:00 2001 From: Linus Metzler Date: Fri, 17 Feb 2023 15:09:37 +0100 Subject: [PATCH 07/10] Laravel 10 Fix styling Fix styling Fix styling fixup! phpstan --- .github/workflows/run-tests.yml | 6 +- .gitignore | 1 + composer.json | 26 ++--- phpstan-baseline.neon | 10 ++ phpunit.xml.dist | 85 ++++++-------- src/Eloquent/SpatialExpression.php | 24 +++- src/MysqlConnection.php | 2 +- ...seTestCase.php => IntegrationBaseCase.php} | 24 ++-- tests/Integration/MigrationTest.php | 10 +- tests/Integration/Migrations/CreateTables.php | 4 +- tests/Integration/Migrations/UpdateTables.php | 4 +- tests/Integration/Models/GeometryModel.php | 2 + .../Models/NoSpatialFieldsModel.php | 2 + tests/Integration/Models/WithSridModel.php | 2 + tests/Integration/SpatialTest.php | 10 +- tests/Integration/SridSpatialTest.php | 9 +- tests/Unit/BaseTestCase.php | 7 +- .../Unit/Connectors/ConnectionFactoryTest.php | 8 +- tests/Unit/Eloquent/BuilderTest.php | 24 ++-- tests/Unit/Eloquent/SpatialTraitTest.php | 106 ++++-------------- tests/Unit/Eloquent/TestModel.php | 37 ++++++ tests/Unit/Eloquent/TestPDO.php | 36 ++++++ tests/Unit/Eloquent/TestRelatedModel.php | 16 +++ tests/Unit/MysqlConnectionTest.php | 4 +- tests/Unit/Schema/BlueprintTest.php | 6 +- tests/Unit/Schema/BuilderTest.php | 6 +- .../Unit/Schema/Grammars/MySqlGrammarTest.php | 9 +- tests/Unit/Stubs/PDOStub.php | 2 +- tests/Unit/Types/GeometryCollectionTest.php | 6 +- tests/Unit/Types/GeometryTest.php | 3 + tests/Unit/Types/LineStringTest.php | 5 +- tests/Unit/Types/MultiLineStringTest.php | 6 +- tests/Unit/Types/MultiPointTest.php | 6 +- tests/Unit/Types/MultiPolygonTest.php | 6 +- tests/Unit/Types/PointTest.php | 3 + tests/Unit/Types/PolygonTest.php | 3 + 36 files changed, 305 insertions(+), 215 deletions(-) rename tests/Integration/{IntegrationBaseTestCase.php => IntegrationBaseCase.php} (82%) create mode 100644 tests/Unit/Eloquent/TestModel.php create mode 100644 tests/Unit/Eloquent/TestPDO.php create mode 100644 tests/Unit/Eloquent/TestRelatedModel.php diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index b58dbb1a..aa437dd6 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -14,11 +14,11 @@ jobs: matrix: os: [ubuntu-latest] php: [8.2, 8.1] - laravel: [9.*] + laravel: [10.*] stability: [prefer-lowest, prefer-stable] include: - - laravel: 9.* - testbench: 7.* + - laravel: 10.* + testbench: 8.* carbon: ^2.63 name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} diff --git a/.gitignore b/.gitignore index f3bf0d33..0bbeb929 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ testbench.yaml vendor _db/ node_modules +.phpunit.cache diff --git a/composer.json b/composer.json index 1ba35930..c1344042 100644 --- a/composer.json +++ b/composer.json @@ -21,27 +21,25 @@ "php": "^8.1", "ext-json": "*", "ext-pdo": "*", - "doctrine/dbal": "^2.5", + "doctrine/dbal": "^3.0", "geo-io/wkb-parser": "^1.0", - "illuminate/contracts": "^9.0", - "illuminate/database": "^9.0", + "illuminate/contracts": "^10.0", + "illuminate/database": "^10.0", "jmikola/geojson": "^1.0" }, - "conflict": { - "doctrine/dbal": "2.13.*" - }, + "conflict": {}, "require-dev": { "laravel/pint": "^1.5", - "laravel/laravel": "^9.5.2", - "laravel/browser-kit-testing": "^6.4", + "laravel/laravel": "^10.0.2", + "laravel/browser-kit-testing": "^7.0", "mockery/mockery": "^1.5.1", - "nunomaduro/collision": "^6.4", + "nunomaduro/collision": "^7.0.4", "nunomaduro/larastan": "^2.4.1", - "orchestra/testbench": "^7.22", + "orchestra/testbench": "^8.0.1", "phpstan/extension-installer": "^1.2", "phpstan/phpstan-deprecation-rules": "^1.1.1", "phpstan/phpstan-phpunit": "^1.3.4", - "phpunit/phpunit": "^9.6.3" + "phpunit/phpunit": "^10.0.7" }, "autoload": { "psr-4": { @@ -49,10 +47,6 @@ } }, "autoload-dev": { - "classmap": [ - "tests/Unit", - "tests/Integration" - ], "psr-4": { "Grimzy\\LaravelMysqlSpatial\\Tests\\": "tests" } @@ -71,6 +65,6 @@ ] } }, - "minimum-stability": "dev", + "minimum-stability": "stable", "prefer-stable": true } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 3388ea2e..ca676425 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,5 +1,15 @@ parameters: ignoreErrors: + - + message: "#^Call to an undefined method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryInterface\\:\\:getSrid\\(\\)\\.$#" + count: 1 + path: src/Eloquent/SpatialExpression.php + + - + 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 diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 4491de92..5d8dd7a1 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,55 +1,34 @@ - - - - ./tests/Unit - - - ./tests/Integration - - - - - - - - - ./src - - - - - - - - - - - - - - - - - - - - - - - + + + + tests + + + + + ./src + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Eloquent/SpatialExpression.php b/src/Eloquent/SpatialExpression.php index 35a1d0a5..e4647524 100644 --- a/src/Eloquent/SpatialExpression.php +++ b/src/Eloquent/SpatialExpression.php @@ -2,11 +2,28 @@ 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')"; } @@ -20,4 +37,9 @@ public function getSrid(): int { return $this->value->getSrid(); } + + public function toWkt(): string + { + return $this->value->toWkt(); + } } diff --git a/src/MysqlConnection.php b/src/MysqlConnection.php index e484a3ab..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'); } diff --git a/tests/Integration/IntegrationBaseTestCase.php b/tests/Integration/IntegrationBaseCase.php similarity index 82% rename from tests/Integration/IntegrationBaseTestCase.php rename to tests/Integration/IntegrationBaseCase.php index 15f2cbe6..e9b5c752 100644 --- a/tests/Integration/IntegrationBaseTestCase.php +++ b/tests/Integration/IntegrationBaseCase.php @@ -1,10 +1,14 @@ register(SpatialServiceProvider::class); @@ -49,9 +51,10 @@ public function setUp(): void $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); @@ -60,9 +63,8 @@ public function setUp(): void public function tearDown(): void { - $this->onMigrations(function ($migrationClass) { - (new $migrationClass())->down(); - }, true); + (new UpdateTables)->down(); + (new CreateTables)->down(); parent::tearDown(); } @@ -70,7 +72,7 @@ public function tearDown(): void // 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', '>='); 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 @@ 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() @@ -130,12 +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/Eloquent/SpatialTraitTest.php b/tests/Unit/Eloquent/SpatialTraitTest.php index d754e983..18b38237 100644 --- a/tests/Unit/Eloquent/SpatialTraitTest.php +++ b/tests/Unit/Eloquent/SpatialTraitTest.php @@ -1,11 +1,14 @@ model = new TestModel(); $this->queries = &$this->model->getConnection()->getPdo()->queries; } @@ -213,10 +218,17 @@ public function testSettingRawAttributes() public function testSpatialFieldsNotDefinedException() { - $model = new TestNoSpatialModel(); + $model = new class extends Model + { + use SpatialTrait; + }; + $this->assertException( + SpatialFieldsNotDefinedException::class, + 'TestNoSpatialModel' + ); $this->assertException( SpatialFieldsNotDefinedException::class, - 'TestNoSpatialModel has to define $spatialFields' + ' has to define $spatialFields' ); $model->getSpatialFields(); } @@ -297,7 +309,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]); } @@ -313,7 +325,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]); } @@ -329,7 +341,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]); } @@ -345,7 +357,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]); } @@ -519,81 +531,3 @@ public function testScopeOrderByDistanceSphere() $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/Unit/Eloquent/TestModel.php b/tests/Unit/Eloquent/TestModel.php new file mode 100644 index 00000000..c7ab32e4 --- /dev/null +++ b/tests/Unit/Eloquent/TestModel.php @@ -0,0 +1,37 @@ +makePartial(); + } + + return new MysqlConnection(static::$pdo); + } + + public function testrelatedmodels() + { + return $this->hasMany(TestRelatedModel::class); + } + + public function testrelatedmodels2() + { + return $this->belongsToMany(TestRelatedModel::class); + } +} diff --git a/tests/Unit/Eloquent/TestPDO.php b/tests/Unit/Eloquent/TestPDO.php new file mode 100644 index 00000000..a642b949 --- /dev/null +++ b/tests/Unit/Eloquent/TestPDO.php @@ -0,0 +1,36 @@ +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/Unit/Eloquent/TestRelatedModel.php b/tests/Unit/Eloquent/TestRelatedModel.php new file mode 100644 index 00000000..d85a067f --- /dev/null +++ b/tests/Unit/Eloquent/TestRelatedModel.php @@ -0,0 +1,16 @@ +belongsTo(TestModel::class); + } + + public function testmodels() + { + return $this->belongsToMany(TestModel::class); + } +} diff --git a/tests/Unit/MysqlConnectionTest.php b/tests/Unit/MysqlConnectionTest.php index c80b14fe..f189eef8 100644 --- a/tests/Unit/MysqlConnectionTest.php +++ b/tests/Unit/MysqlConnectionTest.php @@ -1,9 +1,11 @@ assertEquals($expectedSql, $dropStatements[4]); } - protected function getConnection(): Connection + 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 index 99d2d806..70a77eff 100644 --- a/tests/Unit/Stubs/PDOStub.php +++ b/tests/Unit/Stubs/PDOStub.php @@ -1,6 +1,6 @@ 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 @@ 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 245e0770..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]}'); } 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 @@ Date: Sun, 12 Mar 2023 09:53:57 +0100 Subject: [PATCH 08/10] update deps --- composer.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index c1344042..ed6485ce 100644 --- a/composer.json +++ b/composer.json @@ -29,17 +29,17 @@ }, "conflict": {}, "require-dev": { - "laravel/pint": "^1.5", - "laravel/laravel": "^10.0.2", + "laravel/pint": "^1.6", + "laravel/laravel": "^10.0.4", "laravel/browser-kit-testing": "^7.0", "mockery/mockery": "^1.5.1", - "nunomaduro/collision": "^7.0.4", - "nunomaduro/larastan": "^2.4.1", - "orchestra/testbench": "^8.0.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.1", - "phpstan/phpstan-phpunit": "^1.3.4", - "phpunit/phpunit": "^10.0.7" + "phpstan/phpstan-deprecation-rules": "^1.1.2", + "phpstan/phpstan-phpunit": "^1.3.10", + "phpunit/phpunit": "^10.0.15" }, "autoload": { "psr-4": { From 7006c90e8c8d52878354f715badd99a500fccb6c Mon Sep 17 00:00:00 2001 From: Linus Metzler Date: Sun, 12 Mar 2023 10:27:56 +0100 Subject: [PATCH 09/10] improve types Fix styling --- phpstan-baseline.neon | 64 -------------------------------- src/Types/Geometry.php | 2 +- src/Types/GeometryCollection.php | 13 ++++--- src/Types/GeometryInterface.php | 8 ++++ src/Types/LineString.php | 2 - src/Types/MultiLineString.php | 9 +++-- src/Types/MultiPoint.php | 7 +++- src/Types/MultiPolygon.php | 11 +++--- src/Types/Point.php | 7 ++-- src/Types/PointCollection.php | 6 +-- src/Types/Polygon.php | 7 ++-- 11 files changed, 45 insertions(+), 91 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index ca676425..5338ba2c 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,10 +1,5 @@ parameters: ignoreErrors: - - - message: "#^Call to an undefined method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryInterface\\:\\:getSrid\\(\\)\\.$#" - count: 1 - path: src/Eloquent/SpatialExpression.php - - 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 @@ -30,11 +25,6 @@ parameters: count: 1 path: src/Schema/Grammars/MySqlGrammar.php - - - message: "#^Call to an undefined method GeoJson\\\\GeoJson\\:\\:getGeometry\\(\\)\\.$#" - count: 1 - path: src/Types/Geometry.php - - message: "#^Method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\Geometry\\:\\:fromWKT\\(\\) should return static\\(Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\Geometry\\) but returns Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryInterface\\.$#" count: 1 @@ -45,61 +35,21 @@ parameters: count: 2 path: src/Types/GeometryCollection.php - - - message: "#^Method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryInterface\\:\\:jsonSerialize\\(\\) has no return type specified\\.$#" - count: 1 - path: src/Types/GeometryInterface.php - - - - message: "#^Return type \\(GeoJson\\\\Geometry\\\\LineString\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\LineString\\:\\:jsonSerialize\\(\\) should be compatible with return type \\(GeoJson\\\\Geometry\\\\GeometryCollection\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryCollection\\:\\:jsonSerialize\\(\\)$#" - count: 1 - path: src/Types/LineString.php - - message: "#^Unsafe usage of new static\\(\\)\\.$#" count: 1 path: src/Types/LineString.php - - - message: "#^Return type \\(GeoJson\\\\Geometry\\\\MultiLineString\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\MultiLineString\\:\\:jsonSerialize\\(\\) should be compatible with return type \\(GeoJson\\\\Geometry\\\\GeometryCollection\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryCollection\\:\\:jsonSerialize\\(\\)$#" - count: 1 - path: src/Types/MultiLineString.php - - message: "#^Unsafe usage of new static\\(\\)\\.$#" count: 1 path: src/Types/MultiLineString.php - - - message: "#^Parameter \\#1 \\$callback of function array_map expects \\(callable\\(Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryInterface\\)\\: mixed\\)\\|null, Closure\\(Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\Point\\)\\: non\\-falsy\\-string given\\.$#" - count: 1 - path: src/Types/MultiPoint.php - - - - message: "#^Return type \\(GeoJson\\\\Geometry\\\\MultiPoint\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\MultiPoint\\:\\:jsonSerialize\\(\\) should be compatible with return type \\(GeoJson\\\\Geometry\\\\GeometryCollection\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryCollection\\:\\:jsonSerialize\\(\\)$#" - count: 1 - path: src/Types/MultiPoint.php - - message: "#^Unsafe usage of new static\\(\\)\\.$#" count: 1 path: src/Types/MultiPoint.php - - - message: "#^Method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\MultiPolygon\\:\\:getPolygons\\(\\) should return array\\ but returns array\\\\.$#" - count: 1 - path: src/Types/MultiPolygon.php - - - - message: "#^Parameter \\#1 \\$callback of function array_map expects \\(callable\\(Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryInterface\\)\\: mixed\\)\\|null, Closure\\(Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\Polygon\\)\\: non\\-falsy\\-string given\\.$#" - count: 1 - path: src/Types/MultiPolygon.php - - - - message: "#^Return type \\(GeoJson\\\\Geometry\\\\MultiPolygon\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\MultiPolygon\\:\\:jsonSerialize\\(\\) should be compatible with return type \\(GeoJson\\\\Geometry\\\\GeometryCollection\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryCollection\\:\\:jsonSerialize\\(\\)$#" - count: 1 - path: src/Types/MultiPolygon.php - - message: "#^Unsafe usage of new static\\(\\)\\.$#" count: 1 @@ -110,17 +60,3 @@ parameters: count: 1 path: src/Types/Point.php - - - message: "#^Method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\PointCollection\\:\\:getPoints\\(\\) should return array\\ but returns array\\\\.$#" - count: 1 - path: src/Types/PointCollection.php - - - - message: "#^Parameter \\#1 \\$callback of function array_map expects \\(callable\\(Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\GeometryInterface\\)\\: mixed\\)\\|null, Closure\\(Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\Point\\)\\: string given\\.$#" - count: 1 - path: src/Types/PointCollection.php - - - - message: "#^Return type \\(GeoJson\\\\Geometry\\\\Polygon\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\Polygon\\:\\:jsonSerialize\\(\\) should be compatible with return type \\(GeoJson\\\\Geometry\\\\MultiLineString\\) of method Grimzy\\\\LaravelMysqlSpatial\\\\Types\\\\MultiLineString\\:\\:jsonSerialize\\(\\)$#" - count: 1 - path: src/Types/Polygon.php diff --git a/src/Types/Geometry.php b/src/Types/Geometry.php index f256e377..81986387 100644 --- a/src/Types/Geometry.php +++ b/src/Types/Geometry.php @@ -98,8 +98,8 @@ public static function fromJson(string|GeoJson $geoJson): self return GeometryCollection::fromJson($geoJson); } + /** @var Feature $geoJson */ if ($geoJson->getType() === 'Feature') { - /** @var Feature $geoJson */ $geoJson = $geoJson->getGeometry(); } diff --git a/src/Types/GeometryCollection.php b/src/Types/GeometryCollection.php index f4339f73..3a85a929 100755 --- a/src/Types/GeometryCollection.php +++ b/src/Types/GeometryCollection.php @@ -12,7 +12,12 @@ 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. @@ -27,7 +32,7 @@ class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAcc /** * The items contained in the spatial collection. * - * @var GeometryInterface[] + * @var G[] */ protected array $items = []; @@ -46,7 +51,7 @@ public function __construct(array $geometries, ?int $srid = 0) } /** - * @return GeometryInterface[] + * @return G[] */ public function getGeometries(): array { @@ -140,8 +145,6 @@ public static function fromJson(string|GeoJson $geoJson): self /** * Convert to GeoJson GeometryCollection that is jsonable to GeoJSON. - * - * @return \GeoJson\Geometry\GeometryCollection */ #[\ReturnTypeWillChange] public function jsonSerialize() diff --git a/src/Types/GeometryInterface.php b/src/Types/GeometryInterface.php index ae2388ee..67669d67 100644 --- a/src/Types/GeometryInterface.php +++ b/src/Types/GeometryInterface.php @@ -4,6 +4,9 @@ use GeoJson\GeoJson; +/** + * @template T + */ interface GeometryInterface { public function toWKT(): string; @@ -16,5 +19,10 @@ public static function fromString(string $wktArgument, int $srid = 0): self; public static function fromJson(string|GeoJson $geoJson): self; + /** + * @return T + */ public function jsonSerialize(); + + public function getSrid(): int; } diff --git a/src/Types/LineString.php b/src/Types/LineString.php index 4922d9e8..dbc02525 100644 --- a/src/Types/LineString.php +++ b/src/Types/LineString.php @@ -58,8 +58,6 @@ public static function fromJson(string|GeoJson $geoJson): self /** * Convert to GeoJson LineString that is jsonable to GeoJSON. - * - * @return \GeoJson\Geometry\LineString */ #[\ReturnTypeWillChange] public function jsonSerialize() diff --git a/src/Types/MultiLineString.php b/src/Types/MultiLineString.php index 3374bdea..57beea2a 100644 --- a/src/Types/MultiLineString.php +++ b/src/Types/MultiLineString.php @@ -6,7 +6,12 @@ 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. @@ -72,8 +77,6 @@ public static function fromJson(string|GeoJson $geoJson): self /** * Convert to GeoJson Point that is jsonable to GeoJSON. - * - * @return \GeoJson\Geometry\MultiLineString */ #[\ReturnTypeWillChange] public function jsonSerialize() diff --git a/src/Types/MultiPoint.php b/src/Types/MultiPoint.php index d886623f..f9bdbdea 100644 --- a/src/Types/MultiPoint.php +++ b/src/Types/MultiPoint.php @@ -6,7 +6,10 @@ 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. @@ -61,7 +64,7 @@ public static function fromJson(string|GeoJson $geoJson): self /** * Convert to GeoJson MultiPoint that is jsonable to GeoJSON. */ - public function jsonSerialize(): GeoJsonMultiPoint + public function jsonSerialize() { $points = []; foreach ($this->items as $point) { diff --git a/src/Types/MultiPolygon.php b/src/Types/MultiPolygon.php index 01cfccb2..dc666ca0 100644 --- a/src/Types/MultiPolygon.php +++ b/src/Types/MultiPolygon.php @@ -6,7 +6,12 @@ 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. @@ -38,8 +43,6 @@ public static function fromString(string $wktArgument, int $srid = 0): static /** * Get the polygons that make up this MultiPolygon. - * - * @return array|Polygon[] */ public function getPolygons(): array { @@ -112,8 +115,6 @@ public static function fromJson(string|GeoJson $geoJson): self /** * Convert to GeoJson MultiPolygon that is jsonable to GeoJSON. - * - * @return \GeoJson\Geometry\MultiPolygon */ #[\ReturnTypeWillChange] public function jsonSerialize() diff --git a/src/Types/Point.php b/src/Types/Point.php index 4bfcb997..21c458c6 100644 --- a/src/Types/Point.php +++ b/src/Types/Point.php @@ -6,7 +6,10 @@ use GeoJson\Geometry\Point as GeoJsonPoint; use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException; -class Point extends Geometry +/** + * @implements GeometryInterface + */ +class Point extends Geometry implements GeometryInterface { protected float $lat; @@ -88,8 +91,6 @@ public static function fromJson(string|GeoJson $geoJson): self /** * Convert to GeoJson Point that is jsonable to GeoJSON. - * - * @return \GeoJson\Geometry\Point */ #[\ReturnTypeWillChange] public function jsonSerialize() diff --git a/src/Types/PointCollection.php b/src/Types/PointCollection.php index 2395de24..87232a1d 100755 --- a/src/Types/PointCollection.php +++ b/src/Types/PointCollection.php @@ -2,6 +2,9 @@ namespace Grimzy\LaravelMysqlSpatial\Types; +/** + * @extends GeometryCollection + */ abstract class PointCollection extends GeometryCollection { /** @@ -21,9 +24,6 @@ public function offsetSet($offset, $value): void parent::offsetSet($offset, $value); } - /** - * @return array|\Grimzy\LaravelMysqlSpatial\Types\Point[] - */ public function getPoints(): array { return $this->items; diff --git a/src/Types/Polygon.php b/src/Types/Polygon.php index dafc91a3..7c3e4b60 100644 --- a/src/Types/Polygon.php +++ b/src/Types/Polygon.php @@ -6,7 +6,10 @@ 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(): string { @@ -37,8 +40,6 @@ public static function fromJson(string|GeoJson $geoJson): self /** * Convert to GeoJson Polygon that is jsonable to GeoJSON. - * - * @return \GeoJson\Geometry\Polygon */ #[\ReturnTypeWillChange] public function jsonSerialize() From e3113dcc7a66532243c8b77948ab8c10e4f50211 Mon Sep 17 00:00:00 2001 From: Linus Metzler Date: Sun, 12 Mar 2023 10:36:55 +0100 Subject: [PATCH 10/10] fix tests Fix styling cleanup fix tests Fix styling fix test fix test Fix styling fix tests debugging tests --- .github/workflows/run-tests.yml | 4 +- composer.json | 2 +- .../Eloquent/TestModel.php | 13 +----- .../Eloquent/TestRelatedModel.php | 2 +- tests/Integration/Migrations/CreateTables.php | 15 +++++++ .../SpatialTraitTest.php | 42 ++++++++++++------- tests/Integration/SridSpatialTest.php | 9 +--- .../Unit/Connectors/ConnectionFactoryTest.php | 5 +-- tests/Unit/Eloquent/TestPDO.php | 36 ---------------- tests/Unit/MysqlConnectionTest.php | 3 +- tests/Unit/Stubs/PDOStub.php | 10 ----- 11 files changed, 51 insertions(+), 90 deletions(-) rename tests/{Unit => Integration}/Eloquent/TestModel.php (58%) rename tests/{Unit => Integration}/Eloquent/TestRelatedModel.php (79%) rename tests/{Unit/Eloquent => Integration}/SpatialTraitTest.php (95%) delete mode 100644 tests/Unit/Eloquent/TestPDO.php delete mode 100644 tests/Unit/Stubs/PDOStub.php diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index aa437dd6..346f83e2 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -1,4 +1,4 @@ -name: run-tests +name: PHP Tests on: push: @@ -37,7 +37,7 @@ jobs: 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: none + coverage: xdebug - name: Setup problem matchers run: | diff --git a/composer.json b/composer.json index ed6485ce..617444e5 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "php": "^8.1", "ext-json": "*", "ext-pdo": "*", - "doctrine/dbal": "^3.0", + "doctrine/dbal": "^3.5", "geo-io/wkb-parser": "^1.0", "illuminate/contracts": "^10.0", "illuminate/database": "^10.0", diff --git a/tests/Unit/Eloquent/TestModel.php b/tests/Integration/Eloquent/TestModel.php similarity index 58% rename from tests/Unit/Eloquent/TestModel.php rename to tests/Integration/Eloquent/TestModel.php index c7ab32e4..1afd7f94 100644 --- a/tests/Unit/Eloquent/TestModel.php +++ b/tests/Integration/Eloquent/TestModel.php @@ -1,10 +1,8 @@ makePartial(); - } - - return new MysqlConnection(static::$pdo); - } - public function testrelatedmodels() { return $this->hasMany(TestRelatedModel::class); diff --git a/tests/Unit/Eloquent/TestRelatedModel.php b/tests/Integration/Eloquent/TestRelatedModel.php similarity index 79% rename from tests/Unit/Eloquent/TestRelatedModel.php rename to tests/Integration/Eloquent/TestRelatedModel.php index d85a067f..f9610dce 100644 --- a/tests/Unit/Eloquent/TestRelatedModel.php +++ b/tests/Integration/Eloquent/TestRelatedModel.php @@ -1,6 +1,6 @@ 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'; @@ -56,5 +70,6 @@ public function down(): void Schema::drop('geometry'); Schema::drop('no_spatial_fields'); Schema::drop('with_srid'); + Schema::drop('test_models'); } } diff --git a/tests/Unit/Eloquent/SpatialTraitTest.php b/tests/Integration/SpatialTraitTest.php similarity index 95% rename from tests/Unit/Eloquent/SpatialTraitTest.php rename to tests/Integration/SpatialTraitTest.php index 18b38237..308a724f 100644 --- a/tests/Unit/Eloquent/SpatialTraitTest.php +++ b/tests/Integration/SpatialTraitTest.php @@ -1,34 +1,37 @@ model = new TestModel(); - $this->queries = &$this->model->getConnection()->getPdo()->queries; + DB::listen(function (QueryExecuted $query) { + $this->queries[] = $query->sql; + }); } public function tearDown(): void { - $this->model->getConnection()->getPdo()->resetQueries(); + $this->queries = []; } public function testInsertUpdatePointHasCorrectSql() @@ -78,14 +81,16 @@ 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]); @@ -93,8 +98,13 @@ public function testInsertUpdatePolygonHasCorrectSql() // 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->assertStringContainsString('update `test_models` set `polygon` = ST_GeomFromText(?, ?, \'axis-order=long-lat\') where `id` = ?', $this->queries[1]); // TODO: assert bindings in query @@ -153,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); diff --git a/tests/Integration/SridSpatialTest.php b/tests/Integration/SridSpatialTest.php index 781fdea3..69923ff8 100644 --- a/tests/Integration/SridSpatialTest.php +++ b/tests/Integration/SridSpatialTest.php @@ -111,13 +111,8 @@ public function testInsertPointWithWrongSrid() $geo->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/Connectors/ConnectionFactoryTest.php b/tests/Unit/Connectors/ConnectionFactoryTest.php index 24a18dfc..bc939624 100644 --- a/tests/Unit/Connectors/ConnectionFactoryTest.php +++ b/tests/Unit/Connectors/ConnectionFactoryTest.php @@ -5,7 +5,6 @@ use Grimzy\LaravelMysqlSpatial\Connectors\ConnectionFactory; use Grimzy\LaravelMysqlSpatial\MysqlConnection; use Grimzy\LaravelMysqlSpatial\Tests\Unit\BaseTestCase; -use Grimzy\LaravelMysqlSpatial\Tests\Unit\Stubs\PDOStub; use Illuminate\Container\Container; use Mockery; @@ -13,7 +12,7 @@ class ConnectionFactoryTest extends BaseTestCase { public function testMakeCallsCreateConnection() { - $pdo = new PDOStub(); + $pdo = $this->createMock(\PDO::class); $factory = Mockery::mock(ConnectionFactory::class, [new Container()])->makePartial(); $factory->shouldAllowMockingProtectedMethods(); @@ -24,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/TestPDO.php b/tests/Unit/Eloquent/TestPDO.php deleted file mode 100644 index a642b949..00000000 --- a/tests/Unit/Eloquent/TestPDO.php +++ /dev/null @@ -1,36 +0,0 @@ -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/Unit/MysqlConnectionTest.php b/tests/Unit/MysqlConnectionTest.php index f189eef8..3adc09e4 100644 --- a/tests/Unit/MysqlConnectionTest.php +++ b/tests/Unit/MysqlConnectionTest.php @@ -4,7 +4,6 @@ use Grimzy\LaravelMysqlSpatial\MysqlConnection; use Grimzy\LaravelMysqlSpatial\Schema\Builder; -use Grimzy\LaravelMysqlSpatial\Tests\Unit\Stubs\PDOStub; use PHPUnit\Framework\TestCase; class MysqlConnectionTest extends TestCase @@ -14,7 +13,7 @@ class MysqlConnectionTest extends TestCase protected function setUp(): void { $mysqlConfig = ['driver' => '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/Stubs/PDOStub.php b/tests/Unit/Stubs/PDOStub.php deleted file mode 100644 index 70a77eff..00000000 --- a/tests/Unit/Stubs/PDOStub.php +++ /dev/null @@ -1,10 +0,0 @@ -