Skip to content

Commit 96c1264

Browse files
epdenoudensebastianbergmann
authored andcommitted
Add nicer user error reporting for tearDownAfterClass(), plus new test
1 parent ef6d03e commit 96c1264

File tree

4 files changed

+84
-4
lines changed

4 files changed

+84
-4
lines changed

src/Framework/TestSuite.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -703,10 +703,10 @@ public function run(TestResult $result = null): TestResult
703703
\call_user_func([$this->name, $beforeClassMethod]);
704704
}
705705
}
706-
} catch (SkippedTestSuiteError $e) {
706+
} catch (SkippedTestSuiteError $error) {
707707
foreach ($this->tests() as $test) {
708708
$result->startTest($test);
709-
$result->addFailure($test, $e, 0);
709+
$result->addFailure($test, $error, 0);
710710
$result->endTest($test, 0);
711711
}
712712

@@ -756,7 +756,12 @@ public function run(TestResult $result = null): TestResult
756756
}
757757
}
758758
} catch (Throwable $t) {
759-
\trigger_error($t->__toString(), \E_USER_WARNING);
759+
$error = new SyntheticError($t->getMessage(), 0, $t->getFile(), $t->getLine(), $t->getTrace());
760+
$test = new \Failure('tearDownAfterClass');
761+
762+
$result->startTest($test);
763+
$result->addFailure($test, $error, 0);
764+
$result->endTest($test, 0);
760765
}
761766

762767
$this->tearDown();
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
use PHPUnit\Framework\TestCase;
11+
12+
class ExceptionInTearDownAfterClassTest extends TestCase
13+
{
14+
public $setUp = false;
15+
16+
public $assertPreConditions = false;
17+
18+
public $assertPostConditions = false;
19+
20+
public $tearDown = false;
21+
22+
public $tearDownAfterClass = false;
23+
24+
public $testSomething = false;
25+
26+
public static function tearDownAfterClass(): void
27+
{
28+
throw new Exception('throw Exception in tearDownAfterClass()');
29+
}
30+
31+
protected function setUp(): void
32+
{
33+
$this->setUp = true;
34+
}
35+
36+
protected function tearDown(): void
37+
{
38+
$this->tearDown = true;
39+
}
40+
41+
public function testOne(): void
42+
{
43+
$this->testSomething = true;
44+
$this->assertTrue(true);
45+
}
46+
47+
public function testTwo(): void
48+
{
49+
$this->testSomething = $this->testSomething && true;
50+
$this->assertTrue(true);
51+
}
52+
53+
protected function assertPreConditions(): void
54+
{
55+
$this->assertPreConditions = true;
56+
}
57+
58+
protected function assertPostConditions(): void
59+
{
60+
$this->assertPostConditions = true;
61+
}
62+
}

tests/_files/ExceptionInTearDownTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected function tearDown(): void
3030
{
3131
$this->tearDown = true;
3232

33-
throw new Exception("throw Exception in tearDown()");
33+
throw new Exception('throw Exception in tearDown()');
3434
}
3535

3636
public function testSomething(): void

tests/unit/Framework/TestSuiteTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,17 @@ public function testCreateTestForConstructorlessTestClass(): void
227227
->willReturn(__CLASS__);
228228
TestSuite::createTest($reflection, 'TestForConstructorlessTestClass');
229229
}
230+
231+
public function testTearDownAfterClassInTestSuite(): void
232+
{
233+
$suite = new TestSuite(\ExceptionInTearDownAfterClassTest::class);
234+
$suite->run($this->result);
235+
236+
$this->assertSame(3, $this->result->count());
237+
$this->assertCount(1, $this->result->failures());
238+
239+
/** @var TestFailure $failure */
240+
$failure = $this->result->failures()[0];
241+
$this->assertSame('throw Exception in tearDownAfterClass()', $failure->thrownException()->getMessage());
242+
}
230243
}

0 commit comments

Comments
 (0)