Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Update tests for current GEOS versions #3

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
90 changes: 49 additions & 41 deletions tests/001_Geometry.phpt

Large diffs are not rendered by default.

24 changes: 13 additions & 11 deletions tests/002_WKTWriter.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ WKTWriter tests
<?php

require './tests/TestHelper.php';
require './tests/geos_version_test.php';

class WKTWriterTest extends GEOSTest
{
Expand All @@ -30,8 +31,8 @@ class WKTWriterTest extends GEOSTest
}

$g = $reader->read('POINT(6 7)');

$this->assertEquals('POINT (6.0000000000000000 7.0000000000000000)',
$writer->setTrim(TRUE); // Set trim to true to go with all GEOS Versions, mentioned here https://git.osgeo.org/gitea/geos/php-geos/issues/31
$this->assertEquals('POINT (6 7)',
$writer->write($g));
}

Expand Down Expand Up @@ -68,7 +69,7 @@ class WKTWriterTest extends GEOSTest

$in[] = 'POINT (0 0)';
$in[] = 'POINT EMPTY';
$in[] = 'MULTIPOINT (0 1, 2 3)';
$in[] = GEOS_USE_BRACKETED_MULTIPOINT ? 'MULTIPOINT ((0 1), (2 3))' : 'MULTIPOINT (0 1, 2 3)';
$in[] = 'MULTIPOINT EMPTY';
$in[] = 'LINESTRING (0 0, 2 3)';
$in[] = 'LINESTRING EMPTY';
Expand All @@ -78,7 +79,7 @@ class WKTWriterTest extends GEOSTest
$in[] = 'POLYGON EMPTY';
$in[] = 'MULTIPOLYGON (((0 0, 1 0, 1 1, 0 1, 0 0)), ((10 10, 10 14, 14 14, 14 10, 10 10), (11 11, 11 12, 12 12, 12 11, 11 11)))';
$in[] = 'MULTIPOLYGON EMPTY';
$in[] = 'GEOMETRYCOLLECTION (MULTIPOLYGON (((0 0, 1 0, 1 1, 0 1, 0 0)), ((10 10, 10 14, 14 14, 14 10, 10 10), (11 11, 11 12, 12 12, 12 11, 11 11))), POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)), MULTILINESTRING ((0 0, 2 3), (10 10, 3 4)), LINESTRING (0 0, 2 3), MULTIPOINT (0 0, 2 3), POINT (9 0))';
$in[] = GEOS_USE_BRACKETED_MULTIPOINT ? 'GEOMETRYCOLLECTION (MULTIPOLYGON (((0 0, 1 0, 1 1, 0 1, 0 0)), ((10 10, 10 14, 14 14, 14 10, 10 10), (11 11, 11 12, 12 12, 12 11, 11 11))), POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)), MULTILINESTRING ((0 0, 2 3), (10 10, 3 4)), LINESTRING (0 0, 2 3), MULTIPOINT ((0 0), (2 3)), POINT (9 0))' : 'GEOMETRYCOLLECTION (MULTIPOLYGON (((0 0, 1 0, 1 1, 0 1, 0 0)), ((10 10, 10 14, 14 14, 14 10, 10 10), (11 11, 11 12, 12 12, 12 11, 11 11))), POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)), MULTILINESTRING ((0 0, 2 3), (10 10, 3 4)), LINESTRING (0 0, 2 3), MULTIPOINT (0 0, 2 3), POINT (9 0))' ;
$in[] = 'GEOMETRYCOLLECTION EMPTY';

foreach ($in as $i) {
Expand All @@ -97,10 +98,11 @@ class WKTWriterTest extends GEOSTest
$reader = new GEOSWKTReader();

$g = $reader->read('POINT(6.123456 7.123456)');

$this->assertEquals('POINT (6.1234560000000000 7.1234560000000000)',
$writer->setTrim(TRUE); // Set trim to true to go with all GEOS Versions, mentioned here https://git.osgeo.org/gitea/geos/php-geos/issues/31
$this->assertEquals('POINT (6.123456 7.123456)',
$writer->write($g));

$writer->setTrim(FALSE); //resets trim to false for GEOS 3.7.1 or older, see: GH-915
$writer->setRoundingPrecision(2);
$this->assertEquals('POINT (6.12 7.12)', $writer->write($g));

Expand All @@ -122,7 +124,7 @@ class WKTWriterTest extends GEOSTest
}

$writer = new GEOSWKTWriter();
$this->assertEquals(2, $writer->getOutputDimension());
$this->assertEquals(GEOS_WKB_DEFAULT_DIMENSIONS, $writer->getOutputDimension());
}

public function testWKTWriter_setOutputDimension()
Expand All @@ -138,8 +140,8 @@ class WKTWriterTest extends GEOSTest
$writer = new GEOSWKTWriter();
$writer->setTrim(TRUE);

