PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/hardsshah/bookmarks
PHP | 361 lines | 168 code | 5 blank | 188 comment | 3 complexity | 3c951ef3d47b5653dd3b96ed3e3ce0b0 MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * DboMssqlTest 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. require_once LIBS.'model'.DS.'model.php';
  29. require_once LIBS.'model'.DS.'datasources'.DS.'datasource.php';
  30. require_once LIBS.'model'.DS.'datasources'.DS.'dbo_source.php';
  31. require_once LIBS.'model'.DS.'datasources'.DS.'dbo'.DS.'dbo_mssql.php';
  32. /**
  33. * DboMssqlTestDb class
  34. *
  35. * @package cake
  36. * @subpackage cake.tests.cases.libs.model.datasources.dbo
  37. */
  38. class DboMssqlTestDb extends DboMssql {
  39. /**
  40. * Contructor
  41. *
  42. * @return void
  43. * @access public
  44. */
  45. function __construct() {
  46. }
  47. /**
  48. * connect method
  49. *
  50. * @return boolean
  51. * @access public
  52. */
  53. function connect() {
  54. $this->connected = true;
  55. return true;
  56. }
  57. /**
  58. * lastError method
  59. *
  60. * @return void
  61. * @access public
  62. */
  63. function lastError() {
  64. }
  65. /**
  66. * simulated property
  67. *
  68. * @var array
  69. * @access public
  70. */
  71. var $simulated = array();
  72. /**
  73. * fetchAllResultsStack
  74. *
  75. * @var array
  76. * @access public
  77. */
  78. var $fetchAllResultsStack = array();
  79. /**
  80. * execute method
  81. *
  82. * @param mixed $sql
  83. * @access protected
  84. * @return void
  85. */
  86. function _execute($sql) {
  87. $this->simulated[] = $sql;
  88. return null;
  89. }
  90. /**
  91. * fetchAll method
  92. *
  93. * @param mixed $sql
  94. * @access protected
  95. * @return void
  96. */
  97. function _matchRecords(&$model, $conditions = null) {
  98. return $this->conditions(array('id' => array(1, 2)));
  99. }
  100. /**
  101. * fetchAll method
  102. *
  103. * @param mixed $sql
  104. * @access protected
  105. * @return void
  106. */
  107. function fetchAll($sql, $cache = true, $modelName = null) {
  108. $result = parent::fetchAll($sql, $cache, $modelName);
  109. if (!empty($this->fetchAllResultsStack)) {
  110. return array_pop($this->fetchAllResultsStack);
  111. }
  112. return $result;
  113. }
  114. /**
  115. * getLastQuery method
  116. *
  117. * @access public
  118. * @return void
  119. */
  120. function getLastQuery() {
  121. return $this->simulated[count($this->simulated) - 1];
  122. }
  123. }
  124. /**
  125. * MssqlTestModel class
  126. *
  127. * @package cake
  128. * @subpackage cake.tests.cases.libs.model.datasources
  129. */
  130. class MssqlTestModel extends Model {
  131. /**
  132. * name property
  133. *
  134. * @var string 'MssqlTestModel'
  135. * @access public
  136. */
  137. var $name = 'MssqlTestModel';
  138. /**
  139. * useTable property
  140. *
  141. * @var bool false
  142. * @access public
  143. */
  144. var $useTable = false;
  145. /**
  146. * find method
  147. *
  148. * @param mixed $conditions
  149. * @param mixed $fields
  150. * @param mixed $order
  151. * @param mixed $recursive
  152. * @access public
  153. * @return void
  154. */
  155. function find($conditions = null, $fields = null, $order = null, $recursive = null) {
  156. return $conditions;
  157. }
  158. /**
  159. * findAll method
  160. *
  161. * @param mixed $conditions
  162. * @param mixed $fields
  163. * @param mixed $order
  164. * @param mixed $recursive
  165. * @access public
  166. * @return void
  167. */
  168. function findAll($conditions = null, $fields = null, $order = null, $recursive = null) {
  169. return $conditions;
  170. }
  171. /**
  172. * schema method
  173. *
  174. * @access public
  175. * @return void
  176. */
  177. function schema() {
  178. $this->_schema = array(
  179. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  180. 'client_id' => array('type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11'),
  181. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  182. 'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  183. 'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
  184. 'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
  185. 'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'),
  186. 'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  187. 'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  188. 'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  189. 'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  190. 'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  191. 'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
  192. 'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  193. 'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => ''),
  194. 'last_login'=> array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
  195. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  196. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  197. );
  198. return $this->_schema;
  199. }
  200. }
  201. /**
  202. * DboMssqlTest class
  203. *
  204. * @package cake
  205. * @subpackage cake.tests.cases.libs.model.datasources.dbo
  206. */
  207. class DboMssqlTest extends CakeTestCase {
  208. /**
  209. * The Dbo instance to be tested
  210. *
  211. * @var DboSource
  212. * @access public
  213. */
  214. var $db = null;
  215. /**
  216. * Skip if cannot connect to mssql
  217. *
  218. * @access public
  219. */
  220. function skip() {
  221. $this->_initDb();
  222. $this->skipif ($this->db->config['driver'] != 'mssql', 'SQL Server connection not available');
  223. }
  224. /**
  225. * Sets up a Dbo class instance for testing
  226. *
  227. * @access public
  228. */
  229. function setUp() {
  230. $db = ConnectionManager::getDataSource('test_suite');
  231. $this->db = new DboMssqlTestDb($db->config);
  232. $this->model = new MssqlTestModel();
  233. }
  234. /**
  235. * tearDown method
  236. *
  237. * @access public
  238. * @return void
  239. */
  240. function tearDown() {
  241. unset($this->model);
  242. }
  243. /**
  244. * testQuoting method
  245. *
  246. * @access public
  247. * @return void
  248. */
  249. function testQuoting() {
  250. $result = $this->db->fields($this->model);
  251. $expected = array(
  252. '[MssqlTestModel].[id] AS [MssqlTestModel__0]',
  253. '[MssqlTestModel].[client_id] AS [MssqlTestModel__1]',
  254. '[MssqlTestModel].[name] AS [MssqlTestModel__2]',
  255. '[MssqlTestModel].[login] AS [MssqlTestModel__3]',
  256. '[MssqlTestModel].[passwd] AS [MssqlTestModel__4]',
  257. '[MssqlTestModel].[addr_1] AS [MssqlTestModel__5]',
  258. '[MssqlTestModel].[addr_2] AS [MssqlTestModel__6]',
  259. '[MssqlTestModel].[zip_code] AS [MssqlTestModel__7]',
  260. '[MssqlTestModel].[city] AS [MssqlTestModel__8]',
  261. '[MssqlTestModel].[country] AS [MssqlTestModel__9]',
  262. '[MssqlTestModel].[phone] AS [MssqlTestModel__10]',
  263. '[MssqlTestModel].[fax] AS [MssqlTestModel__11]',
  264. '[MssqlTestModel].[url] AS [MssqlTestModel__12]',
  265. '[MssqlTestModel].[email] AS [MssqlTestModel__13]',
  266. '[MssqlTestModel].[comments] AS [MssqlTestModel__14]',
  267. 'CONVERT(VARCHAR(20), [MssqlTestModel].[last_login], 20) AS [MssqlTestModel__15]',
  268. '[MssqlTestModel].[created] AS [MssqlTestModel__16]',
  269. 'CONVERT(VARCHAR(20), [MssqlTestModel].[updated], 20) AS [MssqlTestModel__17]'
  270. );
  271. $this->assertEqual($result, $expected);
  272. $expected = "1.2";
  273. $result = $this->db->value(1.2, 'float');
  274. $this->assertIdentical($expected, $result);
  275. $expected = "'1,2'";
  276. $result = $this->db->value('1,2', 'float');
  277. $this->assertIdentical($expected, $result);
  278. }
  279. /**
  280. * testDistinctFields method
  281. *
  282. * @access public
  283. * @return void
  284. */
  285. function testDistinctFields() {
  286. $result = $this->db->fields($this->model, null, array('DISTINCT Car.country_code'));
  287. $expected = array('DISTINCT [Car].[country_code] AS [Car__0]');
  288. $this->assertEqual($result, $expected);
  289. $result = $this->db->fields($this->model, null, 'DISTINCT Car.country_code');
  290. $expected = array('DISTINCT [Car].[country_code] AS [Car__1]');
  291. $this->assertEqual($result, $expected);
  292. }
  293. /**
  294. * testDistinctWithLimit method
  295. *
  296. * @access public
  297. * @return void
  298. */
  299. function testDistinctWithLimit() {
  300. $this->db->read($this->model, array(
  301. 'fields' => array('DISTINCT MssqlTestModel.city', 'MssqlTestModel.country'),
  302. 'limit' => 5
  303. ));
  304. $result = $this->db->getLastQuery();
  305. $this->assertPattern('/^SELECT DISTINCT TOP 5/', $result);
  306. }
  307. /**
  308. * testDescribe method
  309. *
  310. * @access public
  311. * @return void
  312. */
  313. function testDescribe() {
  314. $MssqlTableDescription = array(
  315. 0 => array(
  316. 0 => array(
  317. 'Default' => '((0))',
  318. 'Field' => 'count',
  319. 'Key' => 0,
  320. 'Length' => '4',
  321. 'Null' => 'NO',
  322. 'Type' => 'integer',
  323. )
  324. )
  325. );
  326. $this->db->fetchAllResultsStack = array($MssqlTableDescription);
  327. $dummyModel = $this->model;
  328. $result = $this->db->describe($dummyModel);
  329. $expected = array(
  330. 'count' => array(
  331. 'type' => 'integer',
  332. 'null' => false,
  333. 'default' => '0',
  334. 'length' => 4
  335. )
  336. );
  337. $this->assertEqual($result, $expected);
  338. }
  339. /**
  340. * testUpdateAllSyntax method
  341. *
  342. * @return void
  343. * @access public
  344. */
  345. function testUpdateAllSyntax() {
  346. $model = ClassRegistry::init('MssqlTestModel');
  347. $fields = array('MssqlTestModel.client_id' => '[MssqlTestModel].[client_id] + 1');
  348. $conditions = array('MssqlTestModel.updated <' => date('2009-01-01 00:00:00'));
  349. $this->db->update($model, $fields, null, $conditions);
  350. $result = $this->db->getLastQuery();
  351. $this->assertNoPattern('/MssqlTestModel/', $result);
  352. $this->assertPattern('/^UPDATE \[mssql_test_models\]/', $result);
  353. $this->assertPattern('/SET \[client_id\] = \[client_id\] \+ 1/', $result);
  354. }
  355. }
  356. ?>