]> git.openstreetmap.org Git - nominatim.git/blob - test/php/Nominatim/TokenListTest.php
replace database abstraction DB with PDO
[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(Nominatim\DB::class)
60                         ->setMethods(array('getAll', 'getDBQuotedList'))
61                         ->getMock();
62
63         $oDbStub->method('getDBQuotedList')
64                 ->will($this->returnCallback(function ($aVals) {
65                     return array_map(function ($sVal) {
66                         return "'".$sVal."'";
67                     }, $aVals);
68                 }));
69
70
71         $oDbStub->method('getAll')
72                 ->will($this->returnCallback(function ($sql) {
73                     $aResults = array();
74                     if (preg_match('/1051/', $sql)) {
75                         $aResults[] = $this->wordResult(array(
76                                                          'word_id' => 999,
77                                                          'word_token' => '1051',
78                                                          'class' => 'place',
79                                                          'type' => 'house'
80                                                         ));
81                     }
82                     if (preg_match('/64286/', $sql)) {
83                         $aResults[] = $this->wordResult(array(
84                                                          'word_id' => 999,
85                                                          'word_token' => '64286',
86                                                          'word' => '64286',
87                                                          'class' => 'place',
88                                                          'type' => 'postcode'
89                                                         ));
90                     }
91                     if (preg_match('/darmstadt/', $sql)) {
92                         $aResults[] = $this->wordResult(array(
93                                                          'word_id' => 999,
94                                                          'word_token' => 'darmstadt',
95                                                          'count' => 533
96                                                         ));
97                     }
98                     if (preg_match('/alemagne/', $sql)) {
99                         $aResults[] = $this->wordResult(array(
100                                                          'word_id' => 999,
101                                                          'word_token' => 'alemagne',
102                                                          'country_code' => 'de',
103                                                         ));
104                     }
105                     if (preg_match('/mexico/', $sql)) {
106                         $aResults[] = $this->wordResult(array(
107                                                          'word_id' => 999,
108                                                          'word_token' => 'mexico',
109                                                          'country_code' => 'mx',
110                                                         ));
111                     }
112                     return $aResults;
113                 }));
114
115         $aCountryCodes = array('de', 'fr');
116         $sNormQuery = '1051 hauptstr 64286 darmstadt alemagne mexico';
117         $aTokens = explode(' ', $sNormQuery);
118
119         $TL = new TokenList;
120         $TL->addTokensFromDB($oDbStub, $aTokens, $aCountryCodes, $sNormQuery, $this->oNormalizer);
121         $this->assertEquals(4, $TL->count());
122
123         $this->assertEquals(array(new Token\HouseNumber(999, '1051')), $TL->get('1051'));
124         $this->assertEquals(array(new Token\Country(999, 'de')), $TL->get('alemagne'));
125         $this->assertEquals(array(new Token\Postcode(999, '64286')), $TL->get('64286'));
126         $this->assertEquals(array(new Token\Word(999, true, 533)), $TL->get('darmstadt'));
127     }
128 }