diff --git a/cake/tests/lib/cake_fixture_manager.php b/cake/tests/lib/cake_fixture_manager.php index b50b879c8ea..238931a6252 100644 --- a/cake/tests/lib/cake_fixture_manager.php +++ b/cake/tests/lib/cake_fixture_manager.php @@ -1,30 +1,32 @@ fixtures)) { + protected $_initialized = false; + protected $_db = null; + protected $_loaded = array(); + protected $_fixtureMap = array(); + + public function fixturize(CakeTestCase $test) { + if (empty($test->fixtures) || !empty($this->_processed[get_class($test)])) { + $test->db = $this->_db; return; } - if (!self::$_initialized) { - self::_initDb(); - if (!empty(self::$_db)) { - $test->db = self::$_db; - } - } + $this->_initDb(); + $test->db = $this->_db; if (!is_array($test->fixtures)) { $test->fixtures = array_map('trim', explode(',', $test->fixtures)); } if (isset($test->fixtures)) { - self::_loadFixtures($test->fixtures); + $this->_loadFixtures($test->fixtures); } + + $this->_processed[get_class($test)] = true; } - protected static function _initDb() { + protected function _initDb() { + if ($this->_initialized) { + return; + } $testDbAvailable = in_array('test', array_keys(ConnectionManager::enumConnectionObjects())); $_prefix = null; @@ -46,17 +48,18 @@ protected static function _initDb() { $db->config['prefix'] = $_prefix; // Get db connection - self::$_db = ConnectionManager::getDataSource('test_suite'); - self::$_db->cacheSources = false; + $this->_db = ConnectionManager::getDataSource('test_suite'); + $this->_db->cacheSources = false; ClassRegistry::config(array('ds' => 'test_suite')); + $this->_initialized = true; } - protected static function _loadFixtures($fixtures) { + protected function _loadFixtures($fixtures) { foreach ($fixtures as $index => $fixture) { $fixtureFile = null; $fixtureIndex = $fixture; - if (isset(self::$_loaded[$fixture])) { + if (isset($this->_loaded[$fixture])) { continue; } if (strpos($fixture, 'core.') === 0) { @@ -97,18 +100,18 @@ protected static function _loadFixtures($fixtures) { if (isset($fixtureFile)) { require_once($fixtureFile); $fixtureClass = Inflector::camelize($fixture) . 'Fixture'; - self::$_loaded[$fixtureIndex] = new $fixtureClass(self::$_db); - self::$_fixtureMap[$fixtureClass] = self::$_loaded[$fixtureIndex]; + $this->_loaded[$fixtureIndex] = new $fixtureClass($this->_db); + $this->_fixtureMap[$fixtureClass] = $this->_loaded[$fixtureIndex]; } } } - protected static function setupTable($fixture, $db = null, $drop = true) { + protected function _setupTable($fixture, $db = null, $drop = true) { if (!empty($fixture->created)) { return; } if (!$db) { - $db = self::$_db; + $db = $this->_db; } $cacheSources = $db->cacheSources; @@ -127,7 +130,7 @@ protected static function setupTable($fixture, $db = null, $drop = true) { } } - public static function load(CakeTestCase $test) { + public function load(CakeTestCase $test) { if (empty($test->fixtures)) { return; } @@ -137,15 +140,15 @@ public static function load(CakeTestCase $test) { } foreach ($fixtures as $f) { - if (!empty(self::$_loaded[$f])) { - $fixture = self::$_loaded[$f]; - self::setupTable($fixture, $test->db, $test->dropTables); + if (!empty($this->_loaded[$f])) { + $fixture = $this->_loaded[$f]; + $this->_setupTable($fixture, $test->db, $test->dropTables); $fixture->insert($test->db); } } } - public static function unload(CakeTestCase $test) { + public function unload(CakeTestCase $test) { if (empty($test->fixtures)) { return; } @@ -154,8 +157,8 @@ public static function unload(CakeTestCase $test) { return; } foreach ($fixtures as $f) { - if (isset(self::$_loaded[$f])) { - $fixture = self::$_loaded[$f]; + if (isset($this->_loaded[$f])) { + $fixture = $this->_loaded[$f]; if (!empty($fixture->created)) { $fixture->truncate($test->db); } @@ -163,17 +166,25 @@ public static function unload(CakeTestCase $test) { } } - public static function loadSingle($name, $db = null) { + public function loadSingle($name, $db = null) { $name .= 'Fixture'; - if (isset(self::$_fixtureMap[$name])) { + if (isset($this->_fixtureMap[$name])) { if (!$db) { - $db = self::$_db; + $db = $this->_db; } - $fixture = self::$_fixtureMap[$name]; + $fixture = $this->_fixtureMap[$name]; $fixture->truncate($db); $fixture->insert($db); } else { throw new UnexpectedValueException(sprintf(__('Referenced fixture class %s not found'), $name)); } } + + public function shutDown() { + foreach ($this->_loaded as $fixture) { + if (!empty($fixture->created)) { + $fixture->drop($this->_db); + } + } + } } \ No newline at end of file