You are here

class StemmerTest in Snowball Stemmer 8

Same name in this branch
  1. 8 tests/src/Unit/StemmerTest.php \Drupal\Tests\snowball_stemmer\Unit\StemmerTest
  2. 8 tests/src/Unit/Plugin/Processor/StemmerTest.php \Drupal\Tests\snowball_stemmer\Unit\Plugin\Processor\StemmerTest
Same name and namespace in other branches
  1. 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

Expanded class hierarchy of StemmerTest

File

tests/src/Unit/StemmerTest.php, line 17

Namespace

Drupal\Tests\snowball_stemmer\Unit
View 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

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
StemmerTest::languageStrings public function List of words for each language.
StemmerTest::setUp protected function Create new object for Stemmer. Overrides UnitTestCase::setUp
StemmerTest::testOverrides public function Test overrides.
StemmerTest::testStemming public function Test stemming for each language.
StemmerTest::testUnknownLanguage public function Test unknown language.
StemmerTest::testWordCache public function Test word cache.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.