SqlserverTest.php 19 KB

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