Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GroupClassGenerator.php
Go to the documentation of this file.
1 <?php
8 
15 use Mustache_Engine;
16 use Mustache_Loader_FilesystemLoader;
17 
19 {
20  const MUSTACHE_TEMPLATE_NAME = 'SuiteClass';
21  const SUITE_NAME_TAG = 'suiteName';
22  const TEST_COUNT_TAG = 'testCount';
23  const BEFORE_MUSTACHE_KEY = 'before';
24  const AFTER_MUSTACHE_KEY = 'after';
25  const ENTITY_NAME_TAG = 'entityName';
26  const ENTITY_MERGE_KEY = 'stepKey';
27  const REQUIRED_ENTITY_KEY = 'requiredEntities';
28  const LAST_REQUIRED_ENTITY_TAG = 'last';
29  const MUSTACHE_VAR_TAG = 'var';
30  const MAGENTO_CLI_COMMAND_COMMAND = 'command';
32  'comment' => 'print'
33  ];
34  const GROUP_DIR_NAME = 'Group';
35 
41  private $mustacheEngine;
42 
48  public static function getGroupDirPath()
49  {
50  return dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . self::GROUP_DIR_NAME . DIRECTORY_SEPARATOR;
51  }
52 
56  public function __construct()
57  {
58  $this->mustacheEngine = new Mustache_Engine([
59  'loader' => new Mustache_Loader_FilesystemLoader(dirname(__DIR__) . DIRECTORY_SEPARATOR . "views"),
60  'partials_loader' => new Mustache_Loader_FilesystemLoader(
61  dirname(__DIR__) . DIRECTORY_SEPARATOR . "views" . DIRECTORY_SEPARATOR . "partials"
62  )
63  ]);
64  }
65 
74  public function generateGroupClass($suiteObject)
75  {
76  $classContent = $this->createClassContent($suiteObject);
77  $configEntry = self::GROUP_DIR_NAME . DIRECTORY_SEPARATOR . $suiteObject->getName();
78  $filePath = self::getGroupDirPath() . $suiteObject->getName() . '.php';
79  file_put_contents($filePath, $classContent);
80 
81  return str_replace(DIRECTORY_SEPARATOR, "\\", $configEntry);
82  }
83 
91  private function createClassContent($suiteObject)
92  {
93  $mustacheData = [];
94  $mustacheData[self::SUITE_NAME_TAG] = $suiteObject->getName();
95  $mustacheData[self::TEST_COUNT_TAG] = count($suiteObject->getTests());
96 
97  $mustacheData[self::BEFORE_MUSTACHE_KEY] = $this->buildHookMustacheArray($suiteObject->getBeforeHook());
98  $mustacheData[self::AFTER_MUSTACHE_KEY] = $this->buildHookMustacheArray($suiteObject->getAfterHook());
99  $mustacheData[self::MUSTACHE_VAR_TAG] = $this->extractClassVar(
100  $mustacheData[self::BEFORE_MUSTACHE_KEY],
101  $mustacheData[self::AFTER_MUSTACHE_KEY]
102  );
103 
104  return $this->mustacheEngine->render(self::MUSTACHE_TEMPLATE_NAME, $mustacheData);
105  }
106 
115  private function extractClassVar($beforeArray, $afterArray)
116  {
117  $beforeVar = $beforeArray[self::MUSTACHE_VAR_TAG] ?? [];
118  $afterVar = $afterArray[self::MUSTACHE_VAR_TAG] ?? [];
119 
120  return array_merge($beforeVar, $afterVar);
121  }
122 
130  private function buildHookMustacheArray($hookObj)
131  {
132  $actions = [];
133  $mustacheHookArray['actions'][] = ['webDriverInit' => true];
134 
135  foreach ($hookObj->getActions() as $action) {
137  $index = count($actions);
138  //deleteData contains either url or createDataKey, if it contains the former it needs special formatting
139  if ($action->getType() !== "createData"
140  && !array_key_exists(TestGenerator::REQUIRED_ENTITY_REFERENCE, $action->getCustomActionAttributes())) {
141  $actions = $this->buildWebDriverActionsMustacheArray($action, $actions, $index);
142  continue;
143  }
144 
145  // add these as vars to be created a class level in the template
146  if ($action->getType() == 'createData') {
147  $mustacheHookArray[self::MUSTACHE_VAR_TAG][] = [self::ENTITY_MERGE_KEY => $action->getStepKey()];
148  }
149 
150  $entityArray = [];
151  $entityArray[self::ENTITY_MERGE_KEY] = $action->getStepKey();
152  $entityArray[$action->getType()] = $action->getStepKey();
153 
154  $entityArray = $this->buildPersistenceMustacheArray($action, $entityArray);
155  $actions[$index] = $entityArray;
156  }
157  $mustacheHookArray['actions'] = array_merge($mustacheHookArray['actions'], $actions);
158  $mustacheHookArray['actions'][] = ['webDriverReset' => true];
159 
160  return $mustacheHookArray;
161  }
162 
172  private function buildWebDriverActionsMustacheArray($action, $actionEntries)
173  {
174  $step = TestGenerator::getInstance()->generateStepsPhp([$action], TestGenerator::SUITE_SCOPE, 'webDriver');
175  $rawPhp = str_replace(["\t", "\n"], "", $step);
176  $multipleCommands = explode(";", $rawPhp, -1);
177  foreach ($multipleCommands as $command) {
178  $actionEntries = $this->replaceReservedTesterFunctions($command . ";", $actionEntries, 'webDriver');
179  }
180 
181  return $actionEntries;
182  }
183 
193  private function replaceReservedTesterFunctions($formattedStep, $actionEntries, $actor)
194  {
195  foreach (self::REPLACEMENT_ACTIONS as $testAction => $replacement) {
196  $testActionCall = "\${$actor}->{$testAction}";
197  if (substr($formattedStep, 0, strlen($testActionCall)) == $testActionCall) {
198  $resultingStep = str_replace($testActionCall, $replacement, $formattedStep);
199  $actionEntries[] = ['action' => $resultingStep];
200  } else {
201  $actionEntries[] = ['action' => $formattedStep];
202  }
203  }
204 
205  return $actionEntries;
206  }
207 
215  private function buildPersistenceMustacheArray($action, $entityArray)
216  {
217  $entityArray[self::ENTITY_NAME_TAG] =
218  $action->getCustomActionAttributes()['entity'] ??
219  $action->getCustomActionAttributes()[TestGenerator::REQUIRED_ENTITY_REFERENCE];
220 
221  // append entries for any required entities to this entry
222  if (array_key_exists('requiredEntities', $action->getCustomActionAttributes())) {
223  $entityArray[self::REQUIRED_ENTITY_KEY] =
224  $this->buildReqEntitiesMustacheArray($action->getCustomActionAttributes());
225  }
226 
227  // append entries for customFields if specified by the user.
228  if (array_key_exists('customFields', $action->getCustomActionAttributes())) {
229  $entityArray['customFields'] = $action->getStepKey() . 'Fields';
230  }
231 
232  return $entityArray;
233  }
234 
244  private function buildReqEntitiesMustacheArray($customAttributes)
245  {
246  $requiredEntities = [];
247  foreach ($customAttributes as $attribute) {
248  if (!is_array($attribute)) {
249  continue;
250  }
251 
252  if ($attribute[ActionObjectExtractor::NODE_NAME] == 'requiredEntity') {
253  $requiredEntities[] = [self::ENTITY_NAME_TAG => $attribute[TestGenerator::REQUIRED_ENTITY_REFERENCE]];
254  }
255  }
256 
257  //append "last" attribute to final entry for mustache template (omit trailing comma)
258  $requiredEntities[count($requiredEntities)-1][self::LAST_REQUIRED_ENTITY_TAG] = true;
259 
260  return $requiredEntities;
261  }
262 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$replacement
Definition: website.php:23
static getInstance($dir=null, $tests=[], $debug=false)
$index
Definition: list.phtml:44