diff --git a/composer.json b/composer.json index b5b179de..c45ff203 100644 --- a/composer.json +++ b/composer.json @@ -25,5 +25,10 @@ "psr-0": { "CrEOF\\Spatial": "lib/" } + }, + "autoload-dev": { + "psr-0": { + "CrEOF\\Spatial\\Tests": "tests/" + } } } diff --git a/lib/CrEOF/Spatial/PHP/Types/AbstractGeometry.php b/lib/CrEOF/Spatial/PHP/Types/AbstractGeometry.php index adc5a21e..47ec59e3 100644 --- a/lib/CrEOF/Spatial/PHP/Types/AbstractGeometry.php +++ b/lib/CrEOF/Spatial/PHP/Types/AbstractGeometry.php @@ -99,7 +99,7 @@ protected function validatePointValue($point) case ($point instanceof AbstractPoint): return $point->toArray(); break; - case (is_array($point) && count($point) == 2 && is_numeric($point[0]) && is_numeric($point[1])): + case (is_array($point) && count($point) === 2 && is_numeric($point[0]) && is_numeric($point[1])): return array_values($point); break; default: @@ -115,14 +115,8 @@ protected function validatePointValue($point) */ protected function validateRingValue($ring) { - switch (true) { - case ($ring instanceof AbstractLineString): - $ring = $ring->toArray(); - break; - case (is_array($ring)): - break; - default: - throw new InvalidValueException(sprintf('Invalid %s LineString value of type "%s"', $this->getType(), (is_object($ring) ? get_class($ring) : gettype($ring)))); + if (! (is_array($ring) || $ring instanceof AbstractLineString)) { + throw new InvalidValueException(sprintf('Invalid %s LineString value of type "%s"', $this->getType(), (is_object($ring) ? get_class($ring) : gettype($ring)))); } $ring = $this->validateLineStringValue($ring); @@ -187,6 +181,7 @@ protected function validateMultiPolygonValue(array $polygons) if ($polygon instanceof GeometryInterface) { $polygon = $polygon->toArray(); } + $polygon = $this->validatePolygonValue($polygon); } diff --git a/lib/CrEOF/Spatial/PHP/Types/AbstractPoint.php b/lib/CrEOF/Spatial/PHP/Types/AbstractPoint.php index cc9d6c4f..9070c577 100644 --- a/lib/CrEOF/Spatial/PHP/Types/AbstractPoint.php +++ b/lib/CrEOF/Spatial/PHP/Types/AbstractPoint.php @@ -64,16 +64,20 @@ public function __construct() */ public function setX($x) { - $parser = new Parser($x); - - try { - $this->x = (float) $parser->parse(); - } catch (RangeException $e) { - throw new InvalidValueException($e->getMessage(), $e->getCode(), $e->getPrevious()); - } catch (UnexpectedValueException $e) { - throw new InvalidValueException($e->getMessage(), $e->getCode(), $e->getPrevious()); + if (! is_numeric($x)) { + $parser = new Parser($x); + + try { + $x = $parser->parse(); + } catch (RangeException $e) { + throw new InvalidValueException($e->getMessage(), $e->getCode(), $e->getPrevious()); + } catch (UnexpectedValueException $e) { + throw new InvalidValueException($e->getMessage(), $e->getCode(), $e->getPrevious()); + } } + $this->x = (float) $x; + return $this; } @@ -93,16 +97,20 @@ public function getX() */ public function setY($y) { - $parser = new Parser($y); - - try { - $this->y = (float) $parser->parse(); - } catch (RangeException $e) { - throw new InvalidValueException($e->getMessage(), $e->getCode(), $e->getPrevious()); - } catch (UnexpectedValueException $e) { - throw new InvalidValueException($e->getMessage(), $e->getCode(), $e->getPrevious()); + if (! is_numeric($y)) { + $parser = new Parser($y); + + try { + $y = $parser->parse(); + } catch (RangeException $e) { + throw new InvalidValueException($e->getMessage(), $e->getCode(), $e->getPrevious()); + } catch (UnexpectedValueException $e) { + throw new InvalidValueException($e->getMessage(), $e->getCode(), $e->getPrevious()); + } } + $this->y = (float) $y; + return $this; } @@ -177,12 +185,12 @@ protected function validateArguments(array $argv = null) { $argc = count($argv); - if (1 == $argc && is_array($argv[0])) { + if (1 === $argc && is_array($argv[0])) { return $argv[0]; } - if (2 == $argc) { - if (is_array($argv[0]) && (is_numeric($argv[1]) || is_null($argv[1]) || is_string($argv[1]))) { + if (2 === $argc) { + if (is_array($argv[0]) && (is_numeric($argv[1]) || null === $argv[1] || is_string($argv[1]))) { $argv[0][] = $argv[1]; return $argv[0]; @@ -193,10 +201,8 @@ protected function validateArguments(array $argv = null) } } - if (3 == $argc) { - if ((is_numeric($argv[0]) || is_string($argv[0])) && (is_numeric($argv[1]) || is_string($argv[1])) && (is_numeric($argv[2]) || is_null($argv[2]) || is_string($argv[2]))) { - return $argv; - } + if (3 === $argc && ((is_numeric($argv[0]) || is_string($argv[0])) && (is_numeric($argv[1]) || is_string($argv[1])) && (is_numeric($argv[2]) || null === $argv[2] || is_string($argv[2])))) { + return $argv; } array_walk($argv, function (&$value) { diff --git a/lib/CrEOF/Spatial/PHP/Types/AbstractPolygon.php b/lib/CrEOF/Spatial/PHP/Types/AbstractPolygon.php index 0acf4bf2..8d0473f0 100644 --- a/lib/CrEOF/Spatial/PHP/Types/AbstractPolygon.php +++ b/lib/CrEOF/Spatial/PHP/Types/AbstractPolygon.php @@ -79,7 +79,7 @@ public function getRings() */ public function getRing($index) { - if (-1 == $index) { + if (-1 === $index) { $index = count($this->rings) - 1; } diff --git a/lib/CrEOF/Spatial/PHP/Types/Geography/Point.php b/lib/CrEOF/Spatial/PHP/Types/Geography/Point.php index 62b1b9d8..0f2135e2 100644 --- a/lib/CrEOF/Spatial/PHP/Types/Geography/Point.php +++ b/lib/CrEOF/Spatial/PHP/Types/Geography/Point.php @@ -45,16 +45,19 @@ class Point extends AbstractPoint implements GeographyInterface */ public function setX($x) { - $parser = new Parser($x); + if (!is_numeric($x)) { + $parser = new Parser($x); - try { - $x = (float) $parser->parse(); - } catch (RangeException $e) { - throw new InvalidValueException($e->getMessage(), $e->getCode(), $e->getPrevious()); - } catch (UnexpectedValueException $e) { - throw new InvalidValueException($e->getMessage(), $e->getCode(), $e->getPrevious()); + try { + $x = $parser->parse(); + } catch (RangeException $e) { + throw new InvalidValueException($e->getMessage(), $e->getCode(), $e->getPrevious()); + } catch (UnexpectedValueException $e) { + throw new InvalidValueException($e->getMessage(), $e->getCode(), $e->getPrevious()); + } } + $x = (float) $x; if ($x < -180 || $x > 180) { throw new InvalidValueException(sprintf('Invalid longitude value "%s", must be in range -180 to 180.', $x)); } @@ -72,16 +75,19 @@ public function setX($x) */ public function setY($y) { - $parser = new Parser($y); + if (!is_numeric($y)) { + $parser = new Parser($y); - try { - $y = (float) $parser->parse(); - } catch (RangeException $e) { - throw new InvalidValueException($e->getMessage(), $e->getCode(), $e->getPrevious()); - } catch (UnexpectedValueException $e) { - throw new InvalidValueException($e->getMessage(), $e->getCode(), $e->getPrevious()); + try { + $y = $parser->parse(); + } catch (RangeException $e) { + throw new InvalidValueException($e->getMessage(), $e->getCode(), $e->getPrevious()); + } catch (UnexpectedValueException $e) { + throw new InvalidValueException($e->getMessage(), $e->getCode(), $e->getPrevious()); + } } + $y = (float) $y; if ($y < -90 || $y > 90) { throw new InvalidValueException(sprintf('Invalid latitude value "%s", must be in range -90 to 90.', $y)); } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 329337fb..3d835ab7 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,14 +1,17 @@ - + - + ./tests/CrEOF/Spatial/Tests diff --git a/tests/CrEOF/Spatial/Tests/DBAL/Types/SchemaTest.php b/tests/CrEOF/Spatial/Tests/DBAL/Types/SchemaTest.php index 7a92b5fd..6b5f1324 100644 --- a/tests/CrEOF/Spatial/Tests/DBAL/Types/SchemaTest.php +++ b/tests/CrEOF/Spatial/Tests/DBAL/Types/SchemaTest.php @@ -64,7 +64,7 @@ public function testDoctrineTypeMapping() $databaseTypes = $type->getMappedDatabaseTypes($platform); foreach ($databaseTypes as $databaseType) { - $typeMapping = $this->getPlatform()->getDoctrineTypeMapping($databaseType); + $typeMapping = $platform->getDoctrineTypeMapping($databaseType); $this->assertEquals($doctrineType, $typeMapping); } diff --git a/tests/CrEOF/Spatial/Tests/OrmTestCase.php b/tests/CrEOF/Spatial/Tests/OrmTestCase.php index dd3f99f0..df711913 100644 --- a/tests/CrEOF/Spatial/Tests/OrmTestCase.php +++ b/tests/CrEOF/Spatial/Tests/OrmTestCase.php @@ -198,7 +198,7 @@ protected function setUp() */ protected function getEntityManager() { - if (isset($this->entityManager)) { + if (null !== $this->entityManager) { return $this->entityManager; } @@ -209,7 +209,8 @@ protected function getEntityManager() $config = new Configuration(); - $config->setMetadataCacheImpl(new ArrayCache); + $config->setMetadataCacheImpl(new ArrayCache()); + $config->setResultCacheImpl(new ArrayCache()); $config->setProxyDir(__DIR__ . '/Proxies'); $config->setProxyNamespace('CrEOF\Spatial\Tests\Proxies'); $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(array(realpath(__DIR__ . '/Fixtures')), true)); diff --git a/tests/CrEOF/Spatial/Tests/PHP/Types/Geography/LineStringTest.php b/tests/CrEOF/Spatial/Tests/PHP/Types/Geography/LineStringTest.php new file mode 100644 index 00000000..1b6b4526 --- /dev/null +++ b/tests/CrEOF/Spatial/Tests/PHP/Types/Geography/LineStringTest.php @@ -0,0 +1,89 @@ + + * @license http://dlambert.mit-license.org MIT + * + * @group php + */ +class LineStringTest extends \PHPUnit_Framework_TestCase +{ + + /** + * @expectedException \CrEOF\Spatial\Exception\InvalidValueException + * @expectedExceptionMessage Invalid longitude value "550", must be in range -180 to 180. + */ + public function testLineStringFromObjectsToArray() + { + $expected = array( + array(550, 550), + array(551, 551), + array(552, 552), + array(553, 553) + ); + $lineString = new LineString(array( + new Point(550, 550), + new Point(551, 551), + new Point(552, 552), + new Point(553, 553) + )); + + $this->assertCount(4, $lineString->getPoints()); + $this->assertEquals($expected, $lineString->toArray()); + } + + /** + * @expectedException \CrEOF\Spatial\Exception\InvalidValueException + * @expectedExceptionMessage Invalid longitude value "550", must be in range -180 to 180. + */ + public function testLineStringFromArraysGetPoints() + { + $expected = array( + array(550, 550), + array(551, 551), + array(552, 552), + array(553, 553) + ); + $lineString = new LineString( + array( + array(550, 550), + array(551, 551), + array(552, 552), + array(553, 553) + ) + ); + $actual = $lineString->getPoints(); + + $this->assertCount(4, $actual); + $this->assertEquals($expected, $actual); + } +} diff --git a/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/LineStringTest.php b/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/LineStringTest.php index ba5949e1..581a2c44 100644 --- a/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/LineStringTest.php +++ b/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/LineStringTest.php @@ -21,7 +21,7 @@ * SOFTWARE. */ -namespace CrEOF\Spatial\Tests\PHP\Types\Spatial\Geometry; +namespace CrEOF\Spatial\Tests\PHP\Types\Geometry; use CrEOF\Spatial\PHP\Types\Geometry\LineString; use CrEOF\Spatial\PHP\Types\Geometry\Point; diff --git a/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/MultiLineStringTest.php b/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/MultiLineStringTest.php index 7713d430..d8d89de1 100644 --- a/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/MultiLineStringTest.php +++ b/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/MultiLineStringTest.php @@ -131,8 +131,6 @@ public function testSolidMultiLineStringFromArraysGetRings() $this->assertEquals($expected, $multiLineString->getLineStrings()); } - - public function testSolidMultiLineStringAddRings() { $expected = array( diff --git a/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/MultiPointTest.php b/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/MultiPointTest.php index 508be58a..856b872a 100644 --- a/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/MultiPointTest.php +++ b/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/MultiPointTest.php @@ -21,7 +21,7 @@ * SOFTWARE. */ -namespace CrEOF\Spatial\Tests\PHP\Types\Spatial\Geometry; +namespace CrEOF\Spatial\Tests\PHP\Types\Geometry; use CrEOF\Spatial\PHP\Types\Geometry\MultiPoint; use CrEOF\Spatial\PHP\Types\Geometry\Point; @@ -84,8 +84,6 @@ public function testMultiPointFromArraysGetPoints() $this->assertEquals($expected, $actual); } - - public function testMultiPointAddPoints() { $expected = array( diff --git a/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/MultiPolygonTest.php b/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/MultiPolygonTest.php index 4feb5b1f..dcc2ec07 100644 --- a/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/MultiPolygonTest.php +++ b/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/MultiPolygonTest.php @@ -1,14 +1,14 @@ * @license http://dlambert.mit-license.org MIT @@ -204,8 +204,6 @@ public function testSolidMultiPolygonAddPolygon() $this->assertEquals($expected, $multiPolygon->getPolygons()); } - - public function testMultiPolygonFromObjectsGetSinglePolygon() { $polygon1 = new Polygon( diff --git a/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/PointTest.php b/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/PointTest.php index bd14aef5..9cd353da 100644 --- a/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/PointTest.php +++ b/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/PointTest.php @@ -35,8 +35,7 @@ */ class PointTest extends \PHPUnit_Framework_TestCase { - - public function testGoodNumericPoint() + public function testGoodNumericPoints() { $point1 = new Point(-73.7562317, 42.6525793); @@ -49,8 +48,12 @@ public function testGoodNumericPoint() $this->assertEquals(40.446111111111, $point1->getLatitude()); $this->assertEquals(-79.948611111111, $point1->getLongitude()); - } + $point2 = new Point(0.00001, 0.00003); + + $this->assertEquals(0.00003, $point2->getLatitude()); + $this->assertEquals(0.00001, $point2->getLongitude()); + } public function testGoodStringPoints() { @@ -240,7 +243,6 @@ public function testMissingArguments() new Point(); } - /** * Test bad string parameters - Two invalid parameters * diff --git a/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/PolygonTest.php b/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/PolygonTest.php index 226d1d99..eda7424e 100644 --- a/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/PolygonTest.php +++ b/tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/PolygonTest.php @@ -26,6 +26,7 @@ use CrEOF\Spatial\PHP\Types\Geometry\LineString; use CrEOF\Spatial\PHP\Types\Geometry\Point; use CrEOF\Spatial\PHP\Types\Geometry\Polygon; + /** * Polygon object tests * @@ -99,7 +100,6 @@ public function testSolidPolygonFromArraysGetRings() $this->assertEquals($expected, $polygon->getRings()); } - public function testSolidPolygonFromArrayAddRings() { $expected = array( diff --git a/tests/CrEOF/Spatial/Tests/TestInit.php b/tests/CrEOF/Spatial/Tests/TestInit.php deleted file mode 100644 index 13ee135e..00000000 --- a/tests/CrEOF/Spatial/Tests/TestInit.php +++ /dev/null @@ -1,10 +0,0 @@ -add('CrEOF\Spatial\Tests', __DIR__ . '/../../..'); -$loader->add('Doctrine\Tests', __DIR__ . '/../../../../vendor/doctrine/orm/tests'); -$loader->register(); diff --git a/tests/travis/composer.orm2.3.json b/tests/travis/composer.orm2.3.json index bc2ee258..5a1c184c 100644 --- a/tests/travis/composer.orm2.3.json +++ b/tests/travis/composer.orm2.3.json @@ -25,5 +25,10 @@ "psr-0": { "CrEOF\\Spatial": "lib/" } + }, + "autoload-dev": { + "psr-0": { + "CrEOF\\Spatial\\Tests": "tests/" + } } } diff --git a/tests/travis/composer.orm2.4.json b/tests/travis/composer.orm2.4.json index 2ac28127..2f36dc5a 100644 --- a/tests/travis/composer.orm2.4.json +++ b/tests/travis/composer.orm2.4.json @@ -25,5 +25,10 @@ "psr-0": { "CrEOF\\Spatial": "lib/" } + }, + "autoload-dev": { + "psr-0": { + "CrEOF\\Spatial\\Tests": "tests/" + } } } diff --git a/tests/travis/composer.orm2.5.json b/tests/travis/composer.orm2.5.json index 0338cfbf..71c2cda7 100644 --- a/tests/travis/composer.orm2.5.json +++ b/tests/travis/composer.orm2.5.json @@ -25,5 +25,10 @@ "psr-0": { "CrEOF\\Spatial": "lib/" } + }, + "autoload-dev": { + "psr-0": { + "CrEOF\\Spatial\\Tests": "tests/" + } } } diff --git a/tests/travis/travis.mysql.xml b/tests/travis/travis.mysql.xml index 03cbcfc5..1d70b2ac 100644 --- a/tests/travis/travis.mysql.xml +++ b/tests/travis/travis.mysql.xml @@ -1,15 +1,17 @@ - + - + ../CrEOF/Spatial/Tests diff --git a/tests/travis/travis.pgsql.xml b/tests/travis/travis.pgsql.xml index c1d326aa..d87c11d5 100644 --- a/tests/travis/travis.pgsql.xml +++ b/tests/travis/travis.pgsql.xml @@ -1,15 +1,17 @@ - + - + ../CrEOF/Spatial/Tests