Skip to content
Permalink
Browse files

Fixing issue with fixtures not being unloaded if the test method thro…

…ws exceptions or fails in some unexpected way
  • Loading branch information...
lorenzo committed Jun 1, 2010
1 parent ae4f3b2 commit 82da9be2ce3e094789884c8a3a39aabf69675440
@@ -147,6 +147,39 @@ function testLoadFixturesOnDemand() {
$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
*
@@ -51,4 +51,13 @@ public function testSkipIfTrue() {
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();
}
}
@@ -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 @@ public function skipIf($shouldSkip, $message = '') {
*/
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 @@ public function end() {
*/
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());
}

0 comments on commit 82da9be

Please sign in to comment.
You can’t perform that action at this time.