You are here

public function FieldsHelperTest::testExtractFieldValues in Search API 8

Tests extracting field values.

@covers ::extractFieldValues

File

tests/src/Unit/FieldsHelperTest.php, line 49

Class

FieldsHelperTest
Tests the fields helper utility class.

Namespace

Drupal\Tests\search_api\Unit

Code

public function testExtractFieldValues() {
  $field_data = $this
    ->createMock(TestComplexDataInterface::class);
  $field_data_definition = $this
    ->createMock(ComplexDataDefinitionInterface::class);
  $field_data_definition
    ->expects($this
    ->any())
    ->method('isList')
    ->will($this
    ->returnValue(FALSE));
  $field_data_definition
    ->expects($this
    ->any())
    ->method('getMainPropertyName')
    ->will($this
    ->returnValue('value'));
  $field_data
    ->expects($this
    ->any())
    ->method('getDataDefinition')
    ->will($this
    ->returnValue($field_data_definition));
  $value_definition = $this
    ->createMock(DataDefinitionInterface::class);
  $value_definition
    ->expects($this
    ->any())
    ->method('isList')
    ->will($this
    ->returnValue(FALSE));
  $value = $this
    ->createMock(TypedDataInterface::class);
  $value
    ->expects($this
    ->any())
    ->method('getValue')
    ->will($this
    ->returnValue('asd'));
  $value
    ->expects($this
    ->any())
    ->method('getDataDefinition')
    ->will($this
    ->returnValue($value_definition));

  // Mock variants for with and without computed data.
  $field_data
    ->expects($this
    ->any())
    ->method('getProperties')
    ->willReturnMap([
    [
      FALSE,
      [],
    ],
    [
      TRUE,
      [
        'value' => $value,
      ],
    ],
  ]);
  $this
    ->assertArrayEquals([
    'asd',
  ], $this->fieldsHelper
    ->extractFieldValues($field_data));
}