class UtilityTest in Search API 8
Tests the Search API utility class.
@coversDefaultClass \Drupal\search_api\Utility\Utility
@group search_api
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\search_api\Unit\UtilityTest
Expanded class hierarchy of UtilityTest
File
- tests/
src/ Unit/ UtilityTest.php, line 18
Namespace
Drupal\Tests\search_api\UnitView source
class UtilityTest extends UnitTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$container = new ContainerBuilder();
$config_factory = $this
->createMock(ConfigFactoryInterface::class);
$config = $this
->createMock(ImmutableConfig::class);
$config
->method('get')
->willReturn(NULL);
$config_factory
->method('get')
->willReturnMap([
[
'search_api.settings',
$config,
],
]);
$container
->set('config.factory', $config_factory);
\Drupal::setContainer($container);
}
/**
* Tests formatting of boost factors.
*
* @covers ::formatBoostFactor
*/
public function testFormatBoostFactor() {
$this
->assertEquals('0.00', Utility::formatBoostFactor(0));
$this
->assertEquals('0.00', Utility::formatBoostFactor(0.0));
$this
->assertEquals('0.00', Utility::formatBoostFactor(0.0));
$this
->assertEquals('0.00', Utility::formatBoostFactor('0'));
$this
->assertEquals('0.00', Utility::formatBoostFactor('0.0'));
$this
->assertEquals('0.00', Utility::formatBoostFactor('0.00'));
$this
->assertEquals('0.00', Utility::formatBoostFactor(''));
$this
->assertEquals('1.00', Utility::formatBoostFactor(1));
$this
->assertEquals('1.00', Utility::formatBoostFactor(1.0));
$this
->assertEquals('1.00', Utility::formatBoostFactor(1.0));
$this
->assertEquals('1.10', Utility::formatBoostFactor(1.1));
$this
->assertEquals('1.01', Utility::formatBoostFactor(1.01));
$this
->assertEquals('1.00', Utility::formatBoostFactor('1'));
$this
->assertEquals('1.10', Utility::formatBoostFactor('1.1'));
$this
->assertEquals('1.01', Utility::formatBoostFactor('1.01'));
}
/**
* Tests obtaining a list of available boost factors.
*
* @covers ::getBoostFactors
*/
public function testGetBoostFactors() {
$expected = [
'0.00' => '0.00',
'0.10' => '0.10',
'0.20' => '0.20',
'0.30' => '0.30',
'0.50' => '0.50',
'0.60' => '0.60',
'0.70' => '0.70',
'0.80' => '0.80',
'0.90' => '0.90',
'1.00' => '1.00',
'1.10' => '1.10',
'1.20' => '1.20',
'1.30' => '1.30',
'1.40' => '1.40',
'1.50' => '1.50',
'2.00' => '2.00',
'3.00' => '3.00',
'5.00' => '5.00',
'8.00' => '8.00',
'13.00' => '13.00',
'21.00' => '21.00',
];
$this
->assertEquals($expected, Utility::getBoostFactors());
$additional_factors = [
4,
'0.81',
3,
0.8100000000000001,
'3',
'8.2',
'0.81',
14.123,
0.8110000000000001,
];
$expected = [
'0.00' => '0.00',
'0.10' => '0.10',
'0.20' => '0.20',
'0.30' => '0.30',
'0.50' => '0.50',
'0.60' => '0.60',
'0.70' => '0.70',
'0.80' => '0.80',
'0.81' => '0.81',
'0.90' => '0.90',
'1.00' => '1.00',
'1.10' => '1.10',
'1.20' => '1.20',
'1.30' => '1.30',
'1.40' => '1.40',
'1.50' => '1.50',
'2.00' => '2.00',
'3.00' => '3.00',
'4.00' => '4.00',
'5.00' => '5.00',
'8.00' => '8.00',
'8.20' => '8.20',
'13.00' => '13.00',
'14.12' => '14.12',
'21.00' => '21.00',
];
$this
->assertEquals($expected, Utility::getBoostFactors($additional_factors));
}
}
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. | |
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. | |
UtilityTest:: |
protected | function |
Overrides UnitTestCase:: |
|
UtilityTest:: |
public | function | Tests formatting of boost factors. | |
UtilityTest:: |
public | function | Tests obtaining a list of available boost factors. |