Skip to content

Commit

Permalink
TestCase: tearDown() is called even on error in test method [Closes #254
Browse files Browse the repository at this point in the history
][Ref #255]
  • Loading branch information
milo committed Oct 12, 2015
1 parent 3bd4e8f commit d25b672
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,24 @@ public function runTest($method, array $args = NULL)
$data[] = $args;
}

$me = $this;
$errorHandler = function () use ($me, & $prev) {
restore_error_handler();
$rm = new \ReflectionMethod($me, 'tearDown');
$rm->setAccessible(TRUE);

set_error_handler(function () {}); // mute all errors
$rm->invoke($me);
restore_error_handler();

return $prev ? call_user_func_array($prev, func_get_args()) : FALSE;
};

foreach ($data as $params) {
try {
$this->setUp();

$prev = set_error_handler($errorHandler);
try {
if ($info['throws']) {
$tmp = $this;
Expand All @@ -123,6 +137,7 @@ public function runTest($method, array $args = NULL)
}
} catch (\Exception $testException) {
}
restore_error_handler();

try {
$this->tearDown();
Expand Down
41 changes: 41 additions & 0 deletions tests/Framework/TestCase.order.error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* @exitCode 255
* @outputMatch Test::setUp,Test::testMe,Test::tearDown,E_USER_ERROR: STOP%A%
*/

require __DIR__ . '/../bootstrap.php';

Tester\Environment::$useColors = FALSE;


class Test extends Tester\TestCase
{
protected function setUp()
{
echo __METHOD__ . ',';
}

/** @dataProvider data */
public function testMe($arg)
{
echo __METHOD__ . ',';
trigger_error('STOP', E_USER_ERROR);
}

protected function tearDown()
{
echo __METHOD__ . ',';
trigger_error('NOT SHOWN', E_USER_ERROR);
}

protected function data()
{
return array(array('arg'));
}
}


$test = new Test;
$test->run();

0 comments on commit d25b672

Please sign in to comment.