PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/tests/cases/libs/model/datasources/dbo/dbo_mysqli.test.php

https://github.com/hardsshah/bookmarks
PHP | 301 lines | 150 code | 14 blank | 137 comment | 3 complexity | 732cc2e48d4ed14fd0696e3e9ac641f2 MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * DboMysqliTest file
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
  9. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  10. *
  11. * Licensed under The MIT License
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @filesource
  15. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  16. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  17. * @package cake
  18. * @subpackage cake.cake.libs
  19. * @since CakePHP(tm) v 1.2.0
  20. * @version $Revision$
  21. * @modifiedby $LastChangedBy$
  22. * @lastmodified $Date$
  23. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  24. */
  25. if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
  26. define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
  27. }
  28. App::import('Core', array('Model', 'DataSource', 'DboSource', 'DboMysqli'));
  29. /**
  30. * DboMysqliTestDb class
  31. *
  32. * @package cake
  33. * @subpackage cake.tests.cases.libs.model.datasources
  34. */
  35. class DboMysqliTestDb extends DboMysqli {
  36. /**
  37. * simulated property
  38. *
  39. * @var array
  40. * @access public
  41. */
  42. var $simulated = array();
  43. /**
  44. * testing property
  45. *
  46. * @var bool true
  47. * @access public
  48. */
  49. var $testing = true;
  50. /**
  51. * execute method
  52. *
  53. * @param mixed $sql
  54. * @access protected
  55. * @return void
  56. */
  57. function _execute($sql) {
  58. if ($this->testing) {
  59. $this->simulated[] = $sql;
  60. return null;
  61. }
  62. return parent::_execute($sql);
  63. }
  64. /**
  65. * getLastQuery method
  66. *
  67. * @access public
  68. * @return void
  69. */
  70. function getLastQuery() {
  71. return $this->simulated[count($this->simulated) - 1];
  72. }
  73. }
  74. /**
  75. * MysqliTestModel class
  76. *
  77. * @package cake
  78. * @subpackage cake.tests.cases.libs.model.datasources
  79. */
  80. class MysqliTestModel extends Model {
  81. /**
  82. * name property
  83. *
  84. * @var string 'MysqlTestModel'
  85. * @access public
  86. */
  87. var $name = 'MysqliTestModel';
  88. /**
  89. * useTable property
  90. *
  91. * @var bool false
  92. * @access public
  93. */
  94. var $useTable = false;
  95. /**
  96. * find method
  97. *
  98. * @param mixed $conditions
  99. * @param mixed $fields
  100. * @param mixed $order
  101. * @param mixed $recursive
  102. * @access public
  103. * @return void
  104. */
  105. function find($conditions = null, $fields = null, $order = null, $recursive = null) {
  106. return $conditions;
  107. }
  108. /**
  109. * findAll method
  110. *
  111. * @param mixed $conditions
  112. * @param mixed $fields
  113. * @param mixed $order
  114. * @param mixed $recursive
  115. * @access public
  116. * @return void
  117. */
  118. function findAll($conditions = null, $fields = null, $order = null, $recursive = null) {
  119. return $conditions;
  120. }
  121. /**
  122. * schema method
  123. *
  124. * @access public
  125. * @return void
  126. */
  127. function schema() {
  128. return array(
  129. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  130. 'client_id' => array('type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11'),
  131. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  132. 'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  133. 'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
  134. 'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
  135. 'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'),
  136. 'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  137. 'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  138. 'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  139. 'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  140. 'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  141. 'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
  142. 'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  143. 'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => ''),
  144. 'last_login'=> array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
  145. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  146. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  147. );
  148. }
  149. }
  150. /**
  151. * DboMysqliTest class
  152. *
  153. * @package cake
  154. * @subpackage cake.tests.cases.libs.model.datasources.dbo
  155. */
  156. class DboMysqliTest extends CakeTestCase {
  157. /**
  158. * The Dbo instance to be tested
  159. *
  160. * @var DboSource
  161. * @access public
  162. */
  163. var $Db = null;
  164. /**
  165. * Skip if cannot connect to mysqli
  166. *
  167. * @access public
  168. */
  169. function skip() {
  170. $this->_initDb();
  171. $this->skipif($this->db->config['driver'] != 'mysqli', 'MySQLi connection not available');
  172. }
  173. /**
  174. * Sets up a Dbo class instance for testing
  175. *
  176. * @access public
  177. */
  178. function setUp() {
  179. $db = ConnectionManager::getDataSource('test_suite');
  180. $this->db = new DboMysqliTestDb($db->config);
  181. $this->model = new MysqliTestModel();
  182. }
  183. /**
  184. * Sets up a Dbo class instance for testing
  185. *
  186. * @access public
  187. */
  188. function tearDown() {
  189. unset($this->db);
  190. }
  191. /**
  192. * testIndexDetection method
  193. *
  194. * @return void
  195. * @access public
  196. */
  197. function testIndexDetection() {
  198. $this->db->cacheSources = $this->db->testing = false;
  199. $name = $this->db->fullTableName('simple');
  200. $this->db->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id));');
  201. $expected = array('PRIMARY' => array('column' => 'id', 'unique' => 1));
  202. $result = $this->db->index($name, false);
  203. $this->assertEqual($expected, $result);
  204. $this->db->query('DROP TABLE ' . $name);
  205. $name = $this->db->fullTableName('with_a_key');
  206. $this->db->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ));');
  207. $expected = array(
  208. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  209. 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
  210. );
  211. $result = $this->db->index($name, false);
  212. $this->assertEqual($expected, $result);
  213. $this->db->query('DROP TABLE ' . $name);
  214. $name = $this->db->fullTableName('with_two_keys');
  215. $this->db->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ));');
  216. $expected = array(
  217. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  218. 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
  219. 'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
  220. );
  221. $result = $this->db->index($name, false);
  222. $this->assertEqual($expected, $result);
  223. $this->db->query('DROP TABLE ' . $name);
  224. $name = $this->db->fullTableName('with_compound_keys');
  225. $this->db->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ));');
  226. $expected = array(
  227. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  228. 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
  229. 'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
  230. 'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0),
  231. );
  232. $result = $this->db->index($name, false);
  233. $this->assertEqual($expected, $result);
  234. $this->db->query('DROP TABLE ' . $name);
  235. $name = $this->db->fullTableName('with_multiple_compound_keys');
  236. $this->db->query('CREATE TABLE ' . $name . ' (id int(11) AUTO_INCREMENT, bool tinyint(1), small_int tinyint(2), primary key(id), KEY `pointless_bool` ( `bool` ), KEY `pointless_small_int` ( `small_int` ), KEY `one_way` ( `bool`, `small_int` ), KEY `other_way` ( `small_int`, `bool` ));');
  237. $expected = array(
  238. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  239. 'pointless_bool' => array('column' => 'bool', 'unique' => 0),
  240. 'pointless_small_int' => array('column' => 'small_int', 'unique' => 0),
  241. 'one_way' => array('column' => array('bool', 'small_int'), 'unique' => 0),
  242. 'other_way' => array('column' => array('small_int', 'bool'), 'unique' => 0),
  243. );
  244. $result = $this->db->index($name, false);
  245. $this->assertEqual($expected, $result);
  246. $this->db->query('DROP TABLE ' . $name);
  247. }
  248. /**
  249. * testColumn method
  250. *
  251. * @return void
  252. * @access public
  253. */
  254. function testColumn() {
  255. $result = $this->db->column('varchar(50)');
  256. $expected = 'string';
  257. $this->assertEqual($result, $expected);
  258. $result = $this->db->column('text');
  259. $expected = 'text';
  260. $this->assertEqual($result, $expected);
  261. $result = $this->db->column('int(11)');
  262. $expected = 'integer';
  263. $this->assertEqual($result, $expected);
  264. $result = $this->db->column('int(11) unsigned');
  265. $expected = 'integer';
  266. $this->assertEqual($result, $expected);
  267. $result = $this->db->column('tinyint(1)');
  268. $expected = 'boolean';
  269. $this->assertEqual($result, $expected);
  270. $result = $this->db->column('boolean');
  271. $expected = 'boolean';
  272. $this->assertEqual($result, $expected);
  273. $result = $this->db->column('float');
  274. $expected = 'float';
  275. $this->assertEqual($result, $expected);
  276. $result = $this->db->column('float unsigned');
  277. $expected = 'float';
  278. $this->assertEqual($result, $expected);
  279. $result = $this->db->column('double unsigned');
  280. $expected = 'float';
  281. $this->assertEqual($result, $expected);
  282. $result = $this->db->column('decimal(14,7) unsigned');
  283. $expected = 'float';
  284. $this->assertEqual($result, $expected);
  285. }
  286. }
  287. ?>