~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/tests/cases/libs/model/cake_schema.test.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Test for Schema database management
 
4
 *
 
5
 *
 
6
 * PHP versions 4 and 5
 
7
 *
 
8
 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
 
9
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
10
 *
 
11
 *  Licensed under The Open Group Test Suite License
 
12
 *  Redistributions of files must retain the above copyright notice.
 
13
 *
 
14
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
15
 * @link          http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
 
16
 * @package       cake
 
17
 * @subpackage    cake.tests.cases.libs
 
18
 * @since         CakePHP(tm) v 1.2.0.5550
 
19
 * @license       http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
 
20
 */
 
21
App::import('Model', 'CakeSchema', false);
 
22
 
 
23
/**
 
24
 * Test for Schema database management
 
25
 *
 
26
 * @package       cake
 
27
 * @subpackage    cake.tests.cases.libs
 
28
 */
 
29
class MyAppSchema extends CakeSchema {
 
30
 
 
31
/**
 
32
 * name property
 
33
 *
 
34
 * @var string 'MyApp'
 
35
 * @access public
 
36
 */
 
37
        var $name = 'MyApp';
 
38
 
 
39
/**
 
40
 * connection property
 
41
 *
 
42
 * @var string 'test_suite'
 
43
 * @access public
 
44
 */
 
45
        var $connection = 'test_suite';
 
46
 
 
47
/**
 
48
 * comments property
 
49
 *
 
50
 * @var array
 
51
 * @access public
 
52
 */
 
53
        var $comments = array(
 
54
                'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
 
55
                'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
 
56
                'user_id' => array('type' => 'integer', 'null' => false),
 
57
                'title' => array('type' => 'string', 'null' => false, 'length' => 100),
 
58
                'comment' => array('type' => 'text', 'null' => false, 'default' => null),
 
59
                'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
 
60
                'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
 
61
                'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
 
62
                'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
 
63
        );
 
64
 
 
65
/**
 
66
 * posts property
 
67
 *
 
68
 * @var array
 
69
 * @access public
 
70
 */
 
71
        var $posts = array(
 
72
                'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
 
73
                'author_id' => array('type' => 'integer', 'null' => true, 'default' => ''),
 
74
                'title' => array('type' => 'string', 'null' => false, 'default' => 'Title'),
 
75
                'body' => array('type' => 'text', 'null' => true, 'default' => null),
 
76
                'summary' => array('type' => 'text', 'null' => true),
 
77
                'published' => array('type' => 'string', 'null' => true, 'default' => 'Y', 'length' => 1),
 
78
                'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
 
79
                'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
 
80
                'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
 
81
        );
 
82
 
 
83
/**
 
84
 * setup method
 
85
 *
 
86
 * @param mixed $version
 
87
 * @access public
 
88
 * @return void
 
89
 */
 
90
        function setup($version) {
 
91
        }
 
92
 
 
93
/**
 
94
 * teardown method
 
95
 *
 
96
 * @param mixed $version
 
97
 * @access public
 
98
 * @return void
 
99
 */
 
100
        function teardown($version) {
 
101
        }
 
102
}
 
103
 
 
104
/**
 
105
 * TestAppSchema class
 
106
 *
 
107
 * @package       cake
 
108
 * @subpackage    cake.tests.cases.libs.model
 
109
 */
 
