-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathTestContextExtension.php
71 lines (65 loc) · 2.06 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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Extension;
use \Codeception\Events;
/**
* 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',
];
/**
* 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) {
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;
}
}