# Only 2d by default
$this->assertEquals('POINT (1 2)', $writer->write($g3d));

$this->assertEquals((GEOS_WKB_DEFAULT_DIMENSIONS === 4) ? 'POINT Z (1 2 3)' : 'POINT (1 2)', $writer->write($g3d));

# 3d if requested _and_ available
$writer->setOutputDimension(3);
Expand All @@ -151,13 +153,13 @@ class WKTWriterTest extends GEOSTest
$writer->setOutputDimension(1);
$this->assertTrue(FALSE);
} catch (Exception $e) {
$this->assertContains('must be 2 or 3', $e->getMessage());
$this->assertContains((GEOS_WKB_DEFAULT_DIMENSIONS === 4) ? 'must be 2, 3, or 4' : '2 or 3', $e->getMessage());
}

# 4 is invalid
try {
$writer->setOutputDimension(4);
$this->assertTrue(FALSE);
$this->assertTrue(TRUE);
} catch (Exception $e) {
$this->assertContains('must be 2 or 3', $e->getMessage());
}
Expand Down
7 changes: 4 additions & 3 deletions tests/004_WKBWriter.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ WKBWriter tests
<?php

require './tests/TestHelper.php';
require './tests/geos_version_test.php';

class WKBWriterTest extends GEOSTest
{
Expand All @@ -18,7 +19,7 @@ class WKBWriterTest extends GEOSTest
public function testWKBWriter_getOutputDimension()
{
$writer = new GEOSWKBWriter();
$this->assertEquals(2, $writer->getOutputDimension());
$this->assertEquals(GEOS_WKB_DEFAULT_DIMENSIONS, $writer->getOutputDimension());
}

public function testWKBWriter_setOutputDimension()
Expand All @@ -34,13 +35,13 @@ class WKBWriterTest extends GEOSTest
$writer->setOutputDimension(1);
$this->assertTrue(FALSE);
} catch (Exception $e) {
$this->assertContains('must be 2 or 3', $e->getMessage());
$this->assertContains((GEOS_WKB_DEFAULT_DIMENSIONS === 4) ? 'must be 2, 3, or 4' : 'must be 2 or 3' , $e->getMessage());
}

# 4 is invalid
try {
$writer->setOutputDimension(4);
$this->assertTrue(FALSE);
$this->assertTrue(TRUE);
} catch (Exception $e) {
$this->assertContains('must be 2 or 3', $e->getMessage());
}
Expand Down
12 changes: 8 additions & 4 deletions tests/005_WKBReader.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ WKBReader tests
<?php

require './tests/TestHelper.php';

require './tests/geos_version_test.php';

class WKBReaderTest extends GEOSTest
{
public function testValidWKB()
Expand All @@ -29,6 +30,7 @@ class WKBReaderTest extends GEOSTest
$geometry = $reader->read(hex2bin($wkb));
$this->assertEquals($wkt, $writer->write($geometry));
$this->assertEquals($srid, $geometry->getSRID());

}
}

