diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d3510a..bc5d4a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,24 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Added ### Changed +## [2.1.0] - 2016-04-09 +### Added +- Add tokens for 3DM, 3DZ, and 4D coordinates to Lexer. +- Method Lexer::value() to get current token value. +- Match Z, M, or ZM tokens in Parser (not currently used). +- Add test for Parser reuse. +- Add test for Lexer reuse. + +### Changed +- Regex used to catch tokens. +- Allow Lexer instantiation with no parameters so object can be reused. +- Allow Parser instantiation with no parameters so object can be reused. +- Changed visibility of Parser::$type. +- Use Lexer::value() in Parser. +- Set srid to null before parsing value. +- Consolidated all Parser test data into ParserTest. +- Update usage documentation in README.md. + ## [2.0.0] - 2015-11-18 ### Added - Change base namespace to CrEOF\Geo\WKT to avoid class collision with other CrEOF packages. diff --git a/README.md b/README.md index caf6416..628a709 100644 --- a/README.md +++ b/README.md @@ -8,17 +8,36 @@ Lexer and parser library for WKT/EWKT spatial object strings. ## Usage -Pass value to be parsed in the constructor, then call parse() on the created object. +There are two use patterns for the parser. The value to be parsed can be passed into the constructor, then parse() +called on the returned ```Parser``` object: ```php $input = 'POLYGON((0 0,10 0,10 10,0 10,0 0))'; + $parser = new Parser($input); -$value = $parser->parse(); + +$value = $parser->parse(); +``` + +If many values need to be parsed, a single ```Parser``` instance can be used: + +```php +$input1 = 'POLYGON((0 0,10 0,10 10,0 10,0 0))'; +$input2 = 'POINT(0,0)'; + +$parser = new Parser(); + +$value1 = $parser->parse($input1); +$value2 = $parser->parse($input2); ``` ## Return The parser will return an array with the keys ```srid```, ```type```, and ```value```. -- ```srid``` is the SRID if EWKT was passed in the constructor, null otherwise. -- ```type``` is the spatial object type. -- ```value``` will contain an array with 2 numeric values, or nested arrays containing these depending on the spatial object type. +- ```type``` is the spatial object type (POINT, LINESTRING, etc.) +- ```value``` will contain an array with integer or float values for points, or nested arrays containing these based on spatial object type. +- ```srid``` is the SRID if EWKT value was parsed, null otherwise. + +## Exceptions + +The ```Lexer``` and ```Parser``` will throw expections implementing interface ```CrEOF\Geo\WKT\Exception\ExceptionInterface```. diff --git a/composer.json b/composer.json index a06164c..842d31e 100644 --- a/composer.json +++ b/composer.json @@ -13,10 +13,10 @@ "require": { "php": ">=5.3.3", "ext-SPL": "*", - "doctrine/lexer": ">=1.0" + "doctrine/lexer": "~1.0" }, "require-dev": { - "phpunit/phpunit": ">=4.8", + "phpunit/phpunit": "<5.0", "codeclimate/php-test-reporter": "dev-master" }, "autoload": { diff --git a/lib/CrEOF/Geo/WKT/Lexer.php b/lib/CrEOF/Geo/WKT/Lexer.php index ef69213..a418752 100644 --- a/lib/CrEOF/Geo/WKT/Lexer.php +++ b/lib/CrEOF/Geo/WKT/Lexer.php @@ -46,23 +46,46 @@ class Lexer extends AbstractLexer const T_MINUS = 14; const T_SEMICOLON = 50; const T_SRID = 500; + const T_ZM = 501; + const T_Z = 502; + const T_M = 503; // Geometry types > 600 - const T_TYPE = 600; - const T_POINT = 601; - const T_LINESTRING = 602; - const T_POLYGON = 603; - const T_MULTIPOINT = 604; - const T_MULTILINESTRING = 605; - const T_MULTIPOLYGON = 606; - const T_GEOMETRYCOLLECTION = 607; + const T_TYPE = 600; + const T_POINT = 601; + const T_LINESTRING = 602; + const T_POLYGON = 603; + const T_MULTIPOINT = 604; + const T_MULTILINESTRING = 605; + const T_MULTIPOLYGON = 606; + const T_GEOMETRYCOLLECTION = 607; + const T_CIRCULARSTRING = 608; + const T_COMPOUNDCURVE = 609; + const T_CURVEPOLYGON = 610; + const T_MULTICURVE = 611; + const T_MULTISURFACE = 612; + const T_CURVE = 613; + const T_SURFACE = 614; + const T_POLYHEDRALSURFACE = 615; + const T_TIN = 616; + const T_TRIANGLE = 617; /** * @param string $input a query string */ - public function __construct($input) + public function __construct($input = null) { - $this->setInput($input); + if (null !== $input) { + $this->setInput($input); + } + } + + /** + * @return mixed + */ + public function value() + { + return $this->token['value']; } /** @@ -114,8 +137,9 @@ protected function getType(&$value) protected function getCatchablePatterns() { return array( - '[a-z]*', - '(?:[+-]?[0-9]+)(?:[\.][0-9]+)?' + '', + 'zm|[a-z]+[a-ln-z]', + '[+-]?[0-9]+(?:[\.][0-9]+)?' ); } diff --git a/lib/CrEOF/Geo/WKT/Parser.php b/lib/CrEOF/Geo/WKT/Parser.php index aa94c64..0e51cd5 100644 --- a/lib/CrEOF/Geo/WKT/Parser.php +++ b/lib/CrEOF/Geo/WKT/Parser.php @@ -36,7 +36,7 @@ class Parser /** * @var string */ - protected $type; + private $type; /** * @var string @@ -51,25 +51,35 @@ class Parser /** * @var Lexer */ - private $lexer; + private static $lexer; /** * @param string $input */ - public function __construct($input) + public function __construct($input = null) { - $this->input = $input; - $this->lexer = new Lexer($input); + self::$lexer = new Lexer(); + + if (null !== $input) { + $this->input = $input; + } } /** * @return array */ - public function parse() + public function parse($input = null) { - $this->lexer->moveNext(); + if (null !== $input) { + $this->input = $input; + } - if ($this->lexer->lookahead['type'] == Lexer::T_SRID) { + self::$lexer->setInput($this->input); + self::$lexer->moveNext(); + + $this->srid = null; + + if (self::$lexer->isNextToken(Lexer::T_SRID)) { $this->srid = $this->srid(); } @@ -90,7 +100,7 @@ protected function srid() $this->match(Lexer::T_EQUALS); $this->match(Lexer::T_INTEGER); - $srid = $this->lexer->token['value']; + $srid = self::$lexer->value(); $this->match(Lexer::T_SEMICOLON); @@ -106,7 +116,33 @@ protected function type() { $this->match(Lexer::T_TYPE); - return $this->lexer->token['value']; + return self::$lexer->value(); + } + + /** + * Match spatial geometry object + * + * @return array + */ + protected function geometry() + { + $type = $this->type(); + $this->type = $type; + + if (self::$lexer->isNextTokenAny(array(Lexer::T_Z, Lexer::T_M, Lexer::T_ZM))) { + $this->match(self::$lexer->lookahead['type']); + } + + $this->match(Lexer::T_OPEN_PARENTHESIS); + + $value = $this->$type(); + + $this->match(Lexer::T_CLOSE_PARENTHESIS); + + return array( + 'type' => $type, + 'value' => $value + ); } /** @@ -129,18 +165,18 @@ protected function point() */ protected function coordinate() { - $this->match(($this->lexer->isNextToken(Lexer::T_FLOAT) ? Lexer::T_FLOAT : Lexer::T_INTEGER)); + $this->match((self::$lexer->isNextToken(Lexer::T_FLOAT) ? Lexer::T_FLOAT : Lexer::T_INTEGER)); - if (! $this->lexer->isNextToken(Lexer::T_E)) { - return $this->lexer->token['value']; + if (! self::$lexer->isNextToken(Lexer::T_E)) { + return self::$lexer->value(); } - $number = $this->lexer->token['value']; + $number = self::$lexer->value(); $this->match(Lexer::T_E); $this->match(Lexer::T_INTEGER); - return $number * pow(10, $this->lexer->token['value']); + return $number * pow(10, self::$lexer->value()); } /** @@ -172,7 +208,7 @@ protected function pointList() { $points = array($this->point()); - while ($this->lexer->isNextToken(Lexer::T_COMMA)) { + while (self::$lexer->isNextToken(Lexer::T_COMMA)) { $this->match(Lexer::T_COMMA); $points[] = $this->point(); @@ -194,7 +230,7 @@ protected function pointLists() $this->match(Lexer::T_CLOSE_PARENTHESIS); - while ($this->lexer->isNextToken(Lexer::T_COMMA)) { + while (self::$lexer->isNextToken(Lexer::T_COMMA)) { $this->match(Lexer::T_COMMA); $this->match(Lexer::T_OPEN_PARENTHESIS); @@ -219,7 +255,7 @@ protected function multiPolygon() $this->match(Lexer::T_CLOSE_PARENTHESIS); - while ($this->lexer->isNextToken(Lexer::T_COMMA)) { + while (self::$lexer->isNextToken(Lexer::T_COMMA)) { $this->match(Lexer::T_COMMA); $this->match(Lexer::T_OPEN_PARENTHESIS); @@ -251,27 +287,6 @@ protected function multiLineString() return $this->pointLists(); } - /** - * Match spatial geometry object - * - * @return array - */ - protected function geometry() - { - $type = $this->type(); - - $this->match(Lexer::T_OPEN_PARENTHESIS); - - $value = $this->$type(); - - $this->match(Lexer::T_CLOSE_PARENTHESIS); - - return array( - 'type' => $type, - 'value' => $value - ); - } - /** * Match GEOMETRYCOLLECTION value * @@ -281,7 +296,7 @@ protected function geometryCollection() { $collection = array($this->geometry()); - while ($this->lexer->isNextToken(Lexer::T_COMMA)) { + while (self::$lexer->isNextToken(Lexer::T_COMMA)) { $this->match(Lexer::T_COMMA); $collection[] = $this->geometry(); @@ -297,13 +312,13 @@ protected function geometryCollection() */ protected function match($token) { - $lookaheadType = $this->lexer->lookahead['type']; + $lookaheadType = self::$lexer->lookahead['type']; if ($lookaheadType !== $token && ($token !== Lexer::T_TYPE || $lookaheadType <= Lexer::T_TYPE)) { - throw $this->syntaxError($this->lexer->getLiteral($token)); + throw $this->syntaxError(self::$lexer->getLiteral($token)); } - $this->lexer->moveNext(); + self::$lexer->moveNext(); } /** @@ -316,8 +331,8 @@ protected function match($token) private function syntaxError($expected) { $expected = sprintf('Expected %s, got', $expected); - $token = $this->lexer->lookahead; - $found = null === $this->lexer->lookahead ? 'end of string.' : sprintf('"%s"', $token['value']); + $token = self::$lexer->lookahead; + $found = null === self::$lexer->lookahead ? 'end of string.' : sprintf('"%s"', $token['value']); $message = sprintf( '[Syntax Error] line 0, col %d: Error: %s %s in value "%s"', isset($token['position']) ? $token['position'] : '-1', diff --git a/tests/CrEOF/Geo/WKT/Tests/GeometryCollectionParserTest.php b/tests/CrEOF/Geo/WKT/Tests/GeometryCollectionParserTest.php deleted file mode 100644 index e5d78fc..0000000 --- a/tests/CrEOF/Geo/WKT/Tests/GeometryCollectionParserTest.php +++ /dev/null @@ -1,109 +0,0 @@ - - * @license http://dlambert.mit-license.org MIT - */ -class GeometryCollectionParserTest extends \PHPUnit_Framework_TestCase -{ - public function testParsingGeometryCollectionValue() - { - $value = 'GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))'; - $parser = new Parser($value); - $expected = array( - 'srid' => null, - 'type' => 'GEOMETRYCOLLECTION', - 'value' => array( - array( - 'type' => 'POINT', - 'value' => array(10, 10) - ), - array( - 'type' => 'POINT', - 'value' => array(30, 30) - ), - array( - 'type' => 'LINESTRING', - 'value' => array( - array(15, 15), - array(20, 20) - ) - ) - ) - ); - - $actual = $parser->parse(); - - $this->assertEquals($expected, $actual); - } - - public function testParsingGeometryCollectionValueWithSrid() - { - $value = 'SRID=4326;GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))'; - $parser = new Parser($value); - $expected = array( - 'srid' => 4326, - 'type' => 'GEOMETRYCOLLECTION', - 'value' => array( - array( - 'type' => 'POINT', - 'value' => array(10, 10) - ), - array( - 'type' => 'POINT', - 'value' => array(30, 30) - ), - array( - 'type' => 'LINESTRING', - 'value' => array( - array(15, 15), - array(20, 20) - ) - ) - ) - ); - - $actual = $parser->parse(); - - $this->assertEquals($expected, $actual); - } - - /** - * @expectedException \CrEOF\Geo\WKT\Exception\UnexpectedValueException - * @expectedExceptionMessage [Syntax Error] line 0, col 19: Error: Expected CrEOF\Geo\WKT\Lexer::T_TYPE, got "PNT" in value "GEOMETRYCOLLECTION(PNT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))" - */ - public function testParsingGeometryCollectionValueWithBadType() - { - $value = 'GEOMETRYCOLLECTION(PNT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))'; - $parser = new Parser($value); - - $parser->parse(); - } -} diff --git a/tests/CrEOF/Geo/WKT/Tests/LexerTest.php b/tests/CrEOF/Geo/WKT/Tests/LexerTest.php index 3e9aab9..bbf11c8 100644 --- a/tests/CrEOF/Geo/WKT/Tests/LexerTest.php +++ b/tests/CrEOF/Geo/WKT/Tests/LexerTest.php @@ -33,266 +33,247 @@ */ class LexerTest extends \PHPUnit_Framework_TestCase { - public function testScannerRecognizesPointType() - { - $lexer = new Lexer('POINT'); - - $lexer->moveNext(); - - $token = $lexer->lookahead; - - $this->assertEquals(Lexer::T_POINT, $token['type']); - $this->assertEquals('POINT', $token['value']); - } - - public function testScannerRecognizesLineStringType() - { - $lexer = new Lexer('LINESTRING'); - - $lexer->moveNext(); - - $token = $lexer->lookahead; - - $this->assertEquals(Lexer::T_LINESTRING, $token['type']); - $this->assertEquals('LINESTRING', $token['value']); - } - - public function testScannerRecognizesPolygonType() - { - $lexer = new Lexer('POLYGON'); - - $lexer->moveNext(); - - $token = $lexer->lookahead; - - $this->assertEquals(Lexer::T_POLYGON, $token['type']); - $this->assertEquals('POLYGON', $token['value']); - } - - public function testScannerRecognizesMultiPointType() - { - $lexer = new Lexer('MULTIPOINT'); - - $lexer->moveNext(); - - $token = $lexer->lookahead; - - $this->assertEquals(Lexer::T_MULTIPOINT, $token['type']); - $this->assertEquals('MULTIPOINT', $token['value']); - } - - public function testScannerRecognizesMultiLineStringType() - { - $lexer = new Lexer('MULTILINESTRING'); - - $lexer->moveNext(); - - $token = $lexer->lookahead; - - $this->assertEquals(Lexer::T_MULTILINESTRING, $token['type']); - $this->assertEquals('MULTILINESTRING', $token['value']); - } - - public function testScannerRecognizesMultiPolygonType() - { - $lexer = new Lexer('MULTIPOLYGON'); - - $lexer->moveNext(); - - $token = $lexer->lookahead; - $this->assertEquals(Lexer::T_MULTIPOLYGON, $token['type']); - $this->assertEquals('MULTIPOLYGON', $token['value']); - } - - public function testScannerRecognizesGeometryCollectionType() + /** + * @return array + */ + public function tokenData() { - $lexer = new Lexer('GEOMETRYCOLLECTION'); - - $lexer->moveNext(); - - $token = $lexer->lookahead; - - $this->assertEquals(Lexer::T_GEOMETRYCOLLECTION, $token['type']); - $this->assertEquals('GEOMETRYCOLLECTION', $token['value']); - } - - public function testScannerRecognizesPositiveInteger() - { - $lexer = new Lexer('35'); - - $lexer->moveNext(); - - $token = $lexer->lookahead; - - $this->assertEquals(Lexer::T_INTEGER, $token['type']); - $this->assertEquals(35, $token['value']); - } - - public function testScannerRecognizesNegativeInteger() - { - $lexer = new Lexer('-25'); - - $lexer->moveNext(); - - $token = $lexer->lookahead; - - $this->assertEquals(Lexer::T_INTEGER, $token['type']); - $this->assertEquals(-25, $token['value']); - } - - public function testScannerRecognizesPositiveFloat() - { - $lexer = new Lexer('35.635'); - - $lexer->moveNext(); - - $token = $lexer->lookahead; - - $this->assertEquals(Lexer::T_FLOAT, $token['type']); - $this->assertEquals(35.635, $token['value']); - } - - public function testScannerRecognizesNegativeFloat() - { - $lexer = new Lexer('-120.33'); - - $lexer->moveNext(); - - $token = $lexer->lookahead; - - $this->assertEquals(Lexer::T_FLOAT, $token['type']); - $this->assertEquals(-120.33, $token['value']); - } - - public function testScannerRecognizesSrid() - { - $lexer = new Lexer('SRID'); - - $lexer->moveNext(); - - $token = $lexer->lookahead; - - $this->assertEquals(Lexer::T_SRID, $token['type']); - $this->assertEquals('SRID', $token['value']); - } - - public function testScannerTokenizesGeometryValueCorrectly() - { - $value = 'SRID=4326;LINESTRING(0 0.0, 10.1 -10.025, 20.5 25.9, 53E-003 60)'; - $tokens = array( - array( - 'value' => 'SRID', - 'type' => Lexer::T_SRID, - 'position' => 0 + return array( + 'POINT' => array( + 'value' => 'POINT', + 'expected' => array( + array(Lexer::T_POINT, 'POINT', 0) + ) + ), + 'POINTM' => array( + 'value' => 'POINTM', + 'expected' => array( + array(Lexer::T_POINT, 'POINT', 0), + array(Lexer::T_M, 'M', 5) + ) + ), + 'POINT M' => array( + 'value' => 'POINTM', + 'expected' => array( + array(Lexer::T_POINT, 'POINT', 0), + array(Lexer::T_M, 'M', 5) + ) + ), + 'POINT Z' => array( + 'value' => 'POINT Z', + 'expected' => array( + array(Lexer::T_POINT, 'POINT', 0), + array(Lexer::T_Z, 'Z', 6) + ) + ), + 'POINT ZM' => array( + 'value' => 'POINT ZM', + 'expected' => array( + array(Lexer::T_POINT, 'POINT', 0), + array(Lexer::T_ZM, 'ZM', 6) + ) + ), + 'POINTZM' => array( + 'value' => 'POINTZM', + 'expected' => array( + array(Lexer::T_STRING, 'POINTZ', 0), + array(Lexer::T_M, 'M', 6) + ) + ), + 'LINESTRING' => array( + 'value' => 'LINESTRING', + 'expected' => array( + array(Lexer::T_LINESTRING, 'LINESTRING', 0) + ) ), - array( - 'value' => '=', - 'type' => Lexer::T_EQUALS, - 'position' => 4 + 'LINESTRINGM' => array( + 'value' => 'LINESTRINGM', + 'expected' => array( + array(Lexer::T_LINESTRING, 'LINESTRING', 0), + array(Lexer::T_M, 'M', 10) + ) ), - array( - 'value' => '4326', - 'type' => Lexer::T_INTEGER, - 'position' => 5 + 'POLYGON' => array( + 'value' => 'POLYGON', + 'expected' => array( + array(Lexer::T_POLYGON, 'POLYGON', 0) + ) ), - array( - 'value' => ';', - 'type' => Lexer::T_SEMICOLON, - 'position' => 9 + 'POLYGONM' => array( + 'value' => 'POLYGONM', + 'expected' => array( + array(Lexer::T_POLYGON, 'POLYGON', 0), + array(Lexer::T_M, 'M', 7) + ) ), - array( - 'value' => 'LINESTRING', - 'type' => Lexer::T_LINESTRING, - 'position' => 10 + 'MULTIPOINT' => array( + 'value' => 'MULTIPOINT', + 'expected' => array( + array(Lexer::T_MULTIPOINT, 'MULTIPOINT', 0) + ) ), - array( - 'value' => '(', - 'type' => Lexer::T_OPEN_PARENTHESIS, - 'position' => 20 + 'MULTIPOINTM' => array( + 'value' => 'MULTIPOINTM', + 'expected' => array( + array(Lexer::T_MULTIPOINT, 'MULTIPOINT', 0), + array(Lexer::T_M, 'M', 10) + ) ), - array( - 'value' => 0, - 'type' => Lexer::T_INTEGER, - 'position' => 21 + 'MULTILINESTRING' => array( + 'value' => 'MULTILINESTRING', + 'expected' => array( + array(Lexer::T_MULTILINESTRING, 'MULTILINESTRING', 0) + ) ), - array( - 'value' => 0, - 'type' => Lexer::T_FLOAT, - 'position' => 23 + 'MULTILINESTRINGM' => array( + 'value' => 'MULTILINESTRINGM', + 'expected' => array( + array(Lexer::T_MULTILINESTRING, 'MULTILINESTRING', 0), + array(Lexer::T_M, 'M', 15) + ) ), - array( - 'value' => ',', - 'type' => Lexer::T_COMMA, - 'position' => 26 + 'MULTIPOLYGON' => array( + 'value' => 'MULTIPOLYGON', + 'expected' => array( + array(Lexer::T_MULTIPOLYGON, 'MULTIPOLYGON', 0) + ) ), - array( - 'value' => 10.1, - 'type' => Lexer::T_FLOAT, - 'position' => 28 + 'MULTIPOLYGONM' => array( + 'value' => 'MULTIPOLYGONM', + 'expected' => array( + array(Lexer::T_MULTIPOLYGON, 'MULTIPOLYGON', 0), + array(Lexer::T_M, 'M', 12) + ) ), - array( - 'value' => -10.025, - 'type' => Lexer::T_FLOAT, - 'position' => 33 + 'GEOMETRYCOLLECTION' => array( + 'value' => 'GEOMETRYCOLLECTION', + 'expected' => array( + array(Lexer::T_GEOMETRYCOLLECTION, 'GEOMETRYCOLLECTION', 0) + ) ), - array( - 'value' => ',', - 'type' => Lexer::T_COMMA, - 'position' => 40 + 'GEOMETRYCOLLECTIONM' => array( + 'value' => 'GEOMETRYCOLLECTIONM', + 'expected' => array( + array(Lexer::T_GEOMETRYCOLLECTION, 'GEOMETRYCOLLECTION', 0), + array(Lexer::T_M, 'M', 18) + ) ), - array( - 'value' => 20.5, - 'type' => Lexer::T_FLOAT, - 'position' => 42 + 'COMPOUNDCURVE' => array( + 'value' => 'COMPOUNDCURVE', + 'expected' => array( + array(Lexer::T_COMPOUNDCURVE, 'COMPOUNDCURVE', 0) + ) ), - array( - 'value' => 25.9, - 'type' => Lexer::T_FLOAT, - 'position' => 47 + 'COMPOUNDCURVEM' => array( + 'value' => 'COMPOUNDCURVEM', + 'expected' => array( + array(Lexer::T_COMPOUNDCURVE, 'COMPOUNDCURVE', 0), + array(Lexer::T_M, 'M', 13) + ) ), - array( - 'value' => ',', - 'type' => Lexer::T_COMMA, - 'position' => 51 + 'CIRCULARSTRING' => array( + 'value' => 'CIRCULARSTRING', + 'expected' => array( + array(Lexer::T_CIRCULARSTRING, 'CIRCULARSTRING', 0) + ) ), - array( - 'value' => 53, - 'type' => Lexer::T_INTEGER, - 'position' => 53 + 'CIRCULARSTRINGM' => array( + 'value' => 'CIRCULARSTRINGM', + 'expected' => array( + array(Lexer::T_CIRCULARSTRING, 'CIRCULARSTRING', 0), + array(Lexer::T_M, 'M', 14) + ) ), - array( - 'value' => 'E', - 'type' => Lexer::T_E, - 'position' => 55 + '35' => array( + 'value' => '35', + 'expected' => array( + array(Lexer::T_INTEGER, 35, 0) + ) ), - array( - 'value' => -3, - 'type' => Lexer::T_INTEGER, - 'position' => 56 + '-25' => array( + 'value' => '-25', + 'expected' => array( + array(Lexer::T_INTEGER, -25, 0) + ) ), - array( - 'value' => 60, - 'type' => Lexer::T_INTEGER, - 'position' => 61 + '-120.33' => array( + 'value' => '-120.33', + 'expected' => array( + array(Lexer::T_FLOAT, -120.33, 0) + ) ), - array( - 'value' => ')', - 'type' => Lexer::T_CLOSE_PARENTHESIS, - 'position' => 63 + 'SRID' => array( + 'value' => 'SRID', + 'expected' => array( + array(Lexer::T_SRID, 'SRID', 0) + ) + ), + 'SRID=4326;LINESTRING(0 0.0, 10.1 -10.025, 20.5 25.9, 53E-003 60)' => array( + 'value' => 'SRID=4326;LINESTRING(0 0.0, 10.1 -10.025, 20.5 25.9, 53E-003 60)', + 'expected' => array( + array(Lexer::T_SRID, 'SRID', 0), + array(Lexer::T_EQUALS, '=', 4), + array(Lexer::T_INTEGER, 4326, 5), + array(Lexer::T_SEMICOLON, ';', 9), + array(Lexer::T_LINESTRING, 'LINESTRING', 10), + array(Lexer::T_OPEN_PARENTHESIS, '(', 20), + array(Lexer::T_INTEGER, 0, 21), + array(Lexer::T_FLOAT, 0, 23), + array(Lexer::T_COMMA, ',', 26), + array(Lexer::T_FLOAT, 10.1, 28), + array(Lexer::T_FLOAT, -10.025, 33), + array(Lexer::T_COMMA, ',', 40), + array(Lexer::T_FLOAT, 20.5, 42), + array(Lexer::T_FLOAT, 25.9, 47), + array(Lexer::T_COMMA, ',', 51), + array(Lexer::T_INTEGER, 53, 53), + array(Lexer::T_E, 'E', 55), + array(Lexer::T_INTEGER, -3, 56), + array(Lexer::T_INTEGER, 60, 61), + array(Lexer::T_CLOSE_PARENTHESIS, ')', 63) + ) ) ); + } + /** + * @param $value + * @param array $expected + * + * @dataProvider tokenData + */ + public function testTokenRecognition($value, array $expected) + { $lexer = new Lexer($value); - foreach ($tokens as $expected) { + foreach ($expected as $token) { $lexer->moveNext(); $actual = $lexer->lookahead; - $this->assertEquals($expected, $actual); + $this->assertEquals($token[0], $actual['type']); + $this->assertEquals($token[1], $actual['value']); + $this->assertEquals($token[2], $actual['position']); } + } + + public function testTokenRecognitionReuseLexer() + { + $lexer = new Lexer(); + + foreach ($this->tokenData() as $name => $testData) { + $lexer->setInput($testData['value']); + + foreach ($testData['expected'] as $token) { + $lexer->moveNext(); - $this->assertFalse($lexer->moveNext()); + $actual = $lexer->lookahead; + + $this->assertEquals($token[0], $actual['type']); + $this->assertEquals($token[1], $actual['value']); + $this->assertEquals($token[2], $actual['position']); + } + } } } diff --git a/tests/CrEOF/Geo/WKT/Tests/LineStringParserTest.php b/tests/CrEOF/Geo/WKT/Tests/LineStringParserTest.php deleted file mode 100644 index ad741e6..0000000 --- a/tests/CrEOF/Geo/WKT/Tests/LineStringParserTest.php +++ /dev/null @@ -1,95 +0,0 @@ - - * @license http://dlambert.mit-license.org MIT - */ -class LineStringParserTest extends \PHPUnit_Framework_TestCase -{ - public function testParsingLineStringValue() - { - $value = 'LINESTRING(34.23 -87, 45.3 -92)'; - $parser = new Parser($value); - $expected = array( - 'srid' => null, - 'type' => 'LINESTRING', - 'value' => array( - array(34.23, -87), - array(45.3, -92) - ) - ); - - $actual = $parser->parse(); - - $this->assertEquals($expected, $actual); - } - - public function testParsingLineStringValueWithSrid() - { - $value = 'SRID=4326;LINESTRING(34.23 -87, 45.3 -92)'; - $parser = new Parser($value); - $expected = array( - 'srid' => 4326, - 'type' => 'LINESTRING', - 'value' => array( - array(34.23, -87), - array(45.3, -92) - ) - ); - - $actual = $parser->parse(); - - $this->assertEquals($expected, $actual); - } - - /** - * @expectedException \CrEOF\Geo\WKT\Exception\UnexpectedValueException - * @expectedExceptionMessage [Syntax Error] line 0, col 21: Error: Expected CrEOF\Geo\WKT\Lexer::T_CLOSE_PARENTHESIS, got "45.3" in value "LINESTRING(34.23 -87 45.3 -92)" - */ - public function testParsingLineStringValueMissingComma() - { - $value = 'LINESTRING(34.23 -87 45.3 -92)'; - $parser = new Parser($value); - - $parser->parse(); - } - - /** - * @expectedException \CrEOF\Geo\WKT\Exception\UnexpectedValueException - * @expectedExceptionMessage [Syntax Error] line 0, col 26: Error: Expected CrEOF\Geo\WKT\Lexer::T_INTEGER, got ")" in value "LINESTRING(34.23 -87, 45.3)" - */ - public function testParsingLineStringValueMissingCoordinate() - { - $value = 'LINESTRING(34.23 -87, 45.3)'; - $parser = new Parser($value); - - $parser->parse(); - } -} diff --git a/tests/CrEOF/Geo/WKT/Tests/MultiLineStringParserTest.php b/tests/CrEOF/Geo/WKT/Tests/MultiLineStringParserTest.php deleted file mode 100644 index 7189f79..0000000 --- a/tests/CrEOF/Geo/WKT/Tests/MultiLineStringParserTest.php +++ /dev/null @@ -1,103 +0,0 @@ - - * @license http://dlambert.mit-license.org MIT - */ -class MultiLineStringParserTest extends \PHPUnit_Framework_TestCase -{ - public function testParsingMultiLineStringValue() - { - $value = 'MULTILINESTRING((0 0,10 0,10 10,0 10),(5 5,7 5,7 7,5 7))'; - $parser = new Parser($value); - $expected = array( - 'srid' => null, - 'type' => 'MULTILINESTRING', - 'value' => array( - array( - array(0, 0), - array(10, 0), - array(10, 10), - array(0, 10), - ), - array( - array(5, 5), - array(7, 5), - array(7, 7), - array(5, 7), - ) - ) - ); - - $actual = $parser->parse(); - - $this->assertEquals($expected, $actual); - } - - public function testParsingMultiLineStringValueWithSrid() - { - $value = 'SRID=4326;MULTILINESTRING((0 0,10 0,10 10,0 10),(5 5,7 5,7 7,5 7))'; - $parser = new Parser($value); - $expected = array( - 'srid' => 4326, - 'type' => 'MULTILINESTRING', - 'value' => array( - array( - array(0, 0), - array(10, 0), - array(10, 10), - array(0, 10), - ), - array( - array(5, 5), - array(7, 5), - array(7, 7), - array(5, 7), - ) - ) - ); - - $actual = $parser->parse(); - - $this->assertEquals($expected, $actual); - } - - /** - * @expectedException \CrEOF\Geo\WKT\Exception\UnexpectedValueException - * @expectedExceptionMessage [Syntax Error] line 0, col 37: Error: Expected CrEOF\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))" - */ - public function testParsingMultiLineStringValueMissingComma() - { - $value = 'MULTILINESTRING((0 0,10 0,10 10,0 10)(5 5,7 5,7 7,5 7))'; - $parser = new Parser($value); - - $parser->parse(); - } -} diff --git a/tests/CrEOF/Geo/WKT/Tests/MultiPointParserTest.php b/tests/CrEOF/Geo/WKT/Tests/MultiPointParserTest.php deleted file mode 100644 index e04e37d..0000000 --- a/tests/CrEOF/Geo/WKT/Tests/MultiPointParserTest.php +++ /dev/null @@ -1,87 +0,0 @@ - - * @license http://dlambert.mit-license.org MIT - */ -class MultiPointParserTest extends \PHPUnit_Framework_TestCase -{ - public function testParsingMultiPointValue() - { - $value = 'MULTIPOINT(0 0,10 0,10 10,0 10)'; - $parser = new Parser($value); - $expected = array( - 'srid' => null, - 'type' => 'MULTIPOINT', - 'value' => array( - array(0, 0), - array(10, 0), - array(10, 10), - array(0, 10) - ) - ); - - $actual = $parser->parse(); - - $this->assertEquals($expected, $actual); - } - - public function testParsingMultiPointValueWithSrid() - { - $value = 'SRID=4326;MULTIPOINT(0 0,10 0,10 10,0 10)'; - $parser = new Parser($value); - $expected = array( - 'srid' => 4326, - 'type' => 'MULTIPOINT', - 'value' => array( - array(0, 0), - array(10, 0), - array(10, 10), - array(0, 10) - ) - ); - - $actual = $parser->parse(); - - $this->assertEquals($expected, $actual); - } - - /** - * @expectedException \CrEOF\Geo\WKT\Exception\UnexpectedValueException - * @expectedExceptionMessage [Syntax Error] line 0, col 11: Error: Expected CrEOF\Geo\WKT\Lexer::T_INTEGER, got "(" in value "MULTIPOINT((0 0,10 0,10 10,0 10))" - */ - public function testParsingMultiPointValueWithExtraParenthesis() - { - $value = 'MULTIPOINT((0 0,10 0,10 10,0 10))'; - $parser = new Parser($value); - - $parser->parse(); - } -} diff --git a/tests/CrEOF/Geo/WKT/Tests/MultiPolygonParserTest.php b/tests/CrEOF/Geo/WKT/Tests/MultiPolygonParserTest.php deleted file mode 100644 index 662c788..0000000 --- a/tests/CrEOF/Geo/WKT/Tests/MultiPolygonParserTest.php +++ /dev/null @@ -1,129 +0,0 @@ - - * @license http://dlambert.mit-license.org MIT - */ -class MultiPolygonParserTest extends \PHPUnit_Framework_TestCase -{ - public function testParsingMultiPolygonValue() - { - $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)))'; - $parser = new Parser($value); - $expected = array( - 'srid' => null, - 'type' => 'MULTIPOLYGON', - 'value' => array( - array( - array( - array(0, 0), - array(10, 0), - array(10, 10), - array(0, 10), - array(0, 0) - ), - array( - array(5, 5), - array(7, 5), - array(7, 7), - array(5, 7), - array(5, 5) - ) - ), - array( - array( - array(1, 1), - array(3, 1), - array(3, 3), - array(1, 3), - array(1, 1) - ) - ) - ) - ); - - $actual = $parser->parse(); - - $this->assertEquals($expected, $actual); - } - - public function testParsingMultiPolygonValueWithSrid() - { - $value = 'SRID=4326;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)))'; - $parser = new Parser($value); - $expected = array( - 'srid' => 4326, - 'type' => 'MULTIPOLYGON', - 'value' => array( - array( - array( - array(0, 0), - array(10, 0), - array(10, 10), - array(0, 10), - array(0, 0) - ), - array( - array(5, 5), - array(7, 5), - array(7, 7), - array(5, 7), - array(5, 5) - ) - ), - array( - array( - array(1, 1), - array(3, 1), - array(3, 3), - array(1, 3), - array(1, 1) - ) - ) - ) - ); - - $actual = $parser->parse(); - - $this->assertEquals($expected, $actual); - } - - /** - * @expectedException \CrEOF\Geo\WKT\Exception\UnexpectedValueException - * @expectedExceptionMessage [Syntax Error] line 0, col 64: Error: Expected CrEOF\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))" - */ - public function testParsingMultiPolygonValueMissingParenthesis() - { - $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))'; - $parser = new Parser($value); - - $parser->parse(); - } -} diff --git a/tests/CrEOF/Geo/WKT/Tests/ParserTest.php b/tests/CrEOF/Geo/WKT/Tests/ParserTest.php index c34dbef..eb38401 100644 --- a/tests/CrEOF/Geo/WKT/Tests/ParserTest.php +++ b/tests/CrEOF/Geo/WKT/Tests/ParserTest.php @@ -23,6 +23,7 @@ namespace CrEOF\Geo\WKT\Tests; +use CrEOF\Geo\WKT\Exception\UnexpectedValueException; use CrEOF\Geo\WKT\Parser; /** @@ -33,27 +34,414 @@ */ class ParserTest extends \PHPUnit_Framework_TestCase { + public function parserTestData() + { + return array( + 'testParsingGarbage' => array( + 'value' => '@#_$%', + 'expected' => new UnexpectedValueException('[Syntax Error] line 0, col 0: Error: Expected CrEOF\Geo\WKT\Lexer::T_TYPE, got "@" in value "@#_$%"') + ), + 'testParsingBadType' => array( + 'value' => 'PNT(10 10)', + 'expected' => new UnexpectedValueException('[Syntax Error] line 0, col 0: Error: Expected CrEOF\Geo\WKT\Lexer::T_TYPE, got "PNT" in value "PNT(10 10)"') + ), + 'testParsingPointValue' => array( + 'value' => 'POINT(34.23 -87)', + 'expected' => array( + 'srid' => null, + 'type' => 'POINT', + 'value' => array(34.23, -87) + ) + ), + 'testParsingPointValueWithSrid' => array( + 'value' => 'SRID=4326;POINT(34.23 -87)', + 'expected' => array( + 'srid' => 4326, + 'type' => 'POINT', + 'value' => array(34.23, -87) + ) + ), + 'testParsingPointValueScientificWithSrid' => array( + 'value' => 'SRID=4326;POINT(4.23e-005 -8E-003)', + 'expected' => array( + 'srid' => 4326, + 'type' => 'POINT', + 'value' => array(0.0000423, -0.008) + ) + ), + 'testParsingPointValueWithBadSrid' => array( + 'value' => 'SRID=432.6;POINT(34.23 -87)', + 'expected' => new UnexpectedValueException('[Syntax Error] line 0, col 5: Error: Expected CrEOF\Geo\WKT\Lexer::T_INTEGER, got "432.6" in value "SRID=432.6;POINT(34.23 -87)"') + ), + 'testParsingPointValueMissingCoordinate' => array( + 'value' => 'POINT(34.23)', + 'expected' => new UnexpectedValueException('[Syntax Error] line 0, col 11: Error: Expected CrEOF\Geo\WKT\Lexer::T_INTEGER, got ")" in value "POINT(34.23)"') + ), + 'testParsingPointValueShortString' => array( + 'value' => 'POINT(34.23', + 'expected' => new UnexpectedValueException('[Syntax Error] line 0, col -1: Error: Expected CrEOF\Geo\WKT\Lexer::T_INTEGER, got end of string. in value "POINT(34.23"') + ), + 'testParsingPointValueWrongScientificWithSrid' => array( + 'value' => 'SRID=4326;POINT(4.23test-005 -8e-003)', + 'expected' => new UnexpectedValueException('[Syntax Error] line 0, col 20: Error: Expected CrEOF\Geo\WKT\Lexer::T_INTEGER, got "test" in value "SRID=4326;POINT(4.23test-005 -8e-003)"') + ), + 'testParsingPointValueWithComma' => array( + 'value' => 'POINT(10, 10)', + 'expected' => new UnexpectedValueException('[Syntax Error] line 0, col 8: Error: Expected CrEOF\Geo\WKT\Lexer::T_INTEGER, got "," in value "POINT(10, 10)"') + ), + 'testParsingLineStringValue' => array( + 'value' => 'LINESTRING(34.23 -87, 45.3 -92)', + 'expected' => array( + 'srid' => null, + 'type' => 'LINESTRING', + 'value' => array( + array(34.23, -87), + array(45.3, -92) + ) + ) + ), + 'testParsingLineStringValueWithSrid' => array( + 'value' => 'SRID=4326;LINESTRING(34.23 -87, 45.3 -92)', + 'expected' => array( + 'srid' => 4326, + 'type' => 'LINESTRING', + 'value' => array( + array(34.23, -87), + array(45.3, -92) + ) + ) + ), + 'testParsingLineStringValueMissingComma' => array( + 'value' => 'LINESTRING(34.23 -87 45.3 -92)', + 'expected' => new UnexpectedValueException('[Syntax Error] line 0, col 21: Error: Expected CrEOF\Geo\WKT\Lexer::T_CLOSE_PARENTHESIS, got "45.3" in value "LINESTRING(34.23 -87 45.3 -92)"') + ), + 'testParsingLineStringValueMissingCoordinate' => array( + 'value' => 'LINESTRING(34.23 -87, 45.3)', + 'expected' => new UnexpectedValueException('[Syntax Error] line 0, col 26: Error: Expected CrEOF\Geo\WKT\Lexer::T_INTEGER, got ")" in value "LINESTRING(34.23 -87, 45.3)"') + ), + 'testParsingPolygonValue' => array( + 'value' => 'POLYGON((0 0,10 0,10 10,0 10,0 0))', + 'expected' => array( + 'srid' => null, + 'type' => 'POLYGON', + 'value' => array( + array( + array(0, 0), + array(10, 0), + array(10, 10), + array(0, 10), + array(0, 0) + ) + ) + ) + ), + 'testParsingPolygonValueWithSrid' => array( + 'value' => 'SRID=4326;POLYGON((0 0,10 0,10 10,0 10,0 0))', + 'expected' => array( + 'srid' => 4326, + 'type' => 'POLYGON', + 'value' => array( + array( + array(0, 0), + array(10, 0), + array(10, 10), + array(0, 10), + array(0, 0) + ) + ) + ) + ), + 'testParsingPolygonValueMultiRing' => array( + 'value' => 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7,5 5))', + 'expected' => array( + 'srid' => null, + 'type' => 'POLYGON', + 'value' => array( + array( + array(0, 0), + array(10, 0), + array(10, 10), + array(0, 10), + array(0, 0) + ), + array( + array(5, 5), + array(7, 5), + array(7, 7), + array(5, 7), + array(5, 5) + ) + ) + ) + ), + 'testParsingPolygonValueMultiRingWithSrid' => array( + 'value' => 'SRID=4326;POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7,5 5))', + 'expected' => array( + 'srid' => 4326, + 'type' => 'POLYGON', + 'value' => array( + array( + array(0, 0), + array(10, 0), + array(10, 10), + array(0, 10), + array(0, 0) + ), + array( + array(5, 5), + array(7, 5), + array(7, 7), + array(5, 7), + array(5, 5) + ) + ) + ) + ), + 'testParsingPolygonValueMissingParenthesis' => array( + 'value' => 'POLYGON(0 0,10 0,10 10,0 10,0 0)', + 'expected' => new UnexpectedValueException('[Syntax Error] line 0, col 8: Error: Expected CrEOF\Geo\WKT\Lexer::T_OPEN_PARENTHESIS, got "0" in value "POLYGON(0 0,10 0,10 10,0 10,0 0)"') + ), + 'testParsingPolygonValueMultiRingMissingComma' => array( + 'value' => 'POLYGON((0 0,10 0,10 10,0 10,0 0)(5 5,7 5,7 7,5 7,5 5))', + 'expected' => new UnexpectedValueException('[Syntax Error] line 0, col 33: Error: Expected CrEOF\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))"') + ), + 'testParsingMultiPointValue' => array( + 'value' => 'MULTIPOINT(0 0,10 0,10 10,0 10)', + 'expected' => array( + 'srid' => null, + 'type' => 'MULTIPOINT', + 'value' => array( + array(0, 0), + array(10, 0), + array(10, 10), + array(0, 10) + ) + ) + ), + 'testParsingMultiPointValueWithSrid' => array( + 'value' => 'SRID=4326;MULTIPOINT(0 0,10 0,10 10,0 10)', + 'expected' => array( + 'srid' => 4326, + 'type' => 'MULTIPOINT', + 'value' => array( + array(0, 0), + array(10, 0), + array(10, 10), + array(0, 10) + ) + ) + ), + 'testParsingMultiPointValueWithExtraParenthesis' => array( + 'value' => 'MULTIPOINT((0 0,10 0,10 10,0 10))', + 'expected' => new UnexpectedValueException('[Syntax Error] line 0, col 11: Error: Expected CrEOF\Geo\WKT\Lexer::T_INTEGER, got "(" in value "MULTIPOINT((0 0,10 0,10 10,0 10))"') + ), + 'testParsingMultiLineStringValue' => array( + 'value' => 'MULTILINESTRING((0 0,10 0,10 10,0 10),(5 5,7 5,7 7,5 7))', + 'expected' => array( + 'srid' => null, + 'type' => 'MULTILINESTRING', + 'value' => array( + array( + array(0, 0), + array(10, 0), + array(10, 10), + array(0, 10), + ), + array( + array(5, 5), + array(7, 5), + array(7, 7), + array(5, 7), + ) + ) + ) + ), + 'testParsingMultiLineStringValueWithSrid' => array( + 'value' => 'SRID=4326;MULTILINESTRING((0 0,10 0,10 10,0 10),(5 5,7 5,7 7,5 7))', + 'expected' => array( + 'srid' => 4326, + 'type' => 'MULTILINESTRING', + 'value' => array( + array( + array(0, 0), + array(10, 0), + array(10, 10), + array(0, 10), + ), + array( + array(5, 5), + array(7, 5), + array(7, 7), + array(5, 7), + ) + ) + ) + ), + 'testParsingMultiLineStringValueMissingComma' => array( + 'value' => 'MULTILINESTRING((0 0,10 0,10 10,0 10)(5 5,7 5,7 7,5 7))', + 'expected' => new UnexpectedValueException('[Syntax Error] line 0, col 37: Error: Expected CrEOF\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))"') + ), + 'testParsingMultiPolygonValue' => array( + '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)))', + 'expected' => array( + 'srid' => null, + 'type' => 'MULTIPOLYGON', + 'value' => array( + array( + array( + array(0, 0), + array(10, 0), + array(10, 10), + array(0, 10), + array(0, 0) + ), + array( + array(5, 5), + array(7, 5), + array(7, 7), + array(5, 7), + array(5, 5) + ) + ), + array( + array( + array(1, 1), + array(3, 1), + array(3, 3), + array(1, 3), + array(1, 1) + ) + ) + ) + ) + ), + 'testParsingMultiPolygonValueWithSrid' => array( + 'value' => 'SRID=4326;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)))', + 'expected' => array( + 'srid' => 4326, + 'type' => 'MULTIPOLYGON', + 'value' => array( + array( + array( + array(0, 0), + array(10, 0), + array(10, 10), + array(0, 10), + array(0, 0) + ), + array( + array(5, 5), + array(7, 5), + array(7, 7), + array(5, 7), + array(5, 5) + ) + ), + array( + array( + array(1, 1), + array(3, 1), + array(3, 3), + array(1, 3), + array(1, 1) + ) + ) + ) + ) + ), + 'testParsingMultiPolygonValueMissingParenthesis' => array( + '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))', + 'expected' => new UnexpectedValueException('[Syntax Error] line 0, col 64: Error: Expected CrEOF\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))"') + ), + 'testParsingGeometryCollectionValue' => array( + 'value' => 'GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))', + 'expected' => array( + 'srid' => null, + 'type' => 'GEOMETRYCOLLECTION', + 'value' => array( + array( + 'type' => 'POINT', + 'value' => array(10, 10) + ), + array( + 'type' => 'POINT', + 'value' => array(30, 30) + ), + array( + 'type' => 'LINESTRING', + 'value' => array( + array(15, 15), + array(20, 20) + ) + ) + ) + ) + ), + 'testParsingGeometryCollectionValueWithSrid' => array( + 'value' => 'SRID=4326;GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))', + 'expected' => array( + 'srid' => 4326, + 'type' => 'GEOMETRYCOLLECTION', + 'value' => array( + array( + 'type' => 'POINT', + 'value' => array(10, 10) + ), + array( + 'type' => 'POINT', + 'value' => array(30, 30) + ), + array( + 'type' => 'LINESTRING', + 'value' => array( + array(15, 15), + array(20, 20) + ) + ) + ) + ) + ), + 'testParsingGeometryCollectionValueWithBadType' => array( + 'value' => 'GEOMETRYCOLLECTION(PNT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))', + 'expected' => new UnexpectedValueException('[Syntax Error] line 0, col 19: Error: Expected CrEOF\Geo\WKT\Lexer::T_TYPE, got "PNT" in value "GEOMETRYCOLLECTION(PNT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))"') + ) + ); + } + /** - * @expectedException \CrEOF\Geo\WKT\Exception\UnexpectedValueException - * @expectedExceptionMessage [Syntax Error] line 0, col 0: Error: Expected CrEOF\Geo\WKT\Lexer::T_TYPE, got "@" in value "@#_$%" + * @param $value + * @param array $expected + * + * @dataProvider parserTestData */ - public function testParsingGarbage() + public function testParser($value, $expected) { - $value = '@#_$%'; $parser = new Parser($value); - $parser->parse(); + try { + $actual = $parser->parse(); + } catch (\Exception $e) { + $actual = $e; + } + + $this->assertEquals($expected, $actual); } /** - * @expectedException \CrEOF\Geo\WKT\Exception\UnexpectedValueException - * @expectedExceptionMessage [Syntax Error] line 0, col 0: Error: Expected CrEOF\Geo\WKT\Lexer::T_TYPE, got "PNT" in value "PNT(10 10)" */ - public function testParsingBadType() + public function testReusedParser() { - $value = 'PNT(10 10)'; - $parser = new Parser($value); + $parser = new Parser(); + + foreach ($this->parserTestData() as $name => $testData) { + try { + $actual = $parser->parse($testData['value']); + } catch (\Exception $e) { + $actual = $e; + } - $parser->parse(); + $this->assertEquals($testData['expected'], $actual, 'Failed dataset "'. $name . '"'); + } } + } diff --git a/tests/CrEOF/Geo/WKT/Tests/PointParserTest.php b/tests/CrEOF/Geo/WKT/Tests/PointParserTest.php deleted file mode 100644 index 68bdaf2..0000000 --- a/tests/CrEOF/Geo/WKT/Tests/PointParserTest.php +++ /dev/null @@ -1,141 +0,0 @@ - - * @license http://dlambert.mit-license.org MIT - */ -class PointParserTest extends \PHPUnit_Framework_TestCase -{ - public function testParsingPointValue() - { - $value = 'POINT(34.23 -87)'; - $parser = new Parser($value); - $expected = array( - 'srid' => null, - 'type' => 'POINT', - 'value' => array(34.23, -87) - ); - - $actual = $parser->parse(); - - $this->assertEquals($expected, $actual); - } - - public function testParsingPointValueWithSrid() - { - $value = 'SRID=4326;POINT(34.23 -87)'; - $parser = new Parser($value); - $expected = array( - 'srid' => 4326, - 'type' => 'POINT', - 'value' => array(34.23, -87) - ); - - $actual = $parser->parse(); - - $this->assertEquals($expected, $actual); - } - - /** - * @expectedException \CrEOF\Geo\WKT\Exception\UnexpectedValueException - * @expectedExceptionMessage [Syntax Error] line 0, col 5: Error: Expected CrEOF\Geo\WKT\Lexer::T_INTEGER, got "432.6" in value "SRID=432.6;POINT(34.23 -87)" - */ - public function testParsingPointValueWithBadSrid() - { - $value = 'SRID=432.6;POINT(34.23 -87)'; - $parser = new Parser($value); - - $parser->parse(); - } - - /** - * @expectedException \CrEOF\Geo\WKT\Exception\UnexpectedValueException - * @expectedExceptionMessage [Syntax Error] line 0, col 11: Error: Expected CrEOF\Geo\WKT\Lexer::T_INTEGER, got ")" in value "POINT(34.23)" - */ - public function testParsingPointValueMissingCoordinate() - { - $value = 'POINT(34.23)'; - $parser = new Parser($value); - - $parser->parse(); - } - - /** - * @expectedException \CrEOF\Geo\WKT\Exception\UnexpectedValueException - * @expectedExceptionMessage [Syntax Error] line 0, col -1: Error: Expected CrEOF\Geo\WKT\Lexer::T_INTEGER, got end of string. in value "POINT(34.23" - */ - public function testParsingPointValueShortString() - { - $value = 'POINT(34.23'; - $parser = new Parser($value); - - $parser->parse(); - } - - public function testParsingPointValueScientificWithSrid() - { - $value = 'SRID=4326;POINT(4.23e-005 -8E-003)'; - $parser = new Parser($value); - $expected = array( - 'srid' => 4326, - 'type' => 'POINT', - 'value' => array(0.0000423, -0.008) - ); - - $actual = $parser->parse(); - - $this->assertEquals($expected, $actual); - } - - /** - * @expectedException \CrEOF\Geo\WKT\Exception\UnexpectedValueException - * @expectedExceptionMessage [Syntax Error] line 0, col 20: Error: Expected CrEOF\Geo\WKT\Lexer::T_INTEGER, got "test" in value "SRID=4326;POINT(4.23test-005 -8e-003)" - */ - public function testParsingPointValueWrongScientificWithSrid() - { - $value = 'SRID=4326;POINT(4.23test-005 -8e-003)'; - $parser = new Parser($value); - - $parser->parse(); - } - - - /** - * @expectedException \CrEOF\Geo\WKT\Exception\UnexpectedValueException - * @expectedExceptionMessage [Syntax Error] line 0, col 8: Error: Expected CrEOF\Geo\WKT\Lexer::T_INTEGER, got "," in value "POINT(10, 10)" - */ - public function testParsingPointValueWithComma() - { - $value = 'POINT(10, 10)'; - $parser = new Parser($value); - - $parser->parse(); - } -} diff --git a/tests/CrEOF/Geo/WKT/Tests/PolygonParserTest.php b/tests/CrEOF/Geo/WKT/Tests/PolygonParserTest.php deleted file mode 100644 index 188b60d..0000000 --- a/tests/CrEOF/Geo/WKT/Tests/PolygonParserTest.php +++ /dev/null @@ -1,166 +0,0 @@ - - * @license http://dlambert.mit-license.org MIT - */ -class PolygonParserTest extends \PHPUnit_Framework_TestCase -{ - - public function testParsingPolygonValue() - { - $value = 'POLYGON((0 0,10 0,10 10,0 10,0 0))'; - $parser = new Parser($value); - $expected = array( - 'srid' => null, - 'type' => 'POLYGON', - 'value' => array( - array( - array(0, 0), - array(10, 0), - array(10, 10), - array(0, 10), - array(0, 0) - ) - ) - ); - - $actual = $parser->parse(); - - $this->assertEquals($expected, $actual); - } - - public function testParsingPolygonValueWithSrid() - { - $value = 'SRID=4326;POLYGON((0 0,10 0,10 10,0 10,0 0))'; - $parser = new Parser($value); - $expected = array( - 'srid' => 4326, - 'type' => 'POLYGON', - 'value' => array( - array( - array(0, 0), - array(10, 0), - array(10, 10), - array(0, 10), - array(0, 0) - ) - ) - ); - - $actual = $parser->parse(); - - $this->assertEquals($expected, $actual); - } - - /** - * @expectedException \CrEOF\Geo\WKT\Exception\UnexpectedValueException - * @expectedExceptionMessage [Syntax Error] line 0, col 8: Error: Expected CrEOF\Geo\WKT\Lexer::T_OPEN_PARENTHESIS, got "0" in value "POLYGON(0 0,10 0,10 10,0 10,0 0)" - */ - public function testParsingPolygonValueMissingParenthesis() - { - $value = 'POLYGON(0 0,10 0,10 10,0 10,0 0)'; - $parser = new Parser($value); - - $parser->parse(); - } - - public function testParsingPolygonValueMultiRing() - { - $value = 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7,5 5))'; - $parser = new Parser($value); - $expected = array( - 'srid' => null, - 'type' => 'POLYGON', - 'value' => array( - array( - array(0, 0), - array(10, 0), - array(10, 10), - array(0, 10), - array(0, 0) - ), - array( - array(5, 5), - array(7, 5), - array(7, 7), - array(5, 7), - array(5, 5) - ) - ) - ); - - $actual = $parser->parse(); - - $this->assertEquals($expected, $actual); - } - - public function testParsingPolygonValueMultiRingWithSrid() - { - $value = 'SRID=4326;POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7,5 5))'; - $parser = new Parser($value); - $expected = array( - 'srid' => 4326, - 'type' => 'POLYGON', - 'value' => array( - array( - array(0, 0), - array(10, 0), - array(10, 10), - array(0, 10), - array(0, 0) - ), - array( - array(5, 5), - array(7, 5), - array(7, 7), - array(5, 7), - array(5, 5) - ) - ) - ); - - $actual = $parser->parse(); - - $this->assertEquals($expected, $actual); - } - - /** - * @expectedException \CrEOF\Geo\WKT\Exception\UnexpectedValueException - * @expectedExceptionMessage [Syntax Error] line 0, col 33: Error: Expected CrEOF\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))" - */ - public function testParsingPolygonValueMultiRingMissingComma() - { - $value = 'POLYGON((0 0,10 0,10 10,0 10,0 0)(5 5,7 5,7 7,5 7,5 5))'; - $parser = new Parser($value); - - $parser->parse(); - } -}