class PluginFormFactoryTest in Drupal 9
Same name and namespace in other branches
- 8 core/tests/Drupal/Tests/Core/Plugin/PluginFormFactoryTest.php \Drupal\Tests\Core\Plugin\PluginFormFactoryTest
@coversDefaultClass \Drupal\Core\Plugin\PluginFormFactory @group Plugin
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, PhpUnitCompatibilityTrait, PhpUnitWarnings
- class \Drupal\Tests\Core\Plugin\PluginFormFactoryTest
Expanded class hierarchy of PluginFormFactoryTest
File
- core/
tests/ Drupal/ Tests/ Core/ Plugin/ PluginFormFactoryTest.php, line 18
Namespace
Drupal\Tests\Core\PluginView source
class PluginFormFactoryTest extends UnitTestCase {
/**
* The class resolver.
*
* @var \Drupal\Core\DependencyInjection\ClassResolverInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $classResolver;
/**
* The manager being tested.
*
* @var \Drupal\Core\Plugin\PluginFormFactory
*/
protected $manager;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->classResolver = $this
->prophesize(ClassResolverInterface::class);
$this->manager = new PluginFormFactory($this->classResolver
->reveal());
}
/**
* @covers ::createInstance
*/
public function testCreateInstance() {
$plugin_form = $this
->prophesize(PluginFormInterface::class);
$expected = $plugin_form
->reveal();
$this->classResolver
->getInstanceFromDefinition(get_class($expected))
->willReturn($expected);
$plugin = $this
->prophesize(PluginWithFormsInterface::class);
$plugin
->hasFormClass('standard_class')
->willReturn(TRUE);
$plugin
->getFormClass('standard_class')
->willReturn(get_class($expected));
$form_object = $this->manager
->createInstance($plugin
->reveal(), 'standard_class');
$this
->assertSame($expected, $form_object);
}
/**
* @covers ::createInstance
*/
public function testCreateInstanceUsingPlugin() {
$this->classResolver
->getInstanceFromDefinition(Argument::cetera())
->shouldNotBeCalled();
$plugin = $this
->prophesize(PluginWithFormsInterface::class)
->willImplement(PluginFormInterface::class);
$plugin
->hasFormClass('configure')
->willReturn(TRUE);
$plugin
->getFormClass('configure')
->willReturn(get_class($plugin
->reveal()));
$form_object = $this->manager
->createInstance($plugin
->reveal(), 'configure');
$this
->assertSame($plugin
->reveal(), $form_object);
}
/**
* @covers ::createInstance
*/
public function testCreateInstanceUsingPluginWithSlashes() {
$this->classResolver
->getInstanceFromDefinition(Argument::cetera())
->shouldNotBeCalled();
$plugin = $this
->prophesize(PluginWithFormsInterface::class)
->willImplement(PluginFormInterface::class);
$plugin
->hasFormClass('configure')
->willReturn(TRUE);
$plugin
->getFormClass('configure')
->willReturn('\\' . get_class($plugin
->reveal()));
$form_object = $this->manager
->createInstance($plugin
->reveal(), 'configure');
$this
->assertSame($plugin
->reveal(), $form_object);
}
/**
* @covers ::createInstance
*/
public function testCreateInstanceDefaultFallback() {
$this->classResolver
->getInstanceFromDefinition(Argument::cetera())
->shouldNotBeCalled();
$plugin = $this
->prophesize(PluginWithFormsInterface::class)
->willImplement(PluginFormInterface::class);
$plugin
->hasFormClass('missing')
->willReturn(FALSE);
$plugin
->hasFormClass('fallback')
->willReturn(TRUE);
$plugin
->getFormClass('fallback')
->willReturn(get_class($plugin
->reveal()));
$form_object = $this->manager
->createInstance($plugin
->reveal(), 'missing', 'fallback');
$this
->assertSame($plugin
->reveal(), $form_object);
}
/**
* @covers ::createInstance
*/
public function testCreateInstancePluginAware() {
$plugin_form = $this
->prophesize(PluginFormInterface::class)
->willImplement(PluginAwareInterface::class);
$expected = $plugin_form
->reveal();
$this->classResolver
->getInstanceFromDefinition(get_class($expected))
->willReturn($expected);
$plugin = $this
->prophesize(PluginWithFormsInterface::class);
$plugin
->hasFormClass('operation_aware')
->willReturn(TRUE);
$plugin
->getFormClass('operation_aware')
->willReturn(get_class($expected));
$plugin_form
->setPlugin($plugin
->reveal())
->shouldBeCalled();
$form_object = $this->manager
->createInstance($plugin
->reveal(), 'operation_aware');
$this
->assertSame($expected, $form_object);
}
/**
* @covers ::createInstance
*/
public function testCreateInstanceDefinitionException() {
$this
->expectException(InvalidPluginDefinitionException::class);
$this
->expectExceptionMessage('The "the_plugin_id" plugin did not specify a "anything" form class');
$plugin = $this
->prophesize(PluginWithFormsInterface::class);
$plugin
->getPluginId()
->willReturn('the_plugin_id');
$plugin
->hasFormClass('anything')
->willReturn(FALSE);
$form_object = $this->manager
->createInstance($plugin
->reveal(), 'anything');
$this
->assertSame(NULL, $form_object);
}
/**
* @covers ::createInstance
*/
public function testCreateInstanceInvalidException() {
$this
->expectException(InvalidPluginDefinitionException::class);
$this
->expectExceptionMessage('The "the_plugin_id" plugin did not specify a valid "invalid" form class, must implement \\Drupal\\Core\\Plugin\\PluginFormInterface');
$expected = new \stdClass();
$this->classResolver
->getInstanceFromDefinition(get_class($expected))
->willReturn($expected);
$plugin = $this
->prophesize(PluginWithFormsInterface::class);
$plugin
->getPluginId()
->willReturn('the_plugin_id');
$plugin
->hasFormClass('invalid')
->willReturn(TRUE);
$plugin
->getFormClass('invalid')
->willReturn(get_class($expected));
$form_object = $this->manager
->createInstance($plugin
->reveal(), 'invalid');
$this
->assertSame(NULL, $form_object);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpUnitWarnings:: |
private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |
PhpUnitWarnings:: |
public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |
PluginFormFactoryTest:: |
protected | property | The class resolver. | |
PluginFormFactoryTest:: |
protected | property | The manager being tested. | |
PluginFormFactoryTest:: |
protected | function |
Overrides UnitTestCase:: |
|
PluginFormFactoryTest:: |
public | function | @covers ::createInstance | |
PluginFormFactoryTest:: |
public | function | @covers ::createInstance | |
PluginFormFactoryTest:: |
public | function | @covers ::createInstance | |
PluginFormFactoryTest:: |
public | function | @covers ::createInstance | |
PluginFormFactoryTest:: |
public | function | @covers ::createInstance | |
PluginFormFactoryTest:: |
public | function | @covers ::createInstance | |
PluginFormFactoryTest:: |
public | function | @covers ::createInstance | |
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 | 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. | |
UnitTestCase:: |
public static | function |