View source  
  <?php
function run_test() {
  header("Content-type: text");
  include_once '../geoPHP.inc';
  
  $host = 'localhost';
  $database = 'phayes';
  $table = 'test';
  $column = 'geom';
  $user = 'phayes';
  $pass = 'supersecret';
  $connection = pg_connect("host={$host} dbname={$database} user={$user} password={$pass}");
  
  pg_query($connection, "DELETE FROM {$table}");
  
  foreach (scandir('./input') as $file) {
    $parts = explode('.', $file);
    if ($parts[0]) {
      $name = $parts[0];
      $format = $parts[1];
      $value = file_get_contents('./input/' . $file);
      print '---- Testing ' . $file . "\n";
      flush();
      $geometry = geoPHP::load($value, $format);
      test_postgis($name, $format, $geometry, $connection, 'wkb');
      $geometry
        ->setSRID(4326);
      test_postgis($name, $format, $geometry, $connection, 'ewkb');
    }
  }
  print "Testing Done!";
}
function test_postgis($name, $type, $geom, $connection, $format) {
  global $table;
  
  $insert_string = pg_escape_bytea($geom
    ->out($format));
  pg_query($connection, "INSERT INTO {$table} (name, type, geom) values ('{$name}', '{$type}', GeomFromWKB('{$insert_string}'))");
  
  $result = pg_fetch_all(pg_query($connection, "SELECT asBinary(geom) as geom FROM {$table} WHERE name='{$name}'"));
  foreach ($result as $item) {
    $wkb = pg_unescape_bytea($item['geom']);
    
    $geom = geoPHP::load($wkb, $format);
    
  }
  
  $result = pg_fetch_all(pg_query($connection, "SELECT geom as geom FROM {$table} WHERE name='{$name}'"));
  foreach ($result as $item) {
    $wkb = pack('H*', $item['geom']);
    
    $geom = geoPHP::load($wkb, $format);
    
    $unpacked = unpack('H*', $geom
      ->out($format));
    $insert_string = $unpacked[1];
    pg_query($connection, "INSERT INTO {$table} (name, type, geom) values ('{$name}', '{$type}', '{$insert_string}')");
  }
  
  $result = pg_fetch_all(pg_query($connection, "SELECT ST_AsEWKT(geom) as geom FROM {$table} WHERE name='{$name}'"));
  foreach ($result as $item) {
    $wkt = $item['geom'];
    
    $geom = geoPHP::load($wkt, 'ewkt');
    
    $insert_string = $geom
      ->out('ewkt');
    pg_query($connection, "INSERT INTO {$table} (name, type, geom) values ('{$name}', '{$type}', ST_GeomFromEWKT('{$insert_string}'))");
  }
}