Skip to content

Commit

Permalink
Issue #65 (#66)
Browse files Browse the repository at this point in the history
* Enhancement #65 implemented
* Php-Stan check activated
* Add delta because results are different between MySQL5.7 and MySQL8.0
  • Loading branch information
Alexandre-T authored May 11, 2024
1 parent 8178d05 commit 31be31f
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 30 deletions.
33 changes: 12 additions & 21 deletions .github/workflows/full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,18 @@ jobs:
run: ./quality/php-code-sniffer/vendor/bin/phpcs --standard=quality/php-code-sniffer/phpcs.xml -s

## PHP-STAN
# - name: Cache Composer PHP-STAN packages
# if: ${{ matrix.lexer-version == '~3.0.0' }}
# id: composer-php-stan-cache
# uses: actions/cache@v4
# with:
# path: quality/php-cs-fixer/vendor/
# key: ${{ runner.os }}-php-stan-${{ hashFiles('**/composer.lock') }}
# restore-keys: |
# ${{ runner.os }}-php-stan-
# - name: Install PHP-STAN
# if: ${{ matrix.lexer-version == '~3.0.0' }}
# run: composer update --working-dir=quality/php-stan
# - name: Run PHP-STAN
# if: ${{ matrix.lexer-version == '~3.0.0' }}
# run: ./quality/php-stan/vendor/bin/phpstan analyse --configuration=quality/php-stan/php-stan.neon lib tests --error-format=table --no-progress --no-interaction --no-ansi --level=9 --memory-limit=256M -v
- name: Cache Composer PHP-STAN packages
id: composer-php-stan-cache
uses: actions/cache@v4
with:
path: quality/php-cs-fixer/vendor/
key: ${{ runner.os }}-php-stan-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-stan-
- name: Install PHP-STAN
run: composer update --working-dir=quality/php-stan
- name: Run PHP-STAN
run: ./quality/php-stan/vendor/bin/phpstan analyse --configuration=quality/php-stan/php-stan.neon lib tests --error-format=table --no-progress --no-interaction --no-ansi --level=9 --memory-limit=256M -v

other-version-build:
needs: stable-build
Expand All @@ -189,9 +186,6 @@ jobs:
- php: '8.3'
orm: '^3.1'
name: PHP${{ matrix.php }} - doctrine/orm ${{ matrix.orm }}
env:
HAS_CC_SECRET: ${{secrets.CC_TEST_REPORTER_ID != '' }}
HAS_CA_SECRET: ${{secrets.COVERALLS_REPO_TOKEN != '' }}
services:
mysql5:
image: mysql:5.7
Expand Down Expand Up @@ -262,9 +256,6 @@ jobs:
- name: Copy test suites
run: cp .github/phpunit.*.xml .

- name: Install libraries
run: composer --prefer-stable update -vvv

- name: Force doctrine/orm library to ${{ matrix.orm }}
run: composer --prefer-stable require doctrine/orm:${{ matrix.orm }} --with-all-dependencies

Expand Down
2 changes: 2 additions & 0 deletions docs/Glossary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ If your application can be used with another database server than MySql, you sho
+----------------------------------------+-------------+----------+
| Sp_BufferStrategy | YES | Numeric |
+----------------------------------------+-------------+----------+
| Sp_Distance_Sphere | YES | Numeric |
+----------------------------------------+-------------+----------+
| Sp_GeometryType | YES | Numeric |
+----------------------------------------+-------------+----------+
| Sp_LineString | YES | Numeric |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* This file is part of the doctrine spatial extension.
*
* PHP 8.1 | 8.2 | 8.3
* Doctrine ORM 2.19 | 3.1
*
* Copyright Alexandre Tranchant <[email protected]> 2017-2024
* Copyright Longitude One 2020-2024
* Copyright 2015 Derek J. Lambert
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/

declare(strict_types=1);

namespace LongitudeOne\Spatial\ORM\Query\AST\Functions\MySql;

use Doctrine\DBAL\Platforms\MySQLPlatform;
use LongitudeOne\Spatial\ORM\Query\AST\Functions\AbstractSpatialDQLFunction;

