Source of file GeographyTest.php
Size: 4,677 Bytes - Last Modified: 2021-12-23T10:37:40+00:00
/var/www/docs.ssmods.com/process/src/tests/php/GeographyTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 | <?php namespace Smindel\GIS\Tests; use SilverStripe\Dev\SapphireTest; use SilverStripe\Core\Config\Config; use SilverStripe\ORM\DB; use Smindel\GIS\GIS; class GeographyTest extends SapphireTest { protected static $test_methods = [ 'Intersects' => ['Contains', 'Crosses', 'Equals', 'Intersects', 'Overlaps', 'Touches', 'Within'], ]; protected static $test_distances = [ 1000755 => 2, 1111950 => 8, 1223145 => 10, ]; public static function getExtraDataObjects() { if ( static::class == self::class && DB::get_schema()->geography(null) == 'geography' ) { static::$fixture_file = 'TestGeography.yml'; return [TestGeography::class]; } } public function testDbRoundTrip() { if ( static::class == GeographyTest::class && DB::get_schema()->geography(null) != 'geography' ) { $this->markTestSkipped('MySQL does not support Geography.'); } $class = $this->getExtraDataObjects()[0]; // write a geometry $geo = GIS::create([10,53.5]); $address = $class::create(); $address->GeoLocation = (string)$geo; $id = $address->write(); // read it back $address1 = $class::get()->byID($id); $this->assertEquals((string)$geo, $address1->GeoLocation); // change and check changed $wkt = GIS::create([174.5,-41.3]); $address->GeoLocation = (string)$wkt; $address->write(); $this->assertEquals((string)$wkt, $class::get()->byID($id)->GeoLocation); } public function testStGenericFilter() { if ( static::class == GeographyTest::class && DB::get_schema()->geography(null) != 'geography' ) { $this->markTestSkipped('MySQL does not support Geography.'); } $class = $this->getExtraDataObjects()[0]; $reference = $this->objFromFixture($class, 'reference'); $all = $class::get()->exclude('ID', $reference->ID) ->map() ->toArray(); foreach (static::$test_methods as $filter => $geometries) { $matches = $class::get() ->exclude('ID', $reference->ID) ->filter('GeoLocation:ST_' . $filter, $reference->GeoLocation) ->map() ->toArray(); asort($matches); $this->assertEquals($geometries, array_values($matches), $filter); $matches = $class::get() ->exclude('ID', $reference->ID) ->exclude('GeoLocation:ST_' . $filter, $reference->GeoLocation) ->map() ->toArray(); sort($matches); $nots = array_diff($all, $geometries); sort($nots); $this->assertEquals(array_values($nots), array_values($matches), $filter); } } public function testStDistanceFilter() { if ( static::class == GeographyTest::class && DB::get_schema()->geography(null) != 'geography' ) { $this->markTestSkipped('MySQL does not support Geography.'); } $class = $this->getExtraDataObjects()[0]; $reference = $this->objFromFixture($class, 'distance'); foreach (static::$test_distances as $distance => $count) { $within = $class::get() ->exclude('ID', $reference->ID) ->filter('GeoLocation:ST_Distance', [$reference->GeoLocation, $distance]); $notWithin = $class::get() ->exclude('ID', $reference->ID) ->filter('GeoLocation:ST_Distance:not', [$reference->GeoLocation, $distance]); $this->assertEquals($count, $within->count(), 'within ' . $distance); $this->assertEquals(10 - $count, $notWithin->count(), 'not within ' . $distance); } } public function testGeometryTypeFilter() { if ( static::class == GeographyTest::class && DB::get_schema()->geography(null) != 'geography' ) { $this->markTestSkipped('MySQL does not support Geography.'); } $class = $this->getExtraDataObjects()[0]; if ($class == TestGeography::class) { $this->markTestSkipped('GeometryTypeFilter does not yet work with Geography'); } $this->assertEquals(1, $class::get()->filter('GeoLocation:ST_GeometryType', 'MultiLineString')->count()); $this->assertEquals(3, $class::get()->filter('GeoLocation:ST_GeometryType:not', 'Polygon')->count()); } } |