From 3a8b34420839239530ca6a5bb8d0b292c01af651 Mon Sep 17 00:00:00 2001 From: "Mark Story mark@mark-story.com" Date: Mon, 29 Aug 2011 22:07:23 -0400 Subject: [PATCH] Fixing issue where CakeTestCase would erase custom bootstrapped paths in tearDown(). Adding App::paths() to simplify getting all the paths App knows about. Fixes #1934 --- lib/Cake/Core/App.php | 11 ++++++++++ lib/Cake/Test/Case/Core/AppTest.php | 12 +++++++++++ .../Test/Case/TestSuite/CakeTestCaseTest.php | 14 +++++++++++-- lib/Cake/TestSuite/CakeTestCase.php | 20 ++++++++++++++++--- 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Core/App.php b/lib/Cake/Core/App.php index d36ff7966..9a9f17887 100644 --- a/lib/Cake/Core/App.php +++ b/lib/Cake/Core/App.php @@ -227,6 +227,17 @@ class App { return self::$_packages[$type]; } +/** + * Get all the currently loaded paths from App. Useful for inspecting + * or storing all paths App knows about. For a paths to a specific package + * use App::path() + * + * @return array An array of packages and their associated paths. + */ + public static function paths() { + return self::$_packages; + } + /** * Sets up each package location on the file system. You can configure multiple search paths * for each package, those will be used to look for files one folder at a time in the specified order diff --git a/lib/Cake/Test/Case/Core/AppTest.php b/lib/Cake/Test/Case/Core/AppTest.php index 945a76062..59a90edb5 100644 --- a/lib/Cake/Test/Case/Core/AppTest.php +++ b/lib/Cake/Test/Case/Core/AppTest.php @@ -749,4 +749,16 @@ class AppTest extends CakeTestCase { App::uses('MyCustomClass', 'MyPackage/Name'); $this->assertEquals('MyPackage/Name', App::location('MyCustomClass')); } + +/** + * Test that paths() works. + * + * @return void + */ + public function testPaths() { + $result = App::paths(); + $this->assertArrayHasKey('plugins', $result); + $this->assertArrayHasKey('Controller', $result); + $this->assertArrayHasKey('Controller/Component', $result); + } } diff --git a/lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php b/lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php index 07b855d6c..5ed8b2a44 100644 --- a/lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php +++ b/lib/Cake/Test/Case/TestSuite/CakeTestCaseTest.php @@ -46,7 +46,7 @@ class CakeTestCaseTest extends CakeTestCase { * @return void */ public function setUp() { - $this->_debug = Configure::read('debug'); + parent::setUp(); $this->Reporter = $this->getMock('CakeHtmlReporter'); } @@ -56,7 +56,7 @@ class CakeTestCaseTest extends CakeTestCase { * @return void */ public function tearDown() { - Configure::write('debug', $this->_debug); + parent::tearDown(); unset($this->Result); unset($this->Reporter); } @@ -230,4 +230,14 @@ class CakeTestCaseTest extends CakeTestCase { $result = $test->run(); $this->assertEquals(0, $result->skippedCount()); } + +/** + * Test that CakeTestCase::setUp() backs up values. + * + * @return void + */ + public function testSetupBackUpValues() { + $this->assertArrayHasKey('debug', $this->_configure); + $this->assertArrayHasKey('plugins', $this->_pathRestore); + } } diff --git a/lib/Cake/TestSuite/CakeTestCase.php b/lib/Cake/TestSuite/CakeTestCase.php index 082c3bd50..e986abb08 100644 --- a/lib/Cake/TestSuite/CakeTestCase.php +++ b/lib/Cake/TestSuite/CakeTestCase.php @@ -58,6 +58,12 @@ abstract class CakeTestCase extends PHPUnit_Framework_TestCase { */ protected $_configure = array(); +/** + * Path settings to restore at the end of the test. + * + * @var array + */ + protected $_pathRestore = array(); /** * Runs the test case and collects the results in a TestResult object. @@ -112,13 +118,21 @@ abstract class CakeTestCase extends PHPUnit_Framework_TestCase { } /** - * setup the test case, backup the static object values so they can be restored. + * Setup the test case, backup the static object values so they can be restored. + * Specifically backs up the contents of Configure and paths in App if they have + * not already been backed up. * * @return void */ public function setUp() { parent::setUp(); - $this->_configure = Configure::read(); + + if (empty($this->_configure)) { + $this->_configure = Configure::read(); + } + if (empty($this->_pathRestore)) { + $this->_pathRestore = App::paths(); + } if (class_exists('Router', false)) { Router::reload(); } @@ -131,7 +145,7 @@ abstract class CakeTestCase extends PHPUnit_Framework_TestCase { */ public function tearDown() { parent::tearDown(); - App::build(); + App::build($this->_pathRestore, App::RESET); if (class_exists('ClassRegistry', false)) { ClassRegistry::flush(); }