public function ReferenceWidgetTest::testPrepareSelectedOptions in Select (or other) 8
Same name and namespace in other branches
- 8.3 Tests/src/Unit/ReferenceWidgetTest.php \Drupal\Tests\select_or_other\Unit\ReferenceWidgetTest::testPrepareSelectedOptions()
- 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 237
Class
- ReferenceWidgetTest
- Tests the form element implementation.
Namespace
Drupal\Tests\select_or_other\UnitCode
public function testPrepareSelectedOptions() {
$entity_id = 1;
$entity_label = 'Label';
$entity_mock = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')
->disableOriginalConstructor()
->getMock();
$entity_mock
->expects($this
->any())
->method('id')
->willReturn($entity_id);
$entity_mock
->expects($this
->any())
->method('label')
->willReturn($entity_label);
$entity_storage_mock = $this
->getMockForAbstractClass('\\Drupal\\Core\\Entity\\EntityStorageInterface');
$entity_storage_mock
->expects($this
->exactly(2))
->method('loadMultiple')
->willReturnOnConsecutiveCalls([], [
$entity_mock,
]);
$mock = $this->mockBuilder
->disableOriginalConstructor()
->setMethods([
'getEntityStorage',
])
->getMock();
$mock
->expects($this
->exactly(2))
->method('getEntityStorage')
->willReturn($entity_storage_mock);
$get_options = new ReflectionMethod($mock, 'prepareSelectedOptions');
$get_options
->setAccessible(TRUE);
// First invocation returns an empty array because there are no entities.
$options = $get_options
->invokeArgs($mock, [
[],
]);
$expected = [];
$this
->assertArrayEquals($options, $expected);
// Second invocation returns a value array..
$options = $get_options
->invokeArgs($mock, [
[],
]);
$expected = [
"{$entity_label} ({$entity_id})",
];
$this
->assertArrayEquals($options, $expected);
}