Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TestObjectExtractor.php
Go to the documentation of this file.
1 <?php
8 
15 
20 {
21  const TEST_ANNOTATIONS = 'annotations';
22  const TEST_BEFORE_HOOK = 'before';
23  const TEST_AFTER_HOOK = 'after';
24  const TEST_FAILED_HOOK = 'failed';
25  const TEST_BEFORE_ATTRIBUTE = 'before';
26  const TEST_AFTER_ATTRIBUTE = 'after';
27  const TEST_INSERT_BEFORE = 'insertBefore';
28  const TEST_INSERT_AFTER = 'insertAfter';
29  const TEST_FILENAME = 'filename';
30 
36  private $actionObjectExtractor;
37 
43  private $annotationExtractor;
44 
50  private $testHookObjectExtractor;
51 
57  private $modulePathExtractor;
58 
62  public function __construct()
63  {
64  $this->actionObjectExtractor = new ActionObjectExtractor();
65  $this->annotationExtractor = new AnnotationExtractor();
66  $this->testHookObjectExtractor = new TestHookObjectExtractor();
67  $this->modulePathExtractor = new ModulePathExtractor();
68  }
69 
74  public function getAnnotationExtractor()
75  {
76  return $this->annotationExtractor;
77  }
78 
87  public function extractTestData($testData)
88  {
89  // validate the test name for blacklisted char (will cause allure report issues) MQE-483
90  NameValidationUtil::validateName($testData[self::NAME], "Test");
91 
92  $testAnnotations = [];
93  $testHooks = [];
94  $filename = $testData['filename'] ?? null;
95  $fileNames = explode(",", $filename);
96  $baseFileName = $fileNames[0];
97  $module = $this->modulePathExtractor->extractModuleName($baseFileName);
98  $testReference = $testData['extends'] ?? null;
99  $testActions = $this->stripDescriptorTags(
100  $testData,
101  self::NODE_NAME,
102  self::NAME,
103  self::TEST_ANNOTATIONS,
104  self::TEST_BEFORE_HOOK,
105  self::TEST_AFTER_HOOK,
106  self::TEST_FAILED_HOOK,
107  self::TEST_INSERT_BEFORE,
108  self::TEST_INSERT_AFTER,
109  self::TEST_FILENAME,
110  'extends'
111  );
112 
113  $testAnnotations = $this->annotationExtractor->extractAnnotations(
114  $testData[self::TEST_ANNOTATIONS] ?? [],
115  $testData[self::NAME]
116  );
117 
118  //Override features with module name if present, populates it otherwise
119  $testAnnotations["features"] = [$module];
120 
121  // extract before
122  if (array_key_exists(self::TEST_BEFORE_HOOK, $testData) && is_array($testData[self::TEST_BEFORE_HOOK])) {
123  $testHooks[self::TEST_BEFORE_HOOK] = $this->testHookObjectExtractor->extractHook(
124  $testData[self::NAME],
125  'before',
126  $testData[self::TEST_BEFORE_HOOK]
127  );
128  }
129 
130  if (array_key_exists(self::TEST_AFTER_HOOK, $testData) && is_array($testData[self::TEST_AFTER_HOOK])) {
131  // extract after
132  $testHooks[self::TEST_AFTER_HOOK] = $this->testHookObjectExtractor->extractHook(
133  $testData[self::NAME],
134  'after',
135  $testData[self::TEST_AFTER_HOOK]
136  );
137 
138  // extract failed
139  $testHooks[self::TEST_FAILED_HOOK] = $this->testHookObjectExtractor->createDefaultFailedHook(
140  $testData[self::NAME]
141  );
142  }
143 
144  // TODO extract filename info and store
145  try {
146  return new TestObject(
147  $testData[self::NAME],
148  $this->actionObjectExtractor->extractActions($testActions, $testData[self::NAME]),
149  $testAnnotations,
150  $testHooks,
151  $filename,
152  $testReference
153  );
154  } catch (XmlException $exception) {
155  throw new XmlException($exception->getMessage() . ' in Test ' . $filename);
156  }
157  }
158 }