CakeFixtureManager.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. * A factory class to manage the life cycle of test fixtures
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.TestSuite.Fixture
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ConnectionManager', 'Model');
  20. App::uses('ClassRegistry', 'Utility');
  21. /**
  22. * A factory class to manage the life cycle of test fixtures
  23. *
  24. * @package Cake.TestSuite.Fixture
  25. */
  26. class CakeFixtureManager {
  27. /**
  28. * Was this class already initialized?
  29. *
  30. * @var boolean
  31. */
  32. protected $_initialized = false;
  33. /**
  34. * Default datasource to use
  35. *
  36. * @var DataSource
  37. */
  38. protected $_db = null;
  39. /**
  40. * Holds the fixture classes that where instantiated
  41. *
  42. * @var array
  43. */
  44. protected $_loaded = array();
  45. /**
  46. * Holds the fixture classes that where instantiated indexed by class name
  47. *
  48. * @var array
  49. */
  50. protected $_fixtureMap = array();
  51. /**
  52. * Inspects the test to look for unloaded fixtures and loads them
  53. *
  54. * @param CakeTestCase $test the test case to inspect
  55. * @return void
  56. */
  57. public function fixturize($test) {
  58. if (!$this->_initialized) {
  59. ClassRegistry::config(array('ds' => 'test', 'testing' => true));
  60. }
  61. if (empty($test->fixtures) || !empty($this->_processed[get_class($test)])) {
  62. $test->db = $this->_db;
  63. return;
  64. }
  65. $this->_initDb();
  66. $test->db = $this->_db;
  67. if (!is_array($test->fixtures)) {
  68. $test->fixtures = array_map('trim', explode(',', $test->fixtures));
  69. }
  70. if (isset($test->fixtures)) {
  71. $this->_loadFixtures($test->fixtures);
  72. }
  73. $this->_processed[get_class($test)] = true;
  74. }
  75. /**
  76. * Initializes this class with a DataSource object to use as default for all fixtures
  77. *
  78. * @return void
  79. */
  80. protected function _initDb() {
  81. if ($this->_initialized) {
  82. return;
  83. }
  84. $db = ConnectionManager::getDataSource('test');
  85. $db->cacheSources = false;
  86. $this->_db = $db;
  87. $this->_initialized = true;
  88. }
  89. /**
  90. * Looks for fixture files and instantiates the classes accordingly
  91. *
  92. * @param array $fixtures the fixture names to load using the notation {type}.{name}
  93. * @return void
  94. * @throws UnexpectedValueException when a referenced fixture does not exist.
  95. */
  96. protected function _loadFixtures($fixtures) {
  97. foreach ($fixtures as $fixture) {
  98. $fixtureFile = null;
  99. $fixtureIndex = $fixture;
  100. if (isset($this->_loaded[$fixture])) {
  101. continue;
  102. }
  103. if (strpos($fixture, 'core.') === 0) {
  104. $fixture = substr($fixture, strlen('core.'));
  105. $fixturePaths[] = CAKE . 'Test' . DS . 'Fixture';
  106. } elseif (strpos($fixture, 'app.') === 0) {
  107. $fixture = substr($fixture, strlen('app.'));
  108. $fixturePaths = array(
  109. TESTS . 'Fixture'
  110. );
  111. } elseif (strpos($fixture, 'plugin.') === 0) {
  112. $parts = explode('.', $fixture, 3);
  113. $pluginName = $parts[1];
  114. $fixture = $parts[2];
  115. $fixturePaths = array(
  116. CakePlugin::path(Inflector::camelize($pluginName)) . 'Test' . DS . 'Fixture',
  117. TESTS . 'Fixture'
  118. );
  119. } else {
  120. $fixturePaths = array(
  121. TESTS . 'Fixture',
  122. CAKE . 'Test' . DS . 'Fixture'
  123. );
  124. }
  125. $loaded = false;
  126. foreach ($fixturePaths as $path) {
  127. $className = Inflector::camelize($fixture);
  128. if (is_readable($path . DS . $className . 'Fixture.php')) {
  129. $fixtureFile = $path . DS . $className . 'Fixture.php';
  130. require_once $fixtureFile;
  131. $fixtureClass = $className . 'Fixture';
  132. $this->_loaded[$fixtureIndex] = new $fixtureClass();
  133. $this->_fixtureMap[$fixtureClass] = $this->_loaded[$fixtureIndex];
  134. $loaded = true;
  135. break;
  136. }
  137. }
  138. if (!$loaded) {
  139. $firstPath = str_replace(array(APP, CAKE_CORE_INCLUDE_PATH, ROOT), '', $fixturePaths[0] . DS . $className . 'Fixture.php');
  140. throw new UnexpectedValueException(__d('cake_dev', 'Referenced fixture class %s (%s) not found', $className, $firstPath));
  141. }
  142. }
  143. }
  144. /**
  145. * Runs the drop and create commands on the fixtures if necessary.
  146. *
  147. * @param CakeTestFixture $fixture the fixture object to create
  148. * @param DataSource $db the datasource instance to use
  149. * @param boolean $drop whether drop the fixture if it is already created or not
  150. * @return void
  151. */
  152. protected function _setupTable($fixture, $db = null, $drop = true) {
  153. if (!$db) {
  154. if (!empty($fixture->useDbConfig)) {
  155. $db = ConnectionManager::getDataSource($fixture->useDbConfig);
  156. } else {
  157. $db = $this->_db;
  158. }
  159. }
  160. if (!empty($fixture->created) && in_array($db->configKeyName, $fixture->created)) {
  161. return;
  162. }
  163. $sources = (array)$db->listSources();
  164. $table = $db->config['prefix'] . $fixture->table;
  165. $exists = in_array($table, $sources);
  166. if ($drop && $exists) {
  167. $fixture->drop($db);
  168. $fixture->create($db);
  169. } elseif (!$exists) {
  170. $fixture->create($db);
  171. } else {
  172. $fixture->created[] = $db->configKeyName;
  173. }
  174. }
  175. /**
  176. * Creates the fixtures tables and inserts data on them.
  177. *
  178. * @param CakeTestCase $test the test to inspect for fixture loading
  179. * @return void
  180. */
  181. public function load(CakeTestCase $test) {
  182. if (empty($test->fixtures)) {
  183. return;
  184. }
  185. $fixtures = $test->fixtures;
  186. if (empty($fixtures) || !$test->autoFixtures) {
  187. return;
  188. }
  189. foreach ($fixtures as $f) {
  190. if (!empty($this->_loaded[$f])) {
  191. $fixture = $this->_loaded[$f];
  192. $db = ConnectionManager::getDataSource($fixture->useDbConfig);
  193. $db->begin();
  194. $this->_setupTable($fixture, $db, $test->dropTables);
  195. $fixture->insert($db);
  196. $db->commit();
  197. }
  198. }
  199. }
  200. /**
  201. * Truncates the fixtures tables
  202. *
  203. * @param CakeTestCase $test the test to inspect for fixture unloading
  204. * @return void
  205. */
  206. public function unload(CakeTestCase $test) {
  207. $fixtures = !empty($test->fixtures) ? $test->fixtures : array();
  208. foreach (array_reverse($fixtures) as $f) {
  209. if (isset($this->_loaded[$f])) {
  210. $fixture = $this->_loaded[$f];
  211. if (!empty($fixture->created)) {
  212. foreach ($fixture->created as $ds) {
  213. $db = ConnectionManager::getDataSource($ds);
  214. $fixture->truncate($db);
  215. }
  216. }
  217. }
  218. }
  219. }
  220. /**
  221. * Creates a single fixture table and loads data into it.
  222. *
  223. * @param string $name of the fixture
  224. * @param DataSource $db DataSource instance or leave null to get DataSource from the fixture
  225. * @return void
  226. * @throws UnexpectedValueException if $name is not a previously loaded class
  227. */
  228. public function loadSingle($name, $db = null) {
  229. $name .= 'Fixture';
  230. if (isset($this->_fixtureMap[$name])) {
  231. $fixture = $this->_fixtureMap[$name];
  232. if (!$db) {
  233. $db = ConnectionManager::getDataSource($fixture->useDbConfig);
  234. }
  235. $this->_setupTable($fixture, $db);
  236. $fixture->truncate($db);
  237. $fixture->insert($db);
  238. } else {
  239. throw new UnexpectedValueException(__d('cake_dev', 'Referenced fixture class %s not found', $name));
  240. }
  241. }
  242. /**
  243. * Drop all fixture tables loaded by this class
  244. *
  245. * @return void
  246. */
  247. public function shutDown() {
  248. foreach ($this->_loaded as $fixture) {
  249. if (!empty($fixture->created)) {
  250. foreach ($fixture->created as $ds) {
  251. $db = ConnectionManager::getDataSource($ds);
  252. $fixture->drop($db);
  253. }
  254. }
  255. }
  256. }
  257. }