Skip to content

Commit

Permalink
Updating tests - making it so we can turn GEOS on and off for testing…
Browse files Browse the repository at this point in the history
… purposes
  • Loading branch information
phayes committed Jan 9, 2012
1 parent 4957f88 commit 71fb4fa
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
7 changes: 4 additions & 3 deletions geoPHP.inc
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ class geoPHP
);
}

static function geosInstalled() {
static $geos_intalled = NULL;
if ($geos_intalled !== NULL) {
static function geosInstalled($force = NULL) {
static $geos_installed = NULL;
if ($force !== NULL) $geos_installed = $force;
if ($geos_installed !== NULL) {
return $geos_installed;
}
$geos_installed = class_exists('GEOSGeometry');
Expand Down
2 changes: 1 addition & 1 deletion lib/geometry/Geometry.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function asBinary() {
// ---------------------------
public function geos() {
// If it's already been set, just return it
if ($this->geos !== NULL) {
if ($this->geos && geoPHP::geosInstalled()) {
return $this->geos;
}
// It hasn't been set yet, generate it
Expand Down
72 changes: 69 additions & 3 deletions tests/test.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php
header("Content-type: text");

include_once('../geoPHP.inc');

if (geoPHP::geosInstalled()) {
print "GEOS is installed. ";
print "GEOS is installed.\n";
}
else {
print "GEOS is not installed. ";
print "GEOS is not installed.\n";
}

foreach (scandir('./input') as $file) {
Expand All @@ -15,6 +16,8 @@
$format = $parts[1];
$value = file_get_contents('./input/'.$file);
$geometry = geoPHP::load($value, $format);
print '---- Testing '.$file."\n";
test_methods($geometry);
test_geometry($geometry);
}
}
Expand Down Expand Up @@ -111,4 +114,67 @@ function test_geometry($geometry, $test_adapters = TRUE) {

}

print "Done! Test passes!";

function test_methods($geometry) {
// Cannot test methods if GEOS is not intstalled
if (!geoPHP::geosInstalled()) return;

$methods = array(
//'boundary', //@@TODO: Uncomment this and fix errors
'envelope', //@@TODO: Testing reveales errors in this method
'getBBox',
'centroid',
'x',
'y',
'startPoint',
'endPoint',
'isRing',
'isClosed',
'numPoints',
'getCoordinates',
);

foreach ($methods as $method) {
// Turn GEOS on
geoPHP::geosInstalled(TRUE);
$geos_result = $geometry->$method();

// Turn GEOS off
geoPHP::geosInstalled(FALSE);
$norm_result = $geometry->$method();

$geos_type = gettype($geos_result);
$norm_type = gettype($norm_result);

if ($geos_type != $norm_type) {
print 'Type mismatch on '.$method."\n";
var_dump($geos_type);
var_dump($norm_type);
continue;
}

// Now check base on type
if ($geos_type == 'object') {
$geos_wkt = $geos_result->out('wkt');
$norm_wkt = $norm_result->out('wkt');

// Round - we can't expect them to be identitcal
$geos_wkt = preg_replace_callback("/[-+]?[0-9]*\.?[0-9]+/", create_function('$matches','return round($matches[0]);'), $geos_wkt);
$norm_wkt = preg_replace_callback("/[-+]?[0-9]*\.?[0-9]+/", create_function('$matches','return round($matches[0]);'), $norm_wkt);

if ($geos_wkt != $norm_wkt) {
print 'Output mismatch on '.$method.":\n";
print 'GEOS : '.$geos_wkt."\n";
print 'NORM : '.$norm_wkt."\n";
continue;
}
}

//@@TODO: Run tests for output of types boolean, arrays, and text.
}

// Turn GEOS back on
geoPHP::geosInstalled(TRUE);
}

print "Testing Done!";

0 comments on commit 71fb4fa

Please sign in to comment.