From 82da9be2ce3e094789884c8a3a39aabf69675440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Mon, 31 May 2010 23:07:21 -0430 Subject: [PATCH] Fixing issue with fixtures not being unloaded if the test method throws exceptions or fails in some unexpected way --- cake/tests/cases/libs/cake_test_case.test.php | 33 +++++++++++++ cake/tests/fixtures/fixturized_test_case.php | 9 ++++ cake/tests/lib/cake_test_case.php | 47 ++++++++----------- 3 files changed, 62 insertions(+), 27 deletions(-) diff --git a/cake/tests/cases/libs/cake_test_case.test.php b/cake/tests/cases/libs/cake_test_case.test.php index 59fb53845..9aa577be1 100644 --- a/cake/tests/cases/libs/cake_test_case.test.php +++ b/cake/tests/cases/libs/cake_test_case.test.php @@ -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 * diff --git a/cake/tests/fixtures/fixturized_test_case.php b/cake/tests/fixtures/fixturized_test_case.php index a74feae98..1895b63af 100644 --- a/cake/tests/fixtures/fixturized_test_case.php +++ b/cake/tests/fixtures/fixturized_test_case.php @@ -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(); + } } \ No newline at end of file diff --git a/cake/tests/lib/cake_test_case.php b/cake/tests/lib/cake_test_case.php index c182b625e..42389b3f8 100644 --- a/cake/tests/lib/cake_test_case.php +++ b/cake/tests/lib/cake_test_case.php @@ -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()); }