class StemmerTest in Snowball Stemmer 8
Same name in this branch
- 8 tests/src/Unit/StemmerTest.php \Drupal\Tests\snowball_stemmer\Unit\StemmerTest
- 8 tests/src/Unit/Plugin/Processor/StemmerTest.php \Drupal\Tests\snowball_stemmer\Unit\Plugin\Processor\StemmerTest
Same name and namespace in other branches
- 2.x tests/src/Unit/StemmerTest.php \Drupal\Tests\snowball_stemmer\Unit\StemmerTest
Test Snowball Stemmer class wrapping.
@coversDefaultClass \Drupal\snowball_stemmer\Stemmer
@group snowball_stemmer
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\snowball_stemmer\Unit\StemmerTest
Expanded class hierarchy of StemmerTest
File
- tests/
src/ Unit/ StemmerTest.php, line 17
Namespace
Drupal\Tests\snowball_stemmer\UnitView source
class StemmerTest extends UnitTestCase {
/**
* Create new object for Stemmer.
*/
protected function setUp() {
parent::setUp();
$this->stemmer = new Stemmer();
}
/**
* Test stemming for each language.
*
* @dataProvider languageStrings
*/
public function testStemming($language, $original, $stemmed) {
$success = $this->stemmer
->setLanguage($language);
$this
->assertTrue($success);
$word = $this->stemmer
->stem($original);
$this
->assertEquals($word, $stemmed);
}
/**
* Test unknown language.
*/
public function testUnknownLanguage() {
$this
->expectException('\\Drupal\\snowball_stemmer\\LanguageNotSetException');
$success = $this->stemmer
->setLanguage('xx');
$this
->assertFalse($success);
$this->stemmer
->stem('word');
}
/**
* List of words for each language.
*
* The class itself tests multiple strings, just need a string for each
* language to check it is called correctly.
*/
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',
],
];
}
/**
* Test overrides.
*
* @covers ::setOverrides
* @covers ::hasOverride
*/
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');
}
/**
* Test word cache.
*
* Feels like this test could be simpler with a bit of refactoring the class.
*/
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');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
StemmerTest:: |
public | function | List of words for each language. | |
StemmerTest:: |
protected | function |
Create new object for Stemmer. Overrides UnitTestCase:: |
|
StemmerTest:: |
public | function | Test overrides. | |
StemmerTest:: |
public | function | Test stemming for each language. | |
StemmerTest:: |
public | function | Test unknown language. | |
StemmerTest:: |
public | function | Test word cache. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |