PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/TestSuite/Fixture/CakeTestFixture.php

https://bitbucket.org/dosm123/crm
PHP | 258 lines | 159 code | 25 blank | 74 comment | 33 complexity | d1750410c3c913b88b4dfdd47e6cdca4 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  4. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice
  8. *
  9. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  11. * @package Cake.TestSuite.Fixture
  12. * @since CakePHP(tm) v 1.2.0.4667
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. App::uses('CakeSchema', 'Model');
  16. /**
  17. * CakeTestFixture is responsible for building and destroying tables to be used
  18. * during testing.
  19. *
  20. * @package Cake.TestSuite.Fixture
  21. */
  22. class CakeTestFixture {
  23. /**
  24. * Name of the object
  25. *
  26. * @var string
  27. */
  28. public $name = null;
  29. /**
  30. * Cake's DBO driver (e.g: DboMysql).
  31. *
  32. */
  33. public $db = null;
  34. /**
  35. * Fixture Datasource
  36. *
  37. */
  38. public $useDbConfig = 'test';
  39. /**
  40. * Full Table Name
  41. *
  42. */
  43. public $table = null;
  44. /**
  45. * List of datasources where this fixture has been created
  46. *
  47. */
  48. public $created = array();
  49. /**
  50. * Instantiate the fixture.
  51. *
  52. */
  53. public function __construct() {
  54. if ($this->name === null) {
  55. if (preg_match('/^(.*)Fixture$/', get_class($this), $matches)) {
  56. $this->name = $matches[1];
  57. } else {
  58. $this->name = get_class($this);
  59. }
  60. }
  61. $connection = 'test';
  62. if (!empty($this->useDbConfig)) {
  63. $connection = $this->useDbConfig;
  64. if (strpos($connection, 'test') !== 0) {
  65. throw new CakeException(__d('cake_dev', 'Invalid datasource %s for object %s', $connection, $this->name));
  66. }
  67. }
  68. $this->Schema = new CakeSchema(array('name' => 'TestSuite', 'connection' => $connection));
  69. $this->init();
  70. }
  71. /**
  72. * Initialize the fixture.
  73. *
  74. */
  75. public function init() {
  76. if (isset($this->import) && (is_string($this->import) || is_array($this->import))) {
  77. $import = array_merge(
  78. array('connection' => 'default', 'records' => false),
  79. is_array($this->import) ? $this->import : array('model' => $this->import)
  80. );
  81. $this->Schema->connection = $import['connection'];
  82. if (isset($import['model'])) {
  83. list($plugin, $modelClass) = pluginSplit($import['model'], true);
  84. App::uses($modelClass, $plugin . 'Model');
  85. if (!class_exists($modelClass)) {
  86. throw new MissingModelException(array('class' => $modelClass));
  87. }
  88. $model = new $modelClass(null, null, $import['connection']);
  89. $db = $model->getDataSource();
  90. if (empty($model->tablePrefix)) {
  91. $model->tablePrefix = $db->config['prefix'];
  92. }
  93. $this->fields = $model->schema(true);
  94. $this->fields[$model->primaryKey]['key'] = 'primary';
  95. $this->table = $db->fullTableName($model, false, false);
  96. ClassRegistry::config(array('ds' => 'test'));
  97. ClassRegistry::flush();
  98. } elseif (isset($import['table'])) {
  99. $model = new Model(null, $import['table'], $import['connection']);
  100. $db = ConnectionManager::getDataSource($import['connection']);
  101. $db->cacheSources = false;
  102. $model->useDbConfig = $import['connection'];
  103. $model->name = Inflector::camelize(Inflector::singularize($import['table']));
  104. $model->table = $import['table'];
  105. $model->tablePrefix = $db->config['prefix'];
  106. $this->fields = $model->schema(true);
  107. ClassRegistry::flush();
  108. }
  109. if (!empty($db->config['prefix']) && strpos($this->table, $db->config['prefix']) === 0) {
  110. $this->table = str_replace($db->config['prefix'], '', $this->table);
  111. }
  112. if (isset($import['records']) && $import['records'] !== false && isset($model) && isset($db)) {
  113. $this->records = array();
  114. $query = array(
  115. 'fields' => $db->fields($model, null, array_keys($this->fields)),
  116. 'table' => $db->fullTableName($model),
  117. 'alias' => $model->alias,
  118. 'conditions' => array(),
  119. 'order' => null,
  120. 'limit' => null,
  121. 'group' => null
  122. );
  123. $records = $db->fetchAll($db->buildStatement($query, $model), false, $model->alias);
  124. if ($records !== false && !empty($records)) {
  125. $this->records = Set::extract($records, '{n}.' . $model->alias);
  126. }
  127. }
  128. }
  129. if (!isset($this->table)) {
  130. $this->table = Inflector::underscore(Inflector::pluralize($this->name));
  131. }
  132. if (!isset($this->primaryKey) && isset($this->fields['id'])) {
  133. $this->primaryKey = 'id';
  134. }
  135. }
  136. /**
  137. * Run before all tests execute, should return SQL statement to create table for this fixture could be executed successfully.
  138. *
  139. * @param object $db An instance of the database object used to create the fixture table
  140. * @return boolean True on success, false on failure
  141. */
  142. public function create($db) {
  143. if (!isset($this->fields) || empty($this->fields)) {
  144. return false;
  145. }
  146. if (empty($this->fields['tableParameters']['engine'])) {
  147. $canUseMemory = true;
  148. foreach ($this->fields as $field => $args) {
  149. if (is_string($args)) {
  150. $type = $args;
  151. } elseif (!empty($args['type'])) {
  152. $type = $args['type'];
  153. } else {
  154. continue;
  155. }
  156. if (in_array($type, array('blob', 'text', 'binary'))) {
  157. $canUseMemory = false;
  158. break;
  159. }
  160. }
  161. if ($canUseMemory) {
  162. $this->fields['tableParameters']['engine'] = 'MEMORY';
  163. }
  164. }
  165. $this->Schema->build(array($this->table => $this->fields));
  166. try {
  167. $db->execute($db->createSchema($this->Schema), array('log' => false));
  168. $this->created[] = $db->configKeyName;
  169. } catch (Exception $e) {
  170. return false;
  171. }
  172. return true;
  173. }
  174. /**
  175. * Run after all tests executed, should return SQL statement to drop table for this fixture.
  176. *
  177. * @param object $db An instance of the database object used to create the fixture table
  178. * @return boolean True on success, false on failure
  179. */
  180. public function drop($db) {
  181. if (empty($this->fields)) {
  182. return false;
  183. }
  184. $this->Schema->build(array($this->table => $this->fields));
  185. try {
  186. $db->execute($db->dropSchema($this->Schema), array('log' => false));
  187. $this->created = array_diff($this->created, array($db->configKeyName));;
  188. } catch (Exception $e) {
  189. return false;
  190. }
  191. return true;
  192. }
  193. /**
  194. * Run before each tests is executed, should return a set of SQL statements to insert records for the table
  195. * of this fixture could be executed successfully.
  196. *
  197. * @param object $db An instance of the database into which the records will be inserted
  198. * @return boolean on success or if there are no records to insert, or false on failure
  199. */
  200. public function insert($db) {
  201. if (!isset($this->_insert)) {
  202. $values = array();
  203. if (isset($this->records) && !empty($this->records)) {
  204. $fields = array();
  205. foreach ($this->records as $record) {
  206. $fields = array_merge($fields, array_keys(array_intersect_key($record, $this->fields)));
  207. }
  208. $fields = array_unique($fields);
  209. $default = array_fill_keys($fields, null);
  210. foreach ($this->records as $record) {
  211. $fields = array_keys($record);
  212. $values[] = array_values(array_merge($default, $record));
  213. }
  214. return $db->insertMulti($this->table, $fields, $values);
  215. }
  216. return true;
  217. }
  218. }
  219. /**
  220. * Truncates the current fixture. Can be overwritten by classes extending CakeFixture to trigger other events before / after
  221. * truncate.
  222. *
  223. * @param object $db A reference to a db instance
  224. * @return boolean
  225. */
  226. public function truncate($db) {
  227. $fullDebug = $db->fullDebug;
  228. $db->fullDebug = false;
  229. $return = $db->truncate($this->table);
  230. $db->fullDebug = $fullDebug;
  231. return $return;
  232. }
  233. }