public function ReferenceWidgetTest::testGetOptions in Select (or other) 8.3
Same name and namespace in other branches
- 8 tests/src/Unit/ReferenceWidgetTest.php \Drupal\Tests\select_or_other\Unit\ReferenceWidgetTest::testGetOptions()
- 4.x tests/src/Unit/ReferenceWidgetTest.php \Drupal\Tests\select_or_other\Unit\ReferenceWidgetTest::testGetOptions()
Test if defaultSettings() returns the correct keys.
File
- Tests/
src/ Unit/ ReferenceWidgetTest.php, line 52
Class
- ReferenceWidgetTest
- Tests the form element implementation.
Namespace
Drupal\Tests\select_or_other\UnitCode
public function testGetOptions() {
$entityID = 1;
$entityLabel = 'Label';
$entityMock = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\Entity')
->disableOriginalConstructor()
->getMock();
$entityMock
->expects($this
->exactly(1))
->method('id')
->willReturn($entityID);
$entityMock
->expects($this
->exactly(2))
->method('label')
->willReturn($entityLabel);
$entityStorageMock = $this
->getMockForAbstractClass('\\Drupal\\Core\\Entity\\EntityStorageInterface');
$entityStorageMock
->expects($this
->exactly(2))
->method('loadByProperties')
->willReturnOnConsecutiveCalls([], [
$entityMock,
]);
$mock = $this->mockBuilder
->disableOriginalConstructor()
->setMethods([
'getEntityStorage',
'getBundleKey',
'getSelectionHandlerSetting',
])
->getMock();
$mock
->expects($this
->exactly(2))
->method('getEntityStorage')
->willReturn($entityStorageMock);
$mock
->expects($this
->exactly(2))
->method('getBundleKey')
->willReturn('bundle');
$mock
->expects($this
->exactly(2))
->method('getSelectionHandlerSetting')
->willReturn('target_bundle');
$getOptions = new ReflectionMethod($mock, 'getOptions');
$getOptions
->setAccessible(TRUE);
// First invocation returns an empty array because there are no entities.
$options = $getOptions
->invoke($mock);
$expected = [];
$this
->assertArrayEquals($options, $expected);
// Second invocation returns a key=>value array because there is one entity.
$options = $getOptions
->invoke($mock);
$expected = [
"{$entityLabel} ({$entityID})" => $entityLabel,
];
$this
->assertArrayEquals($options, $expected);
}