From 371680df553434ac053885b980adb7267dd1288f Mon Sep 17 00:00:00 2001 From: Alexander Calandra Date: Mon, 30 Apr 2018 13:13:21 -0500 Subject: [PATCH] MQE-985: Empty Suite configuration should not generate - Adding check for empty suites --- .../Suite/SuiteGeneratorTest.php | 25 +++++++++++++++++++ .../Suite/Util/SuiteObjectExtractor.php | 24 ++++++++++++++---- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/dev/tests/unit/Magento/FunctionalTestFramework/Suite/SuiteGeneratorTest.php b/dev/tests/unit/Magento/FunctionalTestFramework/Suite/SuiteGeneratorTest.php index e7c607729..8eb486144 100644 --- a/dev/tests/unit/Magento/FunctionalTestFramework/Suite/SuiteGeneratorTest.php +++ b/dev/tests/unit/Magento/FunctionalTestFramework/Suite/SuiteGeneratorTest.php @@ -13,6 +13,7 @@ use Magento\FunctionalTestingFramework\Suite\Handlers\SuiteObjectHandler; use Magento\FunctionalTestingFramework\Suite\Parsers\SuiteDataParser; use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler; +use Magento\FunctionalTestingFramework\Test\Util\TestObjectExtractor; use Magento\FunctionalTestingFramework\Test\Parsers\TestDataParser; use Magento\FunctionalTestingFramework\Util\Manifest\DefaultTestManifest; use PHPUnit\Framework\TestCase; @@ -87,6 +88,30 @@ public function testGenerateAllSuites() $this->expectOutputString("Suite basicTestSuite generated to _generated/basicTestSuite." . PHP_EOL); } + /** + * Tests attempting to generate a suite with no included/excluded tests and no hooks + * @throws \Exception + */ + public function testGenerateEmptySuite() + { + $suiteDataArrayBuilder = new SuiteDataArrayBuilder(); + $mockData = $suiteDataArrayBuilder + ->withName('basicTestSuite') + ->build(); + unset($mockData['suites']['basicTestSuite'][TestObjectExtractor::TEST_BEFORE_HOOK]); + unset($mockData['suites']['basicTestSuite'][TestObjectExtractor::TEST_AFTER_HOOK]); + + $mockTestData = null; + $this->setMockTestAndSuiteParserOutput($mockTestData, $mockData); + + // set expected error message + $this->expectExceptionMessage("Suites must not be empty. Suite: \"basicTestSuite\""); + + // parse and generate suite object with mocked data + $mockSuiteGenerator = SuiteGenerator::getInstance(); + $mockSuiteGenerator->generateSuite("basicTestSuite"); + } + /** * Function used to set mock for parser return and force init method to run between tests. * diff --git a/src/Magento/FunctionalTestingFramework/Suite/Util/SuiteObjectExtractor.php b/src/Magento/FunctionalTestingFramework/Suite/Util/SuiteObjectExtractor.php index 2e600806b..4f72042bf 100644 --- a/src/Magento/FunctionalTestingFramework/Suite/Util/SuiteObjectExtractor.php +++ b/src/Magento/FunctionalTestingFramework/Suite/Util/SuiteObjectExtractor.php @@ -89,11 +89,6 @@ public function parseSuiteDataIntoObjects($parsedSuiteData) $includeTests = $this->extractTestObjectsFromSuiteRef($groupTestsToInclude); $excludeTests = $this->extractTestObjectsFromSuiteRef($groupTestsToExclude); - // add all test if include tests is completely empty - if (empty($includeTests)) { - $includeTests = TestObjectHandler::getInstance()->getAllObjects(); - } - // parse any object hooks if (array_key_exists(TestObjectExtractor::TEST_BEFORE_HOOK, $parsedSuite)) { $suiteHooks[TestObjectExtractor::TEST_BEFORE_HOOK] = $testHookObjectExtractor->extractHook( @@ -109,12 +104,31 @@ public function parseSuiteDataIntoObjects($parsedSuiteData) $parsedSuite[TestObjectExtractor::TEST_AFTER_HOOK] ); } + if (count($suiteHooks) == 1) { throw new XmlException(sprintf( "Suites that contain hooks must contain both a 'before' and an 'after' hook. Suite: \"%s\"", $parsedSuite[self::NAME] )); } + // check if suite hooks are empty/not included and there are no included tests/groups/modules + $noHooks = count($suiteHooks) == 0 || + ( + empty($suiteHooks['before']->getActions()) && + empty($suiteHooks['after']->getActions()) + ); + // if suite body is empty throw error + if ($noHooks && empty($includeTests) && empty($excludeTests)) { + throw new XmlException(sprintf( + "Suites must not be empty. Suite: \"%s\"", + $parsedSuite[self::NAME] + )); + } + + // add all test if include tests is completely empty + if (empty($includeTests)) { + $includeTests = TestObjectHandler::getInstance()->getAllObjects(); + } // create the new suite object $suiteObjects[$parsedSuite[self::NAME]] = new SuiteObject(