19 use Symfony\Component\Yaml\Yaml;
29 "# Copyright © Magento, Inc. All rights reserved.\n# See COPYING.txt for license details.\n";
36 private static $SUITE_GENERATOR_INSTANCE;
43 private $groupClassGenerator;
48 private function __construct()
60 if (!self::$SUITE_GENERATOR_INSTANCE) {
62 self::clearPreviousGroupPreconditions();
63 self::clearPreviousSessionConfigEntries();
67 return self::$SUITE_GENERATOR_INSTANCE;
80 $suites = $testManifest->getSuiteConfig();
82 foreach ($suites as $suiteName => $suiteContent) {
83 $firstElement = array_values($suiteContent)[0];
86 if (is_string($firstElement)) {
87 $this->generateSuiteFromTest($suiteName, $suiteContent);
91 if (is_array($firstElement)) {
92 $this->generateSplitSuiteFromTest($suiteName, $suiteContent);
105 public function generateSuite($suiteName)
108 $this->generateSuiteFromTest($suiteName, []);
123 private function generateSuiteFromTest($suiteName, $tests = [], $originalSuiteName =
null)
126 $fullPath = TESTS_MODULE_PATH . DIRECTORY_SEPARATOR .
$relativePath . DIRECTORY_SEPARATOR;
131 if (!empty($tests)) {
132 $this->validateTestsReferencedInSuite($suiteName, $tests, $originalSuiteName);
133 foreach ($tests as $testName) {
140 $this->generateRelevantGroupTests($suiteName, $relevantTests);
141 $groupNamespace = $this->generateGroupFile($suiteName, $relevantTests, $originalSuiteName);
143 $this->appendEntriesToConfig($suiteName, $fullPath, $groupNamespace);
161 private function validateTestsReferencedInSuite($suiteName, $testsReferenced, $originalSuiteName)
163 $suiteRef = $originalSuiteName ?? $suiteName;
165 $errorMsg =
"Cannot reference tests whcih are not declared as part of suite.";
167 $invalidTestRef = array_diff($testsReferenced, array_keys($possibleTestRef));
169 if (!empty($invalidTestRef)) {
170 throw new TestReferenceException($errorMsg, [
'suite' => $suiteRef,
'test' => $invalidTestRef]);
183 private function generateSplitSuiteFromTest($suiteName, $suiteContent)
185 foreach ($suiteContent as $suiteSplitName => $tests) {
186 $this->generateSuiteFromTest($suiteSplitName, $tests, $suiteName);
201 private function generateGroupFile($suiteName, $tests, $originalSuiteName)
204 if ($originalSuiteName) {
208 $suiteObject =
new SuiteObject(
212 $originalSuite->getHooks()
217 if (count($suiteObject->getTests()) != count($tests)) {
218 return $this->generateGroupFile($suiteName, $tests, $suiteName);
222 if (!$suiteObject->requiresGroupFile()) {
228 return $this->groupClassGenerator->generateGroupClass($suiteObject);
241 private function appendEntriesToConfig($suiteName, $suitePath, $groupNamespace)
243 $relativeSuitePath = substr($suitePath, strlen(TESTS_BP));
244 $relativeSuitePath = ltrim($relativeSuitePath, DIRECTORY_SEPARATOR);
246 $ymlArray = self::getYamlFileContents();
247 if (!array_key_exists(self::YAML_GROUPS_TAG, $ymlArray)) {
251 if ($groupNamespace) {
256 $ymlText = self::YAML_COPYRIGHT_TEXT . Yaml::dump($ymlArray, 10);
257 file_put_contents(self::getYamlConfigFilePath() . self::YAML_CODECEPTION_CONFIG_FILENAME, $ymlText);
266 private static function clearPreviousSessionConfigEntries()
268 $ymlArray = self::getYamlFileContents();
269 $newYmlArray = $ymlArray;
271 if (array_key_exists(self::YAML_EXTENSIONS_TAG, $ymlArray)) {
272 foreach ($ymlArray[self::YAML_EXTENSIONS_TAG][self::YAML_ENABLED_TAG] as $key => $entry) {
273 if (preg_match(
'/(Group\\\\.*)/', $entry)) {
274 unset($newYmlArray[self::YAML_EXTENSIONS_TAG][self::YAML_ENABLED_TAG][$key]);
280 array_values($newYmlArray[self::YAML_EXTENSIONS_TAG][self::YAML_ENABLED_TAG]);
283 if (array_key_exists(self::YAML_GROUPS_TAG, $newYmlArray)) {
284 unset($newYmlArray[self::YAML_GROUPS_TAG]);
287 $ymlText = self::YAML_COPYRIGHT_TEXT . Yaml::dump($newYmlArray, 10);
288 file_put_contents(self::getYamlConfigFilePath() . self::YAML_CODECEPTION_CONFIG_FILENAME, $ymlText);
301 private function generateRelevantGroupTests(
$path, $tests)
304 $testGenerator->createAllTestFiles(
null, []);
312 private static function clearPreviousGroupPreconditions()
315 array_map(
'unlink', glob(
"$groupFilePath*.php"));
323 private static function getYamlFileContents()
329 if (file_exists($configYmlFile)) {
335 return Yaml::parse($ymlContents) ?? [];
343 private static function getYamlConfigFilePath()
345 return TESTS_BP . DIRECTORY_SEPARATOR;
const YAML_CODECEPTION_DIST_FILENAME
static getInstance($dir=null, $tests=[], $debug=false)
const YAML_COPYRIGHT_TEXT
static createGroupDir($fullPath)
const YAML_CODECEPTION_CONFIG_FILENAME
const YAML_EXTENSIONS_TAG
generateAllSuites($testManifest)