You are here

class BackendPluginBaseTest in Search API 8

Tests methods provided by the backend plugin base class.

@coversDefaultClass \Drupal\search_api\Backend\BackendPluginBase

@group search_api

Hierarchy

Expanded class hierarchy of BackendPluginBaseTest

File

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

Namespace

Drupal\Tests\search_api\Unit
View source
class BackendPluginBaseTest extends UnitTestCase {

  /**
   * Tests whether fulltext fields are correctly extracted from queries.
   *
   * @param string[]|null $query_fields
   *   The fulltext fields set explicitly on the query, if any.
   * @param string[] $expected
   *   The field IDs that are expected to be returned.
   *
   * @covers ::getQueryFulltextFields
   *
   * @dataProvider getQueryFulltextFieldsDataProvider
   */
  public function testGetQueryFulltextFields($query_fields, array $expected) {
    $index = $this
      ->createMock(IndexInterface::class);
    $index
      ->method('getFulltextFields')
      ->willReturn([
      'field1',
      'field2',
    ]);
    $query = $this
      ->createMock(QueryInterface::class);
    $query
      ->method('getFulltextFields')
      ->willReturn($query_fields);
    $query
      ->method('getIndex')
      ->willReturn($index);
    $backend = new TestBackend([], '', []);
    $class = new \ReflectionClass(TestBackend::class);
    $method = $class
      ->getMethod('getQueryFulltextFields');
    $method
      ->setAccessible(TRUE);
    $this
      ->assertEquals($expected, $method
      ->invokeArgs($backend, [
      $query,
    ]));
  }

  /**
   * Provides test data for testGetQueryFulltextFields().
   *
   * @return array[]
   *   An array of argument arrays for testGetQueryFulltextFields().
   */
  public function getQueryFulltextFieldsDataProvider() {
    return [
      'null fields' => [
        NULL,
        [
          'field1',
          'field2',
        ],
      ],
      'field1' => [
        [
          'field1',
        ],
        [
          'field1',
        ],
      ],
      'field2' => [
        [
          'field2',
        ],
        [
          'field2',
        ],
      ],
      'all fields' => [
        [
          'field1',
          'field2',
        ],
        [
          'field1',
          'field2',
        ],
      ],
      'invalid fields' => [
        [
          'field1',
          'foo',
        ],
        [
          'field1',
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BackendPluginBaseTest::getQueryFulltextFieldsDataProvider public function Provides test data for testGetQueryFulltextFields().
BackendPluginBaseTest::testGetQueryFulltextFields public function Tests whether fulltext fields are correctly extracted from queries.
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.
UnitTestCase::setUp protected function 340