class RadiosTest in Plugin 8.2
Same name in this branch
- 8.2 tests/src/Functional/Plugin/PluginSelector/RadiosTest.php \Drupal\Tests\plugin\Functional\Plugin\PluginSelector\RadiosTest
- 8.2 tests/src/Unit/Plugin/Plugin/PluginSelector/RadiosTest.php \Drupal\Tests\plugin\Unit\Plugin\Plugin\PluginSelector\RadiosTest
@coversDefaultClass \Drupal\plugin\Plugin\Plugin\PluginSelector\Radios
@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\RadiosTest
- class \Drupal\Tests\plugin\Unit\Plugin\Plugin\PluginSelector\PluginSelectorBaseTestBase
Expanded class hierarchy of RadiosTest
File
- tests/
src/ Unit/ Plugin/ Plugin/ PluginSelector/ RadiosTest.php, line 17
Namespace
Drupal\Tests\plugin\Unit\Plugin\Plugin\PluginSelectorView source
class RadiosTest extends PluginSelectorBaseTestBase {
/**
* The class under test.
*
* @var \Drupal\plugin\Plugin\Plugin\PluginSelector\Radios
*/
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->sut = new Radios([], $this->pluginId, $this->pluginDefinition, $this->defaultPluginResolver, $this->stringTranslation, $this->responsePolicy);
$this->sut
->setSelectablePluginType($this->selectablePluginType);
}
/**
* @covers ::buildSelectorForm
*/
public function testBuildSelectorFormWithoutAvailablePlugins() {
$form = [];
$form_state = $this
->createMock(FormStateInterface::class);
$this->selectablePluginManager
->expects($this
->any())
->method('getDefinitions')
->willReturn([]);
$build = $this->sut
->buildSelectorForm($form, $form_state);
$this
->assertArrayHasKey('clear', $build);
}
/**
* @covers ::buildSelector
*/
public function testBuildSelector() {
$this->stringTranslation
->expects($this
->any())
->method('translate')
->willReturnArgument(0);
$method = new \ReflectionMethod($this->sut, 'buildSelector');
$method
->setAccessible(TRUE);
$plugin_id = $this
->randomMachineName();
$plugin_label = $this
->randomMachineName();
$plugin_definition = $this
->createMock(PluginLabelDefinitionInterface::class);
$plugin_definition
->expects($this
->atLeastOnce())
->method('getLabel')
->willReturn($plugin_label);
$plugin = $this
->createMock(PluginInspectionInterface::class);
$plugin
->expects($this
->atLeastOnce())
->method('getPluginDefinition')
->willReturn($plugin_definition);
$plugin
->expects($this
->atLeastOnce())
->method('getPluginId')
->willReturn($plugin_id);
$this->selectablePluginType
->expects($this
->atLeastOnce())
->method('ensureTypedPluginDefinition')
->willReturnArgument(0);
$this->sut
->setSelectedPlugin($plugin);
$selector_title = $this
->randomMachineName();
$this->sut
->setLabel($selector_title);
$selector_description = $this
->randomMachineName();
$this->sut
->setDescription($selector_description);
$element = array(
'#parents' => array(
'foo',
'bar',
),
'#title' => $selector_title,
);
$form_state = $this
->createMock(FormStateInterface::class);
$available_plugins = array(
$plugin,
);
$expected_build_plugin_id = array(
'#ajax' => array(
'callback' => array(
Radios::class,
'ajaxRebuildForm',
),
'effect' => 'fade',
'event' => 'change',
'progress' => 'none',
'trigger_as' => array(
'name' => 'foo__bar__select__container__change',
),
),
'#attached' => [
'library' => [
'plugin/plugin_selector.plugin_radios',
],
],
'#default_value' => $plugin_id,
'#empty_value' => 'select',
'#options' => array(
$plugin_id => $plugin_label,
),
'#required' => FALSE,
'#title' => $selector_title,
'#description' => $selector_description,
'#type' => 'radios',
);
$expected_build_change = array(
'#ajax' => array(
'callback' => array(
AdvancedPluginSelectorBase::class,
'ajaxRebuildForm',
),
),
'#attributes' => array(
'class' => array(
'js-hide',
),
),
'#limit_validation_errors' => array(
array(
'foo',
'bar',
'select',
'plugin_id',
),
),
'#name' => 'foo__bar__select__container__change',
'#submit' => [
[
AdvancedPluginSelectorBase::class,
'rebuildForm',
],
],
'#type' => 'submit',
'#value' => 'Choose',
);
$build = $method
->invokeArgs($this->sut, array(
$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. | |
RadiosTest:: |
protected | property | The response policy. | |
RadiosTest:: |
protected | property | The string translator. | |
RadiosTest:: |
protected | property |
The class under test. Overrides PluginSelectorBaseTestBase:: |
|
RadiosTest:: |
protected | function |
Overrides PluginSelectorBaseTestBase:: |
|
RadiosTest:: |
public | function | @covers ::buildSelector | |
RadiosTest:: |
public | function | @covers ::buildSelectorForm | |
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. |