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(\DB::class)
60 ->setMethods(array('getAll'))
62 $oDbStub->method('getAll')
63 ->will($this->returnCallback(function ($sql) {
65 if (preg_match('/1051/', $sql)) {
66 $aResults[] = $this->wordResult(array(
68 'word_token' => '1051',
73 if (preg_match('/64286/', $sql)) {
74 $aResults[] = $this->wordResult(array(
76 'word_token' => '64286',
82 if (preg_match('/darmstadt/', $sql)) {
83 $aResults[] = $this->wordResult(array(
85 'word_token' => 'darmstadt',
89 if (preg_match('/alemagne/', $sql)) {
90 $aResults[] = $this->wordResult(array(
92 'word_token' => 'alemagne',
93 'country_code' => 'de',
96 if (preg_match('/mexico/', $sql)) {
97 $aResults[] = $this->wordResult(array(
99 'word_token' => 'mexico',
100 'country_code' => 'mx',
106 $aCountryCodes = array('de', 'fr');
107 $sNormQuery = '1051 hauptstr 64286 darmstadt alemagne mexico';
108 $aTokens = explode(' ', $sNormQuery);
111 $TL->addTokensFromDB($oDbStub, $aTokens, $aCountryCodes, $sNormQuery, $this->oNormalizer);
112 $this->assertEquals(4, $TL->count());
114 $this->assertEquals(array(new Token\HouseNumber(999, '1051')), $TL->get('1051'));
115 $this->assertEquals(array(new Token\Country(999, 'de')), $TL->get('alemagne'));
116 $this->assertEquals(array(new Token\Postcode(999, '64286')), $TL->get('64286'));
117 $this->assertEquals(array(new Token\Word(999, true, 533)), $TL->get('darmstadt'));