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
<?php
namespace Codeception\Subscriber;
use Codeception\Event\SuiteEvent;
use Codeception\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class BeforeAfterTest implements EventSubscriberInterface
{
use Shared\StaticEvents;
public static $events = [
Events::SUITE_BEFORE => 'beforeClass',
Events::SUITE_AFTER => ['afterClass', 100]
];
protected $hooks = [];
protected $startedTests = [];
protected $unsuccessfulTests = [];
public function beforeClass(SuiteEvent $e)
{
foreach ($e->getSuite()->tests() as $test) {
/** @var $test \PHPUnit\Framework\Test * */
if ($test instanceof \PHPUnit\Framework\TestSuite\DataProvider) {
$potentialTestClass = strstr($test->getName(), '::', true);
$this->hooks[$potentialTestClass] = \PHPUnit\Util\Test::getHookMethods($potentialTestClass);
}
$testClass = get_class($test);
$this->hooks[$testClass] = \PHPUnit\Util\Test::getHookMethods($testClass);
}
$this->runHooks('beforeClass');
}
public function afterClass(SuiteEvent $e)
{
$this->runHooks('afterClass');
}
protected function runHooks($hookName)
{
foreach ($this->hooks as $className => $hook) {
foreach ($hook[$hookName] as $method) {
if (is_callable([$className, $method])) {
call_user_func([$className, $method]);
}
}
}
}
}