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. * @copyright Copyright 2006-2009, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
* @package cake * @package cake
* @subpackage cake.tests.cases.console.libs.tasks * @subpackage cake.tests.cases.console.libs.Shells
* @since CakePHP v 1.3 * @since CakePHP v 1.3
* @license http://www.opensource.org/licenses/mit-license.php The MIT License * @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/ */
App::import('Shell', 'Shell', false); App::import('Shell', 'Shell', false);
App::import('Model', 'CakeSchema', false);


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


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

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


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


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


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


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


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


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


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


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


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