110
class TestAppSchema extends CakeSchema {
 
111
 
 
112
/**
 
113
 * name property
 
114
 *
 
115
 * @var string 'MyApp'
 
116
 * @access public
 
117
 */
 
118
        var $name = 'MyApp';
 
119
 
 
120
/**
 
121
 * comments property
 
122
 *
 
123
 * @var array
 
124
 * @access public
 
125
 */
 
126
        var $comments = array(
 
127
                'id' => array('type' => 'integer', 'null' => false, 'default' => 0,'key' => 'primary'),
 
128
                'article_id' => array('type' => 'integer', 'null' => false),
 
129
                'user_id' => array('type' => 'integer', 'null' => false),
 
130
                'comment' => array('type' => 'text', 'null' => true, 'default' => null),
 
131
                'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
 
132
                'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
 
133
                'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
 
134
                'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
 
135
                'tableParameters' => array(),
 
136
        );
 
137
 
 
138
/**
 
139
 * posts property
 
140
 *
 
141
 * @var array
 
142
 * @access public
 
143
 */
 
144
        var $posts = array(
 
145
                'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
 
146
                'author_id' => array('type' => 'integer', 'null' => false),
 
147
                'title' => array('type' => 'string', 'null' => false),
 
148
                'body' => array('type' => 'text', 'null' => true, 'default' => null),
 
149
                'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
 
150
                'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
 
151
                'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
 
152
                'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
 
153
                'tableParameters' => array(),
 
154
        );
 
155
 
 
156
/**
 
157
 * posts_tags property
 
158
 *
 
159
 * @var array
 
160
 * @access public
 
161
 */
 
162
        var $posts_tags = array(
 
163
                'post_id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'),
 
164
                'tag_id' => array('type' => 'string', 'null' => false, 'key' => 'primary'),
 
165
                'indexes' => array('posts_tag' => array('column' => array('tag_id', 'post_id'), 'unique' => 1)),
 
166
                'tableParameters' => array()
 
167
        );
 
168
 
 
169
/**
 
170
 * tags property
 
171
 *
 
172
 * @var array
 
173
 * @access public
 
174
 */
 
175
        var $tags = array(
 
176
                'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
 
177
                'tag' => array('type' => 'string', 'null' => false),
 
178
                'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
 
179
                'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
 
180
                'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
 
181
                'tableParameters' => array()
 
182
        );
 
183
 
 
184
/**
 
185
 * datatypes property
 
186
 *
 
187
 * @var array
 
188
 * @access public
 
189
 */
 
190
        var $datatypes = array(
 
191
                'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
 
192
                'float_field' => array('type' => 'float', 'null' => false, 'length' => '5,2', 'default' => ''),
 
193
                'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
 
194
                'tableParameters' => array()
 
195
        );
 
196
 
 
197
/**
 
198
 * setup method
 
199
 *
 
200
 * @param mixed $version
 
201
 * @access public
 
202
 * @return void
 
203
 */
 
204
        function setup($version) {
 
205
        }
 
206
 
 
207
/**
 
208
 * teardown method
 
209
 *
 
210
 * @param mixed $version
 
211
 * @access public
 
212
 * @return void
 
213
 */
 
214
        function teardown($version) {
 
215
        }
 
216
}
 
217
 
 
218
/**
 
219
 * SchmeaPost class
 
220
 *
 
221
 * @package       cake
 
222
 * @subpackage    cake.tests.cases.libs.model
 
223
 */
 
224
class SchemaPost extends CakeTestModel {
 
225
 
 
226
/**
 
227
 * name property
 
228
 *
 
229
 * @var string 'SchemaPost'
 
230
 * @access public
 
231
 */
 
232
        var $name = 'SchemaPost';
 
233
 
 
234
/**
 
235
 * useTable property
 
236
 *
 
237
 * @var string 'posts'
 
238
 * @access public
 
239
 */
 
240
        var $useTable = 'posts';
 
241
 
 
242
/**
 
243
 * hasMany property
 
244
 *
 
245
 * @var array
 
246
 * @access public
 
247
 */
 
248
        var $hasMany = array('SchemaComment');
 
249
 
 
250
/**
 
251
 * hasAndBelongsToMany property
 
252
 *
 
253
 * @var array
 
254
 * @access public
 
255
 */
 
256
        var $hasAndBelongsToMany = array('SchemaTag');
 
257
}
 
258
 
 
259
/**
 
260
 * SchemaComment class
 
261
 *
 
262
 * @package       cake
 
263
 * @subpackage    cake.tests.cases.libs.model
 
264
 */
 
265
class SchemaComment extends CakeTestModel {
 
266
 
 
267
/**
 
268
 * name property
 
269
 *
 
270
 * @var string 'SchemaComment'
 
271
 * @access public
 
272
 */
 
273
        var $name = 'SchemaComment';
 
274
 
 
275
/**
 
276
 * useTable property
 
277
 *
 
278
 * @var string 'comments'
 
279
 * @access public
 
280
 */
 
281
        var $useTable = 'comments';
 
282
 
 
283
/**
 
284
 * belongsTo property
 
285
 *
 
286
 * @var array
 
287
 * @access public
 
288
 */
 
289
        var $belongsTo = array('SchemaPost');
 
290
}
 
