PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/lib/cake_test_fixture.php

https://github.com/hardsshah/bookmarks
PHP | 188 lines | 95 code | 10 blank | 83 comment | 19 complexity | c8c0d6b87c0e5516bec5481b210bd358 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. * Cake's DBO driver (e.g: DboMysql).
  36. *
  37. * @access public
  38. */
  39. var $db = null;
  40. /**
  41. * Full Table Name
  42. *
  43. * @access public
  44. */
  45. var $table = null;
  46. /**
  47. * Instantiate the fixture.
  48. *
  49. * @access public
  50. */
  51. function __construct() {
  52. App::import('Model', 'Schema');
  53. $this->Schema = new CakeSchema(array('name' => 'TestSuite', 'connection' => 'test_suite'));
  54. $this->init();
  55. }
  56. /**
  57. * Initialize the fixture.
  58. *
  59. * @param object Cake's DBO driver (e.g: DboMysql).
  60. * @access public
  61. *
  62. */
  63. function init() {
  64. if (isset($this->import) && (is_string($this->import) || is_array($this->import))) {
  65. $import = array_merge(array('connection' => 'default', 'records' => false), is_array($this->import) ? $this->import : array('model' => $this->import));
  66. if (isset($import['model']) && App::import('Model', $import['model'])) {
  67. ClassRegistry::config(array('ds' => $import['connection']));
  68. $model =& ClassRegistry::init($import['model']);
  69. $db =& ConnectionManager::getDataSource($model->useDbConfig);
  70. $db->cacheSources = false;
  71. $this->fields = $model->schema(true);
  72. $this->fields[$model->primaryKey]['key'] = 'primary';
  73. ClassRegistry::config(array('ds' => 'test_suite'));
  74. ClassRegistry::flush();
  75. } elseif (isset($import['table'])) {
  76. $model =& new Model(null, $import['table'], $import['connection']);
  77. $db =& ConnectionManager::getDataSource($import['connection']);
  78. $db->cacheSources = false;
  79. $model->useDbConfig = $import['connection'];
  80. $model->name = Inflector::camelize(Inflector::singularize($import['table']));
  81. $model->table = $import['table'];
  82. $model->tablePrefix = $db->config['prefix'];
  83. $this->fields = $model->schema(true);
  84. }
  85. if (isset($import['records']) && $import['records'] !== false && isset($model) && isset($db)) {
  86. $this->records = array();
  87. $query = array(
  88. 'fields' => array_keys($this->fields),
  89. 'table' => $db->fullTableName($model->table),
  90. 'alias' => $model->alias,
  91. 'conditions' => array(),
  92. 'order' => null,
  93. 'limit' => null,
  94. 'group' => null
  95. );
  96. foreach ($query['fields'] as $index => $field) {
  97. $query['fields'][$index] = $db->name($query['alias']) . '.' . $db->name($field);
  98. }
  99. $records = $db->fetchAll($db->buildStatement($query, $model), false, $model->alias);
  100. if ($records !== false && !empty($records)) {
  101. $this->records = Set::extract($records, '{n}.' . $model->alias);
  102. }
  103. }
  104. }
  105. if (!isset($this->table)) {
  106. $this->table = Inflector::underscore(Inflector::pluralize($this->name));
  107. }
  108. if (!isset($this->primaryKey) && isset($this->fields['id'])) {
  109. $this->primaryKey = 'id';
  110. }
  111. }
  112. /**
  113. * Run before all tests execute, should return SQL statement to create table for this fixture could be executed successfully.
  114. *
  115. * @param object $db An instance of the database object used to create the fixture table
  116. * @return boolean True on success, false on failure
  117. * @access public
  118. */
  119. function create(&$db) {
  120. if (!isset($this->fields) || empty($this->fields)) {
  121. return false;
  122. }
  123. $this->Schema->_build(array($this->table => $this->fields));
  124. return (
  125. $db->execute($db->createSchema($this->Schema), array('log' => false)) !== false
  126. );
  127. }
  128. /**
  129. * Run after all tests executed, should return SQL statement to drop table for this fixture.
  130. *
  131. * @param object $db An instance of the database object used to create the fixture table
  132. * @return boolean True on success, false on failure
  133. * @access public
  134. */
  135. function drop(&$db) {
  136. $this->Schema->_build(array($this->table => $this->fields));
  137. return (
  138. $db->execute($db->dropSchema($this->Schema), array('log' => false)) !== false
  139. );
  140. }
  141. /**
  142. * Run before each tests is executed, should return a set of SQL statements to insert records for the table
  143. * of this fixture could be executed successfully.
  144. *
  145. * @param object $db An instance of the database into which the records will be inserted
  146. * @return boolean on success or if there are no records to insert, or false on failure
  147. * @access public
  148. */
  149. function insert(&$db) {
  150. if (!isset($this->_insert)) {
  151. $values = array();
  152. if (isset($this->records) && !empty($this->records)) {
  153. foreach ($this->records as $record) {
  154. $fields = array_keys($record);
  155. $values[] = '(' . implode(', ', array_map(array(&$db, 'value'), array_values($record))) . ')';
  156. }
  157. return $db->insertMulti($this->table, $fields, $values);
  158. }
  159. return true;
  160. }
  161. }
  162. /**
  163. * Truncates the current fixture. Can be overwritten by classes extending CakeFixture to trigger other events before / after
  164. * truncate.
  165. *
  166. * @param object $db A reference to a db instance
  167. * @return boolean
  168. * @access public
  169. */
  170. function truncate(&$db) {
  171. $fullDebug = $db->fullDebug;
  172. $db->fullDebug = false;
  173. $return = $db->truncate($this->table);
  174. $db->fullDebug = $fullDebug;
  175. return $return;
  176. }
  177. }
  178. ?>