SqlserverTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. <?php
  2. /**
  3. * SqlserverTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake.libs
  16. * @since CakePHP(tm) v 1.2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Model', 'Model');
  20. App::uses('Sqlserver', 'Model/Datasource/Database');
  21. /**
  22. * SqlserverTestDb class
  23. *
  24. * @package cake.tests.cases.libs.model.datasources.dbo
  25. */
  26. class SqlserverTestDb extends Sqlserver {
  27. /**
  28. * simulated property
  29. *
  30. * @var array
  31. */
  32. public $simulated = array();
  33. /**
  34. * execute results stack
  35. *
  36. * @var array
  37. */
  38. public $executeResultsStack = array();
  39. /**
  40. * execute method
  41. *
  42. * @param mixed $sql
  43. * @return mixed
  44. */
  45. protected function _execute($sql) {
  46. $this->simulated[] = $sql;
  47. return empty($this->executeResultsStack) ? null : array_pop($this->executeResultsStack);
  48. }
  49. /**
  50. * fetchAll method
  51. *
  52. * @param mixed $sql
  53. * @return void
  54. */
  55. protected function _matchRecords($model, $conditions = null) {
  56. return $this->conditions(array('id' => array(1, 2)));
  57. }
  58. /**
  59. * getLastQuery method
  60. *
  61. * @return string
  62. */
  63. public function getLastQuery() {
  64. return $this->simulated[count($this->simulated) - 1];
  65. }
  66. /**
  67. * getPrimaryKey method
  68. *
  69. * @param mixed $model
  70. * @return string
  71. */
  72. public function getPrimaryKey($model) {
  73. return parent::_getPrimaryKey($model);
  74. }
  75. /**
  76. * clearFieldMappings method
  77. *
  78. * @return void
  79. */
  80. public function clearFieldMappings() {
  81. $this->_fieldMappings = array();
  82. }
  83. /**
  84. * describe method
  85. *
  86. * @param object $model
  87. * @return void
  88. */
  89. public function describe($model) {
  90. return empty($this->describe) ? parent::describe($model) : $this->describe;
  91. }
  92. }
  93. /**
  94. * SqlserverTestModel class
  95. *
  96. * @package cake.tests.cases.libs.model.datasources
  97. */
  98. class SqlserverTestModel extends Model {
  99. /**
  100. * name property
  101. *
  102. * @var string 'SqlserverTestModel'
  103. */
  104. public $name = 'SqlserverTestModel';
  105. /**
  106. * useTable property
  107. *
  108. * @var bool false
  109. */
  110. public $useTable = false;
  111. /**
  112. * _schema property
  113. *
  114. * @var array
  115. */
  116. protected $_schema = array(
  117. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8', 'key' => 'primary'),
  118. 'client_id' => array('type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11'),
  119. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  120. 'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  121. 'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
  122. 'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
  123. 'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'),
  124. 'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  125. 'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  126. 'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  127. 'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  128. 'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  129. 'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
  130. 'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  131. 'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => ''),
  132. 'last_login'=> array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
  133. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  134. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  135. );
  136. /**
  137. * belongsTo property
  138. *
  139. * @var array
  140. */
  141. public $belongsTo = array(
  142. 'SqlserverClientTestModel' => array(
  143. 'foreignKey' => 'client_id'
  144. )
  145. );
  146. /**
  147. * find method
  148. *
  149. * @param mixed $conditions
  150. * @param mixed $fields
  151. * @param mixed $order
  152. * @param mixed $recursive
  153. * @return void
  154. */
  155. public function find($conditions = null, $fields = null, $order = null, $recursive = null) {
  156. return $conditions;
  157. }
  158. }
  159. /**
  160. * SqlserverClientTestModel class
  161. *
  162. * @package cake.tests.cases.libs.model.datasources
  163. */
  164. class SqlserverClientTestModel extends Model {
  165. /**
  166. * name property
  167. *
  168. * @var string 'SqlserverAssociatedTestModel'
  169. */
  170. public $name = 'SqlserverClientTestModel';
  171. /**
  172. * useTable property
  173. *
  174. * @var bool false
  175. */
  176. public $useTable = false;
  177. /**
  178. * _schema property
  179. *
  180. * @var array
  181. */
  182. protected $_schema = array(
  183. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8', 'key' => 'primary'),
  184. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  185. 'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  186. 'created' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
  187. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  188. );
  189. }
  190. /**
  191. * SqlserverTestResultIterator class
  192. *
  193. * @package cake.tests.cases.libs.model.datasources
  194. */
  195. class SqlserverTestResultIterator extends ArrayIterator {
  196. /**
  197. * closeCursor method
  198. *
  199. * @return void
  200. */
  201. public function closeCursor() {}
  202. }
  203. /**
  204. * SqlserverTest class
  205. *
  206. * @package cake.tests.cases.libs.model.datasources.dbo
  207. */
  208. class SqlserverTest extends CakeTestCase {
  209. /**
  210. * The Dbo instance to be tested
  211. *
  212. * @var DboSource
  213. */
  214. public $db = null;
  215. /**
  216. * autoFixtures property
  217. *
  218. * @var bool false
  219. */
  220. public $autoFixtures = false;
  221. /**
  222. * fixtures property
  223. *
  224. * @var array
  225. */
  226. public $fixtures = array('core.category', 'core.post');
  227. /**
  228. * Sets up a Dbo class instance for testing
  229. *
  230. */
  231. public function setUp() {
  232. $this->Dbo = ConnectionManager::getDataSource('test');
  233. if (!($this->Dbo instanceof Sqlserver)) {
  234. $this->markTestSkipped('Please configure the test datasource to use SQL Server.');
  235. }
  236. $this->db = new SqlserverTestDb($this->Dbo->config);
  237. $this->model = new SqlserverTestModel();
  238. }
  239. /**
  240. * tearDown method
  241. *
  242. * @return void
  243. */
  244. public function tearDown() {
  245. unset($this->Dbo);
  246. unset($this->model);
  247. }
  248. /**
  249. * testQuoting method
  250. *
  251. * @return void
  252. */
  253. public function testQuoting() {
  254. $expected = "1.200000";
  255. $result = $this->db->value(1.2, 'float');
  256. $this->assertIdentical($expected, $result);
  257. $expected = "'1,2'";
  258. $result = $this->db->value('1,2', 'float');
  259. $this->assertIdentical($expected, $result);
  260. $expected = 'NULL';
  261. $result = $this->db->value('', 'integer');
  262. $this->assertIdentical($expected, $result);
  263. $expected = 'NULL';
  264. $result = $this->db->value('', 'float');
  265. $this->assertIdentical($expected, $result);
  266. $expected = "''";
  267. $result = $this->db->value('', 'binary');
  268. $this->assertIdentical($expected, $result);
  269. }
  270. /**
  271. * testFields method
  272. *
  273. * @return void
  274. */
  275. public function testFields() {
  276. $fields = array(
  277. '[SqlserverTestModel].[id] AS [SqlserverTestModel__0]',
  278. '[SqlserverTestModel].[client_id] AS [SqlserverTestModel__1]',
  279. '[SqlserverTestModel].[name] AS [SqlserverTestModel__2]',
  280. '[SqlserverTestModel].[login] AS [SqlserverTestModel__3]',
  281. '[SqlserverTestModel].[passwd] AS [SqlserverTestModel__4]',
  282. '[SqlserverTestModel].[addr_1] AS [SqlserverTestModel__5]',
  283. '[SqlserverTestModel].[addr_2] AS [SqlserverTestModel__6]',
  284. '[SqlserverTestModel].[zip_code] AS [SqlserverTestModel__7]',
  285. '[SqlserverTestModel].[city] AS [SqlserverTestModel__8]',
  286. '[SqlserverTestModel].[country] AS [SqlserverTestModel__9]',
  287. '[SqlserverTestModel].[phone] AS [SqlserverTestModel__10]',
  288. '[SqlserverTestModel].[fax] AS [SqlserverTestModel__11]',
  289. '[SqlserverTestModel].[url] AS [SqlserverTestModel__12]',
  290. '[SqlserverTestModel].[email] AS [SqlserverTestModel__13]',
  291. '[SqlserverTestModel].[comments] AS [SqlserverTestModel__14]',
  292. 'CONVERT(VARCHAR(20), [SqlserverTestModel].[last_login], 20) AS [SqlserverTestModel__15]',
  293. '[SqlserverTestModel].[created] AS [SqlserverTestModel__16]',
  294. 'CONVERT(VARCHAR(20), [SqlserverTestModel].[updated], 20) AS [SqlserverTestModel__17]'
  295. );
  296. $result = $this->db->fields($this->model);
  297. $expected = $fields;
  298. $this->assertEqual($expected, $result);
  299. $this->db->clearFieldMappings();
  300. $result = $this->db->fields($this->model, null, 'SqlserverTestModel.*');
  301. $expected = $fields;
  302. $this->assertEqual($expected, $result);
  303. $this->db->clearFieldMappings();
  304. $result = $this->db->fields($this->model, null, array('*', 'AnotherModel.id', 'AnotherModel.name'));
  305. $expected = array_merge($fields, array(
  306. '[AnotherModel].[id] AS [AnotherModel__18]',
  307. '[AnotherModel].[name] AS [AnotherModel__19]'));
  308. $this->assertEqual($expected, $result);
  309. $this->db->clearFieldMappings();
  310. $result = $this->db->fields($this->model, null, array('*', 'SqlserverClientTestModel.*'));
  311. $expected = array_merge($fields, array(
  312. '[SqlserverClientTestModel].[id] AS [SqlserverClientTestModel__18]',
  313. '[SqlserverClientTestModel].[name] AS [SqlserverClientTestModel__19]',
  314. '[SqlserverClientTestModel].[email] AS [SqlserverClientTestModel__20]',
  315. 'CONVERT(VARCHAR(20), [SqlserverClientTestModel].[created], 20) AS [SqlserverClientTestModel__21]',
  316. 'CONVERT(VARCHAR(20), [SqlserverClientTestModel].[updated], 20) AS [SqlserverClientTestModel__22]'));
  317. $this->assertEqual($expected, $result);
  318. }
  319. /**
  320. * testDistinctFields method
  321. *
  322. * @return void
  323. */
  324. public function testDistinctFields() {
  325. $result = $this->db->fields($this->model, null, array('DISTINCT Car.country_code'));
  326. $expected = array('DISTINCT [Car].[country_code] AS [Car__0]');
  327. $this->assertEqual($expected, $result);
  328. $result = $this->db->fields($this->model, null, 'DISTINCT Car.country_code');
  329. $expected = array('DISTINCT [Car].[country_code] AS [Car__1]');
  330. $this->assertEqual($expected, $result);
  331. }
  332. /**
  333. * testDistinctWithLimit method
  334. *
  335. * @return void
  336. */
  337. public function testDistinctWithLimit() {
  338. $this->db->read($this->model, array(
  339. 'fields' => array('DISTINCT SqlserverTestModel.city', 'SqlserverTestModel.country'),
  340. 'limit' => 5
  341. ));
  342. $result = $this->db->getLastQuery();
  343. $this->assertPattern('/^SELECT DISTINCT TOP 5/', $result);
  344. }
  345. /**
  346. * testDescribe method
  347. *
  348. * @return void
  349. */
  350. public function testDescribe() {
  351. $SqlserverTableDescription = new SqlserverTestResultIterator(array(
  352. (object) array(
  353. 'Default' => '((0))',
  354. 'Field' => 'count',
  355. 'Key' => 0,
  356. 'Length' => '4',
  357. 'Null' => 'NO',
  358. 'Type' => 'integer'
  359. )
  360. ));
  361. $this->db->executeResultsStack = array($SqlserverTableDescription);
  362. $dummyModel = $this->model;
  363. $result = $this->db->describe($dummyModel);
  364. $expected = array(
  365. 'count' => array(
  366. 'type' => 'integer',
  367. 'null' => false,
  368. 'default' => '0',
  369. 'length' => 4
  370. )
  371. );
  372. $this->assertEqual($expected, $result);
  373. }
  374. /**
  375. * testBuildColumn
  376. *
  377. * @return void
  378. */
  379. public function testBuildColumn() {
  380. $column = array('name' => 'id', 'type' => 'integer', 'null' => false, 'default' => '', 'length' => '8', 'key' => 'primary');
  381. $result = $this->db->buildColumn($column);
  382. $expected = '[id] int IDENTITY (1, 1) NOT NULL';
  383. $this->assertEqual($expected, $result);
  384. $column = array('name' => 'client_id', 'type' => 'integer', 'null' => false, 'default' => '0', 'length' => '11');
  385. $result = $this->db->buildColumn($column);
  386. $expected = '[client_id] int DEFAULT 0 NOT NULL';
  387. $this->assertEqual($expected, $result);
  388. $column = array('name' => 'client_id', 'type' => 'integer', 'null' => true);
  389. $result = $this->db->buildColumn($column);
  390. $expected = '[client_id] int NULL';
  391. $this->assertEqual($expected, $result);
  392. // 'name' => 'type' format for columns
  393. $column = array('type' => 'integer', 'name' => 'client_id');
  394. $result = $this->db->buildColumn($column);
  395. $expected = '[client_id] int NULL';
  396. $this->assertEqual($expected, $result);
  397. $column = array('type' => 'string', 'name' => 'name');
  398. $result = $this->db->buildColumn($column);
  399. $expected = '[name] varchar(255) NULL';
  400. $this->assertEqual($expected, $result);
  401. $column = array('name' => 'name', 'type' => 'string', 'null' => false, 'default' => '', 'length' => '255');
  402. $result = $this->db->buildColumn($column);
  403. $expected = '[name] varchar(255) DEFAULT \'\' NOT NULL';
  404. $this->assertEqual($expected, $result);
  405. $column = array('name' => 'name', 'type' => 'string', 'null' => false, 'length' => '255');
  406. $result = $this->db->buildColumn($column);
  407. $expected = '[name] varchar(255) NOT NULL';
  408. $this->assertEqual($expected, $result);
  409. $column = array('name' => 'name', 'type' => 'string', 'null' => false, 'default' => null, 'length' => '255');
  410. $result = $this->db->buildColumn($column);
  411. $expected = '[name] varchar(255) NOT NULL';
  412. $this->assertEqual($expected, $result);
  413. $column = array('name' => 'name', 'type' => 'string', 'null' => true, 'default' => null, 'length' => '255');
  414. $result = $this->db->buildColumn($column);
  415. $expected = '[name] varchar(255) NULL';
  416. $this->assertEqual($expected, $result);
  417. $column = array('name' => 'name', 'type' => 'string', 'null' => true, 'default' => '', 'length' => '255');
  418. $result = $this->db->buildColumn($column);
  419. $expected = '[name] varchar(255) DEFAULT \'\'';
  420. $this->assertEqual($expected, $result);
  421. }
  422. /**
  423. * testBuildIndex method
  424. *
  425. * @return void
  426. */
  427. public function testBuildIndex() {
  428. $indexes = array(
  429. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  430. 'client_id' => array('column' => 'client_id', 'unique' => 1)
  431. );
  432. $result = $this->db->buildIndex($indexes, 'items');
  433. $expected = array(
  434. 'PRIMARY KEY ([id])',
  435. 'ALTER TABLE items ADD CONSTRAINT client_id UNIQUE([client_id]);'
  436. );
  437. $this->assertEqual($expected, $result);
  438. $indexes = array('client_id' => array('column' => 'client_id'));
  439. $result = $this->db->buildIndex($indexes, 'items');
  440. $this->assertEqual($result, array());
  441. $indexes = array('client_id' => array('column' => array('client_id', 'period_id'), 'unique' => 1));
  442. $result = $this->db->buildIndex($indexes, 'items');
  443. $expected = array('ALTER TABLE items ADD CONSTRAINT client_id UNIQUE([client_id], [period_id]);');
  444. $this->assertEqual($expected, $result);
  445. }
  446. /**
  447. * testUpdateAllSyntax method
  448. *
  449. * @return void
  450. */
  451. public function testUpdateAllSyntax() {
  452. $fields = array('SqlserverTestModel.client_id' => '[SqlserverTestModel].[client_id] + 1');
  453. $conditions = array('SqlserverTestModel.updated <' => date('2009-01-01 00:00:00'));
  454. $this->db->update($this->model, $fields, null, $conditions);
  455. $result = $this->db->getLastQuery();
  456. $this->assertNoPattern('/SqlserverTestModel/', $result);
  457. $this->assertPattern('/^UPDATE \[sqlserver_test_models\]/', $result);
  458. $this->assertPattern('/SET \[client_id\] = \[client_id\] \+ 1/', $result);
  459. }
  460. /**
  461. * testGetPrimaryKey method
  462. *
  463. * @return void
  464. */
  465. public function testGetPrimaryKey() {
  466. $schema = $this->model->schema();
  467. $this->db->describe = $schema;
  468. $result = $this->db->getPrimaryKey($this->model);
  469. $this->assertEqual($result, 'id');
  470. unset($schema['id']['key']);
  471. $this->db->describe = $schema;
  472. $result = $this->db->getPrimaryKey($this->model);
  473. $this->assertNull($result);
  474. }
  475. /**
  476. * testInsertMulti
  477. *
  478. * @return void
  479. */
  480. public function testInsertMulti() {
  481. $this->db->describe = $this->model->schema();
  482. $fields = array('id', 'name', 'login');
  483. $values = array(
  484. array(1, 'Larry', 'PhpNut'),
  485. array(2, 'Renan', 'renan.saddam'));
  486. $this->db->simulated = array();
  487. $this->db->insertMulti($this->model, $fields, $values);
  488. $result = $this->db->simulated;
  489. $expected = array(
  490. 'SET IDENTITY_INSERT [sqlserver_test_models] ON',
  491. "INSERT INTO [sqlserver_test_models] ([id], [name], [login]) VALUES (1, 'Larry', 'PhpNut')",
  492. "INSERT INTO [sqlserver_test_models] ([id], [name], [login]) VALUES (2, 'Renan', 'renan.saddam')",
  493. 'SET IDENTITY_INSERT [sqlserver_test_models] OFF'
  494. );
  495. $this->assertEqual($expected, $result);
  496. $fields = array('name', 'login');
  497. $values = array(
  498. array('Larry', 'PhpNut'),
  499. array('Renan', 'renan.saddam'));
  500. $this->db->simulated = array();
  501. $this->db->insertMulti($this->model, $fields, $values);
  502. $result = $this->db->simulated;
  503. $expected = array(
  504. "INSERT INTO [sqlserver_test_models] ([name], [login]) VALUES ('Larry', 'PhpNut')",
  505. "INSERT INTO [sqlserver_test_models] ([name], [login]) VALUES ('Renan', 'renan.saddam')",
  506. );
  507. $this->assertEqual($expected, $result);
  508. }
  509. /**
  510. * SQL server < 11 doesn't have proper limit/offset support, test that our hack works.
  511. *
  512. * @return void
  513. */
  514. public function testLimitOffsetHack() {
  515. $this->loadFixtures('Post');
  516. $query = array(
  517. 'limit' => 1,
  518. 'page' => 1,
  519. 'order' => 'Post.title ASC',
  520. );
  521. $Post = ClassRegistry::init('Post');
  522. $results = $Post->find('all', $query);
  523. $this->assertEquals(1, count($results));
  524. $this->assertEquals('First Post', $results[0]['Post']['title']);
  525. $query = array(
  526. 'limit' => 1,
  527. 'page' => 2,
  528. 'order' => 'Post.title ASC',
  529. );
  530. $Post = ClassRegistry::init('Post');
  531. $results = $Post->find('all', $query);
  532. $this->assertEquals(1, count($results));
  533. $this->assertFalse(isset($results[0][0]));
  534. $this->assertEquals('Second Post', $results[0]['Post']['title']);
  535. }
  536. }