Skip to content

Commit

Permalink
Adding tests for generate() from jperras
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Aug 9, 2009
1 parent 4965551 commit a275753
Showing 1 changed file with 63 additions and 42 deletions.
105 changes: 63 additions & 42 deletions cake/tests/cases/console/libs/schema.test.php
Expand Up @@ -14,11 +14,12 @@
* @copyright Copyright 2006-2009, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
* @package cake
* @subpackage cake.tests.cases.console.libs.tasks
* @subpackage cake.tests.cases.console.libs.Shells
* @since CakePHP v 1.3
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
App::import('Shell', 'Shell', false);
App::import('Model', 'CakeSchema', false);

if (!defined('DISABLE_AUTO_DISPATCH')) {
define('DISABLE_AUTO_DISPATCH', true);
Expand All @@ -44,11 +45,13 @@
array('in', 'out', 'hr', 'createFile', 'error', 'err', '_stop')
);

Mock::generate('CakeSchema', 'MockSchemaCakeSchema');

/**
* SchemaShellTest class
*
* @package cake
* @subpackage cake.tests.cases.console.libs.tasks
* @subpackage cake.tests.cases.console.libs.Shells
*/
class SchemaShellTest extends CakeTestCase {

Expand All @@ -61,8 +64,8 @@ class SchemaShellTest extends CakeTestCase {
*/
function startTest() {
$this->Dispatcher =& new TestSchemaShellMockShellDispatcher();
$this->Task =& new MockSchemaShell($this->Dispatcher);
$this->Task->Dispatch =& $this->Dispatcher;
$this->Shell =& new MockSchemaShell($this->Dispatcher);
$this->Shell->Dispatch =& $this->Dispatcher;
}

/**
Expand All @@ -81,33 +84,33 @@ function endTest() {
* @return void
**/
function testStartup() {
$this->Task->startup();
$this->assertTrue(isset($this->Task->Schema));
$this->assertTrue(is_a($this->Task->Schema, 'CakeSchema'));
$this->assertEqual($this->Task->Schema->name, 'App');
$this->assertEqual($this->Task->Schema->file, 'schema.php');

unset($this->Task->Schema);
$this->Task->params = array(
$this->Shell->startup();
$this->assertTrue(isset($this->Shell->Schema));
$this->assertTrue(is_a($this->Shell->Schema, 'CakeSchema'));
$this->assertEqual($this->Shell->Schema->name, 'App');
$this->assertEqual($this->Shell->Schema->file, 'schema.php');

unset($this->Shell->Schema);
$this->Shell->params = array(
'name' => 'TestSchema'
);
$this->Task->startup();
$this->assertEqual($this->Task->Schema->name, 'TestSchema');
$this->assertEqual($this->Task->Schema->file, 'test_schema.php');
$this->assertEqual($this->Task->Schema->connection, 'default');
$this->assertEqual($this->Task->Schema->path, APP . 'config' . DS . 'schema');
$this->Shell->startup();
$this->assertEqual($this->Shell->Schema->name, 'TestSchema');
$this->assertEqual($this->Shell->Schema->file, 'test_schema.php');
$this->assertEqual($this->Shell->Schema->connection, 'default');
$this->assertEqual($this->Shell->Schema->path, APP . 'config' . DS . 'schema');

unset($this->Task->Schema);
$this->Task->params = array(
unset($this->Shell->Schema);
$this->Shell->params = array(
'file' => 'other_file.php',
'connection' => 'test_suite',
'path' => '/test/path'
);
$this->Task->startup();
$this->assertEqual($this->Task->Schema->name, 'App');
$this->assertEqual($this->Task->Schema->file, 'other_file.php');
$this->assertEqual($this->Task->Schema->connection, 'test_suite');
$this->assertEqual($this->Task->Schema->path, '/test/path');
$this->Shell->startup();
$this->assertEqual($this->Shell->Schema->name, 'App');
$this->assertEqual($this->Shell->Schema->file, 'other_file.php');
$this->assertEqual($this->Shell->Schema->connection, 'test_suite');
$this->assertEqual($this->Shell->Schema->path, '/test/path');
}

/**
Expand All @@ -116,13 +119,13 @@ function testStartup() {
* @return void
**/
function testView() {
$this->Task->startup();
$this->Task->Schema->path = APP . 'config' . DS . 'schema';
$this->Task->params['file'] = 'i18n.php';
$this->Task->expectOnce('_stop');
$this->Task->expectOnce('out');
$this->Task->expectAt(0, 'out', array(new PatternExpectation('/class i18nSchema extends CakeSchema/')));
$this->Task->view();
$this->Shell->startup();
$this->Shell->Schema->path = APP . 'config' . DS . 'schema';
$this->Shell->params['file'] = 'i18n.php';
$this->Shell->expectOnce('_stop');
$this->Shell->expectOnce('out');
$this->Shell->expectAt(0, 'out', array(new PatternExpectation('/class i18nSchema extends CakeSchema/')));
$this->Shell->view();
}

/**
Expand All @@ -131,11 +134,11 @@ function testView() {
* @return void
**/
function testDump() {
$this->Task->params = array('name' => 'i18n');
$this->Task->startup();
$this->Task->Schema->path = APP . 'config' . DS . 'schema';
$this->Task->expectAt(0, 'out', array(new PatternExpectation('/create table `i18n`/i')));
$this->Task->dump();
$this->Shell->params = array('name' => 'i18n');
$this->Shell->startup();
$this->Shell->Schema->path = APP . 'config' . DS . 'schema';
$this->Shell->expectAt(0, 'out', array(new PatternExpectation('/create table `i18n`/i')));
$this->Shell->dump();
}

/**
Expand All @@ -144,17 +147,16 @@ function testDump() {
* @return void
**/
function testDumpWithFileWriting() {
//copy file.
$file =& new File(APP . 'config' . DS . 'schema' . DS . 'i18n.php');
$contents = $file->read();
$file =& new File(TMP . 'tests' . DS . 'i18n.php');
$file->write($contents);

$this->Task->params = array('name' => 'i18n');
$this->Task->args = array('write');
$this->Task->startup();
$this->Task->Schema->path = TMP . 'tests';
$this->Task->dump();
$this->Shell->params = array('name' => 'i18n');
$this->Shell->args = array('write');
$this->Shell->startup();
$this->Shell->Schema->path = TMP . 'tests';
$this->Shell->dump();

$sql =& new File(TMP . 'tests' . DS . 'i18n.sql');
$contents = $sql->read();
Expand All @@ -170,5 +172,24 @@ function testDumpWithFileWriting() {
$sql->delete();
$file->delete();
}

/**
* test generate with snapshot generation
*
* @return void
*/
function testGenerateSnaphot() {
$this->Shell->path = TMP;
$this->Shell->params['file'] = 'schema.php';
$this->Shell->args = array('snapshot');
$this->Shell->Schema =& new MockSchemaCakeSchema();
$this->Shell->Schema->setReturnValue('read', array('schema data'));
$this->Shell->Schema->setReturnValue('write', true);

$this->Shell->Schema->expectOnce('read');
$this->Shell->Schema->expectOnce('write', array(array('schema data', 'file' => 'schema_1.php')));

$this->Shell->generate();
}
}
?>

0 comments on commit a275753

Please sign in to comment.