5 @define('CONST_BasePath', '../../');
7 require_once '../../lib/db.php';
8 require_once '../../lib/cmd.php';
9 require_once '../../lib/TokenList.php';
11 class TokenTest extends \PHPUnit\Framework\TestCase
13 protected function setUp()
15 $this->oNormalizer = $this->getMock(\MockNormalizer::class, array('transliterate'));
16 $this->oNormalizer->method('transliterate')
17 ->will($this->returnCallback(function ($text) {
18 return strtolower($text);
22 private function wordResult($aFields)
30 'country_code' => null,
33 return array_merge($aRow, $aFields);
36 public function testList()
40 $this->assertEquals(0, $TL->count());
42 $TL->addToken('word1', 'token1');
43 $TL->addToken('word1', 'token2');
45 $this->assertEquals(1, $TL->count());
47 $this->assertTrue($TL->contains('word1'));
48 $this->assertEquals(array('token1', 'token2'), $TL->get('word1'));
50 $this->assertFalse($TL->contains('unknownword'));
51 $this->assertEquals(array(), $TL->get('unknownword'));
54 public function testAddress()
56 $this->expectOutputRegex('/<p><tt>/');
58 $oDbStub = $this->getMock(\DB::class, array('getAll'));
59 $oDbStub->method('getAll')
60 ->will($this->returnCallback(function ($sql) {
62 if (preg_match('/1051/', $sql)) {
63 $aResults[] = $this->wordResult(array(
65 'word_token' => '1051',
70 if (preg_match('/64286/', $sql)) {
71 $aResults[] = $this->wordResult(array(
73 'word_token' => '64286',
79 if (preg_match('/darmstadt/', $sql)) {
80 $aResults[] = $this->wordResult(array(
82 'word_token' => 'darmstadt',
86 if (preg_match('/alemagne/', $sql)) {
87 $aResults[] = $this->wordResult(array(
89 'word_token' => 'alemagne',
90 'country_code' => 'de',
93 if (preg_match('/mexico/', $sql)) {
94 $aResults[] = $this->wordResult(array(
96 'word_token' => 'mexico',
97 'country_code' => 'mx',
103 $aCountryCodes = array('de', 'fr');
104 $sNormQuery = '1051 hauptstr 64286 darmstadt alemagne mexico';
105 $aTokens = explode(' ', $sNormQuery);
108 $TL->addTokensFromDB($oDbStub, $aTokens, $aCountryCodes, $sNormQuery, $this->oNormalizer);
109 $this->assertEquals(4, $TL->count());
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'));