You are here

class FieldsHelperTest in Search API 8

Tests the fields helper utility class.

@coversDefaultClass \Drupal\search_api\Utility\FieldsHelper

@group search_api

Hierarchy

Expanded class hierarchy of FieldsHelperTest

File

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

Namespace

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

  /**
   * The field object being tested.
   *
   * @var \Drupal\search_api\Utility\FieldsHelper
   */
  protected $fieldsHelper;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $entity_type_manager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $entity_field_manager = $this
      ->createMock(EntityFieldManagerInterface::class);
    $entity_type_info = $this
      ->createMock(EntityTypeBundleInfoInterface::class);
    $data_type_helper = $this
      ->createMock(DataTypeHelperInterface::class);
    $this->fieldsHelper = new FieldsHelper($entity_type_manager, $entity_field_manager, $entity_type_info, $data_type_helper);
  }

  /**
   * Tests extracting field values.
   *
   * @covers ::extractFieldValues
   */
  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));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldsHelperTest::$fieldsHelper protected property The field object being tested.
FieldsHelperTest::setUp protected function Overrides UnitTestCase::setUp
FieldsHelperTest::testExtractFieldValues public function Tests extracting field values.
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.