Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractMetricCheck.php
Go to the documentation of this file.
1 <?php
8 
9 use Codeception\Exception\ModuleRequireException;
10 use Codeception\Module\WebDriver;
11 use Codeception\Step;
12 use Facebook\WebDriver\Exception\UnexpectedAlertOpenException;
16 use Monolog\Logger;
17 
21 abstract class AbstractMetricCheck
22 {
28  protected $extension;
29 
35  protected $currentValue;
36 
43  protected $storedValue;
44 
50  protected $failCount;
51 
58 
62  protected $logger;
63 
67  protected $verbose;
68 
77  {
78  $this->extension = $extension;
79  $this->logger = LoggingUtil::getInstance()->getLogger(get_class($this));
80  $this->verbose = MftfApplicationConfig::getConfig()->verboseEnabled();
81 
82  // If the clearFailureOnPage() method is overridden, use the configured failure threshold
83  // If not, the default clearFailureOnPage() method does nothing so don't worry about resetting failures
84  $reflector = new \ReflectionMethod($this, 'clearFailureOnPage');
85  if ($reflector->getDeclaringClass()->getName() === get_class($this)) {
86  $this->resetFailureThreshold = $resetFailureThreshold;
87  } else {
88  $this->resetFailureThreshold = -1;
89  }
90 
91  $this->resetTracker();
92  }
93 
100  abstract protected function doesMetricPass($value);
101 
108  abstract protected function fetchValueFromPage();
109 
116  protected function clearFailureOnPage()
117  {
118  return;
119  }
120 
126  public function getName()
127  {
128  $clazz = get_class($this);
129  $namespaceBreak = strrpos($clazz, '\\');
130  if ($namespaceBreak !== false) {
131  $clazz = substr($clazz, $namespaceBreak + 1);
132  }
133  return $clazz;
134  }
135 
146  public function runCheck()
147  {
148  if ($this->doesMetricPass($this->getCurrentValue(true))) {
149  $this->setTracker($this->getCurrentValue(), 0);
150  return true;
151  }
152 
153  return false;
154  }
155 
165  public function finalizeForStep($step)
166  {
167  try {
168  $currentValue = $this->getCurrentValue();
169  } catch (UnexpectedAlertOpenException $exception) {
170  $this->debugLog(
171  'An alert is open, bypassing javascript-based metric check',
172  ['step' => $step->__toString()]
173  );
174  return;
175  }
176 
177  if ($this->doesMetricPass($currentValue)) {
178  $this->setTracker($currentValue, 0);
179  } else {
180  // If failure happened on the same value as before, increment the fail count, otherwise set at 1
181  if (!isset($this->storedValue) || $currentValue !== $this->getStoredValue()) {
182  $failCount = 1;
183  } else {
184  $failCount = $this->getFailureCount() + 1;
185  }
187 
188  $this->errorLog('Failed readiness check', ['step' => $step->__toString()]);
189 
190  if ($this->resetFailureThreshold >= 0 && $failCount >= $this->resetFailureThreshold) {
191  $this->debugLog(
192  'Too many failures, assuming metric is stuck and resetting state',
193  ['step' => $step->__toString()]
194  );
195  $this->resetMetric();
196  }
197  }
198  }
199 
206  protected function getDriver()
207  {
208  return $this->extension->getDriver();
209  }
210 
219  protected function executeJs($script, $arguments = [])
220  {
221  return $this->extension->getDriver()->executeJS($script, $arguments);
222  }
223 
232  private function getCurrentValue($refresh = false)
233  {
234  if ($refresh) {
235  unset($this->currentValue);
236  }
237  if (!isset($this->currentValue)) {
238  $this->currentValue = $this->fetchValueFromPage();
239  }
240  return $this->currentValue;
241  }
242 
248  public function getStoredValue()
249  {
250  return $this->storedValue ?? null;
251  }
252 
259  public function getFailureCount()
260  {
261  return $this->failCount;
262  }
263 
270  private function resetMetric()
271  {
272  $this->clearFailureOnPage();
273  $this->resetTracker();
274  }
275 
283  public function setTracker($value, $failCount)
284  {
285  unset($this->currentValue);
286  $this->storedValue = $value;
287  $this->failCount = $failCount;
288  }
289 
295  public function resetTracker()
296  {
297  unset($this->currentValue);
298  unset($this->storedValue);
299  $this->failCount = 0;
300  }
301 
310  protected function errorLog($message, $context = [])
311  {
312  $context = array_merge($this->getLogContext(), $context);
313  //TODO REMOVE THIS LINE, UNCOMMENT LOGGER
314  //$this->logger->error($message, $context);
315  }
316 
325  protected function infoLog($message, $context = [])
326  {
327  $context = array_merge($this->getLogContext(), $context);
328  //TODO REMOVE THIS LINE, UNCOMMENT LOGGER
329  //$this->logger->info($message, $context);
330  }
331 
340  protected function debugLog($message, $context = [])
341  {
342  if ($this->verbose) {
343  $context = array_merge($this->getLogContext(), $context);
344  //TODO REMOVE THIS LINE, UNCOMMENT LOGGER
345  //$this->logger->debug($message, $context);
346  }
347  }
348 
355  private function getLogContext()
356  {
357  return [
358  'test' => $this->extension->getTestName(),
359  'uri' => $this->extension->getUri(),
360  $this->getName() => $this->getStoredValue(),
361  $this->getName() . '.failCount' => $this->getFailureCount()
362  ];
363  }
364 }
$message
$value
Definition: gender.phtml:16
$arguments