3 use PHPUnit\Framework\SkippedTestError
;
4 use PHPUnit\Framework\SkippedTestSuiteError
;
5 use PHPUnit\Framework\SyntheticError
;
6 use PHPUnit\Framework\TestResult
;
9 * Trait that returns to PHPUnit test suites the support for setUp/tearDown events that
10 * was removed in PHPUnit 8.
14 trait SuiteEventsTrait
{
18 public function run( ?TestResult
$result = null ): TestResult
{
19 // setUp / tearDown handling based on code in TestSuite::run()
20 // (except in the parent only beforeClass / afterClass are run)
21 $result ??
= $this->createResult();
23 // Don't run events if there are no tests (T292239)
24 if ( count( $this ) === 0 ) {
29 if ( is_callable( [ $this, 'setUp' ] ) ) {
33 } catch ( SkippedTestSuiteError
$error ) {
34 $result->startTestSuite( $this );
35 foreach ( $this->tests() as $test ) {
36 $result->startTest( $test );
37 $result->addFailure( $test, $error, 0 );
38 $result->endTest( $test, 0 );
40 $result->endTestSuite( $this );
42 } catch ( \Throwable
$t ) {
44 $result->startTestSuite( $this );
45 foreach ( $this->tests() as $test ) {
46 if ( $result->shouldStop() ) {
49 $result->startTest( $test );
51 $result->addError( $test, $t, 0 );
56 new SkippedTestError( 'Test skipped because of an error in setUp method' ),
60 $result->endTest( $test, 0 );
62 $result->endTestSuite( $this );
67 $result = parent
::run( $result );
69 if ( is_callable( [ $this, 'tearDown' ] ) ) {
73 } catch ( \Throwable
$t ) {
74 $message = "Exception in tearDown" . \PHP_EOL
. $t->getMessage();
75 $error = new SyntheticError( $message, 0, $t->getFile(), $t->getLine(), $t->getTrace() );
76 $placeholderTest = clone $this->testAt( 0 );
77 $placeholderTest->setName( 'tearDown' );
78 // Unlike in parent implementation, $result->endTestSuite() has
79 // already been invoked by parent::run, so we need to reopen
81 $result->startTestSuite( $this );
82 $result->startTest( $placeholderTest );
83 $result->addFailure( $placeholderTest, $error, 0 );
84 $result->endTest( $placeholderTest, 0 );
85 $result->endTestSuite( $this );
89 throw new LogicException(
91 . " uses neither setUp() nor tearDown(), so it doesn't need SuiteEventsTrait"