Skip to content

Commit

Permalink
Allow tests to list supported engines
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Dec 29, 2020
1 parent 68a1594 commit 5e97e32
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
56 changes: 54 additions & 2 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Brick\Geo\PolyhedralSurface;
use Brick\Geo\TIN;
use Brick\Geo\Triangle;
use LogicException;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -86,7 +87,7 @@ final protected function isSpatiaLite(?string $operatorAndVersion = null) : bool
return true;
}

$version = $engine->getSQLite3()->querySingle('SELECT spatialite_version()');
$version = $engine->getSQLite3()->querySingle('SELECT spatialite_version()');

return $this->isVersion($version, $operatorAndVersion);
}
Expand Down Expand Up @@ -121,6 +122,54 @@ final protected function isGEOS(?string $operatorAndVersion = null) : bool
return false;
}

/**
* Skips the test if the current geometry engine does not match the requirements.
*
* Example: ['MySQL', 'MariaDB', 'PostGIS']
*
* Supported engines:
*
* - MySQL
* - MariaDB
* - SpatiaLite
* - PostGIS
* - GEOS
*
* @param string[] $supportedEngines
*
* @return void
*/
final protected function requireEngine(array $supportedEngines): void
{
$diff = array_values(array_diff($supportedEngines, ['MySQL', 'MariaDB', 'SpatiaLite', 'PostGIS', 'GEOS']));

if ($diff) {
throw new LogicException("Unsupported engine: {$diff[0]}");
}

if (in_array('MySQL', $supportedEngines) && $this->isMySQL()) {
return;
}

if (in_array('MariaDB', $supportedEngines) && $this->isMariaDB()) {
return;
}

if (in_array('SpatiaLite', $supportedEngines) && $this->isSpatiaLite()) {
return;
}

if (in_array('PostGIS', $supportedEngines) && $this->isPostGIS()) {
return;
}

if (in_array('GEOS', $supportedEngines) && $this->isGEOS()) {
return;
}

self::markTestSkipped('Not supported on this geometry engine.');
}

/**
* @param Geometry $geometry
*
Expand Down Expand Up @@ -594,7 +643,10 @@ final protected function castToFloat(array & $coords) : void
*/
private function isVersion(string $version, string $operatorAndVersion) : bool
{
preg_match('/^([\<\>]?\=?) ?(.*)/', $operatorAndVersion, $matches);
if (preg_match('/^([\<\>]?\=?) ?(.*)/', $operatorAndVersion, $matches) !== 1) {
throw new LogicException("Invalid operator and version: $operatorAndVersion");
}

[, $operator, $testVersion] = $matches;

if ($operator === '') {
Expand Down
15 changes: 6 additions & 9 deletions tests/GeometryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -556,12 +556,9 @@ public function providerBoundary() : array
*
* @return void
*/
public function testCentroid(string $geometry, float $centroidX, float $centroidY, bool $postgisOnly) : void
public function testCentroid(string $geometry, float $centroidX, float $centroidY, array $supportedEngines) : void
{
if ($postgisOnly && !$this->isPostGIS()) {
$this->expectNotToPerformAssertions();
return;
}
$this->requireEngine($supportedEngines);

$geometry = Geometry::fromText($geometry);

Expand All @@ -577,10 +574,10 @@ public function testCentroid(string $geometry, float $centroidX, float $centroid
public function providerCentroid() : array
{
return [
['POINT (42 42)', 42.0, 42.0, true],
['MULTIPOINT (0 0, 1 1)', 0.5, 0.5, true],
['CIRCULARSTRING (1 1, 2 0, -1 1)', 0.0, -1.373, true],
['POLYGON ((0 1, 1 0, 0 -1, -1 0, 0 1))', 0.0, 0.0, false],
['POINT (42 42)', 42.0, 42.0, ['MySQL', 'SpatiaLite', 'PostGIS', 'GEOS']],
['MULTIPOINT (0 0, 1 1)', 0.5, 0.5, ['MySQL', 'SpatiaLite', 'PostGIS', 'GEOS']],
['CIRCULARSTRING (1 1, 2 0, -1 1)', 0.0, -1.373, ['PostGIS']],
['POLYGON ((0 1, 1 0, 0 -1, -1 0, 0 1))', 0.0, 0.0, ['MySQL', 'MariaDB', 'SpatiaLite', 'PostGIS', 'GEOS']],
];
}

Expand Down

0 comments on commit 5e97e32

Please sign in to comment.