-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathTestContextExtension.php
129 lines (119 loc) · 4.1 KB
/
TestContextExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Extension;
use \Codeception\Events;
use Magento\FunctionalTestingFramework\Extension\ErrorLogger;
/**
* Class TestContextExtension
* @SuppressWarnings(PHPMD.UnusedPrivateField)
*/
class TestContextExtension extends \Codeception\Extension
{
const TEST_PHASE_AFTER = "_after";
/**
* Codeception Events Mapping to methods
* @var array
*/
public static $events = [
Events::TEST_FAIL => 'testFail',
Events::STEP_AFTER => 'afterStep',
Events::TEST_END => 'testError'
];
/**
* Codeception event listener function, triggered on test failure.
* @param \Codeception\Event\FailEvent $e
* @return void
*/
public function testFail(\Codeception\Event\FailEvent $e)
{
$cest = $e->getTest();
$context = $this->extractContext($e->getFail()->getTrace(), $cest->getTestMethod());
// Do not attempt to run _after if failure was in the _after block
// Try to run _after but catch exceptions to prevent them from overwriting original failure.
if ($context != TestContextExtension::TEST_PHASE_AFTER) {
$this->runAfterBlock($e, $cest);
}
}
/**
* Codeception event listener function, triggered on test error.
* @param \Codeception\Event\TestEvent $e
* @return void
*/
public function testError(\Codeception\Event\TestEvent $e)
{
$cest = $e->getTest();
//Access private TestResultObject to find stack and if there are any errors (as opposed to failures)
$testResultObject = call_user_func(\Closure::bind(
function () use ($cest) {
return $cest->getTestResultObject();
},
$cest
));
$errors = $testResultObject->errors();
if (!empty($errors)) {
$stack = $errors[0]->thrownException()->getTrace();
$context = $this->extractContext($stack, $cest->getTestMethod());
// Do not attempt to run _after if failure was in the _after block
// Try to run _after but catch exceptions to prevent them from overwriting original failure.
if ($context != TestContextExtension::TEST_PHASE_AFTER) {
$this->runAfterBlock($e, $cest);
}
}
}
/**
* Runs cest's after block, if necessary.
* @param Symfony\Component\EventDispatcher\Event $e
* @param \Codeception\TestInterface $cest
* @return void
*/
private function runAfterBlock($e, $cest)
{
try {
$actorClass = $e->getTest()->getMetadata()->getCurrent('actor');
$I = new $actorClass($cest->getScenario());
call_user_func(\Closure::bind(
function () use ($cest, $I) {
$cest->executeHook($I, 'after');
},
null,
$cest
));
} catch (\Exception $e) {
// Do not rethrow Exception
}
}
/**
* Extracts hook method from trace, looking specifically for the cest class given.
* @param array $trace
* @param string $class
* @return string
*/
public function extractContext($trace, $class)
{
foreach ($trace as $entry) {
$traceClass = $entry["class"] ?? null;
if (strpos($traceClass, $class) != 0) {
return $entry["function"];
}
}
return null;
}
/**
* Codeception event listener function, triggered after step.
* Calls ErrorLogger to log JS errors encountered.
* @param \Codeception\Event\StepEvent $e
* @return void
*/
public function afterStep(\Codeception\Event\StepEvent $e)
{
// @codingStandardsIgnoreStart
$webDriver = $this->getModule("\Magento\FunctionalTestingFramework\Module\MagentoWebDriver")->webDriver;
// @codingStandardsIgnoreEnd
if (!empty($webDriver)) {
ErrorLogger::getInstance()->logErrors($webDriver, $e);
}
}
}