From 9f6b172c6137d29f3dd7bd19d0f740ab42ef8ee7 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Tue, 5 Mar 2024 00:51:00 +0000 Subject: [PATCH] Issue #3119761 by dww, aleevas, Hardik_Patel_12, quietone, tedbow, _utsavsharma, smustgrave, xjm, longwave: Replace multiple test methods in InfoParserUnitTest with 1 testInfoException and a dataprovider --- .../Core/Extension/InfoParserUnitTest.php | 160 +++++++----------- 1 file changed, 65 insertions(+), 95 deletions(-) diff --git a/core/tests/Drupal/Tests/Core/Extension/InfoParserUnitTest.php b/core/tests/Drupal/Tests/Core/Extension/InfoParserUnitTest.php index 4e70611ee378..667659018eae 100644 --- a/core/tests/Drupal/Tests/Core/Extension/InfoParserUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Extension/InfoParserUnitTest.php @@ -55,94 +55,101 @@ public function testInfoParserNonExisting() { /** * Tests if correct exception is thrown for a broken info file. * - * @covers ::parse + * @param string $yaml + * The YAML to use to create the file to parse. + * @param string $expected_exception_message + * The expected exception message. + * + * @dataProvider providerInfoException */ - public function testInfoParserBroken() { - $broken_info = <<<BROKEN_INFO -# info.yml for testing broken YAML parsing exception handling. -name: File -type: module -description: 'Defines a file field type.' -package: Core -version: VERSION -core_version_requirement: '*' -dependencies::;; - - field -BROKEN_INFO; + public function testInfoException($yaml, $expected_exception_message): void { vfsStream::setup('modules'); vfsStream::create([ 'fixtures' => [ - 'broken.info.txt' => $broken_info, + "broken.info.txt" => $yaml, + "broken-duplicate.info.txt" => $yaml, ], ]); - $filename = vfsStream::url('modules/fixtures/broken.info.txt'); - $this->expectException('\Drupal\Core\Extension\InfoParserException'); - $this->expectExceptionMessage('broken.info.txt'); - $this->infoParser->parse($filename); + + try { + $this->infoParser->parse(vfsStream::url("modules/fixtures/broken.info.txt")); + } + catch (InfoParserException $exception) { + $this->assertSame("$expected_exception_message vfs://modules/fixtures/broken.info.txt", $exception->getMessage()); + } + + $this->expectException(InfoParserException::class); + $this->expectExceptionMessage("$expected_exception_message vfs://modules/fixtures/broken-duplicate.info.txt"); + $this->infoParser->parse(vfsStream::url("modules/fixtures/broken-duplicate.info.txt")); } /** - * Tests that missing required keys are detected. - * - * @covers ::parse + * Data provider for testInfoException(). */ - public function testInfoParserMissingKeys() { - $missing_keys = <<<MISSING_KEYS -# info.yml for testing missing name, description, and type keys. + public static function providerInfoException(): array { + return [ + 'missing required key, type' => [ + <<<YML +name: File +description: Missing key package: Core version: VERSION dependencies: - field -MISSING_KEYS; - - vfsStream::setup('modules'); - vfsStream::create([ - 'fixtures' => [ - 'missing_keys.info.txt' => $missing_keys, +YML, + "Missing required keys (type) in", ], - ]); - $filename = vfsStream::url('modules/fixtures/missing_keys.info.txt'); - $this->expectException('\Drupal\Core\Extension\InfoParserException'); - $this->expectExceptionMessage('Missing required keys (type, name) in vfs://modules/fixtures/missing_keys.info.txt'); - $this->infoParser->parse($filename); + 'missing core_version_requirement' => [ + <<<YML +version: VERSION +type: module +name: Skynet +dependencies: + - self_awareness +YML, + "The 'core_version_requirement' key must be present in", + ], + 'missing two required keys' => [ + <<<YML +package: Core +version: VERSION +dependencies: + - field +YML, + 'Missing required keys (type, name) in', + ], + ]; } /** - * Tests that a missing 'core_version_requirement' key is detected. + * Tests that the correct exception is thrown for a broken info file. * * @covers ::parse */ - public function testMissingCoreVersionRequirement() { - $missing_core_version_requirement = <<<MISSING_CORE_VERSION_REQUIREMENT -# info.yml for testing core_version_requirement. -version: VERSION + public function testInfoParserBroken() { + $broken_info = <<<BROKEN_INFO +# info.yml for testing broken YAML parsing exception handling. +name: File type: module -name: Skynet -dependencies: - - self_awareness -MISSING_CORE_VERSION_REQUIREMENT; +description: 'Defines a file field type.' +package: Core +version: VERSION +core_version_requirement: '*' +dependencies::;; + - field +BROKEN_INFO; vfsStream::setup('modules'); vfsStream::create([ 'fixtures' => [ - 'missing_core_version_requirement.info.txt' => $missing_core_version_requirement, - 'missing_core_version_requirement-duplicate.info.txt' => $missing_core_version_requirement, + 'broken.info.txt' => $broken_info, ], ]); - $exception_message = "The 'core_version_requirement' key must be present in vfs://modules/fixtures/missing_core_version_requirement"; - // Set the expected exception for the 2nd call to parse(). + $filename = vfsStream::url('modules/fixtures/broken.info.txt'); $this->expectException('\Drupal\Core\Extension\InfoParserException'); - $this->expectExceptionMessage("$exception_message-duplicate.info.txt"); - - try { - $this->infoParser->parse(vfsStream::url('modules/fixtures/missing_core_version_requirement.info.txt')); - } - catch (InfoParserException $exception) { - $this->assertSame("$exception_message.info.txt", $exception->getMessage()); - - $this->infoParser->parse(vfsStream::url('modules/fixtures/missing_core_version_requirement-duplicate.info.txt')); - } + $this->expectExceptionMessage('Unable to parse vfs://modules/fixtures/broken.info.txt'); + $this->infoParser->parse($filename); } /** @@ -169,43 +176,6 @@ public function testTestingPackageMissingCoreVersionRequirement() { $this->assertSame($info_values['core_version_requirement'], \Drupal::VERSION); } - /** - * Tests that missing required key is detected. - * - * @covers ::parse - */ - public function testInfoParserMissingKey() { - $missing_key = <<<MISSING_KEY -# info.yml for testing missing type key. -name: File -description: 'Defines a file field type.' -package: Core -version: VERSION -dependencies: - - field -MISSING_KEY; - - vfsStream::setup('modules'); - vfsStream::create([ - 'fixtures' => [ - 'missing_key.info.txt' => $missing_key, - 'missing_key-duplicate.info.txt' => $missing_key, - ], - ]); - // Set the expected exception for the 2nd call to parse(). - $this->expectException(InfoParserException::class); - $this->expectExceptionMessage('Missing required keys (type) in vfs://modules/fixtures/missing_key-duplicate.info.txt'); - try { - $this->infoParser->parse(vfsStream::url('modules/fixtures/missing_key.info.txt')); - } - catch (InfoParserException $exception) { - $this->assertSame('Missing required keys (type) in vfs://modules/fixtures/missing_key.info.txt', $exception->getMessage()); - - $this->infoParser->parse(vfsStream::url('modules/fixtures/missing_key-duplicate.info.txt')); - } - - } - /** * Tests common info file. * -- GitLab