/www/lib/Cake/TestSuite/Fixture/CakeFixtureManager.php

https://gitlab.com/digaotinfo/abear.com.br · PHP · 268 lines · 155 code · 21 blank · 92 comment · 28 complexity · b22734bb593bcdf96146068215cf4774 MD5 · raw file

  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. */
  95. protected function _loadFixtures($fixtures) {
  96. foreach ($fixtures as $index => $fixture) {
  97. $fixtureFile = null;
  98. $fixtureIndex = $fixture;
  99. if (isset($this->_loaded[$fixture])) {
  100. continue;
  101. }
  102. if (strpos($fixture, 'core.') === 0) {
  103. $fixture = substr($fixture, strlen('core.'));
  104. $fixturePaths[] = CAKE . 'Test' . DS . 'Fixture';
  105. } elseif (strpos($fixture, 'app.') === 0) {
  106. $fixture = substr($fixture, strlen('app.'));
  107. $fixturePaths = array(
  108. TESTS . 'Fixture'
  109. );
  110. } elseif (strpos($fixture, 'plugin.') === 0) {
  111. $parts = explode('.', $fixture, 3);
  112. $pluginName = $parts[1];
  113. $fixture = $parts[2];
  114. $fixturePaths = array(
  115. CakePlugin::path(Inflector::camelize($pluginName)) . 'Test' . DS . 'Fixture',
  116. TESTS . 'Fixture'
  117. );
  118. } else {
  119. $fixturePaths = array(
  120. TESTS . 'Fixture',
  121. CAKE . 'Test' . DS . 'Fixture'
  122. );
  123. }
  124. foreach ($fixturePaths as $path) {
  125. $className = Inflector::camelize($fixture);
  126. if (is_readable($path . DS . $className . 'Fixture.php')) {
  127. $fixtureFile = $path . DS . $className . 'Fixture.php';
  128. require_once $fixtureFile;
  129. $fixtureClass = $className . 'Fixture';
  130. $this->_loaded[$fixtureIndex] = new $fixtureClass();
  131. $this->_fixtureMap[$fixtureClass] = $this->_loaded[$fixtureIndex];
  132. break;
  133. }
  134. }
  135. }
  136. }
  137. /**
  138. * Runs the drop and create commands on the fixtures if necessary.
  139. *
  140. * @param CakeTestFixture $fixture the fixture object to create
  141. * @param DataSource $db the datasource instance to use
  142. * @param boolean $drop whether drop the fixture if it is already created or not
  143. * @return void
  144. */
  145. protected function _setupTable($fixture, $db = null, $drop = true) {
  146. if (!$db) {
  147. if (!empty($fixture->useDbConfig)) {
  148. $db = ClassRegistry::getDataSource($fixture->useDbConfig);
  149. } else {
  150. $db = $this->_db;
  151. }
  152. }
  153. if (!empty($fixture->created) && in_array($db->configKeyName, $fixture->created)) {
  154. return;
  155. }
  156. $sources = $db->listSources();
  157. $table = $db->config['prefix'] . $fixture->table;
  158. if ($drop && in_array($table, $sources)) {
  159. $fixture->drop($db);
  160. $fixture->create($db);
  161. } elseif (!in_array($table, $sources)) {
  162. $fixture->create($db);
  163. }
  164. }
  165. /**
  166. * Creates the fixtures tables and inserts data on them.
  167. *
  168. * @param CakeTestCase $test the test to inspect for fixture loading
  169. * @return void
  170. */
  171. public function load(CakeTestCase $test) {
  172. if (empty($test->fixtures)) {
  173. return;
  174. }
  175. $fixtures = $test->fixtures;
  176. if (empty($fixtures) || $test->autoFixtures == false) {
  177. return;
  178. }
  179. $test->db->begin();
  180. foreach ($fixtures as $f) {
  181. if (!empty($this->_loaded[$f])) {
  182. $fixture = $this->_loaded[$f];
  183. $db = ConnectionManager::getDataSource($fixture->useDbConfig);
  184. $this->_setupTable($fixture, $db, $test->dropTables);
  185. $fixture->insert($db);
  186. }
  187. }
  188. $test->db->commit();
  189. }
  190. /**
  191. * Truncates the fixtures tables
  192. *
  193. * @param CakeTestCase $test the test to inspect for fixture unloading
  194. * @return void
  195. */
  196. public function unload(CakeTestCase $test) {
  197. $fixtures = !empty($test->fixtures) ? $test->fixtures : array();
  198. foreach (array_reverse($fixtures) as $f) {
  199. if (isset($this->_loaded[$f])) {
  200. $fixture = $this->_loaded[$f];
  201. if (!empty($fixture->created)) {
  202. foreach ($fixture->created as $ds) {
  203. $db = ConnectionManager::getDataSource($ds);
  204. $fixture->truncate($db);
  205. }
  206. }
  207. }
  208. }
  209. }
  210. /**
  211. * Creates a single fixture table and loads data into it.
  212. *
  213. * @param string $name of the fixture
  214. * @param DataSource $db DataSource instance or leave null to get DataSource from the fixture
  215. * @return void
  216. * @throws UnexpectedValueException if $name is not a previously loaded class
  217. */
  218. public function loadSingle($name, $db = null) {
  219. $name .= 'Fixture';
  220. if (isset($this->_fixtureMap[$name])) {
  221. $fixture = $this->_fixtureMap[$name];
  222. if (!$db) {
  223. $db = ConnectionManager::getDataSource($fixture->useDbConfig);
  224. }
  225. $this->_setupTable($fixture, $db);
  226. $fixture->truncate($db);
  227. $fixture->insert($db);
  228. } else {
  229. throw new UnexpectedValueException(__d('cake_dev', 'Referenced fixture class %s not found', $name));
  230. }
  231. }
  232. /**
  233. * Drop all fixture tables loaded by this class
  234. *
  235. * @return void
  236. */
  237. public function shutDown() {
  238. foreach ($this->_loaded as $fixture) {
  239. if (!empty($fixture->created)) {
  240. foreach ($fixture->created as $ds) {
  241. $db = ConnectionManager::getDataSource($ds);
  242. $fixture->drop($db);
  243. }
  244. }
  245. }
  246. }
  247. }