View source
<?php
namespace Drupal\Tests\snowball_stemmer\Unit;
use Drupal\snowball_stemmer\Stemmer;
use Drupal\Tests\UnitTestCase;
use Wamania\Snowball\Stemmer\German;
use Wamania\Snowball\Stemmer\English;
class StemmerTest extends UnitTestCase {
protected function setUp() {
parent::setUp();
$this->stemmer = new Stemmer();
}
public function testStemming($language, $original, $stemmed) {
$success = $this->stemmer
->setLanguage($language);
$this
->assertTrue($success);
$word = $this->stemmer
->stem($original);
$this
->assertEquals($word, $stemmed);
}
public function testUnknownLanguage() {
$this
->expectException('\\Drupal\\snowball_stemmer\\LanguageNotSetException');
$success = $this->stemmer
->setLanguage('xx');
$this
->assertFalse($success);
$this->stemmer
->stem('word');
}
public function languageStrings() {
return [
[
'da',
'barnløshed',
'barnløs',
],
[
'de',
'aalglatten',
'aalglatt',
],
[
'en',
'backing',
'back',
],
[
'es',
'carnavaleros',
'carnavaler',
],
[
'fr',
'désintéressement',
'désintéress',
],
[
'it',
'entusiastico',
'entusiast',
],
[
'nl',
'fraudebestrijding',
'fraudebestrijd',
],
[
'no',
'gjestearbeidende',
'gjestearbeid',
],
[
'pt',
'humildemente',
'humild',
],
[
'ro',
'intelectualismului',
'intelectualist',
],
[
'ru',
'пересчитаешь',
'пересчита',
],
[
'sv',
'jämförelsen',
'jämför',
],
];
}
public function testOverrides() {
$this->stemmer
->setLanguage('en');
$this->stemmer
->setOverrides([
'our' => 'special',
'term' => 'kept',
]);
$this->stemmer
->setOverrides([
'term' => 'english',
'also' => 'overriden',
], 'en');
$this
->assertEquals($this->stemmer
->stem('our'), 'special');
$this
->assertEquals($this->stemmer
->stem('term'), 'english');
$this
->assertEquals($this->stemmer
->stem('also'), 'overriden');
}
public function testWordCache() {
$testStemmer = new Stemmer();
$reflector = new \ReflectionClass('\\Drupal\\snowball_stemmer\\Stemmer');
$language = $reflector
->getProperty('language');
$language
->setAccessible(TRUE);
$stemmers = $reflector
->getProperty('stemmers');
$stemmers
->setAccessible(TRUE);
$prophecyGerman = $this
->prophesize(German::CLASS);
$prophecyGerman
->stem('wordish')
->willReturn('word');
$prophecyGerman
->stem('otherly')
->willReturn('other');
$prophecyEnglish = $this
->prophesize(English::CLASS);
$prophecyEnglish
->stem('wordish')
->willReturn('wordy');
$prophecyEnglish
->stem('justly')
->willReturn('just');
$stemmers
->setValue($testStemmer, [
'de' => $prophecyGerman
->reveal(),
'en' => $prophecyEnglish
->reveal(),
]);
$language
->setValue($testStemmer, 'de');
$this
->assertEquals($testStemmer
->stem('wordish'), 'word');
$this
->assertEquals($testStemmer
->stem('otherly'), 'other');
$language
->setValue($testStemmer, 'en');
$this
->assertEquals($testStemmer
->stem('wordish'), 'wordy');
$this
->assertEquals($testStemmer
->stem('justly'), 'just');
}
}