class PluginFormFactoryTest

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Plugin/PluginFormFactoryTest.php \Drupal\Tests\Core\Plugin\PluginFormFactoryTest
  2. 10 core/tests/Drupal/Tests/Core/Plugin/PluginFormFactoryTest.php \Drupal\Tests\Core\Plugin\PluginFormFactoryTest
  3. 11.x 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 18

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() {
        $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

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
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
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function

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