]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/TokenListTest.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / php / Nominatim / TokenListTest.php
1 <?php
2
3 namespace Nominatim;
4
5 require_once(CONST_BasePath.'/lib/TokenList.php');
6
7
8 class TokenTest extends \PHPUnit\Framework\TestCase
9 {
10     protected function setUp(): void
11     {
12         $this->oNormalizer = $this->getMockBuilder(\MockNormalizer::class)
13                                   ->setMethods(array('transliterate'))
14                                   ->getMock();
15         $this->oNormalizer->method('transliterate')
16                           ->will($this->returnCallback(function ($text) {
17                               return strtolower($text);
18                           }));
19     }
20
21     private function wordResult($aFields)
22     {
23         $aRow = array(
24                  'word_id' => null,
25                  'word_token' => null,
26                  'word' => null,
27                  'class' => null,
28                  'type' => null,
29                  'country_code' => null,
30                  'count' => 0
31                 );
32         return array_merge($aRow, $aFields);
33     }
34
35     public function testList()
36     {
37         $TL = new TokenList;
38
39         $this->assertEquals(0, $TL->count());
40
41         $TL->addToken('word1', 'token1');
42         $TL->addToken('word1', 'token2');
43
44         $this->assertEquals(1, $TL->count());
45
46         $this->assertTrue($TL->contains('word1'));
47         $this->assertEquals(array('token1', 'token2'), $TL->get('word1'));
48
49         $this->assertFalse($TL->contains('unknownword'));
50         $this->assertEquals(array(), $TL->get('unknownword'));
51     }
52
53     public function testAddress()
54     {
55         $this->expectOutputRegex('/<p><tt>/');
56
57         $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
58                         ->setMethods(array('getAll', 'getDBQuotedList'))
59                         ->getMock();
60
61         $oDbStub->method('getDBQuotedList')
62                 ->will($this->returnCallback(function ($aVals) {
63                     return array_map(function ($sVal) {
64                         return "'".$sVal."'";
65                     }, $aVals);
66                 }));
67
68
69         $oDbStub->method('getAll')
70                 ->will($this->returnCallback(function ($sql) {
71                     $aResults = array();
72                     if (preg_match('/1051/', $sql)) {
73                         $aResults[] = $this->wordResult(array(
74                                                          'word_id' => 999,
75                                                          'word_token' => '1051',
76                                                          'class' => 'place',
77                                                          'type' => 'house'
78                                                         ));
79                     }
80                     if (preg_match('/64286/', $sql)) {
81                         $aResults[] = $this->wordResult(array(
82                                                          'word_id' => 999,
83                                                          'word_token' => '64286',
84                                                          'word' => '64286',
85                                                          'class' => 'place',
86                                                          'type' => 'postcode'
87                                                         ));
88                     }
89                     if (preg_match('/darmstadt/', $sql)) {
90                         $aResults[] = $this->wordResult(array(
91                                                          'word_id' => 999,
92                                                          'word_token' => 'darmstadt',
93                                                          'count' => 533
94                                                         ));
95                     }
96                     if (preg_match('/alemagne/', $sql)) {
97                         $aResults[] = $this->wordResult(array(
98                                                          'word_id' => 999,
99                                                          'word_token' => 'alemagne',
100                                                          'country_code' => 'de',
101                                                         ));
102                     }
103                     if (preg_match('/mexico/', $sql)) {
104                         $aResults[] = $this->wordResult(array(
105                                                          'word_id' => 999,
106                                                          'word_token' => 'mexico',
107                                                          'country_code' => 'mx',
108                                                         ));
109                     }
110                     return $aResults;
111                 }));
112
113         $aCountryCodes = array('de', 'fr');
114         $sNormQuery = '1051 hauptstr 64286 darmstadt alemagne mexico';
115         $aTokens = explode(' ', $sNormQuery);
116
117         $TL = new TokenList;
118         $TL->addTokensFromDB($oDbStub, $aTokens, $aCountryCodes, $sNormQuery, $this->oNormalizer);
119         $this->assertEquals(4, $TL->count());
120
121         $this->assertEquals(array(new Token\HouseNumber(999, '1051')), $TL->get('1051'));
122         $this->assertEquals(array(new Token\Country(999, 'de')), $TL->get('alemagne'));
123         $this->assertEquals(array(new Token\Postcode(999, '64286')), $TL->get('64286'));
124         $this->assertEquals(array(new Token\Word(999, true, 533)), $TL->get('darmstadt'));
125     }
126 }