Skip to content

Commit

Permalink
Convertint CakeFixtureManager into a non-static class to be able to r…
Browse files Browse the repository at this point in the history
…eplace it with custom implementations

and to test it more easily
  • Loading branch information
lorenzo committed May 8, 2010
1 parent 82a1bd6 commit acb9733
Showing 1 changed file with 45 additions and 34 deletions.
79 changes: 45 additions & 34 deletions cake/tests/lib/cake_fixture_manager.php
@@ -1,30 +1,32 @@
<?php

class CakeFixtureManager {
protected static $_initialized = false;
protected static $_db;
protected static $_loaded = array();
protected static $_fixtureMap = array();

public static function fixturize(CakeTestCase $test) {
if (empty($test->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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -154,26 +157,34 @@ 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);
}
}
}
}

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);
}
}
}
}

0 comments on commit acb9733

Please sign in to comment.