2009-08-08 02:10:39 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* SchemaShellTest Test file
|
|
|
|
*
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2009-08-08 02:10:39 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2009-08-08 02:10:39 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Console.Command
|
2009-08-08 02:10:39 +00:00
|
|
|
* @since CakePHP v 1.3
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2009-08-08 02:10:39 +00:00
|
|
|
*/
|
|
|
|
|
2010-12-09 03:45:18 +00:00
|
|
|
App::uses('ShellDispatcher', 'Console');
|
2011-03-08 05:57:52 +00:00
|
|
|
App::uses('ConsoleOutput', 'Console');
|
|
|
|
App::uses('ConsoleInput', 'Console');
|
2010-12-09 03:45:18 +00:00
|
|
|
App::uses('Shell', 'Console');
|
|
|
|
App::uses('CakeSchema', 'Model');
|
2011-03-08 05:57:52 +00:00
|
|
|
App::uses('SchemaShell', 'Console/Command');
|
2009-08-08 02:10:39 +00:00
|
|
|
|
2009-09-07 01:05:24 +00:00
|
|
|
/**
|
|
|
|
* Test for Schema database management
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Console.Command
|
2009-09-07 01:05:24 +00:00
|
|
|
*/
|
|
|
|
class SchemaShellTestSchema extends CakeSchema {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* connection property
|
|
|
|
*
|
2013-06-08 02:29:08 +00:00
|
|
|
* @var string
|
2009-09-07 01:05:24 +00:00
|
|
|
*/
|
2010-09-20 02:58:30 +00:00
|
|
|
public $connection = 'test';
|
2009-09-07 01:05:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* comments property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $comments = array(
|
2009-09-07 01:05:24 +00:00
|
|
|
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
|
|
|
|
'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0),
|
|
|
|
'user_id' => array('type' => 'integer', 'null' => false),
|
|
|
|
'title' => array('type' => 'string', 'null' => false, 'length' => 100),
|
|
|
|
'comment' => array('type' => 'text', 'null' => false, 'default' => null),
|
|
|
|
'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1),
|
|
|
|
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
|
|
|
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
|
|
|
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* posts property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $articles = array(
|
2009-09-07 01:05:24 +00:00
|
|
|
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
|
|
|
|
'user_id' => array('type' => 'integer', 'null' => true, 'default' => ''),
|
|
|
|
'title' => array('type' => 'string', 'null' => false, 'default' => 'Title'),
|
|
|
|
'body' => array('type' => 'text', 'null' => true, 'default' => null),
|
|
|
|
'summary' => array('type' => 'text', 'null' => true),
|
|
|
|
'published' => array('type' => 'string', 'null' => true, 'default' => 'Y', 'length' => 1),
|
|
|
|
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
|
|
|
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
|
|
|
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
|
|
|
|
);
|
2013-04-03 15:37:09 +00:00
|
|
|
|
|
|
|
public $newone = array(
|
|
|
|
'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'),
|
|
|
|
'testit' => array('type' => 'string', 'null' => false, 'default' => 'Title'),
|
|
|
|
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
|
|
|
'updated' => array('type' => 'datetime', 'null' => true, 'default' => null),
|
|
|
|
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)),
|
|
|
|
);
|
2009-09-07 01:05:24 +00:00
|
|
|
}
|
|
|
|
|
2009-08-08 02:10:39 +00:00
|
|
|
/**
|
|
|
|
* SchemaShellTest class
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Console.Command
|
2009-08-08 02:10:39 +00:00
|
|
|
*/
|
|
|
|
class SchemaShellTest extends CakeTestCase {
|
|
|
|
|
2009-11-14 12:18:31 +00:00
|
|
|
/**
|
|
|
|
* Fixtures
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-03-05 03:13:45 +00:00
|
|
|
public $fixtures = array(
|
|
|
|
'core.article', 'core.user', 'core.post', 'core.auth_user', 'core.author',
|
|
|
|
'core.comment', 'core.test_plugin_comment', 'core.aco', 'core.aro', 'core.aros_aco',
|
2010-03-14 17:06:22 +00:00
|
|
|
);
|
2009-11-14 12:18:31 +00:00
|
|
|
|
2009-08-08 02:10:39 +00:00
|
|
|
/**
|
2011-12-04 21:27:51 +00:00
|
|
|
* setUp method
|
2009-08-08 02:10:39 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-12-28 07:03:39 +00:00
|
|
|
public function setUp() : void {
|
2010-10-07 02:52:42 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$out = $this->getMock('ConsoleOutput', array(), array(), '', false);
|
|
|
|
$in = $this->getMock('ConsoleInput', array(), array(), '', false);
|
2010-05-23 04:17:22 +00:00
|
|
|
$this->Shell = $this->getMock(
|
|
|
|
'SchemaShell',
|
|
|
|
array('in', 'out', 'hr', 'createFile', 'error', 'err', '_stop'),
|
2010-10-24 19:11:30 +00:00
|
|
|
array($out, $out, $in)
|
2010-05-23 04:17:22 +00:00
|
|
|
);
|
2009-08-08 02:10:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-11 14:50:54 +00:00
|
|
|
* tearDown method
|
2009-08-08 02:10:39 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-12-28 07:03:39 +00:00
|
|
|
public function tearDown() : void {
|
2010-10-07 02:52:42 +00:00
|
|
|
parent::tearDown();
|
2010-09-21 03:05:35 +00:00
|
|
|
if (!empty($this->file) && $this->file instanceof File) {
|
|
|
|
$this->file->delete();
|
|
|
|
unset($this->file);
|
|
|
|
}
|
2009-08-08 02:10:39 +00:00
|
|
|
}
|
|
|
|
|
2009-08-08 02:19:40 +00:00
|
|
|
/**
|
|
|
|
* test startup method
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testStartup() {
|
2009-08-09 03:21:24 +00:00
|
|
|
$this->Shell->startup();
|
|
|
|
$this->assertTrue(isset($this->Shell->Schema));
|
2013-09-25 02:10:36 +00:00
|
|
|
$this->assertInstanceOf('CakeSchema', $this->Shell->Schema);
|
2012-05-13 17:12:31 +00:00
|
|
|
$this->assertEquals(Inflector::camelize(Inflector::slug(APP_DIR)), $this->Shell->Schema->name);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('schema.php', $this->Shell->Schema->file);
|
2009-08-09 03:21:24 +00:00
|
|
|
|
2010-10-10 04:23:25 +00:00
|
|
|
$this->Shell->Schema = null;
|
2009-08-09 03:21:24 +00:00
|
|
|
$this->Shell->params = array(
|
2009-08-08 02:19:40 +00:00
|
|
|
'name' => 'TestSchema'
|
|
|
|
);
|
2009-08-09 03:21:24 +00:00
|
|
|
$this->Shell->startup();
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('TestSchema', $this->Shell->Schema->name);
|
|
|
|
$this->assertEquals('test_schema.php', $this->Shell->Schema->file);
|
|
|
|
$this->assertEquals('default', $this->Shell->Schema->connection);
|
2017-05-12 06:02:36 +00:00
|
|
|
$this->assertEquals(CONFIG . 'Schema', $this->Shell->Schema->path);
|
2009-09-07 01:05:24 +00:00
|
|
|
|
2010-10-10 04:23:25 +00:00
|
|
|
$this->Shell->Schema = null;
|
2009-08-09 03:21:24 +00:00
|
|
|
$this->Shell->params = array(
|
2009-08-08 02:19:40 +00:00
|
|
|
'file' => 'other_file.php',
|
2010-09-20 02:58:30 +00:00
|
|
|
'connection' => 'test',
|
2009-08-08 02:19:40 +00:00
|
|
|
'path' => '/test/path'
|
|
|
|
);
|
2009-08-09 03:21:24 +00:00
|
|
|
$this->Shell->startup();
|
2012-05-13 17:12:31 +00:00
|
|
|
$this->assertEquals(Inflector::camelize(Inflector::slug(APP_DIR)), $this->Shell->Schema->name);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('other_file.php', $this->Shell->Schema->file);
|
|
|
|
$this->assertEquals('test', $this->Shell->Schema->connection);
|
|
|
|
$this->assertEquals('/test/path', $this->Shell->Schema->path);
|
2009-08-08 02:19:40 +00:00
|
|
|
}
|
2009-08-08 02:28:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test View - and that it dumps the schema file to stdout
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testView() {
|
2009-08-09 03:21:24 +00:00
|
|
|
$this->Shell->startup();
|
2017-05-12 06:02:36 +00:00
|
|
|
$this->Shell->Schema->path = CONFIG . 'Schema';
|
2009-08-09 03:21:24 +00:00
|
|
|
$this->Shell->params['file'] = 'i18n.php';
|
2010-05-23 04:17:22 +00:00
|
|
|
$this->Shell->expects($this->once())->method('_stop');
|
|
|
|
$this->Shell->expects($this->once())->method('out');
|
2009-08-09 03:21:24 +00:00
|
|
|
$this->Shell->view();
|
2009-08-08 02:28:28 +00:00
|
|
|
}
|
|
|
|
|
2009-10-04 18:06:07 +00:00
|
|
|
/**
|
|
|
|
* test that view() can find plugin schema files.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testViewWithPlugins() {
|
2009-10-04 18:06:07 +00:00
|
|
|
App::build(array(
|
2012-02-18 12:31:29 +00:00
|
|
|
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
|
2009-10-04 18:06:07 +00:00
|
|
|
));
|
2011-05-12 04:55:16 +00:00
|
|
|
CakePlugin::load('TestPlugin');
|
2009-10-04 18:06:07 +00:00
|
|
|
$this->Shell->args = array('TestPlugin.schema');
|
|
|
|
$this->Shell->startup();
|
2010-05-23 04:17:22 +00:00
|
|
|
$this->Shell->expects($this->exactly(2))->method('_stop');
|
2012-02-26 15:21:04 +00:00
|
|
|
$this->Shell->expects($this->atLeastOnce())->method('out');
|
2009-10-04 18:06:07 +00:00
|
|
|
$this->Shell->view();
|
|
|
|
|
|
|
|
$this->Shell->args = array();
|
|
|
|
$this->Shell->params = array('plugin' => 'TestPlugin');
|
|
|
|
$this->Shell->startup();
|
|
|
|
$this->Shell->view();
|
|
|
|
|
|
|
|
App::build();
|
2011-05-12 04:55:16 +00:00
|
|
|
CakePlugin::unload();
|
2009-10-04 18:06:07 +00:00
|
|
|
}
|
|
|
|
|
2009-08-09 03:00:40 +00:00
|
|
|
/**
|
|
|
|
* test dump() with sql file generation
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testDumpWithFileWriting() {
|
2009-10-04 18:47:35 +00:00
|
|
|
$this->Shell->params = array(
|
|
|
|
'name' => 'i18n',
|
2011-05-27 06:48:36 +00:00
|
|
|
'connection' => 'test',
|
2009-10-04 18:47:35 +00:00
|
|
|
'write' => TMP . 'tests' . DS . 'i18n.sql'
|
|
|
|
);
|
2010-05-23 04:17:22 +00:00
|
|
|
$this->Shell->expects($this->once())->method('_stop');
|
2009-08-09 03:21:24 +00:00
|
|
|
$this->Shell->startup();
|
|
|
|
$this->Shell->dump();
|
2009-08-09 03:00:40 +00:00
|
|
|
|
2010-11-13 04:05:44 +00:00
|
|
|
$this->file = new File(TMP . 'tests' . DS . 'i18n.sql');
|
2010-09-21 03:05:35 +00:00
|
|
|
$contents = $this->file->read();
|
2022-12-30 08:28:11 +00:00
|
|
|
$this->assertMatchesRegularExpression('/DROP TABLE/', $contents);
|
|
|
|
$this->assertMatchesRegularExpression('/CREATE TABLE.*?i18n/', $contents);
|
|
|
|
$this->assertMatchesRegularExpression('/id/', $contents);
|
|
|
|
$this->assertMatchesRegularExpression('/model/', $contents);
|
|
|
|
$this->assertMatchesRegularExpression('/field/', $contents);
|
|
|
|
$this->assertMatchesRegularExpression('/locale/', $contents);
|
|
|
|
$this->assertMatchesRegularExpression('/foreign_key/', $contents);
|
|
|
|
$this->assertMatchesRegularExpression('/content/', $contents);
|
2009-10-04 18:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that dump() can find and work with plugin schema files.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testDumpFileWritingWithPlugins() {
|
2009-10-04 18:47:35 +00:00
|
|
|
App::build(array(
|
2012-02-18 12:31:29 +00:00
|
|
|
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
|
2009-10-04 18:47:35 +00:00
|
|
|
));
|
2011-05-12 04:55:16 +00:00
|
|
|
CakePlugin::load('TestPlugin');
|
2009-10-04 18:47:35 +00:00
|
|
|
$this->Shell->args = array('TestPlugin.TestPluginApp');
|
|
|
|
$this->Shell->params = array(
|
2010-09-20 02:58:30 +00:00
|
|
|
'connection' => 'test',
|
2009-10-04 18:47:35 +00:00
|
|
|
'write' => TMP . 'tests' . DS . 'dump_test.sql'
|
|
|
|
);
|
|
|
|
$this->Shell->startup();
|
2010-05-23 04:17:22 +00:00
|
|
|
$this->Shell->expects($this->once())->method('_stop');
|
2009-10-04 18:47:35 +00:00
|
|
|
$this->Shell->dump();
|
|
|
|
|
2010-11-13 04:05:44 +00:00
|
|
|
$this->file = new File(TMP . 'tests' . DS . 'dump_test.sql');
|
2010-09-21 03:05:35 +00:00
|
|
|
$contents = $this->file->read();
|
2009-10-04 18:47:35 +00:00
|
|
|
|
2022-12-30 08:28:11 +00:00
|
|
|
$this->assertMatchesRegularExpression('/CREATE TABLE.*?test_plugin_acos/', $contents);
|
|
|
|
$this->assertMatchesRegularExpression('/id/', $contents);
|
|
|
|
$this->assertMatchesRegularExpression('/model/', $contents);
|
2009-10-04 18:47:35 +00:00
|
|
|
|
2010-09-21 03:05:35 +00:00
|
|
|
$this->file->delete();
|
2009-10-04 18:47:35 +00:00
|
|
|
App::build();
|
2011-05-12 04:55:16 +00:00
|
|
|
CakePlugin::unload();
|
2009-08-08 02:28:28 +00:00
|
|
|
}
|
2009-08-09 03:21:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test generate with snapshot generation
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-05-23 04:17:22 +00:00
|
|
|
public function testGenerateSnapshot() {
|
2009-08-09 03:21:24 +00:00
|
|
|
$this->Shell->path = TMP;
|
|
|
|
$this->Shell->params['file'] = 'schema.php';
|
2011-08-30 04:37:57 +00:00
|
|
|
$this->Shell->params['force'] = false;
|
2009-08-09 03:21:24 +00:00
|
|
|
$this->Shell->args = array('snapshot');
|
2010-05-23 04:17:22 +00:00
|
|
|
$this->Shell->Schema = $this->getMock('CakeSchema');
|
|
|
|
$this->Shell->Schema->expects($this->at(0))->method('read')->will($this->returnValue(array('schema data')));
|
|
|
|
$this->Shell->Schema->expects($this->at(0))->method('write')->will($this->returnValue(true));
|
2009-08-09 03:21:24 +00:00
|
|
|
|
2010-05-23 04:17:22 +00:00
|
|
|
$this->Shell->Schema->expects($this->at(1))->method('read');
|
2010-07-08 03:44:12 +00:00
|
|
|
$this->Shell->Schema->expects($this->at(1))->method('write')->with(array('schema data', 'file' => 'schema_0.php'));
|
2009-08-09 03:21:24 +00:00
|
|
|
|
|
|
|
$this->Shell->generate();
|
|
|
|
}
|
2009-08-09 03:46:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test generate without a snapshot.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testGenerateNoOverwrite() {
|
2009-08-09 03:46:00 +00:00
|
|
|
touch(TMP . 'schema.php');
|
|
|
|
$this->Shell->params['file'] = 'schema.php';
|
2011-08-30 04:37:57 +00:00
|
|
|
$this->Shell->params['force'] = false;
|
2009-08-09 03:46:00 +00:00
|
|
|
$this->Shell->args = array();
|
|
|
|
|
2010-05-23 04:17:22 +00:00
|
|
|
$this->Shell->expects($this->once())->method('in')->will($this->returnValue('q'));
|
|
|
|
$this->Shell->Schema = $this->getMock('CakeSchema');
|
2009-08-09 03:46:00 +00:00
|
|
|
$this->Shell->Schema->path = TMP;
|
2010-05-23 04:17:22 +00:00
|
|
|
$this->Shell->Schema->expects($this->never())->method('read');
|
2009-08-09 03:46:00 +00:00
|
|
|
|
2013-01-23 12:45:50 +00:00
|
|
|
$this->Shell->generate();
|
2009-08-09 03:46:00 +00:00
|
|
|
unlink(TMP . 'schema.php');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test generate with overwriting of the schema files.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testGenerateOverwrite() {
|
2009-08-09 03:46:00 +00:00
|
|
|
touch(TMP . 'schema.php');
|
|
|
|
$this->Shell->params['file'] = 'schema.php';
|
2011-08-30 04:37:57 +00:00
|
|
|
$this->Shell->params['force'] = false;
|
2009-08-09 03:46:00 +00:00
|
|
|
$this->Shell->args = array();
|
|
|
|
|
2010-05-23 04:17:22 +00:00
|
|
|
$this->Shell->expects($this->once())->method('in')->will($this->returnValue('o'));
|
|
|
|
|
|
|
|
$this->Shell->expects($this->at(2))->method('out')
|
|
|
|
->with(new PHPUnit_Framework_Constraint_PCREMatch('/Schema file:\s[a-z\.]+\sgenerated/'));
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-05-23 04:17:22 +00:00
|
|
|
$this->Shell->Schema = $this->getMock('CakeSchema');
|
2009-08-09 03:46:00 +00:00
|
|
|
$this->Shell->Schema->path = TMP;
|
2010-05-23 04:17:22 +00:00
|
|
|
$this->Shell->Schema->expects($this->once())->method('read')->will($this->returnValue(array('schema data')));
|
|
|
|
$this->Shell->Schema->expects($this->once())->method('write')->will($this->returnValue(true));
|
2009-08-09 03:46:00 +00:00
|
|
|
|
2010-05-23 04:17:22 +00:00
|
|
|
$this->Shell->Schema->expects($this->once())->method('read');
|
|
|
|
$this->Shell->Schema->expects($this->once())->method('write')
|
|
|
|
->with(array('schema data', 'file' => 'schema.php'));
|
2009-08-09 03:46:00 +00:00
|
|
|
|
|
|
|
$this->Shell->generate();
|
|
|
|
unlink(TMP . 'schema.php');
|
|
|
|
}
|
2009-08-11 01:54:29 +00:00
|
|
|
|
2009-10-09 02:40:17 +00:00
|
|
|
/**
|
|
|
|
* test that generate() can read plugin dirs and generate schema files for the models
|
|
|
|
* in a plugin.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testGenerateWithPlugins() {
|
2009-10-09 02:40:17 +00:00
|
|
|
App::build(array(
|
2012-02-18 12:31:29 +00:00
|
|
|
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
|
2012-02-18 12:04:54 +00:00
|
|
|
), App::RESET);
|
2011-05-12 04:55:16 +00:00
|
|
|
CakePlugin::load('TestPlugin');
|
2010-09-21 03:05:35 +00:00
|
|
|
|
2010-12-03 18:37:46 +00:00
|
|
|
$this->db->cacheSources = false;
|
2009-10-09 02:40:17 +00:00
|
|
|
$this->Shell->params = array(
|
|
|
|
'plugin' => 'TestPlugin',
|
2011-08-30 04:37:57 +00:00
|
|
|
'connection' => 'test',
|
|
|
|
'force' => false
|
2009-10-09 02:40:17 +00:00
|
|
|
);
|
|
|
|
$this->Shell->startup();
|
|
|
|
$this->Shell->Schema->path = TMP . 'tests' . DS;
|
|
|
|
|
|
|
|
$this->Shell->generate();
|
2010-09-21 03:05:35 +00:00
|
|
|
$this->file = new File(TMP . 'tests' . DS . 'schema.php');
|
|
|
|
$contents = $this->file->read();
|
2009-10-09 02:40:17 +00:00
|
|
|
|
2022-12-30 08:28:11 +00:00
|
|
|
$this->assertMatchesRegularExpression('/class TestPluginSchema/', $contents);
|
|
|
|
$this->assertMatchesRegularExpression('/public \$posts/', $contents);
|
|
|
|
$this->assertMatchesRegularExpression('/public \$auth_users/', $contents);
|
|
|
|
$this->assertMatchesRegularExpression('/public \$authors/', $contents);
|
|
|
|
$this->assertMatchesRegularExpression('/public \$test_plugin_comments/', $contents);
|
2022-12-30 08:32:45 +00:00
|
|
|
$this->assertDoesNotMatchRegularExpression('/public \$users/', $contents);
|
|
|
|
$this->assertDoesNotMatchRegularExpression('/public \$articles/', $contents);
|
2011-05-12 04:55:16 +00:00
|
|
|
CakePlugin::unload();
|
2009-10-09 02:40:17 +00:00
|
|
|
}
|
|
|
|
|
2012-08-08 12:15:28 +00:00
|
|
|
/**
|
|
|
|
* test generate with specific models
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testGenerateModels() {
|
|
|
|
App::build(array(
|
|
|
|
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
|
|
|
|
), App::RESET);
|
|
|
|
CakePlugin::load('TestPlugin');
|
|
|
|
|
|
|
|
$this->db->cacheSources = false;
|
|
|
|
$this->Shell->params = array(
|
|
|
|
'plugin' => 'TestPlugin',
|
|
|
|
'connection' => 'test',
|
|
|
|
'models' => 'TestPluginComment',
|
|
|
|
'force' => false,
|
|
|
|
'overwrite' => true
|
|
|
|
);
|
|
|
|
$this->Shell->startup();
|
|
|
|
$this->Shell->Schema->path = TMP . 'tests' . DS;
|
|
|
|
|
|
|
|
$this->Shell->generate();
|
|
|
|
$this->file = new File(TMP . 'tests' . DS . 'schema.php');
|
|
|
|
$contents = $this->file->read();
|
|
|
|
|
2022-12-30 08:28:11 +00:00
|
|
|
$this->assertMatchesRegularExpression('/class TestPluginSchema/', $contents);
|
|
|
|
$this->assertMatchesRegularExpression('/public \$test_plugin_comments/', $contents);
|
2022-12-30 08:32:45 +00:00
|
|
|
$this->assertDoesNotMatchRegularExpression('/public \$authors/', $contents);
|
|
|
|
$this->assertDoesNotMatchRegularExpression('/public \$auth_users/', $contents);
|
|
|
|
$this->assertDoesNotMatchRegularExpression('/public \$posts/', $contents);
|
2012-08-08 12:15:28 +00:00
|
|
|
CakePlugin::unload();
|
|
|
|
}
|
|
|
|
|
2013-03-04 14:10:06 +00:00
|
|
|
/**
|
|
|
|
* test generate with excluded tables
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testGenerateExclude() {
|
2013-03-05 03:37:48 +00:00
|
|
|
Configure::write('Acl.database', 'test');
|
2013-03-04 14:10:06 +00:00
|
|
|
$this->db->cacheSources = false;
|
|
|
|
$this->Shell->params = array(
|
2013-03-05 03:13:45 +00:00
|
|
|
'connection' => 'test',
|
2013-03-04 14:10:06 +00:00
|
|
|
'force' => false,
|
2013-03-05 03:13:45 +00:00
|
|
|
'models' => 'Aro, Aco, Permission',
|
2013-03-04 14:10:06 +00:00
|
|
|
'overwrite' => true,
|
2013-03-05 03:13:45 +00:00
|
|
|
'exclude' => 'acos, aros',
|
2013-03-04 14:10:06 +00:00
|
|
|
);
|
|
|
|
$this->Shell->startup();
|
|
|
|
$this->Shell->Schema->path = TMP . 'tests' . DS;
|
|
|
|
|
|
|
|
$this->Shell->generate();
|
|
|
|
$this->file = new File(TMP . 'tests' . DS . 'schema.php');
|
|
|
|
$contents = $this->file->read();
|
|
|
|
|
2022-12-30 09:44:36 +00:00
|
|
|
$this->assertStringNotContainsString('public $acos = array(', $contents);
|
|
|
|
$this->assertStringNotContainsString('public $aros = array(', $contents);
|
2022-12-30 08:53:58 +00:00
|
|
|
$this->assertStringContainsString('public $aros_acos = array(', $contents);
|
2013-03-04 14:10:06 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 07:13:54 +00:00
|
|
|
/**
|
|
|
|
* Test schema run create with --yes option
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCreateOptionYes() {
|
|
|
|
$this->Shell = $this->getMock(
|
|
|
|
'SchemaShell',
|
|
|
|
array('in', 'out', 'hr', 'createFile', 'error', 'err', '_stop', '_run'),
|
|
|
|
array(&$this->Dispatcher)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->Shell->params = array(
|
|
|
|
'connection' => 'test',
|
|
|
|
'yes' => true,
|
|
|
|
);
|
|
|
|
$this->Shell->args = array('i18n');
|
|
|
|
$this->Shell->expects($this->never())->method('in');
|
|
|
|
$this->Shell->expects($this->exactly(2))->method('_run');
|
|
|
|
$this->Shell->startup();
|
|
|
|
$this->Shell->create();
|
|
|
|
}
|
|
|
|
|
2009-08-11 01:54:29 +00:00
|
|
|
/**
|
|
|
|
* Test schema run create with no table args.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testCreateNoArgs() {
|
2009-08-11 01:54:29 +00:00
|
|
|
$this->Shell->params = array(
|
2010-12-02 04:49:43 +00:00
|
|
|
'connection' => 'test'
|
2009-08-11 01:54:29 +00:00
|
|
|
);
|
2009-10-04 15:16:23 +00:00
|
|
|
$this->Shell->args = array('i18n');
|
2009-08-11 01:54:29 +00:00
|
|
|
$this->Shell->startup();
|
2010-05-23 04:17:22 +00:00
|
|
|
$this->Shell->expects($this->any())->method('in')->will($this->returnValue('y'));
|
2009-10-04 15:16:23 +00:00
|
|
|
$this->Shell->create();
|
2009-09-07 01:05:24 +00:00
|
|
|
|
2010-09-20 02:58:30 +00:00
|
|
|
$db = ConnectionManager::getDataSource('test');
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-12-02 04:49:43 +00:00
|
|
|
$db->cacheSources = false;
|
2009-08-11 01:54:29 +00:00
|
|
|
$sources = $db->listSources();
|
2009-09-07 01:05:24 +00:00
|
|
|
$this->assertTrue(in_array($db->config['prefix'] . 'i18n', $sources));
|
2009-09-16 06:02:40 +00:00
|
|
|
|
2010-05-23 04:17:22 +00:00
|
|
|
$schema = new i18nSchema();
|
2009-09-16 06:02:40 +00:00
|
|
|
$db->execute($db->dropSchema($schema));
|
2009-08-11 01:54:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test schema run create with no table args.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testCreateWithTableArgs() {
|
2010-09-25 04:13:37 +00:00
|
|
|
$db = ConnectionManager::getDataSource('test');
|
|
|
|
$sources = $db->listSources();
|
2013-03-05 03:37:48 +00:00
|
|
|
if (in_array('i18n', $sources)) {
|
|
|
|
$this->markTestSkipped('i18n table already exists, cannot try to create it again.');
|
2010-09-25 04:13:37 +00:00
|
|
|
}
|
2009-08-11 01:54:29 +00:00
|
|
|
$this->Shell->params = array(
|
2010-09-20 02:58:30 +00:00
|
|
|
'connection' => 'test',
|
2013-03-05 03:37:48 +00:00
|
|
|
'name' => 'I18n',
|
2017-05-12 06:02:36 +00:00
|
|
|
'path' => CONFIG . 'Schema'
|
2009-08-11 01:54:29 +00:00
|
|
|
);
|
2013-03-05 03:37:48 +00:00
|
|
|
$this->Shell->args = array('I18n', 'i18n');
|
2009-08-11 01:54:29 +00:00
|
|
|
$this->Shell->startup();
|
2010-05-23 04:17:22 +00:00
|
|
|
$this->Shell->expects($this->any())->method('in')->will($this->returnValue('y'));
|
2009-10-04 15:16:23 +00:00
|
|
|
$this->Shell->create();
|
2009-08-11 01:54:29 +00:00
|
|
|
|
2010-09-25 04:13:37 +00:00
|
|
|
$db = ConnectionManager::getDataSource('test');
|
2010-12-02 04:49:43 +00:00
|
|
|
$db->cacheSources = false;
|
2009-08-11 01:54:29 +00:00
|
|
|
$sources = $db->listSources();
|
2013-03-05 03:37:48 +00:00
|
|
|
$this->assertTrue(in_array($db->config['prefix'] . 'i18n', $sources), 'i18n should be present.');
|
2010-10-19 03:26:50 +00:00
|
|
|
|
2013-03-05 03:37:48 +00:00
|
|
|
$schema = new I18nSchema();
|
|
|
|
$db->execute($db->dropSchema($schema, 'i18n'));
|
2009-08-11 01:54:29 +00:00
|
|
|
}
|
|
|
|
|
2009-09-07 01:05:24 +00:00
|
|
|
/**
|
|
|
|
* test run update with a table arg.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testUpdateWithTable() {
|
2010-09-22 03:25:08 +00:00
|
|
|
$this->Shell = $this->getMock(
|
|
|
|
'SchemaShell',
|
2011-08-20 05:39:30 +00:00
|
|
|
array('in', 'out', 'hr', 'createFile', 'error', 'err', '_stop', '_run'),
|
2010-09-22 03:25:08 +00:00
|
|
|
array(&$this->Dispatcher)
|
|
|
|
);
|
|
|
|
|
2009-09-07 01:05:24 +00:00
|
|
|
$this->Shell->params = array(
|
2010-09-20 02:58:30 +00:00
|
|
|
'connection' => 'test',
|
2010-10-16 04:43:03 +00:00
|
|
|
'force' => true
|
2009-09-07 01:05:24 +00:00
|
|
|
);
|
2009-10-04 15:16:23 +00:00
|
|
|
$this->Shell->args = array('SchemaShellTest', 'articles');
|
2009-09-07 01:05:24 +00:00
|
|
|
$this->Shell->startup();
|
2013-03-01 03:54:31 +00:00
|
|
|
$this->Shell->expects($this->any())
|
|
|
|
->method('in')
|
|
|
|
->will($this->returnValue('y'));
|
|
|
|
$this->Shell->expects($this->once())
|
|
|
|
->method('_run')
|
2010-09-22 03:25:08 +00:00
|
|
|
->with($this->arrayHasKey('articles'), 'update', $this->isInstanceOf('CakeSchema'));
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2009-10-04 15:16:23 +00:00
|
|
|
$this->Shell->update();
|
2009-09-07 01:05:24 +00:00
|
|
|
}
|
|
|
|
|
2013-04-04 12:07:51 +00:00
|
|
|
/**
|
|
|
|
* test run update with a table arg. and checks that a CREATE statement is issued
|
|
|
|
* table creation
|
|
|
|
* @return void
|
|
|
|
*/
|
2013-04-03 15:37:09 +00:00
|
|
|
public function testUpdateWithTableCreate() {
|
|
|
|
$this->Shell = $this->getMock(
|
|
|
|
'SchemaShell',
|
|
|
|
array('in', 'out', 'hr', 'createFile', 'error', 'err', '_stop', '_run'),
|
|
|
|
array(&$this->Dispatcher)
|
|
|
|
);
|
2013-04-04 12:07:51 +00:00
|
|
|
|
2013-04-03 15:37:09 +00:00
|
|
|
$this->Shell->params = array(
|
|
|
|
'connection' => 'test',
|
|
|
|
'force' => true
|
|
|
|
);
|
|
|
|
$this->Shell->args = array('SchemaShellTest', 'newone');
|
|
|
|
$this->Shell->startup();
|
|
|
|
$this->Shell->expects($this->any())
|
|
|
|
->method('in')
|
|
|
|
->will($this->returnValue('y'));
|
2014-02-11 21:38:24 +00:00
|
|
|
$this->Shell->expects($this->once())
|
2013-04-03 15:37:09 +00:00
|
|
|
->method('_run')
|
|
|
|
->with($this->arrayHasKey('newone'), 'update', $this->isInstanceOf('CakeSchema'));
|
2013-04-04 12:07:51 +00:00
|
|
|
|
2013-04-03 15:37:09 +00:00
|
|
|
$this->Shell->update();
|
|
|
|
}
|
|
|
|
|
2013-08-29 07:13:54 +00:00
|
|
|
/**
|
|
|
|
* test run update with --yes option
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testUpdateWithOptionYes() {
|
|
|
|
$this->Shell = $this->getMock(
|
|
|
|
'SchemaShell',
|
|
|
|
array('in', 'out', 'hr', 'createFile', 'error', 'err', '_stop', '_run'),
|
|
|
|
array(&$this->Dispatcher)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->Shell->params = array(
|
|
|
|
'connection' => 'test',
|
|
|
|
'force' => true,
|
|
|
|
'yes' => true,
|
|
|
|
);
|
|
|
|
$this->Shell->args = array('SchemaShellTest', 'articles');
|
|
|
|
$this->Shell->startup();
|
|
|
|
$this->Shell->expects($this->never())->method('in');
|
|
|
|
$this->Shell->expects($this->once())
|
|
|
|
->method('_run')
|
|
|
|
->with($this->arrayHasKey('articles'), 'update', $this->isInstanceOf('CakeSchema'));
|
|
|
|
|
|
|
|
$this->Shell->update();
|
|
|
|
}
|
|
|
|
|
2009-08-20 05:07:21 +00:00
|
|
|
/**
|
|
|
|
* test that the plugin param creates the correct path in the schema object.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testPluginParam() {
|
2009-08-20 05:07:21 +00:00
|
|
|
App::build(array(
|
2012-02-18 12:31:29 +00:00
|
|
|
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
|
2009-08-20 05:07:21 +00:00
|
|
|
));
|
2011-05-12 04:55:16 +00:00
|
|
|
CakePlugin::load('TestPlugin');
|
2009-08-20 05:07:21 +00:00
|
|
|
$this->Shell->params = array(
|
|
|
|
'plugin' => 'TestPlugin',
|
2010-09-20 02:58:30 +00:00
|
|
|
'connection' => 'test'
|
2009-08-20 05:07:21 +00:00
|
|
|
);
|
|
|
|
$this->Shell->startup();
|
2011-04-17 10:35:21 +00:00
|
|
|
$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Config' . DS . 'Schema';
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $this->Shell->Schema->path);
|
2011-05-12 04:55:16 +00:00
|
|
|
CakePlugin::unload();
|
2009-10-04 17:32:33 +00:00
|
|
|
}
|
2009-08-20 05:07:21 +00:00
|
|
|
|
2012-08-08 14:35:59 +00:00
|
|
|
/**
|
|
|
|
* test that underscored names also result in CamelCased class names
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testName() {
|
|
|
|
App::build(array(
|
|
|
|
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
|
|
|
|
));
|
|
|
|
CakePlugin::load('TestPlugin');
|
|
|
|
$this->Shell->params = array(
|
|
|
|
'plugin' => 'TestPlugin',
|
|
|
|
'connection' => 'test',
|
2014-11-10 02:52:10 +00:00
|
|
|
'name' => 'custom_names',
|
2012-08-08 14:35:59 +00:00
|
|
|
'force' => false,
|
|
|
|
'overwrite' => true,
|
|
|
|
);
|
|
|
|
$this->Shell->startup();
|
2014-11-10 02:52:10 +00:00
|
|
|
if (file_exists($this->Shell->Schema->path . DS . 'custom_names.php')) {
|
|
|
|
unlink($this->Shell->Schema->path . DS . 'custom_names.php');
|
2012-08-08 14:35:59 +00:00
|
|
|
}
|
|
|
|
$this->Shell->generate();
|
|
|
|
|
2014-11-10 02:52:10 +00:00
|
|
|
$contents = file_get_contents($this->Shell->Schema->path . DS . 'custom_names.php');
|
2022-12-30 08:28:11 +00:00
|
|
|
$this->assertMatchesRegularExpression('/class CustomNamesSchema/', $contents);
|
2014-11-10 02:52:10 +00:00
|
|
|
unlink($this->Shell->Schema->path . DS . 'custom_names.php');
|
2012-08-08 14:35:59 +00:00
|
|
|
CakePlugin::unload();
|
|
|
|
}
|
|
|
|
|
2013-10-10 04:18:27 +00:00
|
|
|
/**
|
|
|
|
* test that passing name and file creates the passed filename with the
|
2013-10-23 02:59:50 +00:00
|
|
|
* passed class name
|
2013-10-10 04:18:27 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testNameAndFile() {
|
|
|
|
App::build(array(
|
|
|
|
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
|
|
|
|
));
|
|
|
|
CakePlugin::load('TestPlugin');
|
|
|
|
$this->Shell->params = array(
|
|
|
|
'plugin' => 'TestPlugin',
|
|
|
|
'connection' => 'test',
|
|
|
|
'name' => 'custom_name',
|
|
|
|
'file' => 'other_name',
|
|
|
|
'force' => false,
|
|
|
|
'overwrite' => true,
|
|
|
|
);
|
|
|
|
$this->Shell->startup();
|
|
|
|
$file = $this->Shell->Schema->path . DS . 'other_name.php';
|
|
|
|
if (file_exists($file)) {
|
|
|
|
unlink($file);
|
|
|
|
}
|
|
|
|
$this->Shell->generate();
|
|
|
|
|
|
|
|
$this->assertFileExists($file);
|
|
|
|
$contents = file_get_contents($file);
|
2022-12-30 08:28:11 +00:00
|
|
|
$this->assertMatchesRegularExpression('/class CustomNameSchema/', $contents);
|
2013-10-10 04:18:27 +00:00
|
|
|
|
|
|
|
if (file_exists($file)) {
|
|
|
|
unlink($file);
|
|
|
|
}
|
|
|
|
CakePlugin::unload();
|
|
|
|
}
|
|
|
|
|
2009-10-04 17:32:33 +00:00
|
|
|
/**
|
|
|
|
* test that using Plugin.name with write.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function testPluginDotSyntaxWithCreate() {
|
2009-10-04 17:32:33 +00:00
|
|
|
App::build(array(
|
2012-02-18 12:31:29 +00:00
|
|
|
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
|
2009-10-04 17:32:33 +00:00
|
|
|
));
|
2011-05-12 04:55:16 +00:00
|
|
|
CakePlugin::load('TestPlugin');
|
2009-08-20 05:07:21 +00:00
|
|
|
$this->Shell->params = array(
|
2010-09-20 02:58:30 +00:00
|
|
|
'connection' => 'test'
|
2009-08-20 05:07:21 +00:00
|
|
|
);
|
2009-10-04 17:32:33 +00:00
|
|
|
$this->Shell->args = array('TestPlugin.TestPluginApp');
|
2009-08-20 05:07:21 +00:00
|
|
|
$this->Shell->startup();
|
2010-05-23 04:17:22 +00:00
|
|
|
$this->Shell->expects($this->any())->method('in')->will($this->returnValue('y'));
|
2009-10-04 17:32:33 +00:00
|
|
|
$this->Shell->create();
|
2009-08-20 05:07:21 +00:00
|
|
|
|
2011-04-29 15:49:33 +00:00
|
|
|
$db = ConnectionManager::getDataSource('test');
|
2009-10-04 17:32:33 +00:00
|
|
|
$sources = $db->listSources();
|
2010-10-24 19:11:30 +00:00
|
|
|
$this->assertTrue(in_array($db->config['prefix'] . 'test_plugin_acos', $sources));
|
2010-10-19 03:26:50 +00:00
|
|
|
|
2010-10-23 04:54:39 +00:00
|
|
|
$schema = new TestPluginAppSchema();
|
|
|
|
$db->execute($db->dropSchema($schema, 'test_plugin_acos'));
|
2011-05-12 04:55:16 +00:00
|
|
|
CakePlugin::unload();
|
2010-09-25 05:25:42 +00:00
|
|
|
}
|
2009-08-08 02:10:39 +00:00
|
|
|
}
|