]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/TokenListTest.php
tests for legacy tokenizer
[nominatim.git] / test / php / Nominatim / TokenListTest.php
1 <?php
2
3 namespace Nominatim;
4
5 require_once(CONST_LibDir.'/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('/hauptstr/', $sql)) {
81                         $aResults[] = $this->wordResult(array(
82                                                          'word_id' => 999,
83                                                          'word_token' => 'hauptstr',
84                                                          'class' => 'place',
85                                                          'type' => 'street',
86                                                          'operator' => true
87                                                         ));
88                     }
89                     if (preg_match('/64286/', $sql)) {
90                         $aResults[] = $this->wordResult(array(
91                                                          'word_id' => 999,
92                                                          'word_token' => '64286',
93                                                          'word' => '64286',
94                                                          'class' => 'place',
95                                                          'type' => 'postcode'
96                                                         ));
97                     }
98                     if (preg_match('/darmstadt/', $sql)) {
99                         $aResults[] = $this->wordResult(array(
100                                                          'word_id' => 999,
101                                                          'word_token' => 'darmstadt',
102                                                          'count' => 533
103                                                         ));
104                     }
105                     if (preg_match('/alemagne/', $sql)) {
106                         $aResults[] = $this->wordResult(array(
107                                                          'word_id' => 999,
108                                                          'word_token' => 'alemagne',
109                                                          'country_code' => 'de',
110                                                         ));
111                     }
112                     if (preg_match('/mexico/', $sql)) {
113                         $aResults[] = $this->wordResult(array(
114                                                          'word_id' => 999,
115                                                          'word_token' => 'mexico',
116                                                          'country_code' => 'mx',
117                                                         ));
118                     }
119                     return $aResults;
120                 }));
121
122         $aCountryCodes = array('de', 'fr');
123         $sNormQuery = '1051 hauptstr 64286 darmstadt alemagne mexico';
124         $aTokens = explode(' ', $sNormQuery);
125
126         $TL = new TokenList;
127         $TL->addTokensFromDB($oDbStub, $aTokens, $aCountryCodes, $sNormQuery, $this->oNormalizer);
128         $this->assertEquals(5, $TL->count());
129
130         $this->assertEquals(array(new Token\HouseNumber(999, '1051')), $TL->get('1051'));
131         $this->assertEquals(array(new Token\Country(999, 'de')), $TL->get('alemagne'));
132         $this->assertEquals(array(new Token\Postcode(999, '64286')), $TL->get('64286'));
133         $this->assertEquals(array(new Token\Word(999, true, 533, 0)), $TL->get('darmstadt'));
134         $this->assertEquals(array(new Token\SpecialTerm(999, 'place', 'street', true)), $TL->get('hauptstr'));
135     }
136 }