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 $aRes = NearPoint::extractFromQuery(' -12.456,-78.90 ');
39 $this->assertEquals($aRes['pt']->lat(), -12.456);
40 $this->assertEquals($aRes['pt']->lon(), -78.90);
42 // http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
43 // these all represent the same location
45 '40 26.767 N 79 58.933 W',
46 '40° 26.767′ N 79° 58.933′ W',
47 "40° 26.767' N 79° 58.933' W",
48 'N 40 26.767, W 79 58.933',
49 'N 40°26.767′, W 79°58.933′',
50 "N 40°26.767', W 79°58.933'",
52 '40 26 46 N 79 58 56 W',
53 '40° 26′ 46″ N 79° 58′ 56″ W',
54 'N 40 26 46 W 79 58 56',
55 'N 40° 26′ 46″, W 79° 58′ 56″',
56 'N 40° 26\' 46", W 79° 58\' 56"',
60 '40.446° N 79.982° W',
61 'N 40.446° W 79.982°',
68 foreach ($aQueries as $sQuery) {
69 $aRes = NearPoint::extractFromQuery($sQuery);
70 $this->assertEquals(40.446, $aRes['pt']->lat(), 'degrees decimal ' . $sQuery, 0.01);
71 $this->assertEquals(-79.982, $aRes['pt']->lon(), 'degrees decimal ' . $sQuery, 0.01);
75 public function testWithinSQL()
77 $np = new NearPoint(0.1, 23, 1);
80 'ST_DWithin(foo, ST_SetSRID(ST_Point(23,0.1),4326), 1.000000)',
85 public function testDistanceSQL()
87 $np = new NearPoint(0.1, 23, 1);
90 'ST_Distance(ST_SetSRID(ST_Point(23,0.1),4326), foo)',
91 $np->distanceSQL('foo')