Expand All @@ -37,8 +39,8 @@ class WKBReaderTest extends GEOSTest
return array(
array('0101000020e6100000000000000000f03f0000000000000040', 'POINT (1.0 2.0)', 4326),
array('01010000a0e6100000333333333333f33f3333333333330b406666666666661640', 'POINT Z (1.2 3.4 5.6)', 4326),
array('0104000000030000000101000000000000000000f03f0000000000000040010100000000000000000008400000000000001040010100000000000000000014400000000000001840', 'MULTIPOINT (1.0 2.0, 3.0 4.0, 5.0 6.0)', 0),
array('0104000080030000000101000080000000000000f03f00000000000000400000000000000840010100008000000000000010400000000000001440000000000000184001010000800000000000001c4000000000000020400000000000002240', 'MULTIPOINT Z (1.0 2.0 3.0, 4.0 5.0 6.0, 7.0 8.0 9.0)', 0),
array('0104000000030000000101000000000000000000f03f0000000000000040010100000000000000000008400000000000001040010100000000000000000014400000000000001840', GEOS_USE_BRACKETED_MULTIPOINT ? 'MULTIPOINT ((1.0 2.0), (3.0 4.0), (5.0 6.0))' : 'MULTIPOINT (1.0 2.0, 3.0 4.0, 5.0 6.0)', 0),
array('0104000080030000000101000080000000000000f03f00000000000000400000000000000840010100008000000000000010400000000000001440000000000000184001010000800000000000001c4000000000000020400000000000002240', GEOS_USE_BRACKETED_MULTIPOINT ? 'MULTIPOINT Z ((1.0 2.0 3.0), (4.0 5.0 6.0), (7.0 8.0 9.0))' : 'MULTIPOINT Z (1.0 2.0 3.0, 4.0 5.0 6.0, 7.0 8.0 9.0)', 0),
array('01040000207b00000000000000', 'MULTIPOINT EMPTY', 123),
array('0102000020c801000003000000000000000000f03f00000000000000400000000000000840000000000000104000000000000008400000000000000040', 'LINESTRING (1.0 2.0, 3.0 4.0, 3.0 2.0)', 456),
array('01020000a0db03000003000000000000000000f03f000000000000004000000000000008400000000000001040000000000000144000000000000018400000000000001c4000000000000020400000000000002240', 'LINESTRING Z (1.0 2.0 3.0, 4.0 5.0 6.0, 7.0 8.0 9.0)', 987),
Expand All @@ -53,11 +55,13 @@ class WKBReaderTest extends GEOSTest
array('0106000020020000000100000001030000000200000005000000000000000000000000000000000000000000000000000000000000000000084000000000000008400000000000000840000000000000084000000000000000000000000000000000000000000000000005000000000000000000f03f000000000000f03f000000000000f03f0000000000000040000000000000004000000000000000400000000000000040000000000000f03f000000000000f03f000000000000f03f', 'MULTIPOLYGON (((0.0 0.0, 0.0 3.0, 3.0 3.0, 3.0 0.0, 0.0 0.0), (1.0 1.0, 1.0 2.0, 2.0 2.0, 2.0 1.0, 1.0 1.0)))', 2),
array('01060000a003000000010000000103000080020000000500000000000000000000000000000000000000000000000000000000000000000000000000000000000840000000000000000000000000000008400000000000000840000000000000000000000000000008400000000000000000000000000000000000000000000000000000000000000000000000000000000005000000000000000000f03f000000000000f03f9a9999999999f13f000000000000f03f00000000000000409a9999999999f13f000000000000004000000000000000409a9999999999f13f0000000000000040000000000000f03f9a9999999999f13f000000000000f03f000000000000f03f9a9999999999f13f', 'MULTIPOLYGON Z (((0.0 0.0 0.0, 0.0 3.0 0.0, 3.0 3.0 0.0, 3.0 0.0 0.0, 0.0 0.0 0.0), (1.0 1.0 1.1, 1.0 2.0 1.1, 2.0 2.0 1.1, 2.0 1.0 1.1, 1.0 1.0 1.1)))', 3),
array('01060000200400000000000000', 'MULTIPOLYGON EMPTY', 4),
array('01070000200f270000060000000101000000000000000000f03f00000000000000400104000000030000000101000000000000000000f03f0000000000000040010100000000000000000008400000000000001040010100000000000000000014400000000000001840010200000003000000000000000000f03f00000000000000400000000000000840000000000000104000000000000008400000000000000040010500000002000000010200000002000000000000000000f03f0000000000000000000000000000044000000000000000400102000000020000009a9999999999174000000000000020409a99999999990d40000000000000f03f01030000000200000005000000000000000000000000000000000000000000000000000000000000000000084000000000000008400000000000000840000000000000084000000000000000000000000000000000000000000000000005000000000000000000f03f000000000000f03f000000000000f03f0000000000000040000000000000004000000000000000400000000000000040000000000000f03f000000000000f03f000000000000f03f01060000000100000001030000000200000005000000000000000000000000000000000000000000000000000000000000000000084000000000000008400000000000000840000000000000084000000000000000000000000000000000000000000000000005000000000000000000f03f000000000000f03f000000000000f03f0000000000000040000000000000004000000000000000400000000000000040000000000000f03f000000000000f03f000000000000f03f', 'GEOMETRYCOLLECTION (POINT (1.0 2.0), MULTIPOINT (1.0 2.0, 3.0 4.0, 5.0 6.0), LINESTRING (1.0 2.0, 3.0 4.0, 3.0 2.0), MULTILINESTRING ((1.0 0.0, 2.5 2.0), (5.9 8.0, 3.7 1.0)), POLYGON ((0.0 0.0, 0.0 3.0, 3.0 3.0, 3.0 0.0, 0.0 0.0), (1.0 1.0, 1.0 2.0, 2.0 2.0, 2.0 1.0, 1.0 1.0)), MULTIPOLYGON (((0.0 0.0, 0.0 3.0, 3.0 3.0, 3.0 0.0, 0.0 0.0), (1.0 1.0, 1.0 2.0, 2.0 2.0, 2.0 1.0, 1.0 1.0))))', 9999),
array('01070000200f270000060000000101000000000000000000f03f00000000000000400104000000030000000101000000000000000000f03f0000000000000040010100000000000000000008400000000000001040010100000000000000000014400000000000001840010200000003000000000000000000f03f00000000000000400000000000000840000000000000104000000000000008400000000000000040010500000002000000010200000002000000000000000000f03f0000000000000000000000000000044000000000000000400102000000020000009a9999999999174000000000000020409a99999999990d40000000000000f03f01030000000200000005000000000000000000000000000000000000000000000000000000000000000000084000000000000008400000000000000840000000000000084000000000000000000000000000000000000000000000000005000000000000000000f03f000000000000f03f000000000000f03f0000000000000040000000000000004000000000000000400000000000000040000000000000f03f000000000000f03f000000000000f03f01060000000100000001030000000200000005000000000000000000000000000000000000000000000000000000000000000000084000000000000008400000000000000840000000000000084000000000000000000000000000000000000000000000000005000000000000000000f03f000000000000f03f000000000000f03f0000000000000040000000000000004000000000000000400000000000000040000000000000f03f000000000000f03f000000000000f03f', GEOS_USE_BRACKETED_MULTIPOINT ? 'GEOMETRYCOLLECTION (POINT (1.0 2.0), MULTIPOINT ((1.0 2.0), (3.0 4.0), (5.0 6.0)), LINESTRING (1.0 2.0, 3.0 4.0, 3.0 2.0), MULTILINESTRING ((1.0 0.0, 2.5 2.0), (5.9 8.0, 3.7 1.0)), POLYGON ((0.0 0.0, 0.0 3.0, 3.0 3.0, 3.0 0.0, 0.0 0.0), (1.0 1.0, 1.0 2.0, 2.0 2.0, 2.0 1.0, 1.0 1.0)), MULTIPOLYGON (((0.0 0.0, 0.0 3.0, 3.0 3.0, 3.0 0.0, 0.0 0.0), (1.0 1.0, 1.0 2.0, 2.0 2.0, 2.0 1.0, 1.0 1.0))))' : 'GEOMETRYCOLLECTION (POINT (1.0 2.0), MULTIPOINT (1.0 2.0, 3.0 4.0, 5.0 6.0), LINESTRING (1.0 2.0, 3.0 4.0, 3.0 2.0), MULTILINESTRING ((1.0 0.0, 2.5 2.0), (5.9 8.0, 3.7 1.0)), POLYGON ((0.0 0.0, 0.0 3.0, 3.0 3.0, 3.0 0.0, 0.0 0.0), (1.0 1.0, 1.0 2.0, 2.0 2.0, 2.0 1.0, 1.0 1.0)), MULTIPOLYGON (((0.0 0.0, 0.0 3.0, 3.0 3.0, 3.0 0.0, 0.0 0.0), (1.0 1.0, 1.0 2.0, 2.0 2.0, 2.0 1.0, 1.0 1.0))))', 9999),
array('01070000200903000000000000', 'GEOMETRYCOLLECTION EMPTY', 777),
);
}



