5 require_once(CONST_BasePath.'/lib/TokenList.php');
8 class TokenTest extends \PHPUnit\Framework\TestCase
10 protected function setUp(): void
12 $this->oNormalizer = $this->getMockBuilder(\MockNormalizer::class)
13 ->setMethods(array('transliterate'))
15 $this->oNormalizer->method('transliterate')
16 ->will($this->returnCallback(function ($text) {
17 return strtolower($text);
21 private function wordResult($aFields)
29 'country_code' => null,
32 return array_merge($aRow, $aFields);
35 public function testList()
39 $this->assertEquals(0, $TL->count());
41 $TL->addToken('word1', 'token1');
42 $TL->addToken('word1', 'token2');
44 $this->assertEquals(1, $TL->count());
46 $this->assertTrue($TL->contains('word1'));
47 $this->assertEquals(array('token1', 'token2'), $TL->get('word1'));
49 $this->assertFalse($TL->contains('unknownword'));
50 $this->assertEquals(array(), $TL->get('unknownword'));
53 public function testAddress()
55 $this->expectOutputRegex('/<p><tt>/');
57 $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
58 ->setMethods(array('getAll', 'getDBQuotedList'))
61 $oDbStub->method('getDBQuotedList')
62 ->will($this->returnCallback(function ($aVals) {
63 return array_map(function ($sVal) {
69 $oDbStub->method('getAll')
70 ->will($this->returnCallback(function ($sql) {
72 if (preg_match('/1051/', $sql)) {
73 $aResults[] = $this->wordResult(array(
75 'word_token' => '1051',
80 if (preg_match('/64286/', $sql)) {
81 $aResults[] = $this->wordResult(array(
83 'word_token' => '64286',
89 if (preg_match('/darmstadt/', $sql)) {
90 $aResults[] = $this->wordResult(array(
92 'word_token' => 'darmstadt',
96 if (preg_match('/alemagne/', $sql)) {
97 $aResults[] = $this->wordResult(array(
99 'word_token' => 'alemagne',
100 'country_code' => 'de',
103 if (preg_match('/mexico/', $sql)) {
104 $aResults[] = $this->wordResult(array(
106 'word_token' => 'mexico',
107 'country_code' => 'mx',
113 $aCountryCodes = array('de', 'fr');
114 $sNormQuery = '1051 hauptstr 64286 darmstadt alemagne mexico';
115 $aTokens = explode(' ', $sNormQuery);
118 $TL->addTokensFromDB($oDbStub, $aTokens, $aCountryCodes, $sNormQuery, $this->oNormalizer);
119 $this->assertEquals(4, $TL->count());
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'));