Skip to content

Commit

Permalink
Add nicer user error reporting for tearDownAfterClass(), plus new test
Browse files Browse the repository at this point in the history
  • Loading branch information
epdenouden authored and sebastianbergmann committed Jan 21, 2019
1 parent ef6d03e commit 96c1264
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/Framework/TestSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -703,10 +703,10 @@ public function run(TestResult $result = null): TestResult
\call_user_func([$this->name, $beforeClassMethod]);
}
}
} catch (SkippedTestSuiteError $e) {
} catch (SkippedTestSuiteError $error) {
foreach ($this->tests() as $test) {
$result->startTest($test);
$result->addFailure($test, $e, 0);
$result->addFailure($test, $error, 0);
$result->endTest($test, 0);
}

Expand Down Expand Up @@ -756,7 +756,12 @@ public function run(TestResult $result = null): TestResult
}
}
} catch (Throwable $t) {
\trigger_error($t->__toString(), \E_USER_WARNING);
$error = new SyntheticError($t->getMessage(), 0, $t->getFile(), $t->getLine(), $t->getTrace());
$test = new \Failure('tearDownAfterClass');

$result->startTest($test);
$result->addFailure($test, $error, 0);
$result->endTest($test, 0);
}

$this->tearDown();
Expand Down
62 changes: 62 additions & 0 deletions tests/_files/ExceptionInTearDownAfterClassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\Framework\TestCase;

class ExceptionInTearDownAfterClassTest extends TestCase
{
public $setUp = false;

public $assertPreConditions = false;

public $assertPostConditions = false;

public $tearDown = false;

public $tearDownAfterClass = false;

public $testSomething = false;

public static function tearDownAfterClass(): void
{
throw new Exception('throw Exception in tearDownAfterClass()');
}

protected function setUp(): void
{
$this->setUp = true;
}

protected function tearDown(): void
{
$this->tearDown = true;
}

public function testOne(): void
{
$this->testSomething = true;
$this->assertTrue(true);
}

public function testTwo(): void
{
$this->testSomething = $this->testSomething && true;
$this->assertTrue(true);
}

protected function assertPreConditions(): void
{
$this->assertPreConditions = true;
}

protected function assertPostConditions(): void
{
$this->assertPostConditions = true;
}
}
2 changes: 1 addition & 1 deletion tests/_files/ExceptionInTearDownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function tearDown(): void
{
$this->tearDown = true;

throw new Exception("throw Exception in tearDown()");
throw new Exception('throw Exception in tearDown()');
}

public function testSomething(): void
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/Framework/TestSuiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,17 @@ public function testCreateTestForConstructorlessTestClass(): void
->willReturn(__CLASS__);
TestSuite::createTest($reflection, 'TestForConstructorlessTestClass');
}

public function testTearDownAfterClassInTestSuite(): void
{
$suite = new TestSuite(\ExceptionInTearDownAfterClassTest::class);
$suite->run($this->result);

$this->assertSame(3, $this->result->count());
$this->assertCount(1, $this->result->failures());

/** @var TestFailure $failure */
$failure = $this->result->failures()[0];
$this->assertSame('throw Exception in tearDownAfterClass()', $failure->thrownException()->getMessage());
}
}

0 comments on commit 96c1264

Please sign in to comment.