File size: 1,323 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

declare(strict_types=1);

namespace Mautic\CoreBundle\Test\Hooks;

use PHPUnit\Framework\TestFailure;
use PHPUnit\Framework\TestSuite;
use PHPUnit\Runner\AfterTestHook;

/**
 * This extension allows you to run an arbitrary test after every test in the current suite which is
 * extremely helpful when hunting down a functional test that causes your test fail.
 * When your test fails, the execution is stopped immediately and the name of problematic test is written to STDOUT.
 *
 * Example of usage: `MAUTIC_TEST_EXECUTE_TEST_AFTER="Fully\\Qualified\\Class\\NameTest" bin/phpunit`.
 */
class ExecuteTestAfterExtension implements AfterTestHook
{
    public function executeAfterTest(string $test, float $time): void
    {
        $testClass = getenv('MAUTIC_TEST_EXECUTE_TEST_AFTER');

        if (false === $testClass) {
            return;
        }

        $testSuite = new TestSuite();
        $testSuite->addTestSuite($testClass);
        $result = $testSuite->run();

        if (!$result->wasSuccessful()) {
            $failures = array_map(fn (TestFailure $testFailure) => $testFailure->getExceptionAsString(), array_merge($result->failures(), $result->errors()));

            exit(sprintf('The previous test was: "%s". Your test errored with: %s', $test, implode(PHP_EOL, $failures)));
        }
    }
}