You are here

class SuggestionFactoryTest in Search API Autocomplete 8

Tests the functionality of the suggestion factory class.

@group search_api_autocomplete @coversDefaultClass \Drupal\search_api_autocomplete\Suggestion\SuggestionFactory

Hierarchy

Expanded class hierarchy of SuggestionFactoryTest

File

tests/src/Unit/SuggestionFactoryTest.php, line 16

Namespace

Drupal\Tests\search_api_autocomplete\Unit
View source
class SuggestionFactoryTest extends UnitTestCase {

  /**
   * Tests creating a suggestion from the suggested keys.
   *
   * @covers ::createFromSuggestedKeys
   */
  public function testCreateFromSuggestedKeys() {
    $factory = new SuggestionFactory('foo');
    $suggestion = $factory
      ->createFromSuggestedKeys('bar');
    $this
      ->assertEquals('bar', $suggestion
      ->getSuggestedKeys());
    $this
      ->assertNull($suggestion
      ->getUserInput());
    $this
      ->assertEquals('bar', $suggestion
      ->getLabel());
    $this
      ->assertNull($suggestion
      ->getResultsCount());
    $suggestion = $factory
      ->createFromSuggestedKeys('fooo');
    $this
      ->assertEquals('fooo', $suggestion
      ->getSuggestedKeys());
    $this
      ->assertEquals('foo', $suggestion
      ->getUserInput());
    $this
      ->assertNull($suggestion
      ->getLabel());
    $this
      ->assertEquals('o', $suggestion
      ->getSuggestionSuffix());
    $this
      ->assertNull($suggestion
      ->getResultsCount());
    $suggestion = $factory
      ->createFromSuggestedKeys('foooo', 5);
    $this
      ->assertEquals('foooo', $suggestion
      ->getSuggestedKeys());
    $this
      ->assertEquals('foo', $suggestion
      ->getUserInput());
    $this
      ->assertNull($suggestion
      ->getLabel());
    $this
      ->assertEquals('oo', $suggestion
      ->getSuggestionSuffix());
    $this
      ->assertEquals(5, $suggestion
      ->getResultsCount());

    // Test case-insensitivity.
    $suggestion = $factory
      ->createFromSuggestedKeys('Foooo', 5);
    $this
      ->assertEquals('Foooo', $suggestion
      ->getSuggestedKeys());
    $this
      ->assertEquals('Foo', $suggestion
      ->getUserInput());
    $this
      ->assertNull($suggestion
      ->getLabel());
    $this
      ->assertEquals('oo', $suggestion
      ->getSuggestionSuffix());
    $this
      ->assertEquals(5, $suggestion
      ->getResultsCount());

    // Test case-insensitivity with non-ASCII characters.
    Unicode::check();
    $factory = new SuggestionFactory('öd');
    $suggestion = $factory
      ->createFromSuggestedKeys('Ödön', 5);
    $this
      ->assertEquals('Ödön', $suggestion
      ->getSuggestedKeys());
    $this
      ->assertEquals('Öd', $suggestion
      ->getUserInput());
    $this
      ->assertNull($suggestion
      ->getLabel());
    $this
      ->assertEquals('ön', $suggestion
      ->getSuggestionSuffix());
    $this
      ->assertEquals(5, $suggestion
      ->getResultsCount());
  }

  /**
   * Tests creating a suggestion from the suggested suffix.
   *
   * @covers ::createFromSuggestionSuffix
   */
  public function testCreateFromSuggestionSuffix() {
    $factory = new SuggestionFactory('foo');
    $suggestion = $factory
      ->createFromSuggestionSuffix('bar');
    $this
      ->assertEquals('foobar', $suggestion
      ->getSuggestedKeys());
    $this
      ->assertEquals('foo', $suggestion
      ->getUserInput());
    $this
      ->assertEquals('bar', $suggestion
      ->getSuggestionSuffix());
    $this
      ->assertNull($suggestion
      ->getLabel());
    $this
      ->assertNull($suggestion
      ->getResultsCount());
    $suggestion = $factory
      ->createFromSuggestionSuffix('o', 5);
    $this
      ->assertEquals('fooo', $suggestion
      ->getSuggestedKeys());
    $this
      ->assertEquals('foo', $suggestion
      ->getUserInput());
    $this
      ->assertEquals('o', $suggestion
      ->getSuggestionSuffix());
    $this
      ->assertNull($suggestion
      ->getLabel());
    $this
      ->assertEquals(5, $suggestion
      ->getResultsCount());
  }

  /**
   * Tests creating a URL suggestion.
   *
   * @covers ::createUrlSuggestion
   */
  public function testCreateUrlSuggestion() {
    $factory = new SuggestionFactory('foo');

    /** @var \Drupal\Core\Url $url */
    $url = $this
      ->getMockBuilder(Url::class)
      ->disableOriginalConstructor()
      ->getMock();
    $suggestion = $factory
      ->createUrlSuggestion($url, 'Foobar');
    $this
      ->assertSame($url, $suggestion
      ->getUrl());
    $this
      ->assertNull($suggestion
      ->getSuggestedKeys());
    $this
      ->assertNull($suggestion
      ->getUserInput());
    $this
      ->assertEquals('Foobar', $suggestion
      ->getLabel());
    $this
      ->assertNull($suggestion
      ->getResultsCount());
    $this
      ->assertNull($suggestion
      ->getRender());
    $render = [
      'foo' => 'bar',
    ];
    $suggestion = $factory
      ->createUrlSuggestion($url, NULL, $render);
    $this
      ->assertSame($url, $suggestion
      ->getUrl());
    $this
      ->assertNull($suggestion
      ->getSuggestedKeys());
    $this
      ->assertNull($suggestion
      ->getUserInput());
    $this
      ->assertNull($suggestion
      ->getLabel());
    $this
      ->assertNull($suggestion
      ->getResultsCount());
    $this
      ->assertEquals($render, $suggestion
      ->getRender());
  }

}

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.
SuggestionFactoryTest::testCreateFromSuggestedKeys public function Tests creating a suggestion from the suggested keys.
SuggestionFactoryTest::testCreateFromSuggestionSuffix public function Tests creating a suggestion from the suggested suffix.
SuggestionFactoryTest::testCreateUrlSuggestion public function Tests creating a URL suggestion.
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.
UnitTestCase::setUp protected function 340