class ComponentMetadataTest

Same name in this branch
  1. 11.x core/tests/Drupal/Tests/Core/Theme/Component/ComponentMetadataTest.php \Drupal\Tests\Core\Theme\Component\ComponentMetadataTest
Same name and namespace in other branches
  1. 10 core/modules/sdc/tests/src/Unit/ComponentMetadataTest.php \Drupal\Tests\sdc\Unit\ComponentMetadataTest
  2. 10 core/tests/Drupal/Tests/Core/Theme/Component/ComponentMetadataTest.php \Drupal\Tests\Core\Theme\Component\ComponentMetadataTest

Unit tests for the component metadata class.

@coversDefaultClass \Drupal\sdc\Component\ComponentMetadata @group sdc

@internal

Hierarchy

Expanded class hierarchy of ComponentMetadataTest

File

core/modules/sdc/tests/src/Unit/ComponentMetadataTest.php, line 19

Namespace

Drupal\Tests\sdc\Unit
View source
final class ComponentMetadataTest extends UnitTestCaseTest {
    
    /**
     * Tests that the correct data is returned for each property.
     *
     * @dataProvider dataProviderMetadata
     */
    public function testMetadata(array $metadata_info, array $expectations) {
        $metadata = new ComponentMetadata($metadata_info, 'foo/', FALSE);
        $this->assertSame($expectations['path'], $metadata->path);
        $this->assertSame($expectations['status'], $metadata->status);
        $this->assertSame($expectations['thumbnail'], $metadata->getThumbnailPath());
        $this->assertEquals($expectations['props'], $metadata->schema);
    }
    
    /**
     * Tests the correct checks when enforcing schemas or not.
     *
     * @dataProvider dataProviderMetadata
     */
    public function testMetadataEnforceSchema(array $metadata_info, array $expectations, bool $missing_schema) {
        if ($missing_schema) {
            $this->expectException(InvalidComponentException::class);
            $this->expectExceptionMessage('The component "' . $metadata_info['id'] . '" does not provide schema information. Schema definitions are mandatory for components declared in modules. For components declared in themes, schema definitions are only mandatory if the "enforce_prop_schemas" key is set to "true" in the theme info file.');
            new ComponentMetadata($metadata_info, 'foo/', TRUE);
        }
        else {
            new ComponentMetadata($metadata_info, 'foo/', TRUE);
            $this->expectNotToPerformAssertions();
        }
    }
    
    /**
     * Data provider for the test testMetadataEnforceSchema.
     *
     * @return array[]
     *   The batches of data.
     */
    public static function dataProviderMetadata() : array {
        return [
            'minimal example without schemas' => [
                [
                    'path' => 'foo/bar/component-name',
                    'id' => 'sdc:component-name',
                    'name' => 'Component Name',
                    'libraryOverrides' => [
                        'dependencies' => [
                            'core/drupal',
                        ],
                    ],
                    'group' => 'my-group',
                    'description' => 'My description',
                ],
                [
                    'path' => 'bar/component-name',
                    'status' => 'stable',
                    'thumbnail' => '',
                    'props' => NULL,
                ],
                TRUE,
            ],
            'complete example with schema' => [
                [
                    '$schema' => 'https://git.drupalcode.org/project/sdc/-/raw/1.x/src/metadata.schema.json',
                    'id' => 'sdc:my-button',
                    'machineName' => 'my-button',
                    'path' => 'foo/my-other/path',
                    'name' => 'Button',
                    'description' => 'JavaScript enhanced button that tracks the number of times a user clicked it.',
                    'libraryOverrides' => [
                        'dependencies' => [
                            'core/drupal',
                        ],
                    ],
                    'group' => 'my-group',
                    'props' => [
                        'type' => 'object',
                        'required' => [
                            'text',
                        ],
                        'properties' => [
                            'text' => [
                                'type' => 'string',
                                'title' => 'Title',
                                'description' => 'The title for the button',
                                'minLength' => 2,
                                'examples' => [
                                    'Press',
                                    'Submit now',
                                ],
                            ],
                            'iconType' => [
                                'type' => 'string',
                                'title' => 'Icon Type',
                                'enum' => [
                                    'power',
                                    'like',
                                    'external',
                                ],
                            ],
                        ],
                    ],
                ],
                [
                    'path' => 'my-other/path',
                    'status' => 'stable',
                    'thumbnail' => '',
                    'group' => 'my-group',
                    'additionalProperties' => FALSE,
                    'props' => [
                        'type' => 'object',
                        'required' => [
                            'text',
                        ],
                        'additionalProperties' => FALSE,
                        'properties' => [
                            'text' => [
                                'type' => [
                                    'string',
                                    'object',
                                ],
                                'title' => 'Title',
                                'description' => 'The title for the button',
                                'minLength' => 2,
                                'examples' => [
                                    'Press',
                                    'Submit now',
                                ],
                            ],
                            'iconType' => [
                                'type' => [
                                    'string',
                                    'object',
                                ],
                                'title' => 'Icon Type',
                                'enum' => [
                                    'power',
                                    'like',
                                    'external',
                                ],
                            ],
                        ],
                    ],
                ],
                FALSE,
            ],
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ComponentMetadataTest::dataProviderMetadata public static function Data provider for the test testMetadataEnforceSchema.
ComponentMetadataTest::testMetadata public function Tests that the correct data is returned for each property.
ComponentMetadataTest::testMetadataEnforceSchema public function Tests the correct checks when enforcing schemas or not.
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.
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::setUp protected function 354
UnitTestCase::setUpBeforeClass public static function
UnitTestCaseTest::testVarDumpSameProcess public function Tests the dump() function in a test run in the same process.
UnitTestCaseTest::testVarDumpSeparateProcess public function Tests the dump() function in a test run in a separate process.

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