]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/SearchContextTest.php
Merge branch 'patch-1' of https://github.com/ganeshkrishnan1/Nominatim into ganeshkri...
[nominatim.git] / test / php / Nominatim / SearchContextTest.php
1 <?php
2
3 namespace Nominatim;
4
5 require_once(CONST_BasePath.'/lib/SearchContext.php');
6
7 class SearchContextTest extends \PHPUnit\Framework\TestCase
8 {
9     private $oCtx;
10
11
12     protected function setUp(): void
13     {
14         $this->oCtx = new SearchContext();
15     }
16
17     public function testHasNearPoint()
18     {
19         $this->assertFalse($this->oCtx->hasNearPoint());
20         $this->oCtx->setNearPoint(0, 0);
21         $this->assertTrue($this->oCtx->hasNearPoint());
22     }
23
24     public function testNearRadius()
25     {
26         $this->oCtx->setNearPoint(1, 1);
27         $this->assertEquals(0.1, $this->oCtx->nearRadius());
28         $this->oCtx->setNearPoint(1, 1, 0.338);
29         $this->assertEquals(0.338, $this->oCtx->nearRadius());
30     }
31
32     public function testWithinSQL()
33     {
34         $this->oCtx->setNearPoint(0.1, 23, 1);
35
36         $this->assertEquals(
37             'ST_DWithin(foo, ST_SetSRID(ST_Point(23,0.1),4326), 1.000000)',
38             $this->oCtx->withinSQL('foo')
39         );
40     }
41
42     public function testDistanceSQL()
43     {
44         $this->oCtx->setNearPoint(0.1, 23, 1);
45
46         $this->assertEquals(
47             'ST_Distance(ST_SetSRID(ST_Point(23,0.1),4326), foo)',
48             $this->oCtx->distanceSQL('foo')
49         );
50     }
51
52     public function testSetViewboxFromBox()
53     {
54         $viewbox = array(30, 20, 40, 50);
55         $this->oCtx->setViewboxFromBox($viewbox, true);
56         $this->assertEquals(
57             'ST_SetSRID(ST_MakeBox2D(ST_Point(30.000000,20.000000),ST_Point(40.000000,50.000000)),4326)',
58             $this->oCtx->sqlViewboxSmall
59         );
60         // height: 10
61         // width: 30
62         $this->assertEquals(
63             'ST_SetSRID(ST_MakeBox2D(ST_Point(50.000000,80.000000),ST_Point(20.000000,-10.000000)),4326)',
64             $this->oCtx->sqlViewboxLarge
65         );
66
67
68         $viewbox = array(-1.5, -2, 1.5, 2);
69         $this->oCtx->setViewboxFromBox($viewbox, true);
70         $this->assertEquals(
71             'ST_SetSRID(ST_MakeBox2D(ST_Point(-1.500000,-2.000000),ST_Point(1.500000,2.000000)),4326)',
72             $this->oCtx->sqlViewboxSmall
73         );
74         // height: 3
75         // width: 4
76         $this->assertEquals(
77             'ST_SetSRID(ST_MakeBox2D(ST_Point(4.500000,6.000000),ST_Point(-4.500000,-6.000000)),4326)',
78             $this->oCtx->sqlViewboxLarge
79         );
80     }
81 }