PageRenderTime 33ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

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