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