You are here

protected function AggregatedFieldsTest::setUp in Search API 8

Creates a new processor object for use in the tests.

Overrides UnitTestCase::setUp

File

tests/src/Unit/Processor/AggregatedFieldsTest.php, line 63

Class

AggregatedFieldsTest
Tests the "Aggregated fields" processor.

Namespace

Drupal\Tests\search_api\Unit\Processor

Code

protected function setUp() {
  parent::setUp();
  $datasource = $this
    ->createMock(DatasourceInterface::class);
  $datasource
    ->expects($this
    ->any())
    ->method('getPropertyDefinitions')
    ->willReturn([]);
  $this->index = new Index([
    'datasourceInstances' => [
      'entity:test1' => $datasource,
      'entity:test2' => $datasource,
      'entity:test3' => $datasource,
    ],
    'processorInstances' => [],
    'field_settings' => [
      'foo' => [
        'type' => 'string',
        'datasource_id' => 'entity:test1',
        'property_path' => 'foo',
      ],
      'bar' => [
        'type' => 'string',
        'datasource_id' => 'entity:test1',
        'property_path' => 'foo:bar',
      ],
      'bla' => [
        'type' => 'string',
        'datasource_id' => 'entity:test2',
        'property_path' => 'foobaz:bla',
      ],
      'always_empty' => [
        'type' => 'string',
        'datasource_id' => 'entity:test3',
        'property_path' => 'always_empty',
      ],
      'aggregated_field' => [
        'type' => 'text',
        'property_path' => 'aggregated_field',
      ],
    ],
  ], 'search_api_index');
  $this->processor = new AggregatedFields([
    '#index' => $this->index,
  ], 'aggregated_field', []);
  $this->index
    ->addProcessor($this->processor);
  $this
    ->setUpMockContainer();
  $plugin_helper = $this
    ->createMock(PluginHelperInterface::class);
  $this->container
    ->set('search_api.plugin_helper', $plugin_helper);

  // We want to check correct data type handling, so we need a somewhat more
  // complex mock-up for the datatype plugin handler.

  /** @var \PHPUnit\Framework\MockObject\MockObject|\Drupal\search_api\DataType\DataTypePluginManager $data_type_manager */
  $data_type_manager = $this->container
    ->get('plugin.manager.search_api.data_type');
  $data_type_manager
    ->method('hasDefinition')
    ->willReturn(TRUE);
  $this->valueCallback = function ($value) {
    if (is_numeric($value)) {
      return $value + 1;
    }
    else {
      return '*' . $value;
    }
  };
  $data_type = $this
    ->createMock(DataTypeInterface::class);
  $data_type
    ->method('getValue')
    ->willReturnCallback($this->valueCallback);
  $data_type_manager
    ->method('createInstance')
    ->willReturnMap([
    [
      'text',
      [],
      $data_type,
    ],
  ]);
}