Fixing issue with fixtures not being unloaded if the test method throws exceptions or fails in some unexpected way

This commit is contained in:
José Lorenzo Rodríguez 2010-05-31 23:07:21 -04:30
parent ae4f3b24d8
commit 82da9be2ce
3 changed files with 62 additions and 27 deletions

View file

@ -147,6 +147,39 @@ class CakeTestCaseTest extends CakeTestCase {
$this->assertEquals(0, $result->errorCount());
}
/**
* testLoadFixturesOnDemand
*
* @access public
* @return void
*/
function testUnoadFixturesAfterFailure() {
$test = new FixturizedTestCase('testFixtureLoadOnDemand');
$test->autoFixtures = false;
$manager = $this->getMock('CakeFixtureManager');
$manager->fixturize($test);
$test->sharedFixture = $manager;
$manager->expects($this->once())->method('loadSingle');
$result = $test->run();
$this->assertEquals(0, $result->errorCount());
}
/**
* testThrowException
*
* @access public
* @return void
*/
function testThrowException() {
$test = new FixturizedTestCase('testThrowException');
$test->autoFixtures = false;
$manager = $this->getMock('CakeFixtureManager');
$manager->fixturize($test);
$test->sharedFixture = $manager;
$manager->expects($this->once())->method('unload');
$result = $test->run();
$this->assertEquals(1, $result->errorCount());
}
/**
* testSkipIf
*

View file

@ -51,4 +51,13 @@ class FixturizedTestCase extends CakeTestCase {
public function testSkipIfFalse() {
$this->skipIf(false);
}
/**
* test that a fixtures are unoaded even if the test throws exceptions
*
* @return void
*/
public function testThrowException() {
throw new Exception();
}
}

View file

@ -65,6 +65,26 @@ class CakeTestCase extends PHPUnit_Framework_TestCase {
*/
private $__savedGetData = array();
/**
* Runs the test case and collects the results in a TestResult object.
* If no TestResult object is passed a new one will be created.
* This method is run for each test method in this class
*
* @param PHPUnit_Framework_TestResult $result
* @return PHPUnit_Framework_TestResult
* @throws InvalidArgumentException
*/
public function run(PHPUnit_Framework_TestResult $result = NULL) {
if (!empty($this->sharedFixture)) {
$this->sharedFixture->load($this);
}
$result = parent::run($result);
if (!empty($this->sharedFixture)) {
$this->sharedFixture->unload($this);
}
return $result;
}
/**
* Called when a test case (group of methods) is about to start (to be overriden when needed.)
*
@ -123,35 +143,11 @@ class CakeTestCase extends PHPUnit_Framework_TestCase {
*/
protected function assertPreConditions() {
parent::assertPreConditions();
if (!empty($this->sharedFixture)) {
$this->sharedFixture->load($this);
}
if (!in_array(strtolower($this->getName()), $this->methods)) {
$this->startTest($this->getName());
}
}
/**
* Runs as last test to drop tables.
*
* @return void
*/
public function end() {
if (isset($this->_fixtures) && isset($this->db)) {
if ($this->dropTables) {
foreach (array_reverse($this->_fixtures) as $fixture) {
$fixture->drop($this->db);
}
}
$this->db->sources(true);
Configure::write('Cache.disable', false);
}
if (class_exists('ClassRegistry')) {
ClassRegistry::flush();
}
}
/**
* Announces the end of a test.
*
@ -160,9 +156,6 @@ class CakeTestCase extends PHPUnit_Framework_TestCase {
*/
protected function assertPostConditions() {
parent::assertPostConditions();
if (!empty($this->sharedFixture)) {
$this->sharedFixture->unload($this);
}
if (!in_array(strtolower($this->getName()), $this->methods)) {
$this->endTest($this->getName());
}