2009-04-30 02:17:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-10-24 15:26:06 +00:00
|
|
|
* ModelTaskTest file
|
2009-04-30 02:17:08 +00:00
|
|
|
*
|
|
|
|
* Test Case for test generation shell task
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP : Rapid Development Framework (http://cakephp.org)
|
2009-07-17 03:55:41 +00:00
|
|
|
* Copyright 2006-2009, Cake Software Foundation, Inc.
|
2009-04-30 02:17:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2009-07-17 03:55:41 +00:00
|
|
|
* @copyright Copyright 2006-2009, Cake Software Foundation, Inc.
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP Project
|
2009-04-30 02:17:08 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.tests.cases.console.libs.tasks
|
2009-10-24 15:26:06 +00:00
|
|
|
* @since CakePHP v 1.2.6
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2009-04-30 02:17:08 +00:00
|
|
|
*/
|
2009-11-01 17:31:41 +00:00
|
|
|
App::import('Shell', 'Shell', false);
|
2009-04-30 02:17:08 +00:00
|
|
|
|
|
|
|
if (!defined('DISABLE_AUTO_DISPATCH')) {
|
|
|
|
define('DISABLE_AUTO_DISPATCH', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!class_exists('ShellDispatcher')) {
|
|
|
|
ob_start();
|
|
|
|
$argv = false;
|
|
|
|
require CAKE . 'console' . DS . 'cake.php';
|
|
|
|
ob_end_clean();
|
|
|
|
}
|
|
|
|
|
2009-11-01 17:31:41 +00:00
|
|
|
require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'model.php';
|
|
|
|
require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'fixture.php';
|
|
|
|
require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'template.php';
|
2009-04-30 02:17:08 +00:00
|
|
|
|
|
|
|
Mock::generatePartial(
|
|
|
|
'ShellDispatcher', 'TestModelTaskMockShellDispatcher',
|
|
|
|
array('getInput', 'stdout', 'stderr', '_stop', '_initEnvironment')
|
|
|
|
);
|
|
|
|
Mock::generatePartial(
|
|
|
|
'ModelTask', 'MockModelTask',
|
2009-05-15 01:42:07 +00:00
|
|
|
array('in', 'out', 'err', 'createFile', '_stop', '_checkUnitTest')
|
2009-04-30 02:17:08 +00:00
|
|
|
);
|
2009-05-08 04:49:26 +00:00
|
|
|
|
|
|
|
Mock::generate(
|
|
|
|
'Model', 'MockModelTaskModel'
|
|
|
|
);
|
2009-05-10 03:32:22 +00:00
|
|
|
|
|
|
|
Mock::generate(
|
|
|
|
'FixtureTask', 'MockModelTaskFixtureTask'
|
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-30 02:17:08 +00:00
|
|
|
/**
|
|
|
|
* ModelTaskTest class
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.tests.cases.console.libs.tasks
|
|
|
|
*/
|
|
|
|
class ModelTaskTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-30 02:40:49 +00:00
|
|
|
/**
|
|
|
|
* fixtures
|
|
|
|
*
|
|
|
|
* @var array
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-10 03:32:22 +00:00
|
|
|
var $fixtures = array('core.article', 'core.comment', 'core.articles_tag', 'core.tag', 'core.category_thread');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-30 02:17:08 +00:00
|
|
|
/**
|
2009-07-17 03:55:41 +00:00
|
|
|
* starTest method
|
2009-04-30 02:17:08 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function startTest() {
|
|
|
|
$this->Dispatcher =& new TestModelTaskMockShellDispatcher();
|
|
|
|
$this->Task =& new MockModelTask($this->Dispatcher);
|
2009-08-02 22:17:31 +00:00
|
|
|
$this->Task->Dispatch =& $this->Dispatcher;
|
2009-07-07 03:11:57 +00:00
|
|
|
$this->Task->Dispatch->shellPaths = App::path('shells');
|
2009-05-14 03:36:13 +00:00
|
|
|
$this->Task->Template =& new TemplateTask($this->Task->Dispatch);
|
2009-05-15 01:42:07 +00:00
|
|
|
$this->Task->Fixture =& new MockModelTaskFixtureTask();
|
|
|
|
$this->Task->Test =& new MockModelTaskFixtureTask();
|
2009-04-30 02:17:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-30 02:17:08 +00:00
|
|
|
/**
|
2009-07-17 03:55:41 +00:00
|
|
|
* endTest method
|
2009-04-30 02:17:08 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function endTest() {
|
|
|
|
unset($this->Task, $this->Dispatcher);
|
|
|
|
ClassRegistry::flush();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-30 02:17:08 +00:00
|
|
|
/**
|
|
|
|
* Test that listAll scans the database connection and lists all the tables in it.s
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-04-30 02:17:08 +00:00
|
|
|
function testListAll() {
|
|
|
|
$this->Task->expectAt(1, 'out', array('1. Article'));
|
2009-05-10 01:28:51 +00:00
|
|
|
$this->Task->expectAt(2, 'out', array('2. ArticlesTag'));
|
2009-05-10 03:32:22 +00:00
|
|
|
$this->Task->expectAt(3, 'out', array('3. CategoryThread'));
|
|
|
|
$this->Task->expectAt(4, 'out', array('4. Comment'));
|
|
|
|
$this->Task->expectAt(5, 'out', array('5. Tag'));
|
2009-04-30 02:50:43 +00:00
|
|
|
$result = $this->Task->listAll('test_suite');
|
2009-05-10 03:32:22 +00:00
|
|
|
$expected = array('articles', 'articles_tags', 'category_threads', 'comments', 'tags');
|
2009-04-30 02:50:43 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-10 03:32:22 +00:00
|
|
|
$this->Task->expectAt(7, 'out', array('1. Article'));
|
|
|
|
$this->Task->expectAt(8, 'out', array('2. ArticlesTag'));
|
|
|
|
$this->Task->expectAt(9, 'out', array('3. CategoryThread'));
|
|
|
|
$this->Task->expectAt(10, 'out', array('4. Comment'));
|
|
|
|
$this->Task->expectAt(11, 'out', array('5. Tag'));
|
2009-05-10 01:28:51 +00:00
|
|
|
|
2009-05-05 04:10:24 +00:00
|
|
|
$this->Task->connection = 'test_suite';
|
|
|
|
$result = $this->Task->listAll();
|
2009-05-10 03:32:22 +00:00
|
|
|
$expected = array('articles', 'articles_tags', 'category_threads', 'comments', 'tags');
|
2009-05-05 04:10:24 +00:00
|
|
|
$this->assertEqual($result, $expected);
|
2009-04-30 02:17:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-30 02:17:08 +00:00
|
|
|
/**
|
2009-05-15 03:25:52 +00:00
|
|
|
* Test that getName interacts with the user and returns the model name.
|
2009-04-30 02:17:08 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-04-30 02:17:08 +00:00
|
|
|
function testGetName() {
|
|
|
|
$this->Task->setReturnValue('in', 1);
|
|
|
|
|
|
|
|
$this->Task->setReturnValueAt(0, 'in', 'q');
|
|
|
|
$this->Task->expectOnce('_stop');
|
|
|
|
$this->Task->getName('test_suite');
|
|
|
|
|
|
|
|
$this->Task->setReturnValueAt(1, 'in', 1);
|
|
|
|
$result = $this->Task->getName('test_suite');
|
|
|
|
$expected = 'Article';
|
|
|
|
$this->assertEqual($result, $expected);
|
2009-04-30 02:25:42 +00:00
|
|
|
|
2009-05-10 03:32:22 +00:00
|
|
|
$this->Task->setReturnValueAt(2, 'in', 4);
|
2009-04-30 02:17:08 +00:00
|
|
|
$result = $this->Task->getName('test_suite');
|
|
|
|
$expected = 'Comment';
|
|
|
|
$this->assertEqual($result, $expected);
|
2009-04-30 02:25:42 +00:00
|
|
|
|
2009-04-30 02:17:08 +00:00
|
|
|
$this->Task->setReturnValueAt(3, 'in', 10);
|
|
|
|
$result = $this->Task->getName('test_suite');
|
|
|
|
$this->Task->expectOnce('err');
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-30 02:25:42 +00:00
|
|
|
/**
|
|
|
|
* Test table name interactions
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-04-30 02:25:42 +00:00
|
|
|
function testGetTableName() {
|
|
|
|
$this->Task->setReturnValueAt(0, 'in', 'y');
|
|
|
|
$result = $this->Task->getTable('Article', 'test_suite');
|
|
|
|
$expected = 'articles';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
|
|
|
|
$this->Task->setReturnValueAt(1, 'in', 'n');
|
|
|
|
$this->Task->setReturnValueAt(2, 'in', 'my_table');
|
|
|
|
$result = $this->Task->getTable('Article', 'test_suite');
|
|
|
|
$expected = 'my_table';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-08 04:49:26 +00:00
|
|
|
/**
|
|
|
|
* test that initializing the validations works.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-08 04:49:26 +00:00
|
|
|
function testInitValidations() {
|
|
|
|
$result = $this->Task->initValidations();
|
|
|
|
$this->assertTrue(in_array('notempty', $result));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-08 04:49:26 +00:00
|
|
|
/**
|
|
|
|
* test that individual field validation works, with interactive = false
|
2009-05-08 05:09:19 +00:00
|
|
|
* tests the guessing features of validation
|
2009-05-08 04:49:26 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-08 05:09:19 +00:00
|
|
|
function testFieldValidationGuessing() {
|
2009-05-08 04:49:26 +00:00
|
|
|
$this->Task->interactive = false;
|
2009-05-08 05:09:19 +00:00
|
|
|
$this->Task->initValidations();
|
2009-05-08 04:49:26 +00:00
|
|
|
|
|
|
|
$result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
|
2009-05-08 05:09:19 +00:00
|
|
|
$expected = array('notempty' => 'notempty');
|
|
|
|
|
|
|
|
$result = $this->Task->fieldValidation('text', array('type' => 'date', 'length' => 10, 'null' => false));
|
|
|
|
$expected = array('date' => 'date');
|
|
|
|
|
|
|
|
$result = $this->Task->fieldValidation('text', array('type' => 'time', 'length' => 10, 'null' => false));
|
|
|
|
$expected = array('time' => 'time');
|
|
|
|
|
|
|
|
$result = $this->Task->fieldValidation('email', array('type' => 'string', 'length' => 10, 'null' => false));
|
|
|
|
$expected = array('email' => 'email');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-08 05:09:19 +00:00
|
|
|
$result = $this->Task->fieldValidation('test', array('type' => 'integer', 'length' => 10, 'null' => false));
|
|
|
|
$expected = array('numeric' => 'numeric');
|
|
|
|
|
|
|
|
$result = $this->Task->fieldValidation('test', array('type' => 'boolean', 'length' => 10, 'null' => false));
|
|
|
|
$expected = array('numeric' => 'numeric');
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-08 05:09:19 +00:00
|
|
|
/**
|
|
|
|
* test that interactive field validation works and returns multiple validators.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-08 05:09:19 +00:00
|
|
|
function testInteractiveFieldValidation() {
|
2009-05-09 04:35:03 +00:00
|
|
|
$this->Task->initValidations();
|
|
|
|
$this->Task->interactive = true;
|
2009-11-01 17:31:41 +00:00
|
|
|
$this->Task->setReturnValueAt(0, 'in', '19');
|
2009-05-09 04:35:03 +00:00
|
|
|
$this->Task->setReturnValueAt(1, 'in', 'y');
|
2009-11-01 17:31:41 +00:00
|
|
|
$this->Task->setReturnValueAt(2, 'in', '15');
|
2009-05-09 04:35:03 +00:00
|
|
|
$this->Task->setReturnValueAt(3, 'in', 'n');
|
|
|
|
|
|
|
|
$result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
|
|
|
|
$expected = array('notempty' => 'notempty', 'maxlength' => 'maxlength');
|
|
|
|
$this->assertEqual($result, $expected);
|
2009-05-08 04:49:26 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-08 04:49:26 +00:00
|
|
|
/**
|
|
|
|
* test the validation Generation routine
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-08 05:09:19 +00:00
|
|
|
function testNonInteractiveDoValidation() {
|
2009-05-08 04:49:26 +00:00
|
|
|
$Model =& new MockModelTaskModel();
|
2009-05-08 05:09:19 +00:00
|
|
|
$Model->primaryKey = 'id';
|
2009-05-08 04:49:26 +00:00
|
|
|
$Model->setReturnValue('schema', array(
|
|
|
|
'id' => array(
|
|
|
|
'type' => 'integer',
|
|
|
|
'length' => 11,
|
|
|
|
'null' => false,
|
|
|
|
'key' => 'primary',
|
|
|
|
),
|
|
|
|
'name' => array(
|
|
|
|
'type' => 'string',
|
|
|
|
'length' => 20,
|
|
|
|
'null' => false,
|
|
|
|
),
|
|
|
|
'email' => array(
|
|
|
|
'type' => 'string',
|
|
|
|
'length' => 255,
|
|
|
|
'null' => false,
|
|
|
|
),
|
|
|
|
'some_date' => array(
|
|
|
|
'type' => 'date',
|
|
|
|
'length' => '',
|
|
|
|
'null' => false,
|
|
|
|
),
|
|
|
|
'some_time' => array(
|
|
|
|
'type' => 'time',
|
|
|
|
'length' => '',
|
|
|
|
'null' => false,
|
|
|
|
),
|
|
|
|
'created' => array(
|
|
|
|
'type' => 'datetime',
|
|
|
|
'length' => '',
|
|
|
|
'null' => false,
|
|
|
|
)
|
|
|
|
));
|
|
|
|
$this->Task->interactive = false;
|
|
|
|
|
|
|
|
$result = $this->Task->doValidation($Model);
|
|
|
|
$expected = array(
|
|
|
|
'name' => array(
|
2009-05-08 05:09:19 +00:00
|
|
|
'notempty' => 'notempty'
|
2009-05-08 04:49:26 +00:00
|
|
|
),
|
|
|
|
'email' => array(
|
2009-05-08 05:09:19 +00:00
|
|
|
'email' => 'email',
|
2009-05-08 04:49:26 +00:00
|
|
|
),
|
|
|
|
'some_date' => array(
|
2009-05-08 05:09:19 +00:00
|
|
|
'date' => 'date'
|
2009-05-08 04:49:26 +00:00
|
|
|
),
|
|
|
|
'some_time' => array(
|
2009-05-08 05:09:19 +00:00
|
|
|
'time' => 'time'
|
2009-05-08 04:49:26 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-10 01:28:51 +00:00
|
|
|
/**
|
|
|
|
* test that finding primary key works
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-10 01:28:51 +00:00
|
|
|
function testFindPrimaryKey() {
|
|
|
|
$fields = array(
|
|
|
|
'one' => array(),
|
|
|
|
'two' => array(),
|
|
|
|
'key' => array('key' => 'primary')
|
|
|
|
);
|
|
|
|
$this->Task->expectAt(0, 'in', array('*', null, 'key'));
|
|
|
|
$this->Task->setReturnValue('in', 'my_field');
|
|
|
|
$result = $this->Task->findPrimaryKey($fields);
|
|
|
|
$expected = 'my_field';
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-07-08 13:10:18 +00:00
|
|
|
/**
|
|
|
|
* test finding Display field
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-07-08 13:10:18 +00:00
|
|
|
function testFindDisplayField() {
|
2009-07-24 19:18:37 +00:00
|
|
|
$fields = array('id' => array(), 'tagname' => array(), 'body' => array(),
|
2009-07-08 13:10:18 +00:00
|
|
|
'created' => array(), 'modified' => array());
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-07-08 13:10:18 +00:00
|
|
|
$this->Task->setReturnValue('in', 'n');
|
|
|
|
$this->Task->setReturnValueAt(0, 'in', 'n');
|
|
|
|
$result = $this->Task->findDisplayField($fields);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$this->Task->setReturnValueAt(1, 'in', 'y');
|
|
|
|
$this->Task->setReturnValueAt(2, 'in', 2);
|
|
|
|
$result = $this->Task->findDisplayField($fields);
|
|
|
|
$this->assertEqual($result, 'tagname');
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-10 01:28:51 +00:00
|
|
|
/**
|
|
|
|
* test that belongsTo generation works.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-10 01:28:51 +00:00
|
|
|
function testBelongsToGeneration() {
|
|
|
|
$model = new Model(array('ds' => 'test_suite', 'name' => 'Comment'));
|
|
|
|
$result = $this->Task->findBelongsTo($model, array());
|
|
|
|
$expected = array(
|
|
|
|
'belongsTo' => array(
|
|
|
|
array(
|
|
|
|
'alias' => 'Article',
|
|
|
|
'className' => 'Article',
|
|
|
|
'foreignKey' => 'article_id',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'alias' => 'User',
|
|
|
|
'className' => 'User',
|
|
|
|
'foreignKey' => 'user_id',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEqual($result, $expected);
|
2009-05-10 03:32:22 +00:00
|
|
|
|
|
|
|
$model = new Model(array('ds' => 'test_suite', 'name' => 'CategoryThread'));
|
|
|
|
$result = $this->Task->findBelongsTo($model, array());
|
|
|
|
$expected = array(
|
|
|
|
'belongsTo' => array(
|
|
|
|
array(
|
|
|
|
'alias' => 'ParentCategoryThread',
|
|
|
|
'className' => 'CategoryThread',
|
|
|
|
'foreignKey' => 'parent_id',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEqual($result, $expected);
|
2009-05-10 01:28:51 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-10 01:28:51 +00:00
|
|
|
/**
|
|
|
|
* test that hasOne and/or hasMany relations are generated properly.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-10 01:28:51 +00:00
|
|
|
function testHasManyHasOneGeneration() {
|
|
|
|
$model = new Model(array('ds' => 'test_suite', 'name' => 'Article'));
|
|
|
|
$this->Task->connection = 'test_suite';
|
|
|
|
$this->Task->listAll();
|
|
|
|
$result = $this->Task->findHasOneAndMany($model, array());
|
|
|
|
$expected = array(
|
|
|
|
'hasMany' => array(
|
|
|
|
array(
|
|
|
|
'alias' => 'Comment',
|
|
|
|
'className' => 'Comment',
|
|
|
|
'foreignKey' => 'article_id',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'hasOne' => array(
|
|
|
|
array(
|
|
|
|
'alias' => 'Comment',
|
|
|
|
'className' => 'Comment',
|
|
|
|
'foreignKey' => 'article_id',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$this->assertEqual($result, $expected);
|
2009-05-10 03:32:22 +00:00
|
|
|
|
|
|
|
$model = new Model(array('ds' => 'test_suite', 'name' => 'CategoryThread'));
|
|
|
|
$result = $this->Task->findHasOneAndMany($model, array());
|
|
|
|
$expected = array(
|
|
|
|
'hasOne' => array(
|
|
|
|
array(
|
|
|
|
'alias' => 'ChildCategoryThread',
|
|
|
|
'className' => 'CategoryThread',
|
|
|
|
'foreignKey' => 'parent_id',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'hasMany' => array(
|
|
|
|
array(
|
|
|
|
'alias' => 'ChildCategoryThread',
|
|
|
|
'className' => 'CategoryThread',
|
|
|
|
'foreignKey' => 'parent_id',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertEqual($result, $expected);
|
2009-05-10 01:28:51 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-10 01:28:51 +00:00
|
|
|
/**
|
2009-11-14 12:18:31 +00:00
|
|
|
* Test that HABTM generation works
|
2009-05-10 01:28:51 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-10 01:28:51 +00:00
|
|
|
function testHasAndBelongsToManyGeneration() {
|
|
|
|
$model = new Model(array('ds' => 'test_suite', 'name' => 'Article'));
|
|
|
|
$this->Task->connection = 'test_suite';
|
|
|
|
$this->Task->listAll();
|
|
|
|
$result = $this->Task->findHasAndBelongsToMany($model, array());
|
|
|
|
$expected = array(
|
|
|
|
'hasAndBelongsToMany' => array(
|
|
|
|
array(
|
|
|
|
'alias' => 'Tag',
|
|
|
|
'className' => 'Tag',
|
|
|
|
'foreignKey' => 'article_id',
|
|
|
|
'joinTable' => 'articles_tags',
|
|
|
|
'associationForeignKey' => 'tag_id',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$this->assertEqual($result, $expected);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-10 16:11:20 +00:00
|
|
|
/**
|
|
|
|
* test non interactive doAssociations
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-10 16:11:20 +00:00
|
|
|
function testDoAssociationsNonInteractive() {
|
|
|
|
$this->Task->connection = 'test_suite';
|
|
|
|
$this->Task->interactive = false;
|
|
|
|
$model = new Model(array('ds' => 'test_suite', 'name' => 'Article'));
|
|
|
|
$result = $this->Task->doAssociations($model);
|
|
|
|
$expected = array(
|
|
|
|
'hasMany' => array(
|
|
|
|
array(
|
|
|
|
'alias' => 'Comment',
|
|
|
|
'className' => 'Comment',
|
|
|
|
'foreignKey' => 'article_id',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'hasAndBelongsToMany' => array(
|
|
|
|
array(
|
|
|
|
'alias' => 'Tag',
|
|
|
|
'className' => 'Tag',
|
|
|
|
'foreignKey' => 'article_id',
|
|
|
|
'joinTable' => 'articles_tags',
|
|
|
|
'associationForeignKey' => 'tag_id',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-10 03:32:22 +00:00
|
|
|
/**
|
|
|
|
* Ensure that the fixutre object is correctly called.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-15 01:42:07 +00:00
|
|
|
function testBakeFixture() {
|
2009-05-10 03:32:22 +00:00
|
|
|
$this->Task->Fixture->expectAt(0, 'bake', array('Article', 'articles'));
|
2009-05-15 01:42:07 +00:00
|
|
|
$this->Task->bakeFixture('Article', 'articles');
|
2009-06-07 00:37:01 +00:00
|
|
|
|
|
|
|
$this->assertEqual($this->Task->plugin, $this->Task->Fixture->plugin);
|
|
|
|
$this->assertEqual($this->Task->connection, $this->Task->Fixture->connection);
|
2009-05-10 03:32:22 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-07-15 14:00:35 +00:00
|
|
|
/**
|
|
|
|
* Ensure that the test object is correctly called.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-07-15 14:00:35 +00:00
|
|
|
function testBakeTest() {
|
|
|
|
$this->Task->Test->expectAt(0, 'bake', array('Model', 'Article'));
|
|
|
|
$this->Task->bakeTest('Article');
|
|
|
|
|
|
|
|
$this->assertEqual($this->Task->plugin, $this->Task->Test->plugin);
|
|
|
|
$this->assertEqual($this->Task->connection, $this->Task->Test->connection);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-10 05:04:40 +00:00
|
|
|
/**
|
|
|
|
* test confirming of associations, and that when an association is hasMany
|
|
|
|
* a question for the hasOne is also not asked.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-10 05:04:40 +00:00
|
|
|
function testConfirmAssociations() {
|
|
|
|
$associations = array(
|
|
|
|
'hasOne' => array(
|
|
|
|
array(
|
|
|
|
'alias' => 'ChildCategoryThread',
|
|
|
|
'className' => 'CategoryThread',
|
|
|
|
'foreignKey' => 'parent_id',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'hasMany' => array(
|
|
|
|
array(
|
|
|
|
'alias' => 'ChildCategoryThread',
|
|
|
|
'className' => 'CategoryThread',
|
|
|
|
'foreignKey' => 'parent_id',
|
|
|
|
),
|
2009-05-10 16:11:20 +00:00
|
|
|
),
|
|
|
|
'belongsTo' => array(
|
|
|
|
array(
|
|
|
|
'alias' => 'User',
|
|
|
|
'className' => 'User',
|
|
|
|
'foreignKey' => 'user_id',
|
|
|
|
),
|
2009-05-10 05:04:40 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$model = new Model(array('ds' => 'test_suite', 'name' => 'CategoryThread'));
|
|
|
|
$this->Task->setReturnValueAt(0, 'in', 'y');
|
|
|
|
$result = $this->Task->confirmAssociations($model, $associations);
|
|
|
|
$this->assertTrue(empty($result['hasOne']));
|
|
|
|
|
|
|
|
$this->Task->setReturnValue('in', 'n');
|
|
|
|
$result = $this->Task->confirmAssociations($model, $associations);
|
|
|
|
$this->assertTrue(empty($result['hasMany']));
|
|
|
|
$this->assertTrue(empty($result['hasOne']));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-10 15:01:15 +00:00
|
|
|
/**
|
|
|
|
* test that inOptions generates questions and only accepts a valid answer
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-10 15:01:15 +00:00
|
|
|
function testInOptions() {
|
|
|
|
$options = array('one', 'two', 'three');
|
|
|
|
$this->Task->expectAt(0, 'out', array('1. one'));
|
|
|
|
$this->Task->expectAt(1, 'out', array('2. two'));
|
|
|
|
$this->Task->expectAt(2, 'out', array('3. three'));
|
|
|
|
$this->Task->setReturnValueAt(0, 'in', 10);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-10 15:01:15 +00:00
|
|
|
$this->Task->expectAt(3, 'out', array('1. one'));
|
|
|
|
$this->Task->expectAt(4, 'out', array('2. two'));
|
|
|
|
$this->Task->expectAt(5, 'out', array('3. three'));
|
|
|
|
$this->Task->setReturnValueAt(1, 'in', 2);
|
|
|
|
$result = $this->Task->inOptions($options, 'Pick a number');
|
|
|
|
$this->assertEqual($result, 1);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-14 03:36:13 +00:00
|
|
|
/**
|
|
|
|
* test baking validation
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-14 03:36:13 +00:00
|
|
|
function testBakeValidation() {
|
|
|
|
$validate = array(
|
|
|
|
'name' => array(
|
|
|
|
'notempty' => 'notempty'
|
|
|
|
),
|
|
|
|
'email' => array(
|
|
|
|
'email' => 'email',
|
|
|
|
),
|
|
|
|
'some_date' => array(
|
|
|
|
'date' => 'date'
|
|
|
|
),
|
|
|
|
'some_time' => array(
|
|
|
|
'time' => 'time'
|
|
|
|
)
|
|
|
|
);
|
2009-07-09 02:06:42 +00:00
|
|
|
$result = $this->Task->bake('Article', compact('validate'));
|
2009-05-14 03:36:13 +00:00
|
|
|
$this->assertPattern('/class Article extends AppModel \{/', $result);
|
|
|
|
$this->assertPattern('/\$name \= \'Article\'/', $result);
|
|
|
|
$this->assertPattern('/\$validate \= array\(/', $result);
|
2010-01-01 09:19:15 +00:00
|
|
|
$expected = <<< STRINGEND
|
|
|
|
array(
|
|
|
|
'notempty' => array(
|
|
|
|
'rule' => array('notempty'),
|
|
|
|
//'message' => 'Your custom message here',
|
|
|
|
//'allowEmpty' => false,
|
|
|
|
//'required' => false,
|
|
|
|
//'last' => false, // Stop validation after this rule
|
|
|
|
//'on' => 'create', // Limit validation to 'create' or 'update' operations
|
|
|
|
),
|
|
|
|
STRINGEND;
|
|
|
|
|
|
|
|
$this->assertPattern('/' . preg_quote($expected, '/') . '/', $result);
|
2009-05-14 03:36:13 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-14 03:36:13 +00:00
|
|
|
/**
|
|
|
|
* test baking relations
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-14 03:36:13 +00:00
|
|
|
function testBakeRelations() {
|
|
|
|
$associations = array(
|
|
|
|
'belongsTo' => array(
|
|
|
|
array(
|
|
|
|
'alias' => 'SomethingElse',
|
|
|
|
'className' => 'SomethingElse',
|
|
|
|
'foreignKey' => 'something_else_id',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'alias' => 'User',
|
|
|
|
'className' => 'User',
|
|
|
|
'foreignKey' => 'user_id',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'hasOne' => array(
|
|
|
|
array(
|
|
|
|
'alias' => 'OtherModel',
|
|
|
|
'className' => 'OtherModel',
|
|
|
|
'foreignKey' => 'other_model_id',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'hasMany' => array(
|
|
|
|
array(
|
|
|
|
'alias' => 'Comment',
|
|
|
|
'className' => 'Comment',
|
|
|
|
'foreignKey' => 'parent_id',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'hasAndBelongsToMany' => array(
|
|
|
|
array(
|
|
|
|
'alias' => 'Tag',
|
|
|
|
'className' => 'Tag',
|
|
|
|
'foreignKey' => 'article_id',
|
|
|
|
'joinTable' => 'articles_tags',
|
|
|
|
'associationForeignKey' => 'tag_id',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
2009-07-09 02:06:42 +00:00
|
|
|
$result = $this->Task->bake('Article', compact('associations'));
|
2009-05-14 03:36:13 +00:00
|
|
|
$this->assertPattern('/\$hasAndBelongsToMany \= array\(/', $result);
|
|
|
|
$this->assertPattern('/\$hasMany \= array\(/', $result);
|
|
|
|
$this->assertPattern('/\$belongsTo \= array\(/', $result);
|
|
|
|
$this->assertPattern('/\$hasOne \= array\(/', $result);
|
|
|
|
$this->assertPattern('/Tag/', $result);
|
|
|
|
$this->assertPattern('/OtherModel/', $result);
|
|
|
|
$this->assertPattern('/SomethingElse/', $result);
|
|
|
|
$this->assertPattern('/Comment/', $result);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-06-07 00:37:01 +00:00
|
|
|
/**
|
|
|
|
* test bake() with a -plugin param
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-06-07 00:37:01 +00:00
|
|
|
function testBakeWithPlugin() {
|
|
|
|
$this->Task->plugin = 'ControllerTest';
|
|
|
|
|
|
|
|
$path = APP . 'plugins' . DS . 'controller_test' . DS . 'models' . DS . 'article.php';
|
|
|
|
$this->Task->expectAt(0, 'createFile', array($path, '*'));
|
|
|
|
$this->Task->bake('Article', array(), array());
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-06-17 02:29:48 +00:00
|
|
|
$this->Task->plugin = 'controllerTest';
|
|
|
|
|
|
|
|
$path = APP . 'plugins' . DS . 'controller_test' . DS . 'models' . DS . 'article.php';
|
|
|
|
$this->Task->expectAt(1, 'createFile', array(
|
2009-07-09 02:06:42 +00:00
|
|
|
$path, new PatternExpectation('/Article extends ControllerTestAppModel/')));
|
2009-06-17 02:29:48 +00:00
|
|
|
$this->Task->bake('Article', array(), array());
|
2009-06-07 00:37:01 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-15 01:42:07 +00:00
|
|
|
/**
|
|
|
|
* test that execute passes runs bake depending with named model.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-15 01:42:07 +00:00
|
|
|
function testExecuteWithNamedModel() {
|
|
|
|
$this->Task->connection = 'test_suite';
|
|
|
|
$this->Task->path = '/my/path/';
|
|
|
|
$this->Task->args = array('article');
|
|
|
|
$filename = '/my/path/article.php';
|
|
|
|
$this->Task->setReturnValue('_checkUnitTest', 1);
|
|
|
|
$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class Article extends AppModel/')));
|
|
|
|
$this->Task->execute();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-15 01:42:07 +00:00
|
|
|
/**
|
|
|
|
* test that execute runs all() when args[0] = all
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-15 01:42:07 +00:00
|
|
|
function testExecuteIntoAll() {
|
|
|
|
$this->Task->connection = 'test_suite';
|
|
|
|
$this->Task->path = '/my/path/';
|
|
|
|
$this->Task->args = array('all');
|
2009-07-15 14:00:35 +00:00
|
|
|
$this->Task->setReturnValue('_checkUnitTest', true);
|
|
|
|
|
|
|
|
$this->Task->Fixture->expectCallCount('bake', 5);
|
|
|
|
$this->Task->Test->expectCallCount('bake', 5);
|
2009-05-15 02:19:43 +00:00
|
|
|
|
2009-05-15 01:42:07 +00:00
|
|
|
$filename = '/my/path/article.php';
|
|
|
|
$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class Article/')));
|
|
|
|
|
|
|
|
$filename = '/my/path/articles_tag.php';
|
|
|
|
$this->Task->expectAt(1, 'createFile', array($filename, new PatternExpectation('/class ArticlesTag/')));
|
|
|
|
|
|
|
|
$filename = '/my/path/category_thread.php';
|
|
|
|
$this->Task->expectAt(2, 'createFile', array($filename, new PatternExpectation('/class CategoryThread/')));
|
|
|
|
|
|
|
|
$filename = '/my/path/comment.php';
|
|
|
|
$this->Task->expectAt(3, 'createFile', array($filename, new PatternExpectation('/class Comment/')));
|
|
|
|
|
|
|
|
$filename = '/my/path/tag.php';
|
|
|
|
$this->Task->expectAt(4, 'createFile', array($filename, new PatternExpectation('/class Tag/')));
|
|
|
|
|
|
|
|
$this->Task->execute();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-15 02:36:04 +00:00
|
|
|
/**
|
|
|
|
* test the interactive side of bake.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-05-15 02:36:04 +00:00
|
|
|
function testExecuteIntoInteractive() {
|
|
|
|
$this->Task->connection = 'test_suite';
|
|
|
|
$this->Task->path = '/my/path/';
|
2009-07-09 03:30:29 +00:00
|
|
|
$this->Task->interactive = true;
|
2009-05-15 02:36:04 +00:00
|
|
|
|
|
|
|
$this->Task->setReturnValueAt(0, 'in', '1'); //choose article
|
|
|
|
$this->Task->setReturnValueAt(1, 'in', 'n'); //no validation
|
|
|
|
$this->Task->setReturnValueAt(2, 'in', 'y'); //yes to associations
|
|
|
|
$this->Task->setReturnValueAt(3, 'in', 'y'); //yes to comment relation
|
|
|
|
$this->Task->setReturnValueAt(4, 'in', 'y'); //yes to user relation
|
|
|
|
$this->Task->setReturnValueAt(5, 'in', 'y'); //yes to tag relation
|
2009-07-09 03:30:29 +00:00
|
|
|
$this->Task->setReturnValueAt(6, 'in', 'n'); //no to additional assocs
|
|
|
|
$this->Task->setReturnValueAt(7, 'in', 'y'); //yes to looksGood?
|
|
|
|
$this->Task->setReturnValue('_checkUnitTest', true);
|
|
|
|
|
|
|
|
$this->Task->Test->expectOnce('bake');
|
|
|
|
$this->Task->Fixture->expectOnce('bake');
|
2009-05-15 02:36:04 +00:00
|
|
|
|
|
|
|
$filename = '/my/path/article.php';
|
2009-07-09 03:30:29 +00:00
|
|
|
$this->Task->expectOnce('createFile');
|
2009-05-15 02:36:04 +00:00
|
|
|
$this->Task->expectAt(0, 'createFile', array($filename, new PatternExpectation('/class Article/')));
|
|
|
|
$this->Task->execute();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-07-09 03:30:29 +00:00
|
|
|
/**
|
|
|
|
* test using bake interactively with a table that does not exist.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:18:31 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2009-07-09 03:30:29 +00:00
|
|
|
function testExecuteWithNonExistantTableName() {
|
|
|
|
$this->Task->connection = 'test_suite';
|
|
|
|
$this->Task->path = '/my/path/';
|
|
|
|
|
|
|
|
$this->Task->expectOnce('_stop');
|
|
|
|
$this->Task->expectOnce('err');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-07-09 03:30:29 +00:00
|
|
|
$this->Task->setReturnValueAt(0, 'in', 'Foobar');
|
|
|
|
$this->Task->setReturnValueAt(1, 'in', 'y');
|
|
|
|
$this->Task->execute();
|
|
|
|
}
|
2009-04-30 02:17:08 +00:00
|
|
|
}
|
|
|
|
?>
|