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

9-a-lot-of-geometries-arent-implemented-in-parser #19

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/LongitudeOne/Geo/WKT/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ protected function coordinates(int $count): array
*/
protected function geometry(): array
{
$type = $this->type();
try {
$type = $this->type();
} catch (UnexpectedValueException $e) {
throw new NotExistentException((string) $this->lexer->lookahead?->value, $e->getCode(), $e);
}

if ($this->lexer->isNextTokenAny([Lexer::T_Z, Lexer::T_M, Lexer::T_ZM])) {
$this->match($this->lexer->lookahead->type);
Expand Down Expand Up @@ -230,6 +234,8 @@ protected function lineString(): array

/**
* Match token at current position in input.
*
* @throw UnexpectedValueException
*/
protected function match(int $token): void
{
Expand Down
11 changes: 1 addition & 10 deletions tests/LongitudeOne/Geo/WKT/Tests/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use LongitudeOne\Geo\WKT\Exception\NotExistentException;
use LongitudeOne\Geo\WKT\Exception\NotInstantiableException;
use LongitudeOne\Geo\WKT\Exception\NotYetImplementedException;
use LongitudeOne\Geo\WKT\Exception\UnexpectedValueException;
use LongitudeOne\Geo\WKT\Parser;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -60,7 +59,7 @@ public function testNotExistentException(): void
$this->expectException(NotExistentException::class);
$this->expectExceptionMessage('According the ISO 13249-3:2016 standard, the "FOO" type does not exist.');

throw new NotExistentException('FOO');
(new Parser('FOO(42 42)'))->parse();
}

#[DataProvider('notInstantiableTypes')]
Expand All @@ -84,12 +83,4 @@ public function testNotYetImplemented(string $notYetImplemented, string $expecte

(new Parser($toParse))->parse();
}

public function testUnExpectedValue(): void
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('[Syntax Error] line 0, col 0: Error: Expected LongitudeOne\Geo\WKT\Lexer::T_TYPE, got "FOO" in value "FOO(42 42)"');

(new Parser('FOO(42 42)'))->parse();
}
}
23 changes: 20 additions & 3 deletions tests/LongitudeOne/Geo/WKT/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace LongitudeOne\Geo\WKT\Tests;

use LongitudeOne\Geo\WKT\Exception\NotExistentException;
use LongitudeOne\Geo\WKT\Exception\NotInstantiableException;
use LongitudeOne\Geo\WKT\Exception\UnexpectedValueException;
use LongitudeOne\Geo\WKT\Parser;
Expand Down Expand Up @@ -113,6 +114,16 @@ public static function multiPolygonProvider(): \Generator
yield 'testParsingMultiPolygonZMValueWithSrid' => ['SRID=4326;MULTIPOLYGONZM(((0 0 0 1,10 0 0 1,10 10 0 1,0 10 0 1,0 0 0 1),(5 5 1 2,7 5 1 2,7 7 1 2,5 7 1 2,5 5 1 2)),((1 1 0 3, 3 1 0 3, 3 3 0 3, 1 3 0 3, 1 1 0 3)))', 4326, [[[[0, 0, 0, 1], [10, 0, 0, 1], [10, 10, 0, 1], [0, 10, 0, 1], [0, 0, 0, 1]], [[5, 5, 1, 2], [7, 5, 1, 2], [7, 7, 1, 2], [5, 7, 1, 2], [5, 5, 1, 2]]], [[[1, 1, 0, 3], [3, 1, 0, 3], [3, 3, 0, 3], [1, 3, 0, 3], [1, 1, 0, 3]]]], 'ZM'];
}

/**
* @return \Generator<string, array{0: string, 1: string}, null, void>
*/
public static function notExistentValuesProvider(): \Generator
{
yield 'testParsingGarbage' => ['@#_$%', 'According the ISO 13249-3:2016 standard, the "@" type does not exist.'];
yield 'testParsingBadType' => ['PNT(10 10)', 'According the ISO 13249-3:2016 standard, the "PNT" type does not exist.'];
yield 'testParsingGeometryCollectionValueWithBadType' => ['GEOMETRYCOLLECTION(PNT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))', 'According the ISO 13249-3:2016 standard, the "PNT" type does not exist.'];
}