291
 
 
292
/**
 
293
 * SchemaTag class
 
294
 *
 
295
 * @package       cake
 
296
 * @subpackage    cake.tests.cases.libs.model
 
297
 */
 
298
class SchemaTag extends CakeTestModel {
 
299
 
 
300
/**
 
301
 * name property
 
302
 *
 
303
 * @var string 'SchemaTag'
 
304
 * @access public
 
305
 */
 
306
        var $name = 'SchemaTag';
 
307
 
 
308
/**
 
309
 * useTable property
 
310
 *
 
311
 * @var string 'tags'
 
312
 * @access public
 
313
 */
 
314
        var $useTable = 'tags';
 
315
 
 
316
/**
 
317
 * hasAndBelongsToMany property
 
318
 *
 
319
 * @var array
 
320
 * @access public
 
321
 */
 
322
        var $hasAndBelongsToMany = array('SchemaPost');
 
323
}
 
324
 
 
325
/**
 
326
 * SchemaDatatype class
 
327
 *
 
328
 * @package       cake
 
329
 * @subpackage    cake.tests.cases.libs.model
 
330
 */
 
331
class SchemaDatatype extends CakeTestModel {
 
332
 
 
333
/**
 
334
 * name property
 
335
 *
 
336
 * @var string 'SchemaDatatype'
 
337
 * @access public
 
338
 */
 
339
        var $name = 'SchemaDatatype';
 
340
 
 
341
/**
 
342
 * useTable property
 
343
 *
 
344
 * @var string 'datatypes'
 
345
 * @access public
 
346
 */
 
347
        var $useTable = 'datatypes';
 
348
}
 
349
 
 
350
/**
 
351
 * Testdescribe class
 
352
 *
 
353
 * This class is defined purely to inherit the cacheSources variable otherwise
 
354
 * testSchemaCreatTable will fail if listSources has already been called and
 
355
 * its source cache populated - I.e. if the test is run within a group
 
356
 *
 
357
 * @uses          CakeTestModel
 
358
 * @package
 
359
 * @subpackage    cake.tests.cases.libs.model
 
360
 */
 
361
class Testdescribe extends CakeTestModel {
 
362
 
 
363
/**
 
364
 * name property
 
365
 *
 
366
 * @var string 'Testdescribe'
 
367
 * @access public
 
368
 */
 
369
        var $name = 'Testdescribe';
 
370
}
 
371
 
 
372
/**
 
373
 * SchemaCrossDatabase class
 
374
 *
 
375
 * @package       cake
 
376
 * @subpackage    cake.tests.cases.libs.model
 
377
 */
 
378
class SchemaCrossDatabase extends CakeTestModel {
 
379
 
 
380
/**
 
381
 * name property
 
382
 *
 
383
 * @var string 'SchemaCrossDatabase'
 
384
 * @access public
 
385
 */
 
386
        var $name = 'SchemaCrossDatabase';
 
387
 
 
388
/**
 
389
 * useTable property
 
390
 *
 
391
 * @var string 'posts'
 
392
 * @access public
 
393
 */
 
394
        var $useTable = 'cross_database';
 
395
 
 
396
/**
 
397
 * useDbConfig property
 
398
 *
 
399
 * @var string 'test2'
 
400
 * @access public
 
401
 */
 
402
        var $useDbConfig = 'test2';
 
403
}
 
404
 
 
405
/**
 
406
 * SchemaCrossDatabaseFixture class
 
407
 *
 
408
 * @package       cake
 
409
 * @subpackage    cake.tests.cases.libs.model
 
410
 */
 