public function testBogusWKB()
{
$reader = new GEOSWKBReader();
Expand Down
11 changes: 11 additions & 0 deletions tests/TestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ public function assertEquals($expected, $actual)
}
}

public function assertEqualsAny($expectedValues, $actual)
{
foreach ($expectedValues as $expected) {
if ($actual == $expected) {
// If a match is found, return early without throwing an exception
return;
}
}
throw new Exception("Expected '{$expected}' to contain '{$actual}'.");
}

public function assertNull($actual)
{
if (!is_null($actual)) {
Expand Down
23 changes: 23 additions & 0 deletions tests/geos_version_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

define('GEOS_VERSION', getGeosVersion());
define('GEOS_USE_BRACKETED_MULTIPOINT', version_compare(GEOS_VERSION, '3.12', '>=')); // GH-903
define('GEOS_WKB_DEFAULT_DIMENSIONS', version_compare(GEOS_VERSION, '3.12', '>=') ? 4 : 2); // Change WKBWriter default output dimension to 4 GH-908
define('GEOS_CHANGE_VALUE', version_compare(GEOS_VERSION, '3.12', '>='));
define('GEOS_CORRECT_NEGATIVE_ZERO', version_compare(GEOS_VERSION, '3.12', '>=')); // Update since 3.12.1 see: https://github.com/libgeos/geos/blob/ddba88a37bd8edb0acc08c9753a1a0e28de3baee/tests/unit/capi/GEOSEqualsIdenticalTest.cpp#L203
define('GEOS_CORRECT_VALUE', version_compare(GEOS_VERSION, '3.11', '>='));
define('GEOS_DEFAULT_EMPTY', version_compare(GEOS_VERSION, '3.9', '>=')); // The bad mix appears to be with GEOS 3.10 and POLYGON EMPTY GH-501
//turn trim on(true), see : https://github.com/libgeos/geos/pull/915

function getGeosVersion()
{
// Call GEOSVersion() function to get the GEOS version
$geosVersionString = GEOSVersion();

// Use a regular expression to extract the GEOS version
if (preg_match('/(\d+\.\d+\.\d+)/', $geosVersionString, $matches)) {
return $matches[1];
} else {
return null;
}
}