You are here

class ItemFieldTest in Search API 8

Tests functionality of the field class.

@coversDefaultClass \Drupal\search_api\Item\Field

@group search_api

Hierarchy

Expanded class hierarchy of ItemFieldTest

File

tests/src/Unit/ItemFieldTest.php, line 17

Namespace

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

  /**
   * The field object being tested.
   *
   * @var \Drupal\search_api\Item\Field
   */
  protected $field;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $data_type = $this
      ->createMock(DataTypeInterface::class);
    $data_type
      ->expects($this
      ->any())
      ->method('getValue')
      ->willReturnCallback(function ($v) {
      return "*{$v}";
    });

    /** @var \Drupal\search_api\DataType\DataTypePluginManager|\PHPUnit\Framework\MockObject\MockObject $data_type_manager */
    $data_type_manager = $this
      ->getMockBuilder('Drupal\\search_api\\DataType\\DataTypePluginManager')
      ->disableOriginalConstructor()
      ->getMock();
    $data_type_manager
      ->expects($this
      ->any())
      ->method('hasDefinition')
      ->willReturn(TRUE);
    $data_type_manager
      ->expects($this
      ->any())
      ->method('createInstance')
      ->willReturn($data_type);
    $index = new Index([], 'search_api_index');
    $this->field = new Field($index, 'field');
    $this->field
      ->setDataTypeManager($data_type_manager);
  }

  /**
   * Tests setting the Values.
   *
   * @covers ::setValues
   */
  public function testSetValues() {
    $values = [
      '*foo',
      '*bar',
    ];
    $this->field
      ->setValues($values);
    $this
      ->assertEquals($values, $this->field
      ->getValues());
  }

  /**
   * Tests adding a value.
   *
   * Ensures that a string passed to addValue() is processed by the data type
   * plugin.
   *
   * @covers ::addValue
   */
  public function testAddValue() {
    $this->field
      ->setValues([
      '*foo',
    ]);
    $this->field
      ->addValue('bar');
    $this
      ->assertEquals([
      '*foo',
      '*bar',
    ], $this->field
      ->getValues());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ItemFieldTest::$field protected property The field object being tested.
ItemFieldTest::setUp protected function Overrides UnitTestCase::setUp
ItemFieldTest::testAddValue public function Tests adding a value.
ItemFieldTest::testSetValues public function Tests setting the 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.