411
class SchemaCrossDatabaseFixture extends CakeTestFixture {
 
412
 
 
413
/**
 
414
 * name property
 
415
 *
 
416
 * @var string 'CrossDatabase'
 
417
 * @access public
 
418
 */
 
419
        var $name = 'CrossDatabase';
 
420
 
 
421
/**
 
422
 * table property
 
423
 *
 
424
 * @access public
 
425
 */
 
426
        var $table = 'cross_database';
 
427
 
 
428
/**
 
429
 * fields property
 
430
 *
 
431
 * @var array
 
432
 * @access public
 
433
 */
 
434
        var $fields = array(
 
435
                'id' => array('type' => 'integer', 'key' => 'primary'),
 
436
                'name' => 'string'
 
437
        );
 
438
 
 
439
/**
 
440
 * records property
 
441
 *
 
442
 * @var array
 
443
 * @access public
 
444
 */
 
445
        var $records = array(
 
446
                array('id' => 1, 'name' => 'First'),
 
447
                array('id' => 2, 'name' => 'Second'),
 
448
        );
 
449
}
 
450
 
 
451
/**
 
452
 * SchemaPrefixAuthUser class
 
453
 *
 
454
 * @package       cake
 
455
 * @subpackage    cake.tests.cases.libs.model
 
456
 */
 
457
class SchemaPrefixAuthUser extends CakeTestModel {
 
458
/**
 
459
 * name property
 
460
 *
 
461
 * @var string
 
462
 */
 
463
        var $name = 'SchemaPrefixAuthUser';
 
464
/**
 
465
 * table prefix
 
466
 *
 
467
 * @var string
 
468
 */
 
469
        var $tablePrefix = 'auth_';
 
470
/**
 
471
 * useTable
 
472
 *
 
473
 * @var string
 
474
 */
 
475
        var $useTable = 'users';
 
476
}
 
477
 
 
478
/**
 
479
 * CakeSchemaTest
 
480
 *
 
481
 * @package       cake
 
482
 * @subpackage    cake.tests.cases.libs
 
483
 */
 
