PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/tests/lib/cake_test_fixture.php

https://github.com/ata/steak
PHP | 193 lines | 96 code | 9 blank | 88 comment | 19 complexity | 2b78d9577c364e702eae1cfb22104c93 MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * Short description for file.
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.cake.tests.libs
  21. * @since CakePHP(tm) v 1.2.0.4667
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. /**
  28. * Short description for class.
  29. *
  30. * @package cake
  31. * @subpackage cake.cake.tests.lib
  32. */
  33. class CakeTestFixture extends Object {
  34. /**
  35. * Name of the object
  36. *
  37. * @var string
  38. **/
  39. var $name = null;
  40. /**
  41. * Cake's DBO driver (e.g: DboMysql).
  42. *
  43. * @access public
  44. */
  45. var $db = null;
  46. /**
  47. * Full Table Name
  48. *
  49. * @access public
  50. */
  51. var $table = null;
  52. /**
  53. * Instantiate the fixture.
  54. *
  55. * @access public
  56. */
  57. function __construct() {
  58. App::import('Model', 'Schema');
  59. $this->Schema = new CakeSchema(array('name' => 'TestSuite', 'connection' => 'test_suite'));
  60. $this->init();
  61. }
  62. /**
  63. * Initialize the fixture.
  64. *
  65. * @param object Cake's DBO driver (e.g: DboMysql).
  66. * @access public
  67. *
  68. */
  69. function init() {
  70. if (isset($this->import) && (is_string($this->import) || is_array($this->import))) {
  71. $import = array_merge(array('connection' => 'default', 'records' => false), is_array($this->import) ? $this->import : array('model' => $this->import));
  72. if (isset($import['model']) && App::import('Model', $import['model'])) {
  73. ClassRegistry::config(array('ds' => $import['connection']));
  74. $model =& ClassRegistry::init($import['model']);
  75. $db =& ConnectionManager::getDataSource($model->useDbConfig);
  76. $db->cacheSources = false;
  77. $this->fields = $model->schema(true);
  78. $this->fields[$model->primaryKey]['key'] = 'primary';
  79. ClassRegistry::config(array('ds' => 'test_suite'));
  80. ClassRegistry::flush();
  81. } elseif (isset($import['table'])) {
  82. $model =& new Model(null, $import['table'], $import['connection']);
  83. $db =& ConnectionManager::getDataSource($import['connection']);
  84. $db->cacheSources = false;
  85. $model->useDbConfig = $import['connection'];
  86. $model->name = Inflector::camelize(Inflector::singularize($import['table']));
  87. $model->table = $import['table'];
  88. $model->tablePrefix = $db->config['prefix'];
  89. $this->fields = $model->schema(true);
  90. }
  91. if (isset($import['records']) && $import['records'] !== false && isset($model) && isset($db)) {
  92. $this->records = array();
  93. $query = array(
  94. 'fields' => array_keys($this->fields),
  95. 'table' => $db->fullTableName($model->table),
  96. 'alias' => $model->alias,
  97. 'conditions' => array(),
  98. 'order' => null,
  99. 'limit' => null,
  100. 'group' => null
  101. );
  102. foreach ($query['fields'] as $index => $field) {
  103. $query['fields'][$index] = $db->name($query['alias']) . '.' . $db->name($field);
  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. * @access public
  124. */
  125. function create(&$db) {
  126. if (!isset($this->fields) || empty($this->fields)) {
  127. return false;
  128. }
  129. $this->Schema->_build(array($this->table => $this->fields));
  130. return (
  131. $db->execute($db->createSchema($this->Schema), array('log' => false)) !== false
  132. );
  133. }
  134. /**
  135. * Run after all tests executed, should return SQL statement to drop table for this fixture.
  136. *
  137. * @param object $db An instance of the database object used to create the fixture table
  138. * @return boolean True on success, false on failure
  139. * @access public
  140. */
  141. function drop(&$db) {
  142. $this->Schema->_build(array($this->table => $this->fields));
  143. return (
  144. $db->execute($db->dropSchema($this->Schema), array('log' => false)) !== false
  145. );
  146. }
  147. /**
  148. * Run before each tests is executed, should return a set of SQL statements to insert records for the table
  149. * of this fixture could be executed successfully.
  150. *
  151. * @param object $db An instance of the database into which the records will be inserted
  152. * @return boolean on success or if there are no records to insert, or false on failure
  153. * @access public
  154. */
  155. function insert(&$db) {
  156. if (!isset($this->_insert)) {
  157. $values = array();
  158. if (isset($this->records) && !empty($this->records)) {
  159. foreach ($this->records as $record) {
  160. $fields = array_keys($record);
  161. $values[] = '(' . implode(', ', array_map(array(&$db, 'value'), array_values($record))) . ')';
  162. }
  163. return $db->insertMulti($this->table, $fields, $values);
  164. }
  165. return true;
  166. }
  167. }
  168. /**
  169. * Truncates the current fixture. Can be overwritten by classes extending CakeFixture to trigger other events before / after
  170. * truncate.
  171. *
  172. * @param object $db A reference to a db instance
  173. * @return boolean
  174. * @access public
  175. */
  176. function truncate(&$db) {
  177. $fullDebug = $db->fullDebug;
  178. $db->fullDebug = false;
  179. $return = $db->truncate($this->table);
  180. $db->fullDebug = $fullDebug;
  181. return $return;
  182. }
  183. }
  184. ?>