PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/udeshika/fake_twitter
PHP | 236 lines | 147 code | 23 blank | 66 comment | 31 complexity | 5f832a411842c3e44cd6df5cf1a4d94b MD5 | raw file
  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. * Full Table Name
  36. *
  37. */
  38. public $table = null;
  39. /**
  40. * Instantiate the fixture.
  41. *
  42. */
  43. public function __construct() {
  44. if ($this->name === null) {
  45. if (preg_match('/^(.*)Fixture$/', get_class($this), $matches)) {
  46. $this->name = $matches[1];
  47. } else {
  48. $this->name = get_class($this);
  49. }
  50. }
  51. $this->Schema = new CakeSchema(array('name' => 'TestSuite', 'connection' => 'test'));
  52. $this->init();
  53. }
  54. /**
  55. * Initialize the fixture.
  56. *
  57. */
  58. public function init() {
  59. if (isset($this->import) && (is_string($this->import) || is_array($this->import))) {
  60. $import = array_merge(
  61. array('connection' => 'default', 'records' => false),
  62. is_array($this->import) ? $this->import : array('model' => $this->import)
  63. );
  64. if (isset($import['model'])) {
  65. list($plugin, $modelClass) = pluginSplit($import['model'], true);
  66. App::uses($modelClass, $plugin . 'Model');
  67. if (!class_exists($modelClass)) {
  68. throw new MissingModelException(array('class' => $modelClass));
  69. }
  70. $model = new $modelClass(null, null, $import['connection']);
  71. $db = $model->getDataSource();
  72. if (empty($model->tablePrefix)) {
  73. $model->tablePrefix = $db->config['prefix'];
  74. }
  75. $this->fields = $model->schema(true);
  76. $this->fields[$model->primaryKey]['key'] = 'primary';
  77. $this->table = $db->fullTableName($model, false);
  78. ClassRegistry::config(array('ds' => 'test'));
  79. ClassRegistry::flush();
  80. } elseif (isset($import['table'])) {
  81. $model = new Model(null, $import['table'], $import['connection']);
  82. $db = ConnectionManager::getDataSource($import['connection']);
  83. $db->cacheSources = false;
  84. $model->useDbConfig = $import['connection'];
  85. $model->name = Inflector::camelize(Inflector::singularize($import['table']));
  86. $model->table = $import['table'];
  87. $model->tablePrefix = $db->config['prefix'];
  88. $this->fields = $model->schema(true);
  89. ClassRegistry::flush();
  90. }
  91. if (!empty($db->config['prefix']) && strpos($this->table, $db->config['prefix']) === 0) {
  92. $this->table = str_replace($db->config['prefix'], '', $this->table);
  93. }
  94. if (isset($import['records']) && $import['records'] !== false && isset($model) && isset($db)) {
  95. $this->records = array();
  96. $query = array(
  97. 'fields' => $db->fields($model, null, array_keys($this->fields)),
  98. 'table' => $db->fullTableName($model),
  99. 'alias' => $model->alias,
  100. 'conditions' => array(),
  101. 'order' => null,
  102. 'limit' => null,
  103. 'group' => null
  104. );
  105. $records = $db->fetchAll($db->buildStatement($query, $model), false, $model->alias);
  106. if ($records !== false && !empty($records)) {
  107. $this->records = Set::extract($records, '{n}.' . $model->alias);
  108. }
  109. }
  110. }
  111. if (!isset($this->table)) {
  112. $this->table = Inflector::underscore(Inflector::pluralize($this->name));
  113. }
  114. if (!isset($this->primaryKey) && isset($this->fields['id'])) {
  115. $this->primaryKey = 'id';
  116. }
  117. }
  118. /**
  119. * Run before all tests execute, should return SQL statement to create table for this fixture could be executed successfully.
  120. *
  121. * @param object $db An instance of the database object used to create the fixture table
  122. * @return boolean True on success, false on failure
  123. */
  124. public function create($db) {
  125. if (!isset($this->fields) || empty($this->fields)) {
  126. return false;
  127. }
  128. if (empty($this->fields['tableParameters']['engine'])) {
  129. $canUseMemory = true;
  130. foreach ($this->fields as $field => $args) {
  131. if (is_string($args)) {
  132. $type = $args;
  133. } elseif (!empty($args['type'])) {
  134. $type = $args['type'];
  135. } else {
  136. continue;
  137. }
  138. if (in_array($type, array('blob', 'text', 'binary'))) {
  139. $canUseMemory = false;
  140. break;
  141. }
  142. }
  143. if ($canUseMemory) {
  144. $this->fields['tableParameters']['engine'] = 'MEMORY';
  145. }
  146. }
  147. $this->Schema->build(array($this->table => $this->fields));
  148. try {
  149. $db->execute($db->createSchema($this->Schema), array('log' => false));
  150. } catch (Exception $e) {
  151. return false;
  152. }
  153. return true;
  154. }
  155. /**
  156. * Run after all tests executed, should return SQL statement to drop table for this fixture.
  157. *
  158. * @param object $db An instance of the database object used to create the fixture table
  159. * @return boolean True on success, false on failure
  160. */
  161. public function drop($db) {
  162. if (empty($this->fields)) {
  163. return false;
  164. }
  165. $this->Schema->build(array($this->table => $this->fields));
  166. try {
  167. $db->execute($db->dropSchema($this->Schema), array('log' => false));
  168. } catch (Exception $e) {
  169. return false;
  170. }
  171. return true;
  172. }
  173. /**
  174. * Run before each tests is executed, should return a set of SQL statements to insert records for the table
  175. * of this fixture could be executed successfully.
  176. *
  177. * @param object $db An instance of the database into which the records will be inserted
  178. * @return boolean on success or if there are no records to insert, or false on failure
  179. */
  180. public function insert($db) {
  181. if (!isset($this->_insert)) {
  182. $values = array();
  183. if (isset($this->records) && !empty($this->records)) {
  184. $fields = array();
  185. foreach ($this->records as $record) {
  186. $fields = array_merge($fields, array_keys(array_intersect_key($record, $this->fields)));
  187. }
  188. $fields = array_unique($fields);
  189. $default = array_fill_keys($fields, null);
  190. foreach ($this->records as $record) {
  191. $fields = array_keys($record);
  192. $values[] = array_values(array_merge($default, $record));
  193. }
  194. return $db->insertMulti($this->table, $fields, $values);
  195. }
  196. return true;
  197. }
  198. }
  199. /**
  200. * Truncates the current fixture. Can be overwritten by classes extending CakeFixture to trigger other events before / after
  201. * truncate.
  202. *
  203. * @param object $db A reference to a db instance
  204. * @return boolean
  205. */
  206. public function truncate($db) {
  207. $fullDebug = $db->fullDebug;
  208. $db->fullDebug = false;
  209. $return = $db->truncate($this->table);
  210. $db->fullDebug = $fullDebug;
  211. return $return;
  212. }
  213. }