/**
* @return \Generator<string, array{0: string, 1:string}, null, void>
*/
Expand Down Expand Up @@ -165,8 +176,6 @@ public static function polygonProvider(): \Generator
*/
public static function unexpectedValues(): \Generator
{
yield 'testParsingGarbage' => ['@#_$%', '[Syntax Error] line 0, col 0: Error: Expected LongitudeOne\Geo\WKT\Lexer::T_TYPE, got "@" in value "@#_$%"'];
yield 'testParsingBadType' => ['PNT(10 10)', '[Syntax Error] line 0, col 0: Error: Expected LongitudeOne\Geo\WKT\Lexer::T_TYPE, got "PNT" in value "PNT(10 10)"'];
yield 'testParsingPointValueWithBadSrid' => ['SRID=432.6;POINT(34.23 -87)', '[Syntax Error] line 0, col 5: Error: Expected LongitudeOne\Geo\WKT\Lexer::T_INTEGER, got "432.6" in value "SRID=432.6;POINT(34.23 -87)"'];
yield 'testParsingPointValueMissingCoordinate' => ['POINT(34.23)', '[Syntax Error] line 0, col 11: Error: Expected LongitudeOne\Geo\WKT\Lexer::T_INTEGER, got ")" in value "POINT(34.23)"'];
yield 'testParsingPointMValueMissingCoordinate' => ['POINTM(34.23 10)', '[Syntax Error] line 0, col 15: Error: Expected LongitudeOne\Geo\WKT\Lexer::T_INTEGER, got ")" in value "POINTM(34.23 10)"'];
Expand All @@ -183,7 +192,6 @@ public static function unexpectedValues(): \Generator
yield 'testParsingPolygonValueMultiRingMissingComma' => ['POLYGON((0 0,10 0,10 10,0 10,0 0)(5 5,7 5,7 7,5 7,5 5))', '[Syntax Error] line 0, col 33: Error: Expected LongitudeOne\Geo\WKT\Lexer::T_CLOSE_PARENTHESIS, got "(" in value "POLYGON((0 0,10 0,10 10,0 10,0 0)(5 5,7 5,7 7,5 7,5 5))"'];
yield 'testParsingMultiLineStringValueMissingComma' => ['MULTILINESTRING((0 0,10 0,10 10,0 10)(5 5,7 5,7 7,5 7))', '[Syntax Error] line 0, col 37: Error: Expected LongitudeOne\Geo\WKT\Lexer::T_CLOSE_PARENTHESIS, got "(" in value "MULTILINESTRING((0 0,10 0,10 10,0 10)(5 5,7 5,7 7,5 7))"'];
yield 'testParsingMultiPolygonValueMissingParenthesis' => ['MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7,5 5)),(1 1, 3 1, 3 3, 1 3, 1 1))', '[Syntax Error] line 0, col 64: Error: Expected LongitudeOne\Geo\WKT\Lexer::T_OPEN_PARENTHESIS, got "1" in value "MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7,5 5)),(1 1, 3 1, 3 3, 1 3, 1 1))"'];
yield 'testParsingGeometryCollectionValueWithBadType' => ['GEOMETRYCOLLECTION(PNT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))', '[Syntax Error] line 0, col 19: Error: Expected LongitudeOne\Geo\WKT\Lexer::T_TYPE, got "PNT" in value "GEOMETRYCOLLECTION(PNT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))"'];
yield 'testParsingGeometryCollectionValueWithMismatchedDimension' => ['GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30 10), LINESTRING(15 15, 20 20))', '[Syntax Error] line 0, col 45: Error: Expected LongitudeOne\Geo\WKT\Lexer::T_CLOSE_PARENTHESIS, got "10" in value "GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30 10), LINESTRING(15 15, 20 20))"'];
}

Expand Down Expand Up @@ -286,6 +294,15 @@ public function testParserWithUnexpectedValues(string $value, string $exceptionM
$parser->parse();
}

#[DataProvider('notExistentValuesProvider')]
public function testParsingGarbage(string $garbage, string $message): void
{
$parser = new Parser($garbage);
self::expectException(NotExistentException::class);
self::expectExceptionMessage($message);
$parser->parse();
}

/**
* @param array<int|float> $coordinates
*/
Expand Down
Loading