FieldsHelperTest.php in Search API 8
File
tests/src/Unit/FieldsHelperTest.php
View source
<?php
namespace Drupal\Tests\search_api\Unit;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\TypedData\ComplexDataDefinitionInterface;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\search_api\Utility\DataTypeHelperInterface;
use Drupal\search_api\Utility\FieldsHelper;
use Drupal\Tests\UnitTestCase;
class FieldsHelperTest extends UnitTestCase {
protected $fieldsHelper;
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);
}
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));
$field_data
->expects($this
->any())
->method('getProperties')
->willReturnMap([
[
FALSE,
[],
],
[
TRUE,
[
'value' => $value,
],
],
]);
$this
->assertArrayEquals([
'asd',
], $this->fieldsHelper
->extractFieldValues($field_data));
}
}