3 declare(strict_types
=1);
5 namespace PhpMyAdmin\Tests\Gis
;
7 use PhpMyAdmin\Gis\GisPolygon
;
8 use PhpMyAdmin\Image\ImageWrapper
;
12 * @covers \PhpMyAdmin\Gis\GisPolygon
13 * @runTestsInSeparateProcesses
14 * @preserveGlobalState disabled
16 class GisPolygonTest
extends GisGeomTestCase
18 /** @var GisPolygon */
22 * Sets up the fixture, for example, opens a network connection.
23 * This method is called before a test is executed.
25 protected function setUp(): void
28 $this->object = GisPolygon
::singleton();
32 * Tears down the fixture, for example, closes a network connection.
33 * This method is called after a test is executed.
35 protected function tearDown(): void
42 * Provide some common data to data providers
44 * @return array common data for data providers
46 private function getData(): array
98 * data provider for testGenerateWkt
100 * @return array data for testGenerateWkt
102 public function providerForTestGenerateWkt(): array
105 0 => $this->getData(),
109 unset($temp1[0]['POLYGON'][1][3]['y']);
112 $temp2[0]['POLYGON']['no_of_lines'] = 0;
115 $temp3[0]['POLYGON'][1]['no_of_points'] = 3;
122 'POLYGON((35 10,10 20,15 40,45 45,35 10),(20 30,35 32,30 20,20 30))',
124 // values at undefined index
129 'POLYGON(( , , , ))',
131 // if a coordinate is missing, default is empty string
136 'POLYGON((35 10,10 20,15 40,45 45,35 10),(20 30,35 32,30 20,20 ))',
138 // missing coordinates are replaced with provided values (3rd parameter)
143 'POLYGON((35 10,10 20,15 40,45 45,35 10),(20 30,35 32,30 20,20 0))',
145 // should have at least one ring
150 'POLYGON((35 10,10 20,15 40,45 45,35 10))',
152 // a ring should have at least four points
157 'POLYGON((35 10,10 20,15 40,45 45,35 10),(20 30,35 32,30 20,20 30))',
163 * data provider for testGenerateParams
165 * @return array data for testGenerateParams
167 public function providerForTestGenerateParams(): array
169 $temp = $this->getData();
172 $temp1['gis_type'] = 'POLYGON';
176 '\'POLYGON((35 10,10 20,15 40,45 45,35 10),(20 30,35 32,30 20,20 30))\',124',
184 'POLYGON((35 10,10 20,15 40,45 45,35 10),(20 30,35 32,30 20,20 30))',
194 * @param array $ring array of points forming the ring
195 * @param float $area area of the ring
197 * @dataProvider providerForTestArea
199 public function testArea(array $ring, float $area): void
201 $this->assertEquals($area, $this->object->area($ring));
205 * data provider for testArea
207 * @return array data for testArea
209 public function providerForTestArea(): array
229 // first point of the ring repeated as the last point
251 // anticlockwise gives positive area
273 * test for isPointInsidePolygon
275 * @param array $point x, y coordinates of the point
276 * @param array $polygon array of points forming the ring
277 * @param bool $isInside output
279 * @dataProvider providerForTestIsPointInsidePolygon
281 public function testIsPointInsidePolygon(array $point, array $polygon, bool $isInside): void
285 $this->object->isPointInsidePolygon($point, $polygon)
290 * data provider for testIsPointInsidePolygon
292 * @return array data for testIsPointInsidePolygon
294 public function providerForTestIsPointInsidePolygon(): array
316 // point inside the ring
325 // point on an edge of the ring
334 // point on a vertex of the ring
343 // point outside the ring
356 * test for getPointOnSurface
358 * @param array $ring array of points forming the ring
360 * @dataProvider providerForTestGetPointOnSurface
362 public function testGetPointOnSurface(array $ring): void
364 $point = $this->object->getPointOnSurface($ring);
365 $this->assertIsArray($point);
366 $this->assertTrue($this->object->isPointInsidePolygon($point, $ring));
370 * data provider for testGetPointOnSurface
372 * @return array data for testGetPointOnSurface
374 public function providerForTestGetPointOnSurface(): array
376 $temp = $this->getData();
377 unset($temp['POLYGON'][0]['no_of_points']);
378 unset($temp['POLYGON'][1]['no_of_points']);
391 * data provider for testScaleRow
393 * @return array data for testScaleRow
395 public function providerForTestScaleRow(): array
399 'POLYGON((123 0,23 30,17 63,123 0))',
408 'POLYGON((35 10,10 20,15 40,45 45,35 10),(20 30,35 32,30 20,20 30)))',
420 * @requires extension gd
422 public function testPrepareRowAsPng(): void
424 $image = ImageWrapper
::create(200, 124, ['red' => 229, 'green' => 229, 'blue' => 229]);
425 $this->assertNotNull($image);
426 $return = $this->object->prepareRowAsPng(
427 'POLYGON((0 0,100 0,100 100,0 100,0 0),(10 10,10 40,40 40,40 10,10 10),(60 60,90 60,90 90,60 90,60 60))',
430 ['x' => -56, 'y' => -16, 'scale' => 0.94, 'height' => 124],
433 $this->assertEquals(200, $return->width());
434 $this->assertEquals(124, $return->height());
436 $fileExpected = $this->testDir
. '/polygon-expected.png';
437 $fileActual = $this->testDir
. '/polygon-actual.png';
438 $this->assertTrue($image->png($fileActual));
439 $this->assertFileEquals($fileExpected, $fileActual);
443 * test case for prepareRowAsPdf() method
445 * @param string $spatial GIS POLYGON object
446 * @param string $label label for the GIS POLYGON object
447 * @param int[] $color color for the GIS POLYGON object
448 * @param array $scale_data array containing data related to scaling
449 * @param TCPDF $pdf TCPDF instance
451 * @dataProvider providerForPrepareRowAsPdf
453 public function testPrepareRowAsPdf(
460 $return = $this->object->prepareRowAsPdf($spatial, $label, $color, $scale_data, $pdf);
462 $fileExpected = $this->testDir
. '/polygon-expected.pdf';
463 $fileActual = $this->testDir
. '/polygon-actual.pdf';
464 $return->Output($fileActual, 'F');
465 $this->assertFileEquals($fileExpected, $fileActual);
469 * data provider for testPrepareRowAsPdf() test case
471 * @return array test data for testPrepareRowAsPdf() test case
473 public function providerForPrepareRowAsPdf(): array
477 'POLYGON((0 0,100 0,100 100,0 100,0 0),(10 10,10 40,40 40,40 10,10 10),(60 60,90 60,90 90,60 90,60 6'
481 ['x' => -8, 'y' => -32, 'scale' => 1.80, 'height' => 297],
482 $this->createEmptyPdf('POLYGON'),
488 * test case for prepareRowAsSvg() method
490 * @param string $spatial GIS POLYGON object
491 * @param string $label label for the GIS POLYGON object
492 * @param int[] $color color for the GIS POLYGON object
493 * @param array $scaleData array containing data related to scaling
494 * @param string $output expected output
496 * @dataProvider providerForPrepareRowAsSvg
498 public function testPrepareRowAsSvg(
505 $svg = $this->object->prepareRowAsSvg($spatial, $label, $color, $scaleData);
506 $this->assertEquals($output, $svg);
510 * data provider for testPrepareRowAsSvg() test case
512 * @return array test data for testPrepareRowAsSvg() test case
514 public function providerForPrepareRowAsSvg(): array
518 'POLYGON((123 0,23 30,17 63,123 0),(99 12,30 35,25 55,99 12))',
527 '<path d=" M 222, 288 L 22, 228 L 10, 162 Z M 174, 264 L 36, 218 L 26, 178 Z " name="svg" id="svg12'
528 . '34567890" class="polygon vector" stroke="black" stroke-width="0.5" fill="#b02ee0" fill-rule="evenod'
529 . 'd" fill-opacity="0.8"/>',
535 * test case for prepareRowAsOl() method
537 * @param string $spatial GIS POLYGON object
538 * @param int $srid spatial reference ID
539 * @param string $label label for the GIS POLYGON object
540 * @param int[] $color color for the GIS POLYGON object
541 * @param array $scale_data array containing data related to scaling
542 * @param string $output expected output
544 * @dataProvider providerForPrepareRowAsOl
546 public function testPrepareRowAsOl(
554 $ol = $this->object->prepareRowAsOl($spatial, $srid, $label, $color, $scale_data);
555 $this->assertEquals($output, $ol);
559 * data provider for testPrepareRowAsOl() test case
561 * @return array test data for testPrepareRowAsOl() test case
563 public function providerForPrepareRowAsOl(): array
567 'POLYGON((123 0,23 30,17 63,123 0))',
577 'var style = new ol.style.Style({fill: new ol.style.Fill({"color":[176,46,224,0.8]'
578 . '}),stroke: new ol.style.Stroke({"color":[0,0,0],"width":0.5}),text: new ol.styl'
579 . 'e.Text({"text":"Ol"})});var minLoc = [0, 0];var maxLoc = [1, 1];var ext = ol.ex'
580 . 'tent.boundingExtent([minLoc, maxLoc]);ext = ol.proj.transformExtent(ext, ol.pro'
581 . 'j.get("EPSG:4326"), ol.proj.get(\'EPSG:3857\'));map.getView().fit(ext, map.getS'
582 . 'ize());var arr = [];var lineArr = [];var line = new ol.geom.LinearRing(new Arra'
583 . 'y((new ol.geom.Point([123,0]).transform(ol.proj.get("EPSG:4326"), ol.proj.get('
584 . '\'EPSG:3857\'))).getCoordinates(), (new ol.geom.Point([23,30]).transform(ol.pro'
585 . 'j.get("EPSG:4326"), ol.proj.get(\'EPSG:3857\'))).getCoordinates(), (new ol.geom'
586 . '.Point([17,63]).transform(ol.proj.get("EPSG:4326"), ol.proj.get(\'EPSG:3857\'))'
587 . ').getCoordinates(), (new ol.geom.Point([123,0]).transform(ol.proj.get("EPSG:432'
588 . '6"), ol.proj.get(\'EPSG:3857\'))).getCoordinates()));var coord = line.getCoordi'
589 . 'nates();for (var i = 0; i < coord.length; i++) lineArr.push(coord[i]);arr.push(lineArr);'
590 . 'var polygon = new ol.geom.Polygon(arr);var feature = new ol.Feature({geometry: polygon});f'
591 . 'eature.setStyle(style);vectorLayer.addFeature(feature);',
597 * test case for isOuterRing() method
599 * @param array $ring coordinates of the points in a ring
601 * @dataProvider providerForIsOuterRing
603 public function testIsOuterRing(array $ring): void
605 $this->assertTrue($this->object->isOuterRing($ring));
609 * data provider for testIsOuterRing() test case
611 * @return array test data for testIsOuterRing() test case
613 public function providerForIsOuterRing(): array