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
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\search_api_autocomplete\Unit\SuggestionFactoryTest
Expanded class hierarchy of SuggestionFactoryTest
File
- tests/
src/ Unit/ SuggestionFactoryTest.php, line 16
Namespace
Drupal\Tests\search_api_autocomplete\UnitView 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
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. | |
SuggestionFactoryTest:: |
public | function | Tests creating a suggestion from the suggested keys. | |
SuggestionFactoryTest:: |
public | function | Tests creating a suggestion from the suggested suffix. | |
SuggestionFactoryTest:: |
public | function | Tests creating a URL suggestion. | |
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. | |
UnitTestCase:: |
protected | function | 340 |