Skip to content

Commit

Permalink
Updated basic lib kml parser tests #91
Browse files Browse the repository at this point in the history
  • Loading branch information
alexboia committed Feb 9, 2024
1 parent 5f49f4c commit c81f419
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 9 deletions.
6 changes: 5 additions & 1 deletion lib/3rdParty/kml-parser/KmlParser/LatLngAlt.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class LatLngAlt {

const MAX_LATITUDE = 90;

const MIN_ALTITUDE = -PHP_FLOAT_MAX;

const MAX_ALTITUDE = PHP_FLOAT_MAX;

private $longitude = null;

private $latitude = null;
Expand Down Expand Up @@ -58,7 +62,7 @@ private function isValidLatitude($latitude): bool {
}

private function isValidAltitude($altitude): bool {
return !is_null($altitude) && $altitude > PHP_FLOAT_MIN && $altitude < PHP_FLOAT_MAX;
return !is_null($altitude) && $altitude > self::MIN_ALTITUDE && $altitude < self::MAX_ALTITUDE;
}

public function getLatitude(): float|null {
Expand Down
7 changes: 6 additions & 1 deletion lib/3rdParty/kml-parser/KmlParser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use StepanDalecky\KmlParser\Entities\Entity;
use StepanDalecky\KmlParser\Entities\Kml;
use StepanDalecky\KmlParser\Exceptions\InvalidKmlRootElementException;
use StepanDalecky\XmlElement\Element;

class Parser extends Entity {
Expand All @@ -18,6 +19,10 @@ public static function fromFile(string $file): self {

public static function fromString(string $string): self {
$element = new Element(new \SimpleXMLElement($string));
return new self($element);
if ($element->is('kml')) {
return new self($element);
} else {
throw new InvalidKmlRootElementException($element->getName());
}
}
}
6 changes: 5 additions & 1 deletion lib/3rdParty/kml-parser/XmlElement/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ public function hasAttributes(): bool {
return (bool) $this->getAttributes();
}

private function getName(): string {
public function getName(): string {
return $this->xmlElement->getName();
}

public function is(string $tagName): bool {
return strtolower($this->getName()) === strtolower($tagName);
}
}
3 changes: 3 additions & 0 deletions tests/assets/test-kml-empty.kml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
</kml>
73 changes: 72 additions & 1 deletion tests/test-LibKmlParser.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use StepanDalecky\KmlParser\Entities\Kml;
use StepanDalecky\KmlParser\Parser;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectException;

Expand Down Expand Up @@ -37,11 +38,81 @@ class LibKmlParserTests extends WP_UnitTestCase {
use GenericTestHelpers;
use TestDataFileHelpers;

public function test_handParseKml() {
public function test_canParseEmptyKml() {
$fileContents = $this->_readTestDataFileContents('test-kml-empty.kml');
$kmlParser = Parser::fromString($fileContents);
$kml = $kmlParser->getKml();
$this->assertNotNull($kml);
$this->_assertEmptyKml($kml);
}

private function _assertEmptyKml(Kml $kml) {
$this->assertFalse($kml->hasDocument());
$this->assertFalse($kml->hasFolder());
}

public function test_canParseKml_documentAsRoot_noFolders_singlePlacemark() {
$fileContents = $this->_readTestDataFileContents('test-kml-document-as-root-no-folders.kml');
$kmlParser = Parser::fromString($fileContents);

$kml = $kmlParser->getKml();
$this->assertTrue($kml->hasDocument());
$this->assertFalse($kml->hasFolder());

$document = $kml->getDocument();
$this->assertNotNull($document);

$this->assertTrue($document->hasName());
$this->assertEquals('Azuga - Zizin - Vama Buzaului - Brasov.kml', $document->getName());

$this->assertTrue($document->hasPlacemarks());

$placemarks = $document->getPlacemarks();
$this->assertEquals(1, count($placemarks));

$pk0 = $placemarks[0];
$this->assertNotNull($pk0);
$this->assertTrue($pk0->hasName());
$this->assertEquals('Azuga - Zizin - Vama Buzaului - Brasov', $pk0->getName());

$this->assertTrue($pk0->hasLineString());
$this->assertFalse($pk0->hasLinearRing());
$this->assertFalse($pk0->hasPoint());
$this->assertFalse($pk0->hasPolygon());
$this->assertFalse($pk0->hasMultiGeometry());

$ls0 = $pk0->getLineString();
$this->assertNotNull($ls0);
$this->assertTrue($ls0->hasCoordinates());

$coords0 = $ls0->getCoordinates();
$this->assertNotNull($coords0);

$latLngs0 = $coords0->getLatLngAltCollection();
$this->assertNotNull($latLngs0);

$this->assertGreaterThan(10, count($latLngs0));

$p0 = $latLngs0[0];
$this->assertTrue($p0->hasLongitude());
$this->assertTrue($p0->hasLatitude());
$this->assertTrue($p0->hasAltitude());
$this->assertFalse($p0->isEmpty());

$this->assertEquals(25.55129386596317, $p0->getLongitude(), '', 0.0001);
$this->assertEquals(45.44035592109068, $p0->getLatitude(), '', 0.0001);
$this->assertEquals(0, $p0->getAltitude(), '', 0.0001);


$pN = $latLngs0[count($latLngs0) - 1];
$this->assertTrue($pN->hasLongitude());
$this->assertTrue($pN->hasLatitude());
$this->assertTrue($pN->hasAltitude());
$this->assertFalse($pN->isEmpty());

$this->assertEquals(25.99871164164519, $pN->getLongitude(), '', 0.0001);
$this->assertEquals(45.65004792577662, $pN->getLatitude(), '', 0.0001);
$this->assertEquals(0, $pN->getAltitude(), '', 0.0001);
}

protected static function _getRootTestsDir() {
Expand Down
5 changes: 0 additions & 5 deletions tests/test-LibKmlParserLatLngAltCollection.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?php

use StepanDalecky\KmlParser\Entities\LineString;
use StepanDalecky\KmlParser\LatLngAlt;
use StepanDalecky\KmlParser\LatLngAltCollection;
use StepanDalecky\KmlParser\Parser;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectException;

/**
* Copyright (c) 2014-2024 Alexandru Boia and Contributors
Expand Down

0 comments on commit c81f419

Please sign in to comment.