class PluginFormFactoryTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Plugin/PluginFormFactoryTest.php \Drupal\Tests\Core\Plugin\PluginFormFactoryTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Plugin/PluginFormFactoryTest.php \Drupal\Tests\Core\Plugin\PluginFormFactoryTest
  3. 10 core/tests/Drupal/Tests/Core/Plugin/PluginFormFactoryTest.php \Drupal\Tests\Core\Plugin\PluginFormFactoryTest

@coversDefaultClass \Drupal\Core\Plugin\PluginFormFactory @group Plugin

Hierarchy

Expanded class hierarchy of PluginFormFactoryTest

File

core/tests/Drupal/Tests/Core/Plugin/PluginFormFactoryTest.php, line 20

Namespace

Drupal\Tests\Core\Plugin
View 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() : void {
        $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() : void {
        $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() : void {
        $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() : void {
        $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() : void {
        $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() : void {
        $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->assertNull($form_object);
    }
    
    /**
     * @covers ::createInstance
     */
    public function testCreateInstanceInvalidException() : void {
        $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->assertNull($form_object);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
PluginFormFactoryTest::$classResolver protected property The class resolver.
PluginFormFactoryTest::$manager protected property The manager being tested.
PluginFormFactoryTest::setUp protected function Overrides UnitTestCase::setUp
PluginFormFactoryTest::testCreateInstance public function @covers ::createInstance
PluginFormFactoryTest::testCreateInstanceDefaultFallback public function @covers ::createInstance
PluginFormFactoryTest::testCreateInstanceDefinitionException public function @covers ::createInstance
PluginFormFactoryTest::testCreateInstanceInvalidException public function @covers ::createInstance
PluginFormFactoryTest::testCreateInstancePluginAware public function @covers ::createInstance
PluginFormFactoryTest::testCreateInstanceUsingPlugin public function @covers ::createInstance
PluginFormFactoryTest::testCreateInstanceUsingPluginWithSlashes public function @covers ::createInstance
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.