Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EntityDataObject.php
Go to the documentation of this file.
1 <?php
8 
12 
17 {
18  const NO_UNIQUE_PROCESS = 0;
19  const SUITE_UNIQUE_VALUE = 1;
20  const CEST_UNIQUE_VALUE = 2;
23  const SUITE_UNIQUE_FUNCTION = 'msqs';
24  const CEST_UNIQUE_FUNCTION = 'msq';
25 
31  private $name;
32 
38  private $type;
39 
45  private $linkedEntities = [];
46 
52  private $vars;
53 
59  private $data = [];
60 
66  private $uniquenessData = [];
67 
73  private $parentEntity;
74 
86  public function __construct($name, $type, $data, $linkedEntities, $uniquenessData, $vars = [], $parentEntity = null)
87  {
88  $this->name = $name;
89  $this->type = $type;
90  $this->data = $data;
91  $this->linkedEntities = $linkedEntities;
92  if ($uniquenessData) {
93  $this->uniquenessData = $uniquenessData;
94  }
95 
96  $this->vars = $vars;
97  $this->parentEntity = $parentEntity;
98  }
99 
105  public function getName()
106  {
107  return $this->name;
108  }
109 
115  public function getType()
116  {
117  return $this->type;
118  }
119 
125  public function getAllData()
126  {
127  return $this->data;
128  }
129 
138  public function getDataByName($name, $uniquenessFormat)
139  {
140  if (MftfApplicationConfig::getConfig()->verboseEnabled()) {
141  LoggingUtil::getInstance()->getLogger(EntityDataObject::class)
142  ->debug("Fetching data field from entity", ["entity" => $this->getName(), "field" => $name]);
143  }
144 
145  if (!$this->isValidUniqueDataFormat($uniquenessFormat)) {
146  $exceptionMessage = sprintf("Invalid unique data format value: %s \n", $uniquenessFormat);
147  LoggingUtil::getInstance()->getLogger(EntityDataObject::class)
148  ->error($exceptionMessage, ["entity" => $this->getName(), "field" => $name]);
149  throw new TestFrameworkException($exceptionMessage);
150  }
151 
152  $name_lower = strtolower($name);
153 
154  if ($this->data !== null && array_key_exists($name_lower, $this->data)) {
155  $uniquenessData = $this->getUniquenessDataByName($name_lower);
156  if (null === $uniquenessData || $uniquenessFormat == self::NO_UNIQUE_PROCESS) {
157  return $this->data[$name_lower];
158  }
159  return $this->formatUniqueData($name_lower, $uniquenessData, $uniquenessFormat);
160  }
161 
162  return null;
163  }
164 
170  public function getParentName()
171  {
172  return $this->parentEntity;
173  }
174 
184  private function formatUniqueData($name, $uniqueData, $uniqueDataFormat)
185  {
186  switch ($uniqueDataFormat) {
188  $this->checkUniquenessFunctionExists(self::SUITE_UNIQUE_FUNCTION, $uniqueDataFormat);
189  if ($uniqueData == 'prefix') {
190  return msqs($this->getName()) . $this->data[$name];
191  } else { // $uniData == 'suffix'
192  return $this->data[$name] . msqs($this->getName());
193  }
194  break;
196  $this->checkUniquenessFunctionExists(self::CEST_UNIQUE_FUNCTION, $uniqueDataFormat);
197  if ($uniqueData == 'prefix') {
198  return msq($this->getName()) . $this->data[$name];
199  } else { // $uniqueData == 'suffix'
200  return $this->data[$name] . msq($this->getName());
201  }
202  break;
204  if ($uniqueData == 'prefix') {
205  return self::SUITE_UNIQUE_FUNCTION . '("' . $this->getName() . '")' . $this->data[$name];
206  } else { // $uniqueData == 'suffix'
207  return $this->data[$name] . self::SUITE_UNIQUE_FUNCTION . '("' . $this->getName() . '")';
208  }
209  break;
211  if ($uniqueData == 'prefix') {
212  return self::CEST_UNIQUE_FUNCTION . '("' . $this->getName() . '")' . $this->data[$name];
213  } else { // $uniqueData == 'suffix'
214  return $this->data[$name] . self::CEST_UNIQUE_FUNCTION . '("' . $this->getName() . '")';
215  }
216  break;
217  default:
218  break;
219  }
220  return null;
221  }
222 
231  private function checkUniquenessFunctionExists($function, $uniqueDataFormat)
232  {
233  if (!function_exists($function)) {
234  $exceptionMessage = sprintf(
235  'Unique data format value: %s can only be used when running cests.\n',
236  $uniqueDataFormat
237  );
238 
239  throw new TestFrameworkException($exceptionMessage);
240  }
241  }
242 
250  public function getVarReference($key)
251  {
252  if (array_key_exists($key, $this->vars)) {
253  return $this->vars[$key];
254  }
255 
256  return null;
257  }
258 
266  public function getLinkedEntitiesOfType($type)
267  {
268  $groupedArray = [];
269 
270  foreach ($this->linkedEntities as $entityName => $entityType) {
271  if ($entityType == $type) {
272  $groupedArray[] = $entityName;
273  }
274  }
275 
276  return $groupedArray;
277  }
278 
284  public function getLinkedEntities()
285  {
286  return $this->linkedEntities;
287  }
288 
294  public function getVarReferences()
295  {
296  return $this->vars;
297  }
298 
305  public function getUniquenessDataByName($dataName)
306  {
307  $name = strtolower($dataName);
308 
309  if (array_key_exists($name, $this->uniquenessData)) {
310  return $this->uniquenessData[$name];
311  }
312 
313  return null;
314  }
315 
321  public function getUniquenessData()
322  {
323  return $this->uniquenessData;
324  }
325 
332  private function isValidUniqueDataFormat($uniDataFormat)
333  {
334  return in_array(
335  $uniDataFormat,
336  [
337  self::NO_UNIQUE_PROCESS,
338  self::SUITE_UNIQUE_VALUE,
339  self::CEST_UNIQUE_VALUE,
340  self::SUITE_UNIQUE_NOTATION,
341  self::CEST_UNIQUE_NOTATION
342  ],
343  true
344  );
345  }
346 }
__construct($name, $type, $data, $linkedEntities, $uniquenessData, $vars=[], $parentEntity=null)