~artur-barczynski/azsystem/trunk

« back to all changes in this revision

Viewing changes to lib/Cake/Test/Case/Model/Datasource/Database/SqlserverTest.php

  • Committer: Artur Barczynski
  • Date: 2012-09-20 16:31:07 UTC
  • Revision ID: artur@arturkb.pl-20120920163107-oakeg1a4h9e6d37f
Init

Show diffs side-by-side

added added

removed removed

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