SqlserverTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. <?php
  2. /**
  3. * SqlserverTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.Test.Case.Model.Datasource.Database
  17. * @since CakePHP(tm) v 1.2.0
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('Model', 'Model');
  21. App::uses('AppModel', 'Model');
  22. App::uses('Sqlserver', 'Model/Datasource/Database');
  23. require_once dirname(dirname(dirname(__FILE__))) . DS . 'models.php';
  24. /**
  25. * SqlserverTestDb class
  26. *
  27. * @package Cake.Test.Case.Model.Datasource.Database
  28. */
  29. class SqlserverTestDb extends Sqlserver {
  30. /**
  31. * simulated property
  32. *
  33. * @var array
  34. */
  35. public $simulated = array();
  36. /**
  37. * execute results stack
  38. *
  39. * @var array
  40. */
  41. public $executeResultsStack = array();
  42. /**
  43. * execute method
  44. *
  45. * @param mixed $sql
  46. * @param mixed $params
  47. * @param mixed $prepareOptions
  48. * @return mixed
  49. */
  50. protected function _execute($sql, $params = array(), $prepareOptions = array()) {
  51. $this->simulated[] = $sql;
  52. return empty($this->executeResultsStack) ? null : array_pop($this->executeResultsStack);
  53. }
  54. /**
  55. * fetchAll method
  56. *
  57. * @param mixed $sql
  58. * @return void
  59. */
  60. protected function _matchRecords(Model $model, $conditions = null) {
  61. return $this->conditions(array('id' => array(1, 2)));
  62. }
  63. /**
  64. * getLastQuery method
  65. *
  66. * @return string
  67. */
  68. public function getLastQuery() {
  69. return $this->simulated[count($this->simulated) - 1];
  70. }
  71. /**
  72. * getPrimaryKey method
  73. *
  74. * @param mixed $model
  75. * @return string
  76. */
  77. public function getPrimaryKey($model) {
  78. return parent::_getPrimaryKey($model);
  79. }
  80. /**
  81. * clearFieldMappings method
  82. *
  83. * @return void
  84. */
  85. public function clearFieldMappings() {
  86. $this->_fieldMappings = array();
  87. }
  88. /**
  89. * describe method
  90. *
  91. * @param object $model
  92. * @return void
  93. */
  94. public function describe($model) {
  95. return empty($this->describe) ? parent::describe($model) : $this->describe;
  96. }
  97. }
  98. /**
  99. * SqlserverTestModel class
  100. *
  101. * @package Cake.Test.Case.Model.Datasource.Database
  102. */
  103. class SqlserverTestModel extends CakeTestModel {
  104. /**
  105. * name property
  106. *
  107. * @var string 'SqlserverTestModel'
  108. */
  109. public $name = 'SqlserverTestModel';
  110. /**
  111. * useTable property
  112. *
  113. * @var bool false
  114. */
  115. public $useTable = false;
  116. /**
  117. * _schema property
  118. *
  119. * @var array
  120. */
  121. protected $_schema = array(
  122. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8', 'key' => 'primary'),
  123. 'client_id' => array('type' => 'integer', 'null' => '', 'default' => '0', 'length' => '11'),
  124. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  125. 'login' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  126. 'passwd' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
  127. 'addr_1' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
  128. 'addr_2' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '25'),
  129. 'zip_code' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  130. 'city' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  131. 'country' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  132. 'phone' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  133. 'fax' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  134. 'url' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '255'),
  135. 'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  136. 'comments' => array('type' => 'text', 'null' => '1', 'default' => '', 'length' => ''),
  137. 'last_login' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
  138. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  139. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  140. );
  141. /**
  142. * belongsTo property
  143. *
  144. * @var array
  145. */
  146. public $belongsTo = array(
  147. 'SqlserverClientTestModel' => array(
  148. 'foreignKey' => 'client_id'
  149. )
  150. );
  151. /**
  152. * find method
  153. *
  154. * @param mixed $conditions
  155. * @param mixed $fields
  156. * @param mixed $order
  157. * @param mixed $recursive
  158. * @return void
  159. */
  160. public function find($conditions = null, $fields = null, $order = null, $recursive = null) {
  161. return $conditions;
  162. }
  163. }
  164. /**
  165. * SqlserverClientTestModel class
  166. *
  167. * @package Cake.Test.Case.Model.Datasource.Database
  168. */
  169. class SqlserverClientTestModel extends CakeTestModel {
  170. /**
  171. * name property
  172. *
  173. * @var string 'SqlserverAssociatedTestModel'
  174. */
  175. public $name = 'SqlserverClientTestModel';
  176. /**
  177. * useTable property
  178. *
  179. * @var bool false
  180. */
  181. public $useTable = false;
  182. /**
  183. * _schema property
  184. *
  185. * @var array
  186. */
  187. protected $_schema = array(
  188. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8', 'key' => 'primary'),
  189. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  190. 'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
  191. 'created' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
  192. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  193. );
  194. }
  195. /**
  196. * SqlserverTestResultIterator class
  197. *
  198. * @package Cake.Test.Case.Model.Datasource.Database
  199. */
  200. class SqlserverTestResultIterator extends ArrayIterator {
  201. /**
  202. * closeCursor method
  203. *
  204. * @return void
  205. */
  206. public function closeCursor() {
  207. }
  208. /**
  209. * fetch method
  210. *
  211. * @return void
  212. */
  213. public function fetch() {
  214. if (!$this->valid()) {
  215. return null;
  216. }
  217. $current = $this->current();
  218. $this->next();
  219. return $current;
  220. }
  221. }
  222. /**
  223. * SqlserverTest class
  224. *
  225. * @package Cake.Test.Case.Model.Datasource.Database
  226. */
  227. class SqlserverTest extends CakeTestCase {
  228. /**
  229. * The Dbo instance to be tested
  230. *
  231. * @var DboSource
  232. */
  233. public $db = null;
  234. /**
  235. * autoFixtures property
  236. *
  237. * @var bool false
  238. */
  239. public $autoFixtures = false;
  240. /**
  241. * fixtures property
  242. *
  243. * @var array
  244. */
  245. public $fixtures = array('core.user', 'core.category', 'core.author', 'core.post');
  246. /**
  247. * Sets up a Dbo class instance for testing
  248. *
  249. */
  250. public function setUp() {
  251. $this->Dbo = ConnectionManager::getDataSource('test');
  252. if (!($this->Dbo instanceof Sqlserver)) {
  253. $this->markTestSkipped('Please configure the test datasource to use SQL Server.');
  254. }
  255. $this->db = new SqlserverTestDb($this->Dbo->config);
  256. $this->model = new SqlserverTestModel();
  257. }
  258. /**
  259. * tearDown method
  260. *
  261. * @return void
  262. */
  263. public function tearDown() {
  264. unset($this->Dbo);
  265. unset($this->model);
  266. }
  267. /**
  268. * testQuoting method
  269. *
  270. * @return void
  271. */
  272. public function testQuoting() {
  273. $expected = "1.2";
  274. $result = $this->db->value(1.2, 'float');
  275. $this->assertSame($expected, $result);
  276. $expected = "'1,2'";
  277. $result = $this->db->value('1,2', 'float');
  278. $this->assertSame($expected, $result);
  279. $expected = 'NULL';
  280. $result = $this->db->value('', 'integer');
  281. $this->assertSame($expected, $result);
  282. $expected = 'NULL';
  283. $result = $this->db->value('', 'float');
  284. $this->assertSame($expected, $result);
  285. $expected = "''";
  286. $result = $this->db->value('', 'binary');
  287. $this->assertSame($expected, $result);
  288. }
  289. /**
  290. * testFields method
  291. *
  292. * @return void
  293. */
  294. public function testFields() {
  295. $fields = array(
  296. '[SqlserverTestModel].[id] AS [SqlserverTestModel__id]',
  297. '[SqlserverTestModel].[client_id] AS [SqlserverTestModel__client_id]',
  298. '[SqlserverTestModel].[name] AS [SqlserverTestModel__name]',
  299. '[SqlserverTestModel].[login] AS [SqlserverTestModel__login]',
  300. '[SqlserverTestModel].[passwd] AS [SqlserverTestModel__passwd]',
  301. '[SqlserverTestModel].[addr_1] AS [SqlserverTestModel__addr_1]',
  302. '[SqlserverTestModel].[addr_2] AS [SqlserverTestModel__addr_2]',
  303. '[SqlserverTestModel].[zip_code] AS [SqlserverTestModel__zip_code]',
  304. '[SqlserverTestModel].[city] AS [SqlserverTestModel__city]',
  305. '[SqlserverTestModel].[country] AS [SqlserverTestModel__country]',
  306. '[SqlserverTestModel].[phone] AS [SqlserverTestModel__phone]',
  307. '[SqlserverTestModel].[fax] AS [SqlserverTestModel__fax]',
  308. '[SqlserverTestModel].[url] AS [SqlserverTestModel__url]',
  309. '[SqlserverTestModel].[email] AS [SqlserverTestModel__email]',
  310. '[SqlserverTestModel].[comments] AS [SqlserverTestModel__comments]',
  311. 'CONVERT(VARCHAR(20), [SqlserverTestModel].[last_login], 20) AS [SqlserverTestModel__last_login]',
  312. '[SqlserverTestModel].[created] AS [SqlserverTestModel__created]',
  313. 'CONVERT(VARCHAR(20), [SqlserverTestModel].[updated], 20) AS [SqlserverTestModel__updated]'
  314. );
  315. $result = $this->db->fields($this->model);
  316. $expected = $fields;
  317. $this->assertEquals($expected, $result);
  318. $this->db->clearFieldMappings();
  319. $result = $this->db->fields($this->model, null, 'SqlserverTestModel.*');
  320. $expected = $fields;
  321. $this->assertEquals($expected, $result);
  322. $this->db->clearFieldMappings();
  323. $result = $this->db->fields($this->model, null, array('*', 'AnotherModel.id', 'AnotherModel.name'));
  324. $expected = array_merge($fields, array(
  325. '[AnotherModel].[id] AS [AnotherModel__id]',
  326. '[AnotherModel].[name] AS [AnotherModel__name]'));
  327. $this->assertEquals($expected, $result);
  328. $this->db->clearFieldMappings();
  329. $result = $this->db->fields($this->model, null, array('*', 'SqlserverClientTestModel.*'));
  330. $expected = array_merge($fields, array(
  331. '[SqlserverClientTestModel].[id] AS [SqlserverClientTestModel__id]',
  332. '[SqlserverClientTestModel].[name] AS [SqlserverClientTestModel__name]',
  333. '[SqlserverClientTestModel].[email] AS [SqlserverClientTestModel__email]',
  334. 'CONVERT(VARCHAR(20), [SqlserverClientTestModel].[created], 20) AS [SqlserverClientTestModel__created]',
  335. 'CONVERT(VARCHAR(20), [SqlserverClientTestModel].[updated], 20) AS [SqlserverClientTestModel__updated]'));
  336. $this->assertEquals($expected, $result);
  337. }
  338. /**
  339. * testDistinctFields method
  340. *
  341. * @return void
  342. */
  343. public function testDistinctFields() {
  344. $result = $this->db->fields($this->model, null, array('DISTINCT Car.country_code'));
  345. $expected = array('DISTINCT [Car].[country_code] AS [Car__country_code]');
  346. $this->assertEquals($expected, $result);
  347. $result = $this->db->fields($this->model, null, 'DISTINCT Car.country_code');
  348. $expected = array('DISTINCT [Car].[country_code] AS [Car__country_code]');
  349. $this->assertEquals($expected, $result);
  350. }
  351. /**
  352. * testDistinctWithLimit method
  353. *
  354. * @return void
  355. */
  356. public function testDistinctWithLimit() {
  357. $this->db->read($this->model, array(
  358. 'fields' => array('DISTINCT SqlserverTestModel.city', 'SqlserverTestModel.country'),
  359. 'limit' => 5
  360. ));
  361. $result = $this->db->getLastQuery();
  362. $this->assertRegExp('/^SELECT DISTINCT TOP 5/', $result);
  363. }
  364. /**
  365. * testDescribe method
  366. *
  367. * @return void
  368. */
  369. public function testDescribe() {
  370. $SqlserverTableDescription = new SqlserverTestResultIterator(array(
  371. (object)array(
  372. 'Default' => '((0))',
  373. 'Field' => 'count',
  374. 'Key' => 0,
  375. 'Length' => '4',
  376. 'Null' => 'NO',
  377. 'Type' => 'integer'
  378. ),
  379. (object)array(
  380. 'Default' => '',
  381. 'Field' => 'body',
  382. 'Key' => 0,
  383. 'Length' => '-1',
  384. 'Null' => 'YES',
  385. 'Type' => 'nvarchar'
  386. ),
  387. (object)array(
  388. 'Default' => '',
  389. 'Field' => 'published',
  390. 'Key' => 0,
  391. 'Type' => 'datetime2',
  392. 'Length' => 8,
  393. 'Null' => 'YES',
  394. 'Size' => ''
  395. ),
  396. (object)array(
  397. 'Default' => '',
  398. 'Field' => 'id',
  399. 'Key' => 1,
  400. 'Type' => 'nchar',
  401. 'Length' => 72,
  402. 'Null' => 'NO',
  403. 'Size' => ''
  404. ),
  405. (object)array(
  406. 'Default' => null,
  407. 'Field' => 'parent_id',
  408. 'Key' => '0',
  409. 'Type' => 'bigint',
  410. 'Length' => 8,
  411. 'Null' => 'YES',
  412. 'Size' => '0',
  413. ),
  414. ));
  415. $this->db->executeResultsStack = array($SqlserverTableDescription);
  416. $dummyModel = $this->model;
  417. $result = $this->db->describe($dummyModel);
  418. $expected = array(
  419. 'count' => array(
  420. 'type' => 'integer',
  421. 'null' => false,
  422. 'default' => '0',
  423. 'length' => 4
  424. ),
  425. 'body' => array(
  426. 'type' => 'text',
  427. 'null' => true,
  428. 'default' => null,
  429. 'length' => null
  430. ),
  431. 'published' => array(
  432. 'type' => 'datetime',
  433. 'null' => true,
  434. 'default' => '',
  435. 'length' => null
  436. ),
  437. 'id' => array(
  438. 'type' => 'string',
  439. 'null' => false,
  440. 'default' => '',
  441. 'length' => 36,
  442. 'key' => 'primary'
  443. ),
  444. 'parent_id' => array(
  445. 'type' => 'biginteger',
  446. 'null' => true,
  447. 'default' => null,
  448. 'length' => 8,
  449. ),
  450. );
  451. $this->assertEquals($expected, $result);
  452. $this->assertSame($expected['parent_id'], $result['parent_id']);
  453. }
  454. /**
  455. * testBuildColumn
  456. *
  457. * @return void
  458. */
  459. public function testBuildColumn() {
  460. $column = array('name' => 'id', 'type' => 'integer', 'null' => false, 'default' => '', 'length' => '8', 'key' => 'primary');
  461. $result = $this->db->buildColumn($column);
  462. $expected = '[id] int IDENTITY (1, 1) NOT NULL';
  463. $this->assertEquals($expected, $result);
  464. $column = array('name' => 'client_id', 'type' => 'integer', 'null' => false, 'default' => '0', 'length' => '11');
  465. $result = $this->db->buildColumn($column);
  466. $expected = '[client_id] int DEFAULT 0 NOT NULL';
  467. $this->assertEquals($expected, $result);
  468. $column = array('name' => 'client_id', 'type' => 'integer', 'null' => true);
  469. $result = $this->db->buildColumn($column);
  470. $expected = '[client_id] int NULL';
  471. $this->assertEquals($expected, $result);
  472. // 'name' => 'type' format for columns
  473. $column = array('type' => 'integer', 'name' => 'client_id');
  474. $result = $this->db->buildColumn($column);
  475. $expected = '[client_id] int NULL';
  476. $this->assertEquals($expected, $result);
  477. $column = array('type' => 'string', 'name' => 'name');
  478. $result = $this->db->buildColumn($column);
  479. $expected = '[name] nvarchar(255) NULL';
  480. $this->assertEquals($expected, $result);
  481. $column = array('name' => 'name', 'type' => 'string', 'null' => false, 'default' => '', 'length' => '255');
  482. $result = $this->db->buildColumn($column);
  483. $expected = '[name] nvarchar(255) DEFAULT \'\' NOT NULL';
  484. $this->assertEquals($expected, $result);
  485. $column = array('name' => 'name', 'type' => 'string', 'null' => false, 'length' => '255');
  486. $result = $this->db->buildColumn($column);
  487. $expected = '[name] nvarchar(255) NOT NULL';
  488. $this->assertEquals($expected, $result);
  489. $column = array('name' => 'name', 'type' => 'string', 'null' => false, 'default' => null, 'length' => '255');
  490. $result = $this->db->buildColumn($column);
  491. $expected = '[name] nvarchar(255) NOT NULL';
  492. $this->assertEquals($expected, $result);
  493. $column = array('name' => 'name', 'type' => 'string', 'null' => true, 'default' => null, 'length' => '255');
  494. $result = $this->db->buildColumn($column);
  495. $expected = '[name] nvarchar(255) NULL';
  496. $this->assertEquals($expected, $result);
  497. $column = array('name' => 'name', 'type' => 'string', 'null' => true, 'default' => '', 'length' => '255');
  498. $result = $this->db->buildColumn($column);
  499. $expected = '[name] nvarchar(255) DEFAULT \'\'';
  500. $this->assertEquals($expected, $result);
  501. $column = array('name' => 'body', 'type' => 'text');
  502. $result = $this->db->buildColumn($column);
  503. $expected = '[body] nvarchar(MAX)';
  504. $this->assertEquals($expected, $result);
  505. $column = array(
  506. 'name' => 'checked',
  507. 'type' => 'boolean',
  508. 'length' => 10,
  509. 'default' => '1'
  510. );
  511. $result = $this->db->buildColumn($column);
  512. $expected = "[checked] bit DEFAULT '1'";
  513. $this->assertEquals($expected, $result);
  514. $column = array(
  515. 'name' => 'huge',
  516. 'type' => 'biginteger',
  517. );
  518. $result = $this->db->buildColumn($column);
  519. $expected = "[huge] bigint";
  520. $this->assertEquals($expected, $result);
  521. }
  522. /**
  523. * testBuildIndex method
  524. *
  525. * @return void
  526. */
  527. public function testBuildIndex() {
  528. $indexes = array(
  529. 'PRIMARY' => array('column' => 'id', 'unique' => 1),
  530. 'client_id' => array('column' => 'client_id', 'unique' => 1)
  531. );
  532. $result = $this->db->buildIndex($indexes, 'items');
  533. $expected = array(
  534. 'PRIMARY KEY ([id])',
  535. 'ALTER TABLE items ADD CONSTRAINT client_id UNIQUE([client_id]);'
  536. );
  537. $this->assertEquals($expected, $result);
  538. $indexes = array('client_id' => array('column' => 'client_id'));
  539. $result = $this->db->buildIndex($indexes, 'items');
  540. $this->assertSame(array(), $result);
  541. $indexes = array('client_id' => array('column' => array('client_id', 'period_id'), 'unique' => 1));
  542. $result = $this->db->buildIndex($indexes, 'items');
  543. $expected = array('ALTER TABLE items ADD CONSTRAINT client_id UNIQUE([client_id], [period_id]);');
  544. $this->assertEquals($expected, $result);
  545. }
  546. /**
  547. * testUpdateAllSyntax method
  548. *
  549. * @return void
  550. */
  551. public function testUpdateAllSyntax() {
  552. $fields = array('SqlserverTestModel.client_id' => '[SqlserverTestModel].[client_id] + 1');
  553. $conditions = array('SqlserverTestModel.updated <' => date('2009-01-01 00:00:00'));
  554. $this->db->update($this->model, $fields, null, $conditions);
  555. $result = $this->db->getLastQuery();
  556. $this->assertNotRegExp('/SqlserverTestModel/', $result);
  557. $this->assertRegExp('/^UPDATE \[sqlserver_test_models\]/', $result);
  558. $this->assertRegExp('/SET \[client_id\] = \[client_id\] \+ 1/', $result);
  559. }
  560. /**
  561. * testGetPrimaryKey method
  562. *
  563. * @return void
  564. */
  565. public function testGetPrimaryKey() {
  566. $schema = $this->model->schema();
  567. $this->db->describe = $schema;
  568. $result = $this->db->getPrimaryKey($this->model);
  569. $this->assertEquals('id', $result);
  570. unset($schema['id']['key']);
  571. $this->db->describe = $schema;
  572. $result = $this->db->getPrimaryKey($this->model);
  573. $this->assertNull($result);
  574. }
  575. /**
  576. * SQL server < 11 doesn't have proper limit/offset support, test that our hack works.
  577. *
  578. * @return void
  579. */
  580. public function testLimitOffsetHack() {
  581. $this->loadFixtures('Author', 'Post', 'User');
  582. $query = array(
  583. 'limit' => 2,
  584. 'page' => 1,
  585. 'order' => 'User.user ASC',
  586. );
  587. $User = ClassRegistry::init('User');
  588. $results = $User->find('all', $query);
  589. $this->assertEquals(2, count($results));
  590. $this->assertEquals('garrett', $results[0]['User']['user']);
  591. $this->assertEquals('larry', $results[1]['User']['user']);
  592. $query = array(
  593. 'limit' => 2,
  594. 'page' => 2,
  595. 'order' => 'User.user ASC',
  596. );
  597. $User = ClassRegistry::init('User');
  598. $results = $User->find('all', $query);
  599. $this->assertEquals(2, count($results));
  600. $this->assertFalse(isset($results[0][0]));
  601. $this->assertEquals('mariano', $results[0]['User']['user']);
  602. $this->assertEquals('nate', $results[1]['User']['user']);
  603. }
  604. /**
  605. * Test that the return of stored procedures is honoured
  606. *
  607. * @return void
  608. */
  609. public function testStoredProcedureReturn() {
  610. $sql = <<<SQL
  611. CREATE PROCEDURE cake_test_procedure
  612. AS
  613. BEGIN
  614. RETURN 2;
  615. END
  616. SQL;
  617. $this->Dbo->execute($sql);
  618. $sql = <<<SQL
  619. DECLARE @return_value int
  620. EXEC @return_value = [cake_test_procedure]
  621. SELECT 'value' = @return_value
  622. SQL;
  623. $query = $this->Dbo->execute($sql);
  624. $this->Dbo->execute('DROP PROC cake_test_procedure');
  625. $result = $query->fetch();
  626. $this->assertEquals(2, $result['value']);
  627. }
  628. }