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