484
class CakeSchemaTest extends CakeTestCase {
 
485
 
 
486
/**
 
487
 * fixtures property
 
488
 *
 
489
 * @var array
 
490
 * @access public
 
491
 */
 
492
        var $fixtures = array(
 
493
                'core.post', 'core.tag', 'core.posts_tag', 'core.test_plugin_comment',
 
494
                'core.datatype', 'core.auth_user', 'core.author',
 
495
                'core.test_plugin_article', 'core.user', 'core.comment'
 
496
        );
 
497
 
 
498
/**
 
499
 * setUp method
 
500
 *
 
501
 * @access public
 
502
 * @return void
 
503
 */
 
504
        function startTest() {
 
505
                $this->Schema = new TestAppSchema();
 
506
        }
 
507
 
 
508
/**
 
509
 * tearDown method
 
510
 *
 
511
 * @access public
 
512
 * @return void
 
513
 */
 
514
        function tearDown() {
 
515
                @unlink(TMP . 'tests' . DS .'schema.php');
 
516
                unset($this->Schema);
 
517
                ClassRegistry::flush();
 
518
        }
 
519
 
 
520
/**
 
521
 * testSchemaName method
 
522
 *
 
523
 * @access public
 
524
 * @return void
 
525
 */
 
526
        function testSchemaName() {
 
527
                $Schema = new CakeSchema();
 
528
                $this->assertEqual(strtolower($Schema->name), strtolower(APP_DIR));
 
529
 
 
530
                Configure::write('App.dir', 'Some.name.with.dots');
 
531
                $Schema = new CakeSchema();
 
532
                $this->assertEqual($Schema->name, 'SomeNameWithDots');
 
533
 
 
534
                Configure::write('App.dir', 'app');
 
535
        }
 
536
 
 
537
/**
 
538
 * testSchemaRead method
 
539
 *
 
540
 * @access public
 
541
 * @return void
 
542
 */
 
543
        function testSchemaRead() {
 
544
                $read = $this->Schema->read(array(
 
545
                        'connection' => 'test_suite',
 
546
                        'name' => 'TestApp',
 
547
                        'models' => array('SchemaPost', 'SchemaComment', 'SchemaTag', 'SchemaDatatype')
 
548
                ));
 
549
                unset($read['tables']['missing']);
 
550
 
 
551
                $expected = array('comments', 'datatypes', 'posts', 'posts_tags', 'tags');
 
552
                $this->assertEqual(array_keys($read['tables']), $expected);
 
553
                foreach ($read['tables'] as $table => $fields) {
 
554
                        $this->assertEqual(array_keys($fields), array_keys($this->Schema->tables[$table]));
 
555
                }
 
556
 
 
557
                $this->assertEqual(
 
558
                        $read['tables']['datatypes']['float_field'],
 
559
                        $this->Schema->tables['datatypes']['float_field']
 
560
                );
 
561
 
 
562
                $SchemaPost =& ClassRegistry::init('SchemaPost');
 
563
                $SchemaPost->table = 'sts';
 
564
                $SchemaPost->tablePrefix = 'po';
 
565
                $read = $this->Schema->read(array(
 
566
                        'connection' => 'test_suite',
 
567
                        'name' => 'TestApp',
 
568
                        'models' => array('SchemaPost')
 
569
                ));
 
570
                $this->assertFalse(isset($read['tables']['missing']['posts']), 'Posts table was not read from tablePrefix %s');
 
571
 
 
572
                $read = $this->Schema->read(array(
 
573
                        'connection' => 'test_suite',
 
574
                        'name' => 'TestApp',
 
575
                        'models' => array('SchemaComment', 'SchemaTag', 'SchemaPost')
 
576
                ));
 
577
                $this->assertFalse(isset($read['tables']['missing']['posts_tags']), 'Join table marked as missing %s');
 
578
        }
 
579
 
 
580
/**
 
581
 * test read() with tablePrefix properties.
 
582
 *
 
583
 * @return void
 
584
 */
 
585
        function testSchemaReadWithTablePrefix() {
 
586
                $model =& new SchemaPrefixAuthUser();
 
587
 
 
588
                $Schema =& new CakeSchema();
 
589
                $read = $Schema->read(array(
 
590
                        'connection' => 'test_suite',
 
591
                        'name' => 'TestApp',
 
592
                        'models' => array('SchemaPrefixAuthUser')
 
593
                ));
 
594
                unset($read['tables']['missing']);
 
595
                $this->assertTrue(isset($read['tables']['auth_users']), 'auth_users key missing %s');
 
596
 
 
597
        }
 
598
 
 
599
/**
 
600
 * test reading schema with config prefix.
 
601
 *
 
602
 * @return void
 
603
 */
 
604
        function testSchemaReadWithConfigPrefix() {
 
605
                $db =& ConnectionManager::getDataSource('test_suite');
 
606
                $config = $db->config;
 
607
                $config['prefix'] = 'schema_test_prefix_';
 
608
                ConnectionManager::create('schema_prefix', $config);
 
609
                $read = $this->Schema->read(array('connection' => 'schema_prefix', 'models' => false));
 
610
                $this->assertTrue(empty($read['tables']));
 
611
        }
 
612
 
 
613
/**
 
614
 * test reading schema from plugins.
 
615
 *
 
616
 * @return void
 
617
 */
 
618
        function testSchemaReadWithPlugins() {
 
619
                App::objects('model', null, false);
 
620
                App::build(array(
 
621
                        'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
 
622
                ));
 
623
 
 
624
                $Schema =& new CakeSchema();
 
625
                $Schema->plugin = 'TestPlugin';
 
626
                $read = $Schema->read(array(
 
627
                        'connection' => 'test_suite',
 
628
                        'name' => 'TestApp',
 
629
                        'models' => true
 
630
                ));
 
631
                unset($read['tables']['missing']);
 
632
                $this->assertTrue(isset($read['tables']['auth_users']));
 
633
                $this->assertTrue(isset($read['tables']['authors']));
 
634
                $this->assertTrue(isset($read['tables']['test_plugin_comments']));
 
635
                $this->assertTrue(isset($read['tables']['posts']));
 
636
                $this->assertEqual(count($read['tables']), 4);
 
637
 
 
638
                App::build();
 
639
        }
 
640
 
 
641
/**
 
642
 * test reading schema with tables from another database.
 
643
 *
 
644
 * @return void
 
645
 */
 
646
        function testSchemaReadWithCrossDatabase() {
 
647
                $config = new DATABASE_CONFIG();
 
648
                $skip = $this->skipIf(
 
649
                        !isset($config->test) || !isset($config->test2),
 
650
                         '%s Primary and secondary test databases not configured, skipping cross-database '
 
651
                        .'join tests.'
 
652
                        .' To run these tests, you must define $test and $test2 in your database configuration.'
 
653
                );
 
654
                if ($skip) {
 
655
                        return;
 
656
                }
 
657
 
 
658
                $db2 =& ConnectionManager::getDataSource('test2');
 
659
                $fixture = new SchemaCrossDatabaseFixture();
 
660
                $fixture->create($db2);
 
661
                $fixture->insert($db2);
 
662
 
 
663
                $read = $this->Schema->read(array(
 
664
                        'connection' => 'test_suite',
 
665
                        'name' => 'TestApp',
 
666
                        'models' => array('SchemaCrossDatabase', 'SchemaPost')
 
667
                ));
 
668
                $this->assertTrue(isset($read['tables']['posts']));
 
669
                $this->assertFalse(isset($read['tables']['cross_database']), 'Cross database should not appear');
 
670
                $this->assertFalse(isset($read['tables']['missing']['cross_database']), 'Cross database should not appear');
 
671
 
 
672
                $read = $this->Schema->read(array(
 
673
                        'connection' => 'test2',
 
674
                        'name' => 'TestApp',
 
675
                        'models' => array('SchemaCrossDatabase', 'SchemaPost')
 
676
                ));
 
677
                $this->assertFalse(isset($read['tables']['posts']), 'Posts should not appear');
 
678
                $this->assertFalse(isset($read['tables']['posts']), 'Posts should not appear');
 
679
                $this->assertTrue(isset($read['tables']['cross_database']));
 
680
 
 
681
                $fixture->drop($db2);
 
682
        }
 
683
 
 
684
/**
 
685
 * test that tables are generated correctly
 
686
 *
 
687
 * @return void
 
688
 */
 
689
        function testGenerateTable() {
 
690
                $fields = array(
 
691
                        'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
 
692
                        'author_id' => array('type' => 'integer', 'null' => false),
 
693
                        'title' => array('type' => 'string', 'null' => false),
 
694
                        'body' => array('type' => 'text', 'null' => true, 'default' => null),
 
695
                        'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
 
696
                        'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
 
697
                        'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
 
698
                        'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
 
699
                );
 
700
                $result = $this->Schema->generateTable('posts', $fields);
 
701
                $this->assertPattern('/var \$posts/', $result);
 
702
 
 
703
                eval(substr($result, 4));
 
704
                $this->assertEqual($posts, $fields);
 
705
        }
 
706
/**
 
707
 * testSchemaWrite method
 
708
 *
 
709
 * @access public
 
710
 * @return void
 
711
 */
 
712
        function testSchemaWrite() {
 
713
                $write = $this->Schema->write(array('name' => 'MyOtherApp', 'tables' => $this->Schema->tables, 'path' => TMP . 'tests'));
 
714
                $file = file_get_contents(TMP . 'tests' . DS .'schema.php');
 
715
                $this->assertEqual($write, $file);
 
716
 
 
717
                require_once( TMP . 'tests' . DS .'schema.php');
 
718
                $OtherSchema = new MyOtherAppSchema();
 
719
                $this->assertEqual($this->Schema->tables, $OtherSchema->tables);
 
720
        }
 
721
 
 
722
/**
 
723
 * testSchemaComparison method
 
724
 *
 
725
 * @access public
 
726
 * @return void
 
727
 */
 
728
        function testSchemaComparison() {
 
729
                $New = new MyAppSchema();
 
730
                $compare = $New->compare($this->Schema);
 
731
                $expected = array(
 
732
                        'comments' => array(
 
733
                                'add' => array(
 
734
                                        'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
 
735
                                        'title' => array('type' => 'string', 'null' => false, 'length' => 100),
 
736
                                ),
 
737
                                'drop' => array(
 
738
                                        'article_id' => array('type' => 'integer', 'null' => false),
 
739
                                        'tableParameters' => array(),
 
740
                                ),
 
741
                                'change' => array(
 
742
                                        'comment' => array('type' => 'text', 'null' => false, 'default' => null),
 
743
                                )
 
744
                        ),
 
745
                        'posts' => array(
 
746
                                'add' => array(
 
747
                                        'summary' => array('type' => 'text', 'null' => 1),
 
748
                                ),
 
749
                                'drop' => array(
 
750
                                        'tableParameters' => array(),
 
751
                                ),
 
752
                                'change' => array(
 
753
                                        'author_id' => array('type' => 'integer', 'null' => true, 'default' => ''),
 
754
                                        'title' => array('type' => 'string', 'null' => false, 'default' => 'Title'),
 
755
                                        'published' => array('type' => 'string', 'null' => true, 'default' => 'Y', 'length' => '1')
 
756
                                )
 
757
                        ),
 
758
                );
 
759
                $this->assertEqual($expected, $compare);
 
760
 
 
761
                $tables = array(
 
762
                        'missing' => array(
 
763
                                'categories' => array(
 
764
                                        'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
 
765
                                        'created' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
 
766
                                        'modified' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
 
767
                                        'name' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 100),
 
768
                                        'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
 
769
                                        'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'MyISAM')
 
770
                                )
 
771
                        ),
 
772
                        'ratings' => array(
 
773
                                'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
 
774
                                'foreign_key' => array('type' => 'integer', 'null' => false, 'default' => NULL),
 
775
                                'model' => array('type' => 'varchar', 'null' => false, 'default' => NULL),
 
776
                                'value' => array('type' => 'float', 'null' => false, 'length' => '5,2', 'default' => NULL),
 
777
                                'created' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
 
778
                                'modified' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
 
779
                                'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
 
780
                                'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'MyISAM')
 
781
                        )
 
782
                );
 
783
                $compare = $New->compare($this->Schema, $tables);
 
784
                $expected = array(
 
785
                        'ratings' => array(
 
786
                                'add' => array(
 
787
                                        'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
 
788
                                        'foreign_key' => array('type' => 'integer', 'null' => false, 'default' => NULL),
 
789
                                        'model' => array('type' => 'varchar', 'null' => false, 'default' => NULL),
 
790
                                        'value' => array('type' => 'float', 'null' => false, 'length' => '5,2', 'default' => NULL),
 
791
                                        'created' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
 
792
                                        'modified' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
 
793
                                        'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
 
794
                                        'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'MyISAM')
 
795
                                )
 
796
                        )
 
797
                );
 
798
                $this->assertEqual($expected, $compare);
 
799
        }
 
