5 require '../../lib/NearPoint.php';
7 class NearPointTest extends \PHPUnit_Framework_TestCase
11 protected function setUp()
15 public function testExtractFromQuery()
17 // no coordinates expected
18 $this->assertFalse(NearPoint::extractFromQuery(''));
19 $this->assertFalse(NearPoint::extractFromQuery('abc'));
20 $this->assertFalse(NearPoint::extractFromQuery('12 34'));
21 $this->assertFalse(NearPoint::extractFromQuery('200.1 89.9')); // because latitude > 180
23 // coordinates expected
24 $this->assertNotNull(NearPoint::extractFromQuery('0.0 -0.0'));
26 $aRes = NearPoint::extractFromQuery(' abc 12.456 -78.90 def ');
27 $this->assertEquals($aRes['pt']->lat(), 12.456);
28 $this->assertEquals($aRes['pt']->lon(), -78.90);
29 $this->assertEquals($aRes['pt']->radius(), 0.1);
30 $this->assertEquals($aRes['query'], 'abc def');
32 $aRes = NearPoint::extractFromQuery(' [12.456,-78.90] ');
33 $this->assertEquals($aRes['pt']->lat(), 12.456);
34 $this->assertEquals($aRes['pt']->lon(), -78.90);
35 $this->assertEquals($aRes['pt']->radius(), 0.1);
36 $this->assertEquals($aRes['query'], '');
38 // http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
39 // these all represent the same location
41 '40 26.767 N 79 58.933 W',
42 '40° 26.767′ N 79° 58.933′ W',
43 "40° 26.767' N 79° 58.933' W",
44 'N 40 26.767, W 79 58.933',
45 'N 40°26.767′, W 79°58.933′',
46 "N 40°26.767', W 79°58.933'",
48 '40 26 46 N 79 58 56 W',
49 '40° 26′ 46″ N 79° 58′ 56″ W',
50 'N 40 26 46 W 79 58 56',
51 'N 40° 26′ 46″, W 79° 58′ 56″',
52 'N 40° 26\' 46", W 79° 58\' 56"',
56 '40.446° N 79.982° W',
57 'N 40.446° W 79.982°',
64 foreach ($aQueries as $sQuery) {
65 $aRes = NearPoint::extractFromQuery($sQuery);
66 $this->assertEquals(40.446, $aRes['pt']->lat(), 'degrees decimal ' . $sQuery, 0.01);
67 $this->assertEquals(-79.982, $aRes['pt']->lon(), 'degrees decimal ' . $sQuery, 0.01);
71 public function testWithinSQL()
73 $np = new NearPoint(0.1, 23, 1);
76 'ST_DWithin(foo, ST_SetSRID(ST_Point(23,0.1),4326), 1.000000)',
81 public function testDistanceSQL()
83 $np = new NearPoint(0.1, 23, 1);
86 'ST_Distance(ST_SetSRID(ST_Point(23,0.1),4326), foo)',
87 $np->distanceSQL('foo')