You are here

public function ReferenceWidgetTest::testPrepareSelectedOptions in Select (or other) 8.3

Same name and namespace in other branches
  1. 8 tests/src/Unit/ReferenceWidgetTest.php \Drupal\Tests\select_or_other\Unit\ReferenceWidgetTest::testPrepareSelectedOptions()
  2. 4.x tests/src/Unit/ReferenceWidgetTest.php \Drupal\Tests\select_or_other\Unit\ReferenceWidgetTest::testPrepareSelectedOptions()

Tests if the selected options are propery prepared.

File

Tests/src/Unit/ReferenceWidgetTest.php, line 253

Class

ReferenceWidgetTest
Tests the form element implementation.

Namespace

Drupal\Tests\select_or_other\Unit

Code

public function testPrepareSelectedOptions() {
  $entityID = 1;
  $entityLabel = 'Label';
  $entityMock = $this
    ->getMockBuilder('\\Drupal\\Core\\Entity\\Entity')
    ->disableOriginalConstructor()
    ->getMock();
  $entityMock
    ->expects($this
    ->any())
    ->method('id')
    ->willReturn($entityID);
  $entityMock
    ->expects($this
    ->any())
    ->method('label')
    ->willReturn($entityLabel);
  $entityStorageMock = $this
    ->getMockForAbstractClass('\\Drupal\\Core\\Entity\\EntityStorageInterface');
  $entityStorageMock
    ->expects($this
    ->exactly(2))
    ->method('loadMultiple')
    ->willReturnOnConsecutiveCalls([], [
    $entityMock,
  ]);
  $mock = $this->mockBuilder
    ->disableOriginalConstructor()
    ->setMethods([
    'getEntityStorage',
  ])
    ->getMock();
  $mock
    ->expects($this
    ->exactly(2))
    ->method('getEntityStorage')
    ->willReturn($entityStorageMock);
  $getOptions = new ReflectionMethod($mock, 'prepareSelectedOptions');
  $getOptions
    ->setAccessible(TRUE);

  // First invocation returns an empty array because there are no entities.
  $options = $getOptions
    ->invokeArgs($mock, [
    [],
  ]);
  $expected = [];
  $this
    ->assertArrayEquals($options, $expected);

  // Second invocation returns a value array..
  $options = $getOptions
    ->invokeArgs($mock, [
    [],
  ]);
  $expected = [
    "{$entityLabel} ({$entityID})",
  ];
  $this
    ->assertArrayEquals($options, $expected);
}