comparison cake/tests/cases/console/libs/schema.test.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
1 <?php
2 /**
3 * SchemaShellTest Test file
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP : Rapid Development Framework (http://cakephp.org)
8 * Copyright 2006-2010, Cake Software Foundation, Inc.
9 *
10 * Licensed under The MIT License
11 * Redistributions of files must retain the above copyright notice.
12 *
13 * @copyright Copyright 2006-2010, Cake Software Foundation, Inc.
14 * @link http://cakephp.org CakePHP Project
15 * @package cake
16 * @subpackage cake.tests.cases.console.libs.Shells
17 * @since CakePHP v 1.3
18 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
19 */
20 App::import('Shell', 'Shell', false);
21 App::import('Model', 'CakeSchema', false);
22
23 if (!defined('DISABLE_AUTO_DISPATCH')) {
24 define('DISABLE_AUTO_DISPATCH', true);
25 }
26
27 if (!class_exists('ShellDispatcher')) {
28 ob_start();
29 $argv = false;
30 require CAKE . 'console' . DS . 'cake.php';
31 ob_end_clean();
32 }
33
34 if (!class_exists('SchemaShell')) {
35 require CAKE . 'console' . DS . 'libs' . DS . 'schema.php';
36 }
37
38 Mock::generatePartial(
39 'ShellDispatcher', 'TestSchemaShellMockShellDispatcher',
40 array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
41 );
42 Mock::generatePartial(
43 'SchemaShell', 'MockSchemaShell',
44 array('in', 'out', 'hr', 'createFile', 'error', 'err', '_stop')
45 );
46
47 Mock::generate('CakeSchema', 'MockSchemaCakeSchema');
48
49 /**
50 * Test for Schema database management
51 *
52 * @package cake
53 * @subpackage cake.tests.cases.libs
54 */
55 class SchemaShellTestSchema extends CakeSchema {
56
57 /**
58 * name property
59 *
60 * @var string 'MyApp'
61 * @access public
62 */
63 var $name = 'SchemaShellTest';
64
65 /**
66 * connection property
67 *
68 * @var string 'test_suite'
69 * @access public
70 */
71 var $connection = 'test_suite';
72
73 /**
74 * comments property
75 *
76 * @var array
77 * @access public
78 */
79 var $comments = array(
80 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
81 'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
82 'user_id' => array('type' => 'integer', 'null' => false),
83 'title' => array('type' => 'string', 'null' => false, 'length' => 100),
84 'comment' => array('type' => 'text', 'null' => false, 'default' => null),
85 'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
86 'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
87 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
88 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
89 );
90
91 /**
92 * posts property
93 *
94 * @var array
95 * @access public
96 */
97 var $articles = array(
98 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
99 'user_id' => array('type' => 'integer', 'null' => true, 'default' => ''),
100 'title' => array('type' => 'string', 'null' => false, 'default' => 'Title'),
101 'body' => array('type' => 'text', 'null' => true, 'default' => null),
102 'summary' => array('type' => 'text', 'null' => true),
103 'published' => array('type' => 'string', 'null' => true, 'default' => 'Y', 'length' => 1),
104 'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
105 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
106 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
107 );
108 }
109
110 /**
111 * SchemaShellTest class
112 *
113 * @package cake
114 * @subpackage cake.tests.cases.console.libs.Shells
115 */
116 class SchemaShellTest extends CakeTestCase {
117
118 /**
119 * Fixtures
120 *
121 * @var array
122 * @access public
123 */
124 var $fixtures = array('core.article', 'core.user', 'core.post', 'core.auth_user', 'core.author',
125 'core.comment', 'core.test_plugin_comment'
126 );
127
128 /**
129 * startTest method
130 *
131 * @return void
132 * @access public
133 */
134 function startTest() {
135 $this->Dispatcher =& new TestSchemaShellMockShellDispatcher();
136 $this->Shell =& new MockSchemaShell($this->Dispatcher);
137 $this->Shell->Dispatch =& $this->Dispatcher;
138 }
139
140 /**
141 * endTest method
142 *
143 * @return void
144 * @access public
145 */
146 function endTest() {
147 ClassRegistry::flush();
148 }
149
150 /**
151 * test startup method
152 *
153 * @return void
154 * @access public
155 */
156 function testStartup() {
157 $this->Shell->startup();
158 $this->assertTrue(isset($this->Shell->Schema));
159 $this->assertTrue(is_a($this->Shell->Schema, 'CakeSchema'));
160 $this->assertEqual(strtolower($this->Shell->Schema->name), strtolower(APP_DIR));
161 $this->assertEqual($this->Shell->Schema->file, 'schema.php');
162
163 unset($this->Shell->Schema);
164 $this->Shell->params = array(
165 'name' => 'TestSchema'
166 );
167 $this->Shell->startup();
168 $this->assertEqual($this->Shell->Schema->name, 'TestSchema');
169 $this->assertEqual($this->Shell->Schema->file, 'test_schema.php');
170 $this->assertEqual($this->Shell->Schema->connection, 'default');
171 $this->assertEqual($this->Shell->Schema->path, APP . 'config' . DS . 'schema');
172
173 unset($this->Shell->Schema);
174 $this->Shell->params = array(
175 'file' => 'other_file.php',
176 'connection' => 'test_suite',
177 'path' => '/test/path'
178 );
179 $this->Shell->startup();
180 $this->assertEqual(strtolower($this->Shell->Schema->name), strtolower(APP_DIR));
181 $this->assertEqual($this->Shell->Schema->file, 'other_file.php');
182 $this->assertEqual($this->Shell->Schema->connection, 'test_suite');
183 $this->assertEqual($this->Shell->Schema->path, '/test/path');
184 }
185
186 /**
187 * Test View - and that it dumps the schema file to stdout
188 *
189 * @return void
190 * @access public
191 */
192 function testView() {
193 $this->Shell->startup();
194 $this->Shell->Schema->path = APP . 'config' . DS . 'schema';
195 $this->Shell->params['file'] = 'i18n.php';
196 $this->Shell->expectOnce('_stop');
197 $this->Shell->expectOnce('out');
198 $this->Shell->view();
199 }
200
201 /**
202 * test that view() can find plugin schema files.
203 *
204 * @return void
205 * @access public
206 */
207 function testViewWithPlugins() {
208 App::build(array(
209 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
210 ));
211 $this->Shell->args = array('TestPlugin.schema');
212 $this->Shell->startup();
213 $this->Shell->expectCallCount('_stop', 2);
214 $this->Shell->expectCallCount('out', 2);
215 $this->Shell->view();
216
217 $this->Shell->args = array();
218 $this->Shell->params = array('plugin' => 'TestPlugin');
219 $this->Shell->startup();
220 $this->Shell->view();
221
222 App::build();
223 }
224
225 /**
226 * test dump() with sql file generation
227 *
228 * @return void
229 * @access public
230 */
231 function testDumpWithFileWriting() {
232 $this->Shell->params = array(
233 'name' => 'i18n',
234 'write' => TMP . 'tests' . DS . 'i18n.sql'
235 );
236 $this->Shell->expectOnce('_stop');
237 $this->Shell->startup();
238 $this->Shell->dump();
239
240 $sql =& new File(TMP . 'tests' . DS . 'i18n.sql');
241 $contents = $sql->read();
242 $this->assertPattern('/DROP TABLE/', $contents);
243 $this->assertPattern('/CREATE TABLE `i18n`/', $contents);
244 $this->assertPattern('/id/', $contents);
245 $this->assertPattern('/model/', $contents);
246 $this->assertPattern('/field/', $contents);
247 $this->assertPattern('/locale/', $contents);
248 $this->assertPattern('/foreign_key/', $contents);
249 $this->assertPattern('/content/', $contents);
250
251 $sql->delete();
252 }
253
254 /**
255 * test that dump() can find and work with plugin schema files.
256 *
257 * @return void
258 * @access public
259 */
260 function testDumpFileWritingWithPlugins() {
261 App::build(array(
262 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
263 ));
264 $this->Shell->args = array('TestPlugin.TestPluginApp');
265 $this->Shell->params = array(
266 'connection' => 'test_suite',
267 'write' => TMP . 'tests' . DS . 'dump_test.sql'
268 );
269 $this->Shell->startup();
270 $this->Shell->expectOnce('_stop');
271 $this->Shell->dump();
272
273 $file =& new File(TMP . 'tests' . DS . 'dump_test.sql');
274 $contents = $file->read();
275
276 $this->assertPattern('/CREATE TABLE `acos`/', $contents);
277 $this->assertPattern('/id/', $contents);
278 $this->assertPattern('/model/', $contents);
279
280 $file->delete();
281 App::build();
282 }
283
284 /**
285 * test generate with snapshot generation
286 *
287 * @return void
288 * @access public
289 */
290 function testGenerateSnaphot() {
291 $this->Shell->path = TMP;
292 $this->Shell->params['file'] = 'schema.php';
293 $this->Shell->args = array('snapshot');
294 $this->Shell->Schema =& new MockSchemaCakeSchema();
295 $this->Shell->Schema->setReturnValue('read', array('schema data'));
296 $this->Shell->Schema->setReturnValue('write', true);
297
298 $this->Shell->Schema->expectOnce('read');
299 $this->Shell->Schema->expectOnce('write', array(array('schema data', 'file' => 'schema_1.php')));
300
301 $this->Shell->generate();
302 }
303
304 /**
305 * test generate without a snapshot.
306 *
307 * @return void
308 * @access public
309 */
310 function testGenerateNoOverwrite() {
311 touch(TMP . 'schema.php');
312 $this->Shell->params['file'] = 'schema.php';
313 $this->Shell->args = array();
314
315 $this->Shell->setReturnValue('in', 'q');
316 $this->Shell->Schema =& new MockSchemaCakeSchema();
317 $this->Shell->Schema->path = TMP;
318 $this->Shell->Schema->expectNever('read');
319
320 $result = $this->Shell->generate();
321 unlink(TMP . 'schema.php');
322 }
323
324 /**
325 * test generate with overwriting of the schema files.
326 *
327 * @return void
328 * @access public
329 */
330 function testGenerateOverwrite() {
331 touch(TMP . 'schema.php');
332 $this->Shell->params['file'] = 'schema.php';
333 $this->Shell->args = array();
334
335 $this->Shell->setReturnValue('in', 'o');
336 $this->Shell->expectAt(1, 'out', array(new PatternExpectation('/Schema file:\s[a-z\.]+\sgenerated/')));
337 $this->Shell->Schema =& new MockSchemaCakeSchema();
338 $this->Shell->Schema->path = TMP;
339 $this->Shell->Schema->setReturnValue('read', array('schema data'));
340 $this->Shell->Schema->setReturnValue('write', true);
341
342 $this->Shell->Schema->expectOnce('read');
343 $this->Shell->Schema->expectOnce('write', array(array('schema data', 'file' => 'schema.php')));
344
345 $this->Shell->generate();
346 unlink(TMP . 'schema.php');
347 }
348
349 /**
350 * test that generate() can read plugin dirs and generate schema files for the models
351 * in a plugin.
352 *
353 * @return void
354 * @access public
355 */
356 function testGenerateWithPlugins() {
357 App::build(array(
358 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
359 ));
360 $this->Shell->params = array(
361 'plugin' => 'TestPlugin',
362 'connection' => 'test_suite'
363 );
364 $this->Shell->startup();
365 $this->Shell->Schema->path = TMP . 'tests' . DS;
366
367 $this->Shell->generate();
368 $file =& new File(TMP . 'tests' . DS . 'schema.php');
369 $contents = $file->read();
370
371 $this->assertPattern('/class TestPluginSchema/', $contents);
372 $this->assertPattern('/var \$posts/', $contents);
373 $this->assertPattern('/var \$auth_users/', $contents);
374 $this->assertPattern('/var \$authors/', $contents);
375 $this->assertPattern('/var \$test_plugin_comments/', $contents);
376 $this->assertNoPattern('/var \$users/', $contents);
377 $this->assertNoPattern('/var \$articles/', $contents);
378
379 $file->delete();
380 App::build();
381 }
382
383 /**
384 * Test schema run create with no table args.
385 *
386 * @return void
387 * @access public
388 */
389 function testCreateNoArgs() {
390 $this->Shell->params = array(
391 'connection' => 'test_suite',
392 'path' => APP . 'config' . DS . 'sql'
393 );
394 $this->Shell->args = array('i18n');
395 $this->Shell->startup();
396 $this->Shell->setReturnValue('in', 'y');
397 $this->Shell->create();
398
399 $db =& ConnectionManager::getDataSource('test_suite');
400 $sources = $db->listSources();
401 $this->assertTrue(in_array($db->config['prefix'] . 'i18n', $sources));
402
403 $schema =& new i18nSchema();
404 $db->execute($db->dropSchema($schema));
405 }
406
407 /**
408 * Test schema run create with no table args.
409 *
410 * @return void
411 * @access public
412 */
413 function testCreateWithTableArgs() {
414 $this->Shell->params = array(
415 'connection' => 'test_suite',
416 'name' => 'DbAcl',
417 'path' => APP . 'config' . DS . 'schema'
418 );
419 $this->Shell->args = array('DbAcl', 'acos');
420 $this->Shell->startup();
421 $this->Shell->setReturnValue('in', 'y');
422 $this->Shell->create();
423
424 $db =& ConnectionManager::getDataSource('test_suite');
425 $sources = $db->listSources();
426 $this->assertTrue(in_array($db->config['prefix'] . 'acos', $sources));
427 $this->assertFalse(in_array($db->config['prefix'] . 'aros', $sources));
428 $this->assertFalse(in_array('aros_acos', $sources));
429
430 $db->execute('DROP TABLE ' . $db->config['prefix'] . 'acos');
431 }
432
433 /**
434 * test run update with a table arg.
435 *
436 * @return void
437 * @access public
438 */
439 function testUpdateWithTable() {
440 $this->Shell->params = array(
441 'connection' => 'test_suite',
442 'f' => true
443 );
444 $this->Shell->args = array('SchemaShellTest', 'articles');
445 $this->Shell->startup();
446 $this->Shell->setReturnValue('in', 'y');
447 $this->Shell->update();
448
449 $article =& new Model(array('name' => 'Article', 'ds' => 'test_suite'));
450 $fields = $article->schema();
451 $this->assertTrue(isset($fields['summary']));
452
453 $this->_fixtures['core.article']->drop($this->db);
454 $this->_fixtures['core.article']->create($this->db);
455 }
456
457 /**
458 * test that the plugin param creates the correct path in the schema object.
459 *
460 * @return void
461 * @access public
462 */
463 function testPluginParam() {
464 App::build(array(
465 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
466 ));
467 $this->Shell->params = array(
468 'plugin' => 'TestPlugin',
469 'connection' => 'test_suite'
470 );
471 $this->Shell->startup();
472 $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin' . DS . 'config' . DS . 'schema';
473 $this->assertEqual($this->Shell->Schema->path, $expected);
474
475 App::build();
476 }
477
478 /**
479 * test that using Plugin.name with write.
480 *
481 * @return void
482 * @access public
483 */
484 function testPluginDotSyntaxWithCreate() {
485 App::build(array(
486 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
487 ));
488 $this->Shell->params = array(
489 'connection' => 'test_suite'
490 );
491 $this->Shell->args = array('TestPlugin.TestPluginApp');
492 $this->Shell->startup();
493 $this->Shell->setReturnValue('in', 'y');
494 $this->Shell->create();
495
496 $db =& ConnectionManager::getDataSource('test_suite');
497 $sources = $db->listSources();
498 $this->assertTrue(in_array($db->config['prefix'] . 'acos', $sources));
499
500 $db->execute('DROP TABLE ' . $db->config['prefix'] . 'acos');
501 App::build();
502 }
503 }