Skip to content
Permalink
Browse files

Updated What3Words to API2, added Unit Test and refactored

CoordinateTest. (Test needs API key, so disabled for now.)
  • Loading branch information...
mbirth committed Jul 5, 2016
1 parent b9fc14e commit c67b844e96717609d41b028bae7d33e8b262f73c
Showing with 128 additions and 153 deletions.
  1. +5 −14 htdocs/lib2/logic/coordinate.class.php
  2. +8 −1 phpunit.xml.dist
  3. +115 −138 tests/Modules/Lib2/Logic/CoordinateTest.php
@@ -630,30 +630,21 @@ public function getW3W($language)
$params = [
'key' => $opt['lib']['w3w']['apikey'],
'position' => sprintf('%f,%f', $this->nLat, $this->nLon),
'coords' => sprintf('%f,%f', $this->nLat, $this->nLon),
'lang' => strtolower($language),
];
$params_str = http_build_query($params);
$context = stream_context_create(
[
'http' => [
'method' => 'POST',
'header' => "Content-Type: application/x-www-form-urlencoded\r\n" .
"Content-Length: " . strlen($params_str) . "\r\n",
'content' => $params_str,
],
]
);
$result = @file_get_contents('http://api.what3words.com/position', false, $context);
$result = @file_get_contents('https://api.what3words.com/v2/reverse?' . $params_str);
if ($result === false) {
return false;
}
$json = json_decode($result, true);
if (!isset($json['words'])) {
return false;
}
return implode('.', $json['words']);
return $json['words'];
}
}
@@ -1,11 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.5/phpunit.xsd"
bootstrap="./htdocs/vendor/autoload.php"
verbose="true">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
<testsuite name="login">
<directory>tests/Oc/Login/LoginTest</directory>
</testsuite>
<testsuite name="coords">
<directory>tests/Modules/Lib2/Logic</directory>
</testsuite>
</testsuites>
</phpunit>
@@ -9,8 +9,6 @@
namespace Oc\Modules\Lib2\Logic;
use Oc\Modules\AbstractModuleTest;
/*
Testdaten:
OCADD6 Nordhalbkugel, Westen (USA)
@@ -26,215 +24,194 @@
N 60.63367° E 004.81313°
*/
class CoordinateTest extends AbstractModuleTest
class CoordinateTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
global $opt;
$opt['lib']['w3w']['apikey'] = 'GETAPIKEY';
}
public function utmProvider()
{
return array(
array(51.52775, -120.89720, '10', 'U', 'N 5710611', 'E 645865'),
array(-8.81687, 13.24057, '33', 'L', 'N 9024939', 'E 306489'),
array(52.67578, 6.77300, '32', 'U', 'N 5838532', 'E 349438'),
array(60.63367, 4.81313, '32', 'V', 'N 6729280', 'E 271052'),
array(58.682079, 5.793595, '32', 'V', 'N 6509097', 'E 314134'),
);
}
/**
* @group unit-tests
* @covers \coordinate::getUTM()
* @dataProvider utmProvider
*
* @return void
*/
public function testUTM()
public function testUTM($lat, $lon, $zone, $letter, $north, $east)
{
$coord = new \coordinate(51.52775, -120.89720);
$coord = new \coordinate($lat, $lon);
$utm = $coord->getUTM();
self::assertEquals('10', $utm['zone']);
self::assertEquals('U', $utm['letter']);
self::assertEquals('N 5710611', $utm['north']);
self::assertEquals('E 645865', $utm['east']);
$coord = new \coordinate(-8.81687, 13.24057);
$utm = $coord->getUTM();
self::assertEquals('33', $utm['zone']);
self::assertEquals('L', $utm['letter']);
self::assertEquals('N 9024939', $utm['north']);
self::assertEquals('E 306489', $utm['east']);
$coord = new \coordinate(52.67578, 6.77300);
$utm = $coord->getUTM();
self::assertEquals('32', $utm['zone']);
self::assertEquals('U', $utm['letter']);
self::assertEquals('N 5838532', $utm['north']);
self::assertEquals('E 349438', $utm['east']);
$coord = new \coordinate(60.63367, 4.81313);
$utm = $coord->getUTM();
self::assertEquals('32', $utm['zone']);
self::assertEquals('V', $utm['letter']);
self::assertEquals('N 6729280', $utm['north']);
self::assertEquals('E 271052', $utm['east']);
$coord = new \coordinate(58.682079, 5.793595);
$utm = $coord->getUTM();
self::assertEquals($zone, $utm['zone']);
self::assertEquals($letter, $utm['letter']);
self::assertEquals($north, $utm['north']);
self::assertEquals($east, $utm['east']);
}
self::assertEquals('32', $utm['zone']);
self::assertEquals('V', $utm['letter']);
self::assertEquals('N 6509097', $utm['north']);
self::assertEquals('E 314134', $utm['east']);
public function gkProvider()
{
return array(
array(51.52775, -120.89720, 'R -39562771 H 5710022'),
array(-8.81687, 13.24057, 'R 4636588 H -975608'),
array(52.67578, 6.77300, 'R 2552325 H 5838386'),
array(60.63367, 4.81313, 'R 2435086 H 6724824'),
);
}
/**
* @group unit-tests
* @covers \coordinate::getGK()
* @dataProvider gkProvider
*
* @return void
*/
public function testGK()
public function testGK($lat, $lon, $expectedGK)
{
$coord = new \coordinate(51.52775, -120.89720);
$gk = $coord->getGK();
self::assertEquals('R -39562771 H 5710022', $gk);
$coord = new \coordinate(-8.81687, 13.24057);
$coord = new \coordinate($lat, $lon);
$gk = $coord->getGK();
self::assertEquals('R 4636588 H -975608', $gk);
$coord = new \coordinate(52.67578, 6.77300);
$gk = $coord->getGK();
self::assertEquals('R 2552325 H 5838386', $gk);
$coord = new \coordinate(60.63367, 4.81313);
$gk = $coord->getGK();
self::assertEquals($expectedGK, $gk);
}
self::assertEquals('R 2435086 H 6724824', $gk);
public function swissGridProvider()
{
return array(
array(51.52775, -120.89720, '-3944504 / 8019927'),
array(-8.81687, 13.24057, '1499586 / -6904936'),
array(52.67578, 6.77300, '554738 / 837985'),
array(60.63367, 4.81313, '451121 / 1739767'),
);
}
/**
* @group unit-tests
* @covers \coordinate::getSwissGrid()
* @dataProvider swissGridProvider
*
* @return void
*/
public function testSwissGrid()
public function testSwissGrid($lat, $lon, $expectedSG)
{
$coord = new \coordinate(51.52775, -120.89720);
$swissGrid = $coord->getSwissGrid();
self::assertEquals('-3944504 / 8019927', $swissGrid['coord']);
$coord = new \coordinate(-8.81687, 13.24057);
$swissGrid = $coord->getSwissGrid();
self::assertEquals('1499586 / -6904936', $swissGrid['coord']);
$coord = new \coordinate(52.67578, 6.77300);
$coord = new \coordinate($lat, $lon);
$swissGrid = $coord->getSwissGrid();
self::assertEquals('554738 / 837985', $swissGrid['coord']);
$coord = new \coordinate(60.63367, 4.81313);
$swissGrid = $coord->getSwissGrid();
self::assertEquals($expectedSG, $swissGrid['coord']);
}
self::assertEquals('451121 / 1739767', $swissGrid['coord']);
public function dutchGridProvider()
{
return array(
array(52.67578, 6.77300, 'X 248723 Y 521824'),
array(60.63367, 4.81313, 'X 123409 Y 1408833'),
);
}
/**
* @group unit-tests
* @covers \coordinate::getRD()
* @dataProvider dutchGridProvider
*
* @return void
*/
public function testDutchGrid()
public function testDutchGrid($lat, $lon, $expectedDG)
{
// DutchGrid Berechnungen funktionieren nur auf dem Nord-Ost-Quadranten
$coord = new \coordinate(52.67578, 6.77300);
$coord = new \coordinate($lat, $lon);
$dutchGrid = $coord->getRD();
self::assertEquals('X 248723 Y 521824', $dutchGrid);
$coord = new \coordinate(60.63367, 4.81313);
$dutchGrid = $coord->getRD();
self::assertEquals($expectedDG, $dutchGrid);
}
self::assertEquals('X 123409 Y 1408833', $dutchGrid);
public function qthProvider()
{
return array(
array(51.52775, -120.89720, 'CO91NM'),
array(-8.81687, 13.24057, 'JI61OE'),
array(52.67578, 6.77300, 'JO32JQ'),
array(60.63367, 4.81313, 'JP20JP'),
);
}
/**
* @group unit-tests
* @covers \coordinate::getQTH()
* @dataProvider qthProvider
*
* @return void
*/
public function testQTHLocator()
public function testQTHLocator($lat, $lon, $expectedQTH)
{
$coord = new \coordinate(51.52775, -120.89720);
$coord = new \coordinate($lat, $lon);
$qthLocator = $coord->getQTH();
self::assertEquals('CO91NM', $qthLocator);
$coord = new \coordinate(-8.81687, 13.24057);
$qthLocator = $coord->getQTH();
self::assertEquals('JI61OE', $qthLocator);
self::assertEquals($expectedQTH, $qthLocator);
}
$coord = new \coordinate(52.67578, 6.77300);
$qthLocator = $coord->getQTH();
public function what3WordsProvider()
{
return array(
array(52.473570, 13.371317, 'DE', 'gewinn.kopf.digitalen'),
array(60.168947, 24.958826, 'DE', 'kurzem.knie.ringen'),
array(45.999639, -1.213892, 'DE', 'bewohnbar.modernes.empfundenen'),
array(52.473570, 13.371317, 'EN', 'steer.removed.smashes'),
array(60.168947, 24.958826, 'EN', 'dished.mailing.starred'),
array(45.999639, -1.213892, 'EN', 'declaim.alright.loaning'),
);
}
self::assertEquals('JO32JQ', $qthLocator);
/**
* @group unit-tests
* @covers \coordinate::getW3W
* @dataProvider what3WordsProvider
*
* @return void
*/
public function need_api_key_testWhat3Words($lat, $lon, $language, $expectedW3W)
{
$coord = new \coordinate($lat, $lon);
$w3w = $coord->getW3W($language);
$coord = new \coordinate(60.63367, 4.81313);
$qthLocator = $coord->getQTH();
$this->assertEquals($expectedW3W, $w3w);
}
self::assertEquals('JP20JP', $qthLocator);
public function formatProvider()
{
return array(
array(51.52775, -120.89720, array('lat'=>"N 51° 31.665'", 'lon'=>"W 120° 53.832'"), array('lat'=>"N 51° 31' 39''", 'lon'=>"W 120° 53' 49''")),
array(-8.81687, 13.24057, array('lat'=>"S 08° 49.012'", 'lon'=>"E 013° 14.434'"), array('lat'=>"S 08° 49' 00''", 'lon'=>"E 013° 14' 26''")),
array(52.67578, 6.77300, array('lat'=>"N 52° 40.547'", 'lon'=>"E 006° 46.380'"), array('lat'=>"N 52° 40' 32''", 'lon'=>"E 006° 46' 22''")),
array(60.63367, 4.81313, array('lat'=>"N 60° 38.020'", 'lon'=>"E 004° 48.788'"), array('lat'=>"N 60° 38' 01''", 'lon'=>"E 004° 48' 47''")),
);
}
/**
* @group unit-tests
* @covers \coordinate::getDecimalMinutes
* @dataProvider formatProvider
*
* @return void
*/
public function testFormatConversions()
public function testFormatConversions($lat, $lon, $expectedMin, $expectedMinSec)
{
$coord = new \coordinate(51.52775, -120.89720);
$decimalMin = $coord->getDecimalMinutes();
self::assertEquals("N 51° 31.665'", $decimalMin['lat']);
self::assertEquals("W 120° 53.832'", $decimalMin['lon']);
$decimalMinSec = $coord->getDecimalMinutesSeconds();
self::assertEquals("N 51° 31' 39''", $decimalMinSec['lat']);
self::assertEquals("W 120° 53' 49''", $decimalMinSec['lon']);
$coord = new \coordinate(-8.81687, 13.24057);
$decimalMin = $coord->getDecimalMinutes();
self::assertEquals("S 08° 49.012'", $decimalMin['lat']);
self::assertEquals("E 013° 14.434'", $decimalMin['lon']);
$decimalMinSec = $coord->getDecimalMinutesSeconds();
self::assertEquals("S 08° 49' 00''", $decimalMinSec['lat']);
self::assertEquals("E 013° 14' 26''", $decimalMinSec['lon']);
$coord = new \coordinate(52.67578, 6.77300);
$decimalMin = $coord->getDecimalMinutes();
self::assertEquals("N 52° 40.547'", $decimalMin['lat']);
self::assertEquals("E 006° 46.380'", $decimalMin['lon']);
$decimalMinSec = $coord->getDecimalMinutesSeconds();
self::assertEquals("N 52° 40' 32''", $decimalMinSec['lat']);
self::assertEquals("E 006° 46' 22''", $decimalMinSec['lon']);
$coord = new \coordinate(60.63367, 4.81313);
$decimalMin = $coord->getDecimalMinutes();
self::assertEquals("N 60° 38.020'", $decimalMin['lat']);
self::assertEquals("E 004° 48.788'", $decimalMin['lon']);
$decimalMinSec = $coord->getDecimalMinutesSeconds();
$coord = new \coordinate($lat, $lon);
self::assertEquals("N 60° 38' 01''", $decimalMinSec['lat']);
self::assertEquals("E 004° 48' 47''", $decimalMinSec['lon']);
$this->assertEquals($expectedMin, $coord->getDecimalMinutes());
$this->assertEquals($expectedMinSec, $coord->getDecimalMinutesSeconds());
}
}

0 comments on commit c67b844

Please sign in to comment.
You can’t perform that action at this time.