]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/SearchContextTest.php
Merge pull request #2602 from lonvia/filter-bad-housenumbers
[nominatim.git] / test / php / Nominatim / SearchContextTest.php
1 <?php
2 /**
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This file is part of Nominatim. (https://nominatim.org)
6  *
7  * Copyright (C) 2022 by the Nominatim developer community.
8  * For a full list of authors see the git log.
9  */
10
11 namespace Nominatim;
12
13 require_once(CONST_LibDir.'/SearchContext.php');
14
15 class SearchContextTest extends \PHPUnit\Framework\TestCase
16 {
17     private $oCtx;
18
19
20     protected function setUp(): void
21     {
22         $this->oCtx = new SearchContext();
23     }
24
25     public function testHasNearPoint()
26     {
27         $this->assertFalse($this->oCtx->hasNearPoint());
28         $this->oCtx->setNearPoint(0, 0);
29         $this->assertTrue($this->oCtx->hasNearPoint());
30     }
31
32     public function testNearRadius()
33     {
34         $this->oCtx->setNearPoint(1, 1);
35         $this->assertEquals(0.1, $this->oCtx->nearRadius());
36         $this->oCtx->setNearPoint(1, 1, 0.338);
37         $this->assertEquals(0.338, $this->oCtx->nearRadius());
38     }
39
40     public function testWithinSQL()
41     {
42         $this->oCtx->setNearPoint(0.1, 23, 1);
43
44         $this->assertEquals(
45             'ST_DWithin(foo, ST_SetSRID(ST_Point(23,0.1),4326), 1.000000)',
46             $this->oCtx->withinSQL('foo')
47         );
48     }
49
50     public function testDistanceSQL()
51     {
52         $this->oCtx->setNearPoint(0.1, 23, 1);
53
54         $this->assertEquals(
55             'ST_Distance(ST_SetSRID(ST_Point(23,0.1),4326), foo)',
56             $this->oCtx->distanceSQL('foo')
57         );
58     }
59
60     public function testSetViewboxFromBox()
61     {
62         $viewbox = array(30, 20, 40, 50);
63         $this->oCtx->setViewboxFromBox($viewbox, true);
64         $this->assertEquals(
65             'ST_SetSRID(ST_MakeBox2D(ST_Point(30.000000,20.000000),ST_Point(40.000000,50.000000)),4326)',
66             $this->oCtx->sqlViewboxSmall
67         );
68         // height: 10
69         // width: 30
70         $this->assertEquals(
71             'ST_SetSRID(ST_MakeBox2D(ST_Point(50.000000,80.000000),ST_Point(20.000000,-10.000000)),4326)',
72             $this->oCtx->sqlViewboxLarge
73         );
74
75
76         $viewbox = array(-1.5, -2, 1.5, 2);
77         $this->oCtx->setViewboxFromBox($viewbox, true);
78         $this->assertEquals(
79             'ST_SetSRID(ST_MakeBox2D(ST_Point(-1.500000,-2.000000),ST_Point(1.500000,2.000000)),4326)',
80             $this->oCtx->sqlViewboxSmall
81         );
82         // height: 3
83         // width: 4
84         $this->assertEquals(
85             'ST_SetSRID(ST_MakeBox2D(ST_Point(4.500000,6.000000),ST_Point(-4.500000,-6.000000)),4326)',
86             $this->oCtx->sqlViewboxLarge
87         );
88     }
89 }