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
This commit is contained in:
Mark Story mark@mark-story.com 2011-08-29 22:07:23 -04:00
parent 0e85170f28
commit 3a8b344208
4 changed files with 52 additions and 5 deletions

View file

@ -227,6 +227,17 @@ class App {
return self::$_packages[$type]; 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 * 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 * for each package, those will be used to look for files one folder at a time in the specified order

View file

@ -749,4 +749,16 @@ class AppTest extends CakeTestCase {
App::uses('MyCustomClass', 'MyPackage/Name'); App::uses('MyCustomClass', 'MyPackage/Name');
$this->assertEquals('MyPackage/Name', App::location('MyCustomClass')); $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);
}
} }

View file

@ -46,7 +46,7 @@ class CakeTestCaseTest extends CakeTestCase {
* @return void * @return void
*/ */
public function setUp() { public function setUp() {
$this->_debug = Configure::read('debug'); parent::setUp();
$this->Reporter = $this->getMock('CakeHtmlReporter'); $this->Reporter = $this->getMock('CakeHtmlReporter');
} }
@ -56,7 +56,7 @@ class CakeTestCaseTest extends CakeTestCase {
* @return void * @return void
*/ */
public function tearDown() { public function tearDown() {
Configure::write('debug', $this->_debug); parent::tearDown();
unset($this->Result); unset($this->Result);
unset($this->Reporter); unset($this->Reporter);
} }
@ -230,4 +230,14 @@ class CakeTestCaseTest extends CakeTestCase {
$result = $test->run(); $result = $test->run();
$this->assertEquals(0, $result->skippedCount()); $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);
}
} }

View file

@ -58,6 +58,12 @@ abstract class CakeTestCase extends PHPUnit_Framework_TestCase {
*/ */
protected $_configure = array(); 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. * 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 * @return void
*/ */
public function setUp() { public function setUp() {
parent::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)) { if (class_exists('Router', false)) {
Router::reload(); Router::reload();
} }
@ -131,7 +145,7 @@ abstract class CakeTestCase extends PHPUnit_Framework_TestCase {
*/ */
public function tearDown() { public function tearDown() {
parent::tearDown(); parent::tearDown();
App::build(); App::build($this->_pathRestore, App::RESET);
if (class_exists('ClassRegistry', false)) { if (class_exists('ClassRegistry', false)) {
ClassRegistry::flush(); ClassRegistry::flush();
} }