800
 
 
801
/**
 
802
 * Test comparing tableParameters and indexes.
 
803
 *
 
804
 * @return void
 
805
 */
 
806
        function testTableParametersAndIndexComparison() {
 
807
                $old = array(
 
808
                        'posts' => array(
 
809
                                'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
 
810
                                'author_id' => array('type' => 'integer', 'null' => false),
 
811
                                'title' => array('type' => 'string', 'null' => false),
 
812
                                'indexes' => array(
 
813
                                        'PRIMARY' => array('column' => 'id', 'unique' => true)
 
814
                                ),
 
815
                                'tableParameters' => array(
 
816
                                        'charset' => 'latin1',
 
817
                                        'collate' => 'latin1_general_ci'
 
818
                                )
 
819
                        ),
 
820
                        'comments' => array(
 
821
                                'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
 
822
                                'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
 
823
                                'comment' => array('type' => 'text'),
 
824
                                'indexes' => array(
 
825
                                        'PRIMARY' => array('column' => 'id', 'unique' => true),
 
826
                                        'post_id' => array('column' => 'post_id'),
 
827
                                ),
 
828
                                'tableParameters' => array(
 
829
                                        'engine' => 'InnoDB',
 
830
                                        'charset' => 'latin1',
 
831
                                        'collate' => 'latin1_general_ci'
 
832
                                )
 
833
                        )
 
834
                );
 
835
                $new = array(
 
836
                        'posts' => array(
 
837
                                'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
 
838
                                'author_id' => array('type' => 'integer', 'null' => false),
 
839
                                'title' => array('type' => 'string', 'null' => false),
 
840
                                'indexes' => array(
 
841
                                        'PRIMARY' => array('column' => 'id', 'unique' => true),
 
842
                                        'author_id' => array('column' => 'author_id'),
 
843
                                ),
 
844
                                'tableParameters' => array(
 
845
                                        'charset' => 'utf8',
 
846
                                        'collate' => 'utf8_general_ci',
 
847
                                        'engine' => 'MyISAM'
 
848
                                )
 
849
                        ),
 
850
                        'comments' => array(
 
851
                                'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
 
852
                                'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
 
853
                                'comment' => array('type' => 'text'),
 
854
                                'indexes' => array(
 
855
                                        'PRIMARY' => array('column' => 'id', 'unique' => true),
 
856
                                ),
 
857
                                'tableParameters' => array(
 
858
                                        'charset' => 'utf8',
 
859
                                        'collate' => 'utf8_general_ci'
 
860
                                )
 
861
                        )
 
862
                );
 
863
                $compare = $this->Schema->compare($old, $new);
 
864
                $expected = array(
 
865
                        'posts' => array(
 
866
                                'add' => array(
 
867
                                        'indexes' => array('author_id' => array('column' => 'author_id')),
 
868
                                ),
 
869
                                'change' => array(
 
870
                                        'tableParameters' => array(
 
871
                                                'charset' => 'utf8',
 
872
                                                'collate' => 'utf8_general_ci',
 
873
                                                'engine' => 'MyISAM'
 
874
                                        )
 
875
                                )
 
876
                        ),
 
877
                        'comments' => array(
 
878
                                'drop' => array(
 
879
                                        'indexes' => array('post_id' => array('column' => 'post_id')),
 
880
                                ),
 
881
                                'change' => array(
 
882
                                        'tableParameters' => array(
 
883
                                                'charset' => 'utf8',
 
884
                                                'collate' => 'utf8_general_ci',
 
885
                                        )
 
886
                                )
 
887
                        )
 
888
                );
 
889
                $this->assertEqual($compare, $expected);
 
890
        }
 
