You are here

class UtilityTest in Search API 8

Tests the Search API utility class.

@coversDefaultClass \Drupal\search_api\Utility\Utility

@group search_api

Hierarchy

Expanded class hierarchy of UtilityTest

File

tests/src/Unit/UtilityTest.php, line 18

Namespace

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

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.
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.
UtilityTest::setUp protected function Overrides UnitTestCase::setUp
UtilityTest::testFormatBoostFactor public function Tests formatting of boost factors.
UtilityTest::testGetBoostFactors public function Tests obtaining a list of available boost factors.