test.php in geoPHP 7
File
geoPHP/tests/test.php
View source
<?php
if (getenv("GEOPHP_RUN_TESTS") == 1) {
run_test();
}
else {
print "Skipping tests. Please set GEOPHP_RUN_TESTS=1 environment variable if you wish to run tests\n";
}
function run_test() {
set_time_limit(0);
set_error_handler("FailOnError");
header("Content-type: text");
include_once '../geoPHP.inc';
if (geoPHP::geosInstalled()) {
print "GEOS is installed.\n";
}
else {
print "GEOS is not installed.\n";
}
foreach (scandir('./input') as $file) {
$parts = explode('.', $file);
if ($parts[0]) {
$format = $parts[1];
$value = file_get_contents('./input/' . $file);
print '---- Testing ' . $file . "\n";
$geometry = geoPHP::load($value, $format);
test_adapters($geometry, $format, $value);
test_methods($geometry);
test_geometry($geometry);
test_detection($value, $format, $file);
}
}
print "\33[32m" . "PASS" . "\33[39m\n";
}
function test_geometry($geometry) {
$geometry
->area();
$geometry
->boundary();
$geometry
->envelope();
$geometry
->getBBox();
$geometry
->centroid();
$geometry
->length();
$geometry
->greatCircleLength();
$geometry
->haversineLength();
$geometry
->y();
$geometry
->x();
$geometry
->numGeometries();
$geometry
->geometryN(1);
$geometry
->startPoint();
$geometry
->endPoint();
$geometry
->isRing();
$geometry
->isClosed();
$geometry
->numPoints();
$geometry
->pointN(1);
$geometry
->exteriorRing();
$geometry
->numInteriorRings();
$geometry
->interiorRingN(1);
$geometry
->dimension();
$geometry
->geometryType();
$geometry
->SRID();
$geometry
->setSRID(4326);
$geometry
->getCentroid();
$geometry
->getArea();
$geometry
->getX();
$geometry
->getY();
$geometry
->getGeos();
$geometry
->getGeomType();
$geometry
->getSRID();
$geometry
->asText();
$geometry
->asBinary();
$geometry
->geos();
$geometry
->setGeos($geometry
->geos());
$geometry
->pointOnSurface();
$geometry
->equals($geometry);
$geometry
->equalsExact($geometry);
$geometry
->relate($geometry);
$geometry
->checkValidity();
$geometry
->isSimple();
$geometry
->buffer(10);
$geometry
->intersection($geometry);
$geometry
->convexHull();
$geometry
->difference($geometry);
$geometry
->symDifference($geometry);
$geometry
->union($geometry);
$geometry
->simplify(0);
$geometry
->disjoint($geometry);
$geometry
->touches($geometry);
$geometry
->intersects($geometry);
$geometry
->crosses($geometry);
$geometry
->within($geometry);
$geometry
->contains($geometry);
$geometry
->overlaps($geometry);
$geometry
->covers($geometry);
$geometry
->coveredBy($geometry);
$geometry
->distance($geometry);
$geometry
->hausdorffDistance($geometry);
$geometry
->hasZ();
$geometry
->is3D();
$geometry
->isMeasured();
$geometry
->isEmpty();
$geometry
->coordinateDimension();
$geometry
->z();
$geometry
->m();
}
function test_adapters($geometry, $format, $input) {
foreach (geoPHP::getAdapterMap() as $adapter_key => $adapter_class) {
if ($adapter_key != 'google_geocode') {
$output = $geometry
->out($adapter_key);
if ($output) {
$adapter_loader = new $adapter_class();
$test_geom_1 = $adapter_loader
->read($output);
$test_geom_2 = $adapter_loader
->read($test_geom_1
->out($adapter_key));
if ($test_geom_1
->out('wkt') != $test_geom_2
->out('wkt')) {
print "Mismatched adapter output in " . $adapter_class . "\n";
}
}
else {
print "Empty output on " . $adapter_key . "\n";
}
}
}
if (!geoPHP::geosInstalled()) {
return;
}
foreach (geoPHP::getAdapterMap() as $adapter_key => $adapter_class) {
if ($adapter_key != 'google_geocode') {
geoPHP::geosInstalled(TRUE);
$output = $geometry
->out($adapter_key);
if ($output) {
$adapter_loader = new $adapter_class();
$test_geom_1 = $adapter_loader
->read($output);
geoPHP::geosInstalled(FALSE);
$test_geom_2 = $adapter_loader
->read($output);
geoPHP::geosInstalled(TRUE);
if ($test_geom_1
->out('wkt') != $test_geom_2
->out('wkt')) {
print "Mismatched adapter output between GEOS and NORM in " . $adapter_class . "\n";
}
}
}
}
}
function test_methods($geometry) {
if (!geoPHP::geosInstalled()) {
return;
}
$methods = array(
'envelope',
'getBBox',
'x',
'y',
'startPoint',
'endPoint',
'isRing',
'isClosed',
'numPoints',
);
foreach ($methods as $method) {
geoPHP::geosInstalled(TRUE);
$geos_result = $geometry
->{$method}();
geoPHP::geosInstalled(FALSE);
$norm_result = $geometry
->{$method}();
geoPHP::geosInstalled(TRUE);
$geos_type = gettype($geos_result);
$norm_type = gettype($norm_result);
if ($geos_type != $norm_type) {
print 'Type mismatch on ' . $method . "\n";
continue;
}
if ($geos_type == 'object') {
$haus_dist = $geos_result
->hausdorffDistance(geoPHP::load($norm_result
->out('wkt'), 'wkt'));
$bb = $geos_result
->getBBox();
$scale = sqrt(($bb['maxy'] - $bb['miny'] ^ 2) + ($bb['maxx'] - $bb['minx'] ^ 2));
if ($haus_dist / $scale > 0.5) {
print 'Output mismatch on ' . $method . ":\n";
print 'GEOS : ' . $geos_result
->out('wkt') . "\n";
print 'NORM : ' . $norm_result
->out('wkt') . "\n";
continue;
}
}
if ($geos_type == 'boolean' || $geos_type == 'string') {
if ($geos_result !== $norm_result) {
print 'Output mismatch on ' . $method . ":\n";
print 'GEOS : ' . (string) $geos_result . "\n";
print 'NORM : ' . (string) $norm_result . "\n";
continue;
}
}
}
}
function test_detection($value, $format, $file) {
$detected = geoPHP::detectFormat($value);
if ($detected != $format) {
if ($detected) {
print 'detected as ' . $detected . "\n";
}
else {
print "format not detected\n";
}
}
geoPHP::load($value);
}
function FailOnError($error_level, $error_message, $error_file, $error_line, $error_context) {
echo "{$error_level}: {$error_message} in {$error_file} on line {$error_line}\n";
echo "\33[31m" . "FAIL" . "\33[39m\n";
exit(1);
}