class SelectListTest in Plugin 8.2
Same name in this branch
- 8.2 tests/src/Functional/Plugin/PluginSelector/SelectListTest.php \Drupal\Tests\plugin\Functional\Plugin\PluginSelector\SelectListTest
- 8.2 tests/src/Unit/Plugin/Plugin/PluginSelector/SelectListTest.php \Drupal\Tests\plugin\Unit\Plugin\Plugin\PluginSelector\SelectListTest
@coversDefaultClass \Drupal\plugin\Plugin\Plugin\PluginSelector\SelectList
@group Plugin
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\plugin\Unit\Plugin\Plugin\PluginSelector\PluginSelectorBaseTestBase
- class \Drupal\Tests\plugin\Unit\Plugin\Plugin\PluginSelector\SelectListTest
- class \Drupal\Tests\plugin\Unit\Plugin\Plugin\PluginSelector\PluginSelectorBaseTestBase
Expanded class hierarchy of SelectListTest
File
- tests/
src/ Unit/ Plugin/ Plugin/ PluginSelector/ SelectListTest.php, line 18
Namespace
Drupal\Tests\plugin\Unit\Plugin\Plugin\PluginSelectorView source
class SelectListTest extends PluginSelectorBaseTestBase {
/**
* The class under test.
*
* @var \Drupal\plugin\Plugin\Plugin\PluginSelector\SelectList
*/
protected $sut;
/**
* The response policy.
*
* @var \Drupal\Core\PageCache\ResponsePolicyInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $responsePolicy;
/**
* The string translator.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $stringTranslation;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->responsePolicy = $this
->getMockBuilder(KillSwitch::class)
->disableOriginalConstructor()
->getMock();
$this->stringTranslation = $this
->getStringTranslationStub();
$this->selectablePluginType
->expects($this
->any())
->method('ensureTypedPluginDefinition')
->willReturnArgument(0);
$this->sut = new SelectList([], $this->pluginId, $this->pluginDefinition, $this->defaultPluginResolver, $this->stringTranslation, $this->responsePolicy);
$this->sut
->setSelectablePluginType($this->selectablePluginType);
}
/**
* @covers ::buildSelector
* @covers ::buildOptionsLevel
*/
public function testBuildSelector() {
$this->stringTranslation
->expects($this
->any())
->method('translate')
->willReturnArgument(0);
$method = new \ReflectionMethod($this->sut, 'buildSelector');
$method
->setAccessible(TRUE);
$plugin_id_a = $this
->randomMachineName();
$plugin_label_a = $this
->randomMachineName();
$plugin_definition_a = $this
->createMock(PluginLabelDefinitionInterface::class);
$plugin_definition_a
->expects($this
->atLeastOnce())
->method('getLabel')
->willReturn($plugin_label_a);
$plugin_a = $this
->createMock(PluginInspectionInterface::class);
$plugin_a
->expects($this
->atLeastOnce())
->method('getPluginId')
->willReturn($plugin_id_a);
$plugin_id_b = $this
->randomMachineName();
$plugin_definition_b = $this
->createMock(PluginDefinitionInterface::class);
$plugin_definition_b
->expects($this
->atLeastOnce())
->method('getId')
->willReturn($plugin_id_b);
$plugin_b = $this
->createMock(PluginInspectionInterface::class);
$this->sut
->setSelectedPlugin($plugin_a);
$selector_title = $this
->randomMachineName();
$this->sut
->setLabel($selector_title);
$selector_description = $this
->randomMachineName();
$this->sut
->setDescription($selector_description);
$element = array(
'#parents' => array(
'foo',
'bar',
),
);
$form_state = $this
->createMock(FormStateInterface::class);
$available_plugins = [
$plugin_a,
$plugin_b,
];
$this->selectablePluginManager
->expects($this
->atLeastOnce())
->method('getDefinitions')
->willReturn([
$plugin_id_a => $plugin_definition_a,
$plugin_id_b => $plugin_definition_b,
]);
$expected_build_plugin_id = [
'#ajax' => [
'callback' => [
SelectList::class,
'ajaxRebuildForm',
],
'effect' => 'fade',
'event' => 'change',
'trigger_as' => [
'name' => 'foo__bar__select__container__change',
],
],
'#default_value' => $plugin_id_a,
'#empty_value' => '',
'#options' => [
$plugin_id_a => $plugin_label_a,
$plugin_id_b => $plugin_id_b,
],
'#required' => FALSE,
'#title' => $selector_title,
'#description' => $selector_description,
'#type' => 'select',
];
$expected_build_change = [
'#ajax' => [
'callback' => [
AdvancedPluginSelectorBase::class,
'ajaxRebuildForm',
],
],
'#attributes' => [
'class' => [
'js-hide',
],
],
'#limit_validation_errors' => [
[
'foo',
'bar',
'select',
'plugin_id',
],
],
'#name' => 'foo__bar__select__container__change',
'#submit' => [
[
AdvancedPluginSelectorBase::class,
'rebuildForm',
],
],
'#type' => 'submit',
'#value' => 'Choose',
];
$build = $method
->invokeArgs($this->sut, [
$element,
$form_state,
$available_plugins,
]);
$this
->assertEquals($expected_build_plugin_id, $build['container']['plugin_id']);
$this
->assertEquals($expected_build_change, $build['container']['change']);
$this
->assertSame('container', $build['container']['#type']);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
PluginSelectorBaseTestBase:: |
protected | property | The default plugin resolver. | |
PluginSelectorBaseTestBase:: |
protected | property | The plugin definition of the class under test. | |
PluginSelectorBaseTestBase:: |
protected | property | The plugin ID of the class plugin under test. | |
PluginSelectorBaseTestBase:: |
protected | property | The plugin manager through which to select plugins. | |
PluginSelectorBaseTestBase:: |
protected | property | The plugin type of which to select plugins. | |
PluginSelectorBaseTestBase:: |
protected | property | The selected plugin. | |
SelectListTest:: |
protected | property | The response policy. | |
SelectListTest:: |
protected | property | The string translator. | |
SelectListTest:: |
protected | property |
The class under test. Overrides PluginSelectorBaseTestBase:: |
|
SelectListTest:: |
protected | function |
Overrides PluginSelectorBaseTestBase:: |
|
SelectListTest:: |
public | function | @covers ::buildSelector @covers ::buildOptionsLevel | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |