You are here

class SearchApiBackendUnitTest in Search API Solr 8.2

Same name and namespace in other branches
  1. 8.3 tests/src/Unit/SearchApiBackendUnitTest.php \Drupal\Tests\search_api_solr\Unit\SearchApiBackendUnitTest
  2. 8 tests/src/Unit/SearchApiBackendUnitTest.php \Drupal\Tests\search_api_solr\Unit\SearchApiBackendUnitTest
  3. 4.x tests/src/Unit/SearchApiBackendUnitTest.php \Drupal\Tests\search_api_solr\Unit\SearchApiBackendUnitTest

Tests functionality of the backend.

@coversDefaultClass \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend

@group search_api_solr

Hierarchy

Expanded class hierarchy of SearchApiBackendUnitTest

File

tests/src/Unit/SearchApiBackendUnitTest.php, line 30

Namespace

Drupal\Tests\search_api_solr\Unit
View source
class SearchApiBackendUnitTest extends UnitTestCase {
  use InvokeMethodTrait;

  /**
   * @covers       ::addIndexField
   *
   * @dataProvider addIndexFieldDataProvider
   *
   * @param mixed $input
   *   Field value.
   *
   * @param string $type
   *   Field type.
   *
   * @param mixed $expected
   *   Expected result.
   */
  public function testIndexField($input, $type, $expected) {
    $field = 'testField';
    $document = $this
      ->prophesize(Document::class);
    if (NULL !== $expected) {
      if (is_array($expected)) {
        $document
          ->addField($field, $expected[0], $expected[1])
          ->shouldBeCalled();
      }
      else {
        $document
          ->addField($field, $expected)
          ->shouldBeCalled();
      }
    }
    else {
      $document
        ->addField($field, $expected)
        ->shouldNotBeCalled();
    }
    $args = [
      $document
        ->reveal(),
      $field,
      [
        $input,
      ],
      $type,
    ];

    // Get dummies for most constructor args of SearchApiSolrBackend.
    $module_handler = $this
      ->prophesize(ModuleHandlerInterface::class)
      ->reveal();
    $config = $this
      ->prophesize(Config::class)
      ->reveal();
    $language_manager = $this
      ->prophesize(LanguageManagerInterface::class)
      ->reveal();
    $solr_connector_plugin_manager = $this
      ->prophesize(SolrConnectorPluginManager::class)
      ->reveal();
    $fields_helper = $this
      ->prophesize(FieldsHelperInterface::class)
      ->reveal();
    $data_type_helper = $this
      ->prophesize(DataTypeHelperInterface::class)
      ->reveal();

    // This helper is actually used.
    $query_helper = new Helper();
    $backend = new SearchApiSolrBackend([], NULL, [], $module_handler, $config, $language_manager, $solr_connector_plugin_manager, $fields_helper, $data_type_helper, $query_helper);

    // addIndexField() should convert the $input according to $type and call
    // Document::addField() with the correctly converted $input.
    $this
      ->invokeMethod($backend, 'addIndexField', $args, []);
  }

  /**
   * Data provider for testIndexField method. Set of values can be extended to
   * check other field types and values.
   *
   * @return array
   */
  public function addIndexFieldDataProvider() {
    return [
      // addIndexField() should be called.
      [
        '0',
        'boolean',
        'false',
      ],
      [
        '1',
        'boolean',
        'true',
      ],
      [
        0,
        'boolean',
        'false',
      ],
      [
        1,
        'boolean',
        'true',
      ],
      [
        FALSE,
        'boolean',
        'false',
      ],
      [
        TRUE,
        'boolean',
        'true',
      ],
      [
        '2016-05-25T14:00:00+10',
        'date',
        '2016-05-25T04:00:00Z',
      ],
      [
        '1465819200',
        'date',
        '2016-06-13T12:00:00Z',
      ],
      [
        new DateRangeValue('2016-05-25T14:00:00+10', '2017-05-25T14:00:00+10'),
        'solr_date_range',
        '[2016-05-25T04:00:00Z TO 2017-05-25T04:00:00Z]',
      ],
      [
        -1,
        'integer',
        -1,
      ],
      [
        0,
        'integer',
        0,
      ],
      [
        1,
        'integer',
        1,
      ],
      [
        -1.0,
        'decimal',
        -1.0,
      ],
      [
        0.0,
        'decimal',
        0.0,
      ],
      [
        1.3,
        'decimal',
        1.3,
      ],
      [
        'foo',
        'string',
        'foo',
      ],
      [
        new TextValue('foo bar'),
        'text',
        'foo bar',
      ],
      [
        (new TextValue(''))
          ->setTokens([
          new TextToken('bar'),
        ]),
        'text',
        [
          'bar',
          1,
        ],
      ],
      // addIndexField() should not be called.
      [
        NULL,
        'boolean',
        NULL,
      ],
      [
        NULL,
        'date',
        NULL,
      ],
      [
        NULL,
        'solr_date_range',
        NULL,
      ],
      [
        NULL,
        'integer',
        NULL,
      ],
      [
        NULL,
        'decimal',
        NULL,
      ],
      [
        NULL,
        'string',
        NULL,
      ],
      [
        '',
        'string',
        NULL,
      ],
      [
        new TextValue(''),
        'text',
        NULL,
      ],
      [
        (new TextValue(''))
          ->setTokens([
          new TextToken(''),
        ]),
        'text',
        NULL,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
InvokeMethodTrait::invokeMethod protected function Calls protected/private method of a class.
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.
SearchApiBackendUnitTest::addIndexFieldDataProvider public function Data provider for testIndexField method. Set of values can be extended to check other field types and values.
SearchApiBackendUnitTest::testIndexField public function @covers ::addIndexField
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