5 require_once(CONST_LibDir.'/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('/hauptstr/', $sql)) {
81 $aResults[] = $this->wordResult(array(
83 'word_token' => 'hauptstr',
89 if (preg_match('/64286/', $sql)) {
90 $aResults[] = $this->wordResult(array(
92 'word_token' => '64286',
98 if (preg_match('/darmstadt/', $sql)) {
99 $aResults[] = $this->wordResult(array(
101 'word_token' => 'darmstadt',
105 if (preg_match('/alemagne/', $sql)) {
106 $aResults[] = $this->wordResult(array(
108 'word_token' => 'alemagne',
109 'country_code' => 'de',
112 if (preg_match('/mexico/', $sql)) {
113 $aResults[] = $this->wordResult(array(
115 'word_token' => 'mexico',
116 'country_code' => 'mx',
122 $aCountryCodes = array('de', 'fr');
123 $sNormQuery = '1051 hauptstr 64286 darmstadt alemagne mexico';
124 $aTokens = explode(' ', $sNormQuery);
127 $TL->addTokensFromDB($oDbStub, $aTokens, $aCountryCodes, $sNormQuery, $this->oNormalizer);
128 $this->assertEquals(5, $TL->count());
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'));