SqlserverTest.php 19 KB

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