SqlserverTest.php 19 KB

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