Skip to content

Commit

Permalink
Fixing issue with fixtures not being unloaded if the test method thro…
Browse files Browse the repository at this point in the history
…ws exceptions or fails in some unexpected way
  • Loading branch information
lorenzo committed Jun 1, 2010
1 parent ae4f3b2 commit 82da9be
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 27 deletions.
33 changes: 33 additions & 0 deletions cake/tests/cases/libs/cake_test_case.test.php
Expand Up @@ -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
*
Expand Down
9 changes: 9 additions & 0 deletions cake/tests/fixtures/fixturized_test_case.php
Expand Up @@ -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();
}
}
47 changes: 20 additions & 27 deletions cake/tests/lib/cake_test_case.php
Expand Up @@ -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.)
*
Expand Down Expand Up @@ -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.
*
Expand All @@ -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());
}
Expand Down

0 comments on commit 82da9be

Please sign in to comment.