/**
* ST_Distance_Sphere DQL function.
*
* Be careful, this function is not described in the ISO/IEC 13249.
* So this class is not in the Standard directory.
* With MySQL, its name's ST_Distance_Sphere.
* With PostGreSQL, its name's ST_DistanceSphere since PostGis 2.1.
* So these two functions cannot be merged in a class stored in the Common directory.
*
* @author Alexandre Tranchant <[email protected]>
*/
class SpDistanceSphere extends AbstractSpatialDQLFunction
{
/**
* Function SQL name getter.
*/
protected function getFunctionName(): string
{
return 'ST_Distance_Sphere';
}

/**
* Maximum number of parameter for the spatial function.
*
* @return int the inherited methods shall NOT return null, but 0 when function has no parameter
*/
protected function getMaxParameter(): int
{
return 2;
}

/**
* Minimum number of parameter for the spatial function.
*
* @since 2.0 This function replace the protected property minGeomExpr.
*
* @return int the inherited methods shall NOT return null, but 0 when function has no parameter
*/
protected function getMinParameter(): int
{
return 2;
}

/**
* Get the platforms accepted.
*
* @since 2.0 This function replace the protected property platforms.
* @since 5.0 This function returns the class-string[] instead of string[]
*
* @return class-string[] a non-empty array of accepted platforms
*/
protected function getPlatforms(): array
{
return [MySQLPlatform::class];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@
/**
* ST_Distance_Sphere DQL function.
*
* @author Derek J. Lambert <[email protected]>
* Be careful, this function is not described in the ISO/IEC 13249.
* So this class is not in the Standard directory.
* With MySQL, its name's ST_Distance_Sphere.
* With PostGreSQL, its name's ST_DistanceSphere since PostGis 2.1.
* So these two functions cannot be merged in a class stored in the Common directory.
*
* @author Alexandre Tranchant <[email protected]>
* @license https://dlambert.mit-license.org MIT
*/
class SpDistanceSphere extends AbstractSpatialDQLFunction
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* This file is part of the doctrine spatial extension.
*
* PHP 8.1 | 8.2 | 8.3
* Doctrine ORM 2.19 | 3.1
*
* Copyright Alexandre Tranchant <[email protected]> 2017-2024
* Copyright Longitude One 2020-2024
* Copyright 2015 Derek J. Lambert
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/

declare(strict_types=1);

/**
* This file is part of the doctrine spatial extension.
*
* PHP 8.1
*
* Copyright Alexandre Tranchant <[email protected]> 2017-2024
* Copyright Longitude One 2020-2024
* Copyright 2015 Derek J. Lambert
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace LongitudeOne\Spatial\ORM\Query\AST\Functions\Standard;

use LongitudeOne\Spatial\ORM\Query\AST\Functions\AbstractSpatialDQLFunction;

/**
* ST_Distance_Sphere DQL function.
*
* @author Derek J. Lambert <[email protected]>
* @author Alexandre Tranchant <[email protected]>
* @license https://dlambert.mit-license.org MIT
*/
class StDistanceSphere extends AbstractSpatialDQLFunction
{
/**
* Function SQL name getter.
*/
protected function getFunctionName(): string
{
return 'ST_DistanceSphere';
}

/**
* Maximum number of parameter for the spatial function.
*
* @return int the inherited methods shall NOT return null, but 0 when function has no parameter
*/
protected function getMaxParameter(): int
{
return 2;
}

/**
* Minimum number of parameter for the spatial function.
*
* @return int the inherited methods shall NOT return null, but 0 when function has no parameter
*/
protected function getMinParameter(): int
{
return 2;
}

/**
* Get the platforms accepted.
*
* @since 2.0 This function replace the protected property platforms.
*
* @return string[] a non-empty array of accepted platforms
*/
protected function getPlatforms(): array
{
return ['postgresql', 'mysql'];
}
}
10 changes: 5 additions & 5 deletions quality/php-stan/phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
parameters:
ignoreErrors:
-
message: "#^Class Doctrine\\\\DBAL\\\\Platforms\\\\MySqlPlatform not found\\.$#"
count: 1
path: ../../lib/LongitudeOne/Spatial/DBAL/Types/AbstractSpatialType.php

-
message: "#^Call to function is_object\\(\\) with array\\<float\\|int\\> will always evaluate to false\\.$#"
count: 1
Expand Down Expand Up @@ -40,6 +35,11 @@ parameters:
count: 1
path: ../../tests/LongitudeOne/Spatial/Tests/ORM/Query/AST/Functions/PostgreSql/SpTransformTest.php

-
message: "#^Instantiated class \\\\Doctrine\\\\DBAL\\\\Platforms\\\\SqlitePlatform not found\\.$#"
count: 1
path: ../../tests/LongitudeOne/Spatial/Tests/OrmMockTestCase.php

-
message: "#^Return type of call to method PHPUnit\\\\Framework\\\\MockObject\\\\MockBuilder\\<Doctrine\\\\DBAL\\\\Driver\\\\PDO\\\\SQLite\\\\Driver\\>\\:\\:getMock\\(\\) contains unresolvable type\\.$#"
count: 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* This file is part of the doctrine spatial extension.
*
* PHP 8.1 | 8.2 | 8.3
* Doctrine ORM 2.19 | 3.1
*
* Copyright Alexandre Tranchant <[email protected]> 2017-2024
* Copyright Longitude One 2020-2024
* Copyright 2015 Derek J. Lambert
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/

declare(strict_types=1);

/**
* This file is part of the doctrine spatial extension.
*
* PHP 8.1
*
* Copyright Alexandre Tranchant <[email protected]> 2017-2024
* Copyright Longitude One 2020-2024
* Copyright 2015 Derek J. Lambert
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace LongitudeOne\Spatial\Tests\ORM\Query\AST\Functions\MySql;

use Doctrine\DBAL\Platforms\MySQLPlatform;
use LongitudeOne\Spatial\Tests\Helper\PersistantPointHelperTrait;
use LongitudeOne\Spatial\Tests\PersistOrmTestCase;

/**
* ST_Distance_Sphere DQL function tests.
*
* @author Alexandre Tranchant <[email protected]>
*
* @group dql
*
* @internal
*
* @coversDefaultClass
*/
class SpDistanceSphereTest extends PersistOrmTestCase
{
use PersistantPointHelperTrait;

/**
* Set up the function type test.
*/
protected function setUp(): void
{
$this->usesEntity(self::POINT_ENTITY);
$this->supportsPlatform(MySQLPlatform::class);

parent::setUp();
}

/**
* Test a DQL containing function to test in the select.
*
* @group geometry
*/
public function testSelectStDistanceSphereGeometry(): void
{
$newYork = $this->persistNewYorkGeometry();
$losAngeles = $this->persistLosAngelesGeometry();
$dallas = $this->persistDallasGeometry();

$query = $this->getEntityManager()->createQuery(
// phpcs:disable Generic.Files.LineLength.MaxExceeded
'SELECT p, MySQL_DistanceSphere(p.point, ST_GeomFromText(:p)) FROM LongitudeOne\Spatial\Tests\Fixtures\PointEntity p'
// phpcs:enable
);

$query->setParameter('p', 'POINT(-89.4 43.066667)', 'string');

$result = $query->getResult();

static::assertIsArray($result);
static::assertCount(3, $result);
static::assertEquals($newYork, $result[0][0]);
// These numeric results are different from PostGis result
static::assertEqualsWithDelta(1305891.280669479, $result[0][1], 0.0000001);
static::assertEquals($losAngeles, $result[1][0]);
static::assertEqualsWithDelta(2684072.4889816786, $result[1][1], 0.0000001);
static::assertEquals($dallas, $result[2][0]);
static::assertEqualsWithDelta(1313749.9111938346, $result[2][1], 0.0000001);
}
}
4 changes: 2 additions & 2 deletions tests/LongitudeOne/Spatial/Tests/OrmMockTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ protected function getMockConnection(): Connection
}

// Doctrine ORM ^3.0
if (class_exists('Doctrine\DBAL\Platforms\SQLitePlatform')) {
$platformClass = 'Doctrine\DBAL\Platforms\SQLitePlatform';
if (class_exists('\Doctrine\DBAL\Platforms\SQLitePlatform')) {
$platformClass = '\Doctrine\DBAL\Platforms\SQLitePlatform';
}

if (null === $platformClass) {
Expand Down
2 changes: 2 additions & 0 deletions tests/LongitudeOne/Spatial/Tests/OrmTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
use LongitudeOne\Spatial\ORM\Query\AST\Functions\MySql\SpBuffer;
use LongitudeOne\Spatial\ORM\Query\AST\Functions\MySql\SpBufferStrategy;
use LongitudeOne\Spatial\ORM\Query\AST\Functions\MySql\SpDistance;
use LongitudeOne\Spatial\ORM\Query\AST\Functions\MySql\SpDistanceSphere as MySQLDistanceSphere;
use LongitudeOne\Spatial\ORM\Query\AST\Functions\MySql\SpGeometryType as MySqlGeometryType;
use LongitudeOne\Spatial\ORM\Query\AST\Functions\MySql\SpLineString;
use LongitudeOne\Spatial\ORM\Query\AST\Functions\MySql\SpMbrContains;
Expand Down Expand Up @@ -556,6 +557,7 @@ private function addSpecificMySqlFunctions(Configuration $configuration): void
$configuration->addCustomNumericFunction('Mysql_Distance', SpDistance::class);
$configuration->addCustomNumericFunction('Mysql_Buffer', SpBuffer::class);
$configuration->addCustomNumericFunction('Mysql_BufferStrategy', SpBufferStrategy::class);
$configuration->addCustomNumericFunction('Mysql_DistanceSphere', MySQLDistanceSphere::class);
$configuration->addCustomNumericFunction('Mysql_GeometryType', MySqlGeometryType::class);
$configuration->addCustomNumericFunction('Mysql_LineString', SpLineString::class);
$configuration->addCustomNumericFunction('Mysql_MBRContains', SpMbrContains::class);
Expand Down

0 comments on commit 31be31f

Please sign in to comment.