891
 
 
892
/**
 
893
 * testSchemaLoading method
 
894
 *
 
895
 * @access public
 
896
 * @return void
 
897
 */
 
898
        function testSchemaLoading() {
 
899
                $Other =& $this->Schema->load(array('name' => 'MyOtherApp', 'path' => TMP . 'tests'));
 
900
                $this->assertEqual($Other->name, 'MyOtherApp');
 
901
                $this->assertEqual($Other->tables, $this->Schema->tables);
 
902
        }
 
903
 
 
904
/**
 
905
 * test loading schema files inside of plugins.
 
906
 *
 
907
 * @return void
 
908
 */
 
909
        function testSchemaLoadingFromPlugin() {
 
910
                App::build(array(
 
911
                        'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
 
912
                ));
 
913
                $Other =& $this->Schema->load(array('name' => 'TestPluginApp', 'plugin' => 'TestPlugin'));
 
914
                $this->assertEqual($Other->name, 'TestPluginApp');
 
915
                $this->assertEqual(array_keys($Other->tables), array('acos'));
 
916
 
 
917
                App::build();
 
918
        }
 
919
 
 
920
/**
 
921
 * testSchemaCreateTable method
 
922
 *
 
923
 * @access public
 
924
 * @return void
 
925
 */
 
926
        function testSchemaCreateTable() {
 
927
                $db =& ConnectionManager::getDataSource('test_suite');
 
928
                $db->cacheSources = false;
 
929
 
 
930
                $Schema =& new CakeSchema(array(
 
931
                        'connection' => 'test_suite',
 
932
                        'testdescribes' => array(
 
933
                                'id' => array('type' => 'integer', 'key' => 'primary'),
 
934
                                'int_null' => array('type' => 'integer', 'null' => true),
 
935
                                'int_not_null' => array('type' => 'integer', 'null' => false),
 
936
                        ),
 
937
                ));
 
938
                $sql = $db->createSchema($Schema);
 
939
 
 
940
                $col = $Schema->tables['testdescribes']['int_null'];
 
941
                $col['name'] = 'int_null';
 
942
                $column = $this->db->buildColumn($col);
 
943
                $this->assertPattern('/' . preg_quote($column, '/') . '/', $sql);
 
944
 
 
945
                $col = $Schema->tables['testdescribes']['int_not_null'];
 
946
                $col['name'] = 'int_not_null';
 
947
                $column = $this->db->buildColumn($col);
 
948
                $this->assertPattern('/' . preg_quote($column, '/') . '/', $sql);
 
949
        }
 
950
}