5 // require_once(CONST_BasePath.'/lib/db.php');
6 // require_once(CONST_BasePath.'/lib/cmd.php');
7 require_once(CONST_BasePath.'/lib/TokenList.php');
10 class TokenTest extends \PHPUnit\Framework\TestCase
12 protected function setUp()
14 $this->oNormalizer = $this->getMockBuilder(\MockNormalizer::class)
15 ->setMethods(array('transliterate'))
17 $this->oNormalizer->method('transliterate')
18 ->will($this->returnCallback(function ($text) {
19 return strtolower($text);
23 private function wordResult($aFields)
31 'country_code' => null,
34 return array_merge($aRow, $aFields);
37 public function testList()
41 $this->assertEquals(0, $TL->count());
43 $TL->addToken('word1', 'token1');
44 $TL->addToken('word1', 'token2');
46 $this->assertEquals(1, $TL->count());
48 $this->assertTrue($TL->contains('word1'));
49 $this->assertEquals(array('token1', 'token2'), $TL->get('word1'));
51 $this->assertFalse($TL->contains('unknownword'));
52 $this->assertEquals(array(), $TL->get('unknownword'));
55 public function testAddress()
57 $this->expectOutputRegex('/<p><tt>/');
59 $oDbStub = $this->getMockBuilder(Nominatim\DB::class)
60 ->setMethods(array('getAll', 'getDBQuotedList'))
63 $oDbStub->method('getDBQuotedList')
64 ->will($this->returnCallback(function ($aVals) {
65 return array_map(function ($sVal) {
71 $oDbStub->method('getAll')
72 ->will($this->returnCallback(function ($sql) {
74 if (preg_match('/1051/', $sql)) {
75 $aResults[] = $this->wordResult(array(
77 'word_token' => '1051',
82 if (preg_match('/64286/', $sql)) {
83 $aResults[] = $this->wordResult(array(
85 'word_token' => '64286',
91 if (preg_match('/darmstadt/', $sql)) {
92 $aResults[] = $this->wordResult(array(
94 'word_token' => 'darmstadt',
98 if (preg_match('/alemagne/', $sql)) {
99 $aResults[] = $this->wordResult(array(
101 'word_token' => 'alemagne',
102 'country_code' => 'de',
105 if (preg_match('/mexico/', $sql)) {
106 $aResults[] = $this->wordResult(array(
108 'word_token' => 'mexico',
109 'country_code' => 'mx',
115 $aCountryCodes = array('de', 'fr');
116 $sNormQuery = '1051 hauptstr 64286 darmstadt alemagne mexico';
117 $aTokens = explode(' ', $sNormQuery);
120 $TL->addTokensFromDB($oDbStub, $aTokens, $aCountryCodes, $sNormQuery, $this->oNormalizer);
121 $this->assertEquals(4, $TL->count());
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'));