2009-04-29 00:32:15 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-07-17 03:55:41 +00:00
|
|
|
* The FixtureTask handles creating and updating fixture files.
|
2009-04-29 00:32:15 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 12:28:17 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-04-29 00:32:15 +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-04-29 00:32:15 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2009-04-29 00:32:15 +00:00
|
|
|
* @since CakePHP(tm) v 1.3
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2009-04-29 00:32:15 +00:00
|
|
|
*/
|
2010-12-08 00:20:56 +00:00
|
|
|
|
2011-11-25 21:05:00 +00:00
|
|
|
App::uses('AppShell', 'Console/Command');
|
2010-12-08 00:20:56 +00:00
|
|
|
App::uses('BakeTask', 'Console/Command/Task');
|
2010-12-09 03:45:18 +00:00
|
|
|
App::uses('Model', 'Model');
|
2010-12-08 00:20:56 +00:00
|
|
|
|
2009-04-29 00:32:15 +00:00
|
|
|
/**
|
|
|
|
* Task class for creating and updating fixtures files.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Console.Command.Task
|
2009-04-29 00:32:15 +00:00
|
|
|
*/
|
2010-03-05 02:30:26 +00:00
|
|
|
class FixtureTask extends BakeTask {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-29 00:32:15 +00:00
|
|
|
/**
|
|
|
|
* Tasks to be loaded by this Task
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $tasks = array('DbConfig', 'Model', 'Template');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-29 00:32:15 +00:00
|
|
|
/**
|
|
|
|
* path to fixtures directory
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $path = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-07-20 04:59:18 +00:00
|
|
|
/**
|
|
|
|
* Schema instance
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @var CakeSchema
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-04 06:36:12 +00:00
|
|
|
protected $_Schema = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-29 00:32:15 +00:00
|
|
|
/**
|
|
|
|
* Override initialize
|
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @param ConsoleOutput $stdout A ConsoleOutput object for stdout.
|
|
|
|
* @param ConsoleOutput $stderr A ConsoleOutput object for stderr.
|
|
|
|
* @param ConsoleInput $stdin A ConsoleInput object for stdin.
|
2009-04-29 00:32:15 +00:00
|
|
|
*/
|
2010-10-24 19:27:44 +00:00
|
|
|
public function __construct($stdout = null, $stderr = null, $stdin = null) {
|
|
|
|
parent::__construct($stdout, $stderr, $stdin);
|
2011-05-30 19:04:38 +00:00
|
|
|
$this->path = APP . 'Test' . DS . 'Fixture' . DS;
|
2009-04-29 00:32:15 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-10-12 04:04:10 +00:00
|
|
|
/**
|
2013-12-05 23:38:32 +00:00
|
|
|
* Gets the option parser instance and configures it.
|
2010-10-12 04:04:10 +00:00
|
|
|
*
|
2013-12-05 23:38:32 +00:00
|
|
|
* @return ConsoleOptionParser
|
2010-10-12 04:04:10 +00:00
|
|
|
*/
|
|
|
|
public function getOptionParser() {
|
|
|
|
$parser = parent::getOptionParser();
|
2013-12-05 23:38:32 +00:00
|
|
|
|
|
|
|
$parser->description(
|
2011-03-19 17:32:35 +00:00
|
|
|
__d('cake_console', 'Generate fixtures for use with the test suite. You can use `bake fixture all` to bake all fixtures.')
|
2010-10-12 04:04:10 +00:00
|
|
|
)->addArgument('name', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Name of the fixture to bake. Can use Plugin.name to bake plugin fixtures.')
|
2010-10-12 04:04:10 +00:00
|
|
|
))->addOption('count', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'When using generated data, the number of records to include in the fixture(s).'),
|
2010-10-12 04:04:10 +00:00
|
|
|
'short' => 'n',
|
2015-09-13 11:12:53 +00:00
|
|
|
'default' => 1
|
2010-10-12 04:04:10 +00:00
|
|
|
))->addOption('connection', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'Which database configuration to use for baking.'),
|
2010-10-12 04:04:10 +00:00
|
|
|
'short' => 'c',
|
|
|
|
'default' => 'default'
|
|
|
|
))->addOption('plugin', array(
|
2011-03-19 17:32:35 +00:00
|
|
|
'help' => __d('cake_console', 'CamelCased name of the plugin to bake fixtures for.'),
|
2013-12-05 23:38:32 +00:00
|
|
|
'short' => 'p'
|
2013-05-01 22:19:46 +00:00
|
|
|
))->addOption('schema', array(
|
|
|
|
'help' => __d('cake_console', 'Importing schema for fixtures rather than hardcoding it.'),
|
|
|
|
'short' => 's',
|
|
|
|
'boolean' => true
|
2013-05-29 13:41:57 +00:00
|
|
|
))->addOption('theme', array(
|
2013-05-29 13:49:12 +00:00
|
|
|
'short' => 't',
|
|
|
|
'help' => __d('cake_console', 'Theme to use when baking code.')
|
2013-07-18 11:09:00 +00:00
|
|
|
))->addOption('force', array(
|
|
|
|
'short' => 'f',
|
|
|
|
'help' => __d('cake_console', 'Force overwriting existing files without prompting.')
|
2010-10-12 04:04:10 +00:00
|
|
|
))->addOption('records', array(
|
2013-12-05 23:38:32 +00:00
|
|
|
'help' => __d('cake_console', 'Used with --count and <name>/all commands to pull [n] records from the live tables, ' .
|
|
|
|
'where [n] is either --count or the default of 10.'),
|
2010-10-12 04:04:10 +00:00
|
|
|
'short' => 'r',
|
|
|
|
'boolean' => true
|
2013-12-05 23:38:32 +00:00
|
|
|
))->epilog(
|
|
|
|
__d('cake_console', 'Omitting all arguments and options will enter into an interactive mode.')
|
|
|
|
);
|
|
|
|
|
|
|
|
return $parser;
|
2009-04-29 00:32:15 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-29 00:32:15 +00:00
|
|
|
/**
|
|
|
|
* Execution method always used for tasks
|
2011-12-02 05:58:09 +00:00
|
|
|
* Handles dispatching to interactive, named, or all processes.
|
2009-04-29 00:32:15 +00:00
|
|
|
*
|
2010-04-24 01:57:59 +00:00
|
|
|
* @return void
|
2009-04-29 00:32:15 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function execute() {
|
2010-10-19 03:09:23 +00:00
|
|
|
parent::execute();
|
2009-04-29 00:32:15 +00:00
|
|
|
if (empty($this->args)) {
|
2010-04-24 01:57:59 +00:00
|
|
|
$this->_interactive();
|
2009-04-29 00:32:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->args[0])) {
|
2009-10-24 18:32:56 +00:00
|
|
|
$this->interactive = false;
|
2009-05-08 03:59:43 +00:00
|
|
|
if (!isset($this->connection)) {
|
|
|
|
$this->connection = 'default';
|
|
|
|
}
|
2013-02-12 02:38:08 +00:00
|
|
|
if (strtolower($this->args[0]) === 'all') {
|
2009-04-29 00:32:15 +00:00
|
|
|
return $this->all();
|
|
|
|
}
|
2010-03-06 02:30:58 +00:00
|
|
|
$model = $this->_modelName($this->args[0]);
|
2009-05-08 03:59:43 +00:00
|
|
|
$this->bake($model);
|
2009-04-29 00:32:15 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-29 00:32:15 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Bake All the Fixtures at once. Will only bake fixtures for models that exist.
|
2009-04-29 00:32:15 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-24 01:57:59 +00:00
|
|
|
public function all() {
|
2009-04-30 02:50:43 +00:00
|
|
|
$this->interactive = false;
|
2009-10-24 18:32:56 +00:00
|
|
|
$this->Model->interactive = false;
|
2009-05-04 01:43:22 +00:00
|
|
|
$tables = $this->Model->listAll($this->connection, false);
|
2013-05-01 22:19:46 +00:00
|
|
|
|
2009-04-30 02:50:43 +00:00
|
|
|
foreach ($tables as $table) {
|
|
|
|
$model = $this->_modelName($table);
|
2013-05-01 22:19:46 +00:00
|
|
|
$importOptions = array();
|
|
|
|
if (!empty($this->params['schema'])) {
|
|
|
|
$importOptions['schema'] = $model;
|
|
|
|
}
|
|
|
|
$this->bake($model, false, $importOptions);
|
2009-04-30 02:50:43 +00:00
|
|
|
}
|
2009-04-29 00:32:15 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-29 00:32:15 +00:00
|
|
|
/**
|
|
|
|
* Interactive baking function
|
|
|
|
*
|
2010-04-24 01:57:59 +00:00
|
|
|
* @return void
|
2009-04-29 00:32:15 +00:00
|
|
|
*/
|
2010-04-24 01:57:59 +00:00
|
|
|
protected function _interactive() {
|
2010-03-09 02:18:53 +00:00
|
|
|
$this->DbConfig->interactive = $this->Model->interactive = $this->interactive = true;
|
2009-04-29 00:45:07 +00:00
|
|
|
$this->hr();
|
2012-02-05 17:00:30 +00:00
|
|
|
$this->out(__d('cake_console', "Bake Fixture\nPath: %s", $this->getPath()));
|
2009-04-29 00:45:07 +00:00
|
|
|
$this->hr();
|
|
|
|
|
2009-05-04 01:43:22 +00:00
|
|
|
if (!isset($this->connection)) {
|
|
|
|
$this->connection = $this->DbConfig->getConfig();
|
|
|
|
}
|
|
|
|
$modelName = $this->Model->getName($this->connection);
|
|
|
|
$useTable = $this->Model->getTable($modelName, $this->connection);
|
2009-04-30 00:52:12 +00:00
|
|
|
$importOptions = $this->importOptions($modelName);
|
|
|
|
$this->bake($modelName, $useTable, $importOptions);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-30 00:52:12 +00:00
|
|
|
/**
|
|
|
|
* Interacts with the User to setup an array of import options. For a fixture.
|
|
|
|
*
|
|
|
|
* @param string $modelName Name of model you are dealing with.
|
|
|
|
* @return array Array of import options.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function importOptions($modelName) {
|
2009-04-30 00:52:12 +00:00
|
|
|
$options = array();
|
2013-05-01 22:19:46 +00:00
|
|
|
|
|
|
|
if (!empty($this->params['schema'])) {
|
2009-04-30 00:52:12 +00:00
|
|
|
$options['schema'] = $modelName;
|
2013-05-01 22:19:46 +00:00
|
|
|
} else {
|
|
|
|
$doSchema = $this->in(__d('cake_console', 'Would you like to import schema for this fixture?'), array('y', 'n'), 'n');
|
|
|
|
if ($doSchema === 'y') {
|
|
|
|
$options['schema'] = $modelName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($this->params['records'])) {
|
|
|
|
$doRecords = 'y';
|
|
|
|
} else {
|
|
|
|
$doRecords = $this->in(__d('cake_console', 'Would you like to use record importing for this fixture?'), array('y', 'n'), 'n');
|
2009-04-30 00:52:12 +00:00
|
|
|
}
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($doRecords === 'y') {
|
2009-04-30 00:52:12 +00:00
|
|
|
$options['records'] = true;
|
|
|
|
}
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($doRecords === 'n') {
|
2011-03-19 17:32:35 +00:00
|
|
|
$prompt = __d('cake_console', "Would you like to build this fixture with data from %s's table?", $modelName);
|
2009-07-21 04:00:39 +00:00
|
|
|
$fromTable = $this->in($prompt, array('y', 'n'), 'n');
|
2013-02-12 02:38:08 +00:00
|
|
|
if (strtolower($fromTable) === 'y') {
|
2009-07-20 04:59:18 +00:00
|
|
|
$options['fromTable'] = true;
|
|
|
|
}
|
|
|
|
}
|
2009-04-30 00:52:12 +00:00
|
|
|
return $options;
|
2009-04-29 00:32:15 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-29 00:32:15 +00:00
|
|
|
/**
|
|
|
|
* Assembles and writes a Fixture file
|
|
|
|
*
|
2009-04-30 00:52:12 +00:00
|
|
|
* @param string $model Name of model to bake.
|
|
|
|
* @param string $useTable Name of table to use.
|
2010-04-04 07:14:00 +00:00
|
|
|
* @param array $importOptions Options for public $import
|
2015-09-25 15:11:20 +00:00
|
|
|
* @return string|null Baked fixture content, otherwise null.
|
2009-04-29 00:32:15 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function bake($model, $useTable = false, $importOptions = array()) {
|
2010-12-09 03:45:18 +00:00
|
|
|
App::uses('CakeSchema', 'Model');
|
2010-12-31 18:05:04 +00:00
|
|
|
$table = $schema = $records = $import = $modelImport = null;
|
|
|
|
$importBits = array();
|
|
|
|
|
2009-04-30 00:52:12 +00:00
|
|
|
if (!$useTable) {
|
|
|
|
$useTable = Inflector::tableize($model);
|
|
|
|
} elseif ($useTable != Inflector::tableize($model)) {
|
2009-05-11 02:16:38 +00:00
|
|
|
$table = $useTable;
|
2009-04-30 00:52:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($importOptions)) {
|
|
|
|
if (isset($importOptions['schema'])) {
|
2010-12-31 18:05:04 +00:00
|
|
|
$modelImport = true;
|
|
|
|
$importBits[] = "'model' => '{$importOptions['schema']}'";
|
2009-04-30 00:52:12 +00:00
|
|
|
}
|
|
|
|
if (isset($importOptions['records'])) {
|
2010-12-31 18:05:04 +00:00
|
|
|
$importBits[] = "'records' => true";
|
2009-05-04 01:43:22 +00:00
|
|
|
}
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($this->connection !== 'default') {
|
2010-12-31 18:05:04 +00:00
|
|
|
$importBits[] .= "'connection' => '{$this->connection}'";
|
2009-04-30 00:52:12 +00:00
|
|
|
}
|
2010-12-31 18:05:04 +00:00
|
|
|
if (!empty($importBits)) {
|
|
|
|
$import = sprintf("array(%s)", implode(', ', $importBits));
|
2009-07-21 04:00:39 +00:00
|
|
|
}
|
2009-04-30 00:52:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->_Schema = new CakeSchema();
|
2009-05-04 01:43:22 +00:00
|
|
|
$data = $this->_Schema->read(array('models' => false, 'connection' => $this->connection));
|
2009-04-30 00:52:12 +00:00
|
|
|
if (!isset($data['tables'][$useTable])) {
|
2014-12-16 02:28:55 +00:00
|
|
|
$this->err("<warning>Warning:</warning> Could not find the '${useTable}' table for ${model}.");
|
2015-09-25 15:11:20 +00:00
|
|
|
return null;
|
2009-04-30 00:52:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$tableInfo = $data['tables'][$useTable];
|
2013-08-16 18:12:49 +00:00
|
|
|
if ($modelImport === null) {
|
2009-05-11 02:16:38 +00:00
|
|
|
$schema = $this->_generateSchema($tableInfo);
|
2009-04-30 00:52:12 +00:00
|
|
|
}
|
|
|
|
|
2011-12-14 01:40:30 +00:00
|
|
|
if (empty($importOptions['records']) && !isset($importOptions['fromTable'])) {
|
2009-04-30 00:52:12 +00:00
|
|
|
$recordCount = 1;
|
|
|
|
if (isset($this->params['count'])) {
|
|
|
|
$recordCount = $this->params['count'];
|
|
|
|
}
|
2009-07-21 04:00:39 +00:00
|
|
|
$records = $this->_makeRecordString($this->_generateRecords($tableInfo, $recordCount));
|
|
|
|
}
|
2011-12-14 01:40:30 +00:00
|
|
|
if (!empty($this->params['records']) || isset($importOptions['fromTable'])) {
|
2009-07-21 04:00:39 +00:00
|
|
|
$records = $this->_makeRecordString($this->_getRecordsFromTable($model, $useTable));
|
2009-04-30 00:52:12 +00:00
|
|
|
}
|
2012-12-20 12:47:03 +00:00
|
|
|
$out = $this->generateFixtureFile($model, compact('records', 'table', 'schema', 'import'));
|
2009-05-04 01:43:22 +00:00
|
|
|
return $out;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-04 01:43:22 +00:00
|
|
|
/**
|
|
|
|
* Generate the fixture file, and write to disk
|
|
|
|
*
|
|
|
|
* @param string $model name of the model being generated
|
2011-07-29 02:03:44 +00:00
|
|
|
* @param string $otherVars Contents of the fixture file.
|
2009-12-30 03:50:43 +00:00
|
|
|
* @return string Content saved into fixture file.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function generateFixtureFile($model, $otherVars) {
|
2009-05-11 02:16:38 +00:00
|
|
|
$defaults = array('table' => null, 'schema' => null, 'records' => null, 'import' => null, 'fields' => null);
|
|
|
|
$vars = array_merge($defaults, $otherVars);
|
2009-06-07 00:03:04 +00:00
|
|
|
|
2010-03-05 02:57:48 +00:00
|
|
|
$path = $this->getPath();
|
2011-04-11 01:26:06 +00:00
|
|
|
$filename = Inflector::camelize($model) . 'Fixture.php';
|
2009-05-11 02:16:38 +00:00
|
|
|
|
2009-05-13 01:26:37 +00:00
|
|
|
$this->Template->set('model', $model);
|
|
|
|
$this->Template->set($vars);
|
2009-07-01 04:50:38 +00:00
|
|
|
$content = $this->Template->generate('classes', 'fixture');
|
2009-05-11 02:16:38 +00:00
|
|
|
|
2011-04-25 19:17:59 +00:00
|
|
|
$this->out("\n" . __d('cake_console', 'Baking test fixture for %s...', $model), 1, Shell::QUIET);
|
2009-04-30 00:52:12 +00:00
|
|
|
$this->createFile($path . $filename, $content);
|
2009-05-11 02:16:38 +00:00
|
|
|
return $content;
|
2009-04-30 00:52:12 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-03-05 02:57:48 +00:00
|
|
|
/**
|
|
|
|
* Get the path to the fixtures.
|
|
|
|
*
|
2010-04-24 01:57:59 +00:00
|
|
|
* @return string Path for the fixtures
|
2010-03-05 02:57:48 +00:00
|
|
|
*/
|
2010-04-24 01:57:59 +00:00
|
|
|
public function getPath() {
|
2010-03-05 02:57:48 +00:00
|
|
|
$path = $this->path;
|
|
|
|
if (isset($this->plugin)) {
|
2011-05-30 19:04:38 +00:00
|
|
|
$path = $this->_pluginPath($this->plugin) . 'Test' . DS . 'Fixture' . DS;
|
2010-03-05 02:57:48 +00:00
|
|
|
}
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
2009-04-30 00:52:12 +00:00
|
|
|
/**
|
|
|
|
* Generates a string representation of a schema.
|
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @param array $tableInfo Table schema array
|
2009-04-30 00:52:12 +00:00
|
|
|
* @return string fields definitions
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _generateSchema($tableInfo) {
|
2012-12-03 11:31:56 +00:00
|
|
|
$schema = trim($this->_Schema->generateTable('f', $tableInfo), "\n");
|
|
|
|
return substr($schema, 13, -1);
|
2009-04-30 00:52:12 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-30 00:52:12 +00:00
|
|
|
/**
|
|
|
|
* Generate String representation of Records
|
|
|
|
*
|
2011-07-29 02:03:44 +00:00
|
|
|
* @param array $tableInfo Table schema array
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int $recordCount The number of records to generate.
|
2009-07-21 04:00:39 +00:00
|
|
|
* @return array Array of records to use in the fixture.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _generateRecords($tableInfo, $recordCount = 1) {
|
2009-07-21 04:00:39 +00:00
|
|
|
$records = array();
|
2009-04-30 00:52:12 +00:00
|
|
|
for ($i = 0; $i < $recordCount; $i++) {
|
2009-07-21 04:00:39 +00:00
|
|
|
$record = array();
|
2009-04-30 00:52:12 +00:00
|
|
|
foreach ($tableInfo as $field => $fieldInfo) {
|
|
|
|
if (empty($fieldInfo['type'])) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-03-01 18:42:39 +00:00
|
|
|
$insert = '';
|
2009-04-30 00:52:12 +00:00
|
|
|
switch ($fieldInfo['type']) {
|
|
|
|
case 'integer':
|
2009-11-09 03:22:08 +00:00
|
|
|
case 'float':
|
2009-04-30 00:52:12 +00:00
|
|
|
$insert = $i + 1;
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2009-11-09 03:22:08 +00:00
|
|
|
case 'string':
|
|
|
|
case 'binary':
|
2009-07-21 04:00:39 +00:00
|
|
|
$isPrimaryUuid = (
|
2013-02-12 02:38:08 +00:00
|
|
|
isset($fieldInfo['key']) && strtolower($fieldInfo['key']) === 'primary' &&
|
2009-07-21 04:00:39 +00:00
|
|
|
isset($fieldInfo['length']) && $fieldInfo['length'] == 36
|
|
|
|
);
|
|
|
|
if ($isPrimaryUuid) {
|
2015-01-05 00:00:57 +00:00
|
|
|
$insert = CakeText::uuid();
|
2009-07-21 04:00:39 +00:00
|
|
|
} else {
|
|
|
|
$insert = "Lorem ipsum dolor sit amet";
|
|
|
|
if (!empty($fieldInfo['length'])) {
|
2012-11-14 11:24:08 +00:00
|
|
|
$insert = substr($insert, 0, (int)$fieldInfo['length'] - 2);
|
2009-07-21 04:00:39 +00:00
|
|
|
}
|
2009-04-30 00:52:12 +00:00
|
|
|
}
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2009-04-30 02:50:43 +00:00
|
|
|
case 'timestamp':
|
2011-08-21 00:08:45 +00:00
|
|
|
$insert = time();
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2009-04-30 00:52:12 +00:00
|
|
|
case 'datetime':
|
2011-08-21 00:08:45 +00:00
|
|
|
$insert = date('Y-m-d H:i:s');
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2009-04-30 00:52:12 +00:00
|
|
|
case 'date':
|
2011-08-21 00:08:45 +00:00
|
|
|
$insert = date('Y-m-d');
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2009-04-30 00:52:12 +00:00
|
|
|
case 'time':
|
2011-08-21 00:08:45 +00:00
|
|
|
$insert = date('H:i:s');
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2009-04-30 00:52:12 +00:00
|
|
|
case 'boolean':
|
|
|
|
$insert = 1;
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2009-04-30 00:52:12 +00:00
|
|
|
case 'text':
|
2011-08-21 00:08:45 +00:00
|
|
|
$insert = "Lorem ipsum dolor sit amet, aliquet feugiat.";
|
2009-04-30 00:52:12 +00:00
|
|
|
$insert .= " Convallis morbi fringilla gravida,";
|
|
|
|
$insert .= " phasellus feugiat dapibus velit nunc, pulvinar eget sollicitudin";
|
|
|
|
$insert .= " venenatis cum nullam, vivamus ut a sed, mollitia lectus. Nulla";
|
|
|
|
$insert .= " vestibulum massa neque ut et, id hendrerit sit,";
|
|
|
|
$insert .= " feugiat in taciti enim proin nibh, tempor dignissim, rhoncus";
|
2011-08-21 00:08:45 +00:00
|
|
|
$insert .= " duis vestibulum nunc mattis convallis.";
|
2013-07-02 22:52:48 +00:00
|
|
|
break;
|
2009-04-30 00:52:12 +00:00
|
|
|
}
|
2009-07-21 04:00:39 +00:00
|
|
|
$record[$field] = $insert;
|
|
|
|
}
|
|
|
|
$records[] = $record;
|
|
|
|
}
|
|
|
|
return $records;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-07-21 04:00:39 +00:00
|
|
|
/**
|
2014-07-30 20:11:03 +00:00
|
|
|
* Convert a $records array into a string.
|
2009-07-21 04:00:39 +00:00
|
|
|
*
|
|
|
|
* @param array $records Array of records to be converted to string
|
|
|
|
* @return string A string value of the $records array.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _makeRecordString($records) {
|
2009-07-21 04:00:39 +00:00
|
|
|
$out = "array(\n";
|
|
|
|
foreach ($records as $record) {
|
|
|
|
$values = array();
|
|
|
|
foreach ($record as $field => $value) {
|
2011-08-21 00:08:45 +00:00
|
|
|
$val = var_export($value, true);
|
2012-05-09 14:08:50 +00:00
|
|
|
if ($val === 'NULL') {
|
|
|
|
$val = 'null';
|
|
|
|
}
|
2011-08-21 00:08:45 +00:00
|
|
|
$values[] = "\t\t\t'$field' => $val";
|
2009-04-30 00:52:12 +00:00
|
|
|
}
|
|
|
|
$out .= "\t\tarray(\n";
|
2009-07-21 04:00:39 +00:00
|
|
|
$out .= implode(",\n", $values);
|
2009-04-30 00:52:12 +00:00
|
|
|
$out .= "\n\t\t),\n";
|
|
|
|
}
|
2009-05-11 02:16:38 +00:00
|
|
|
$out .= "\t)";
|
2009-04-30 00:52:12 +00:00
|
|
|
return $out;
|
2009-04-29 00:32:15 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-07-21 04:00:39 +00:00
|
|
|
/**
|
|
|
|
* Interact with the user to get a custom SQL condition and use that to extract data
|
|
|
|
* to build a fixture.
|
2009-07-24 19:18:37 +00:00
|
|
|
*
|
2009-07-21 04:00:39 +00:00
|
|
|
* @param string $modelName name of the model to take records from.
|
|
|
|
* @param string $useTable Name of table to use.
|
|
|
|
* @return array Array of records.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _getRecordsFromTable($modelName, $useTable = null) {
|
2015-07-23 16:03:42 +00:00
|
|
|
$modelObject = new Model(array('name' => $modelName, 'table' => $useTable, 'ds' => $this->connection));
|
2009-10-24 18:32:56 +00:00
|
|
|
if ($this->interactive) {
|
|
|
|
$condition = null;
|
2011-06-20 23:33:00 +00:00
|
|
|
$prompt = __d('cake_console', "Please provide a SQL fragment to use as conditions\nExample: WHERE 1=1");
|
2009-10-24 18:32:56 +00:00
|
|
|
while (!$condition) {
|
2011-06-20 23:33:00 +00:00
|
|
|
$condition = $this->in($prompt, null, 'WHERE 1=1');
|
2009-10-24 18:32:56 +00:00
|
|
|
}
|
2015-07-23 16:03:42 +00:00
|
|
|
|
|
|
|
$recordsFound = $modelObject->find('count', array(
|
|
|
|
'conditions' => $condition,
|
|
|
|
'recursive' => -1,
|
|
|
|
));
|
|
|
|
|
2011-06-20 23:33:00 +00:00
|
|
|
$prompt = __d('cake_console', "How many records do you want to import?");
|
2015-07-23 16:03:42 +00:00
|
|
|
$recordCount = $this->in($prompt, null, ($recordsFound < 10 ) ? $recordsFound : 10);
|
2009-10-24 18:32:56 +00:00
|
|
|
} else {
|
2011-06-20 23:33:00 +00:00
|
|
|
$condition = 'WHERE 1=1';
|
|
|
|
$recordCount = (isset($this->params['count']) ? $this->params['count'] : 10);
|
2009-07-21 04:00:39 +00:00
|
|
|
}
|
2015-07-23 16:03:42 +00:00
|
|
|
|
2009-07-21 04:00:39 +00:00
|
|
|
$records = $modelObject->find('all', array(
|
|
|
|
'conditions' => $condition,
|
2011-06-20 23:33:00 +00:00
|
|
|
'recursive' => -1,
|
|
|
|
'limit' => $recordCount
|
2009-07-21 04:00:39 +00:00
|
|
|
));
|
2012-12-20 12:47:03 +00:00
|
|
|
|
2009-12-30 03:39:49 +00:00
|
|
|
$schema = $modelObject->schema(true);
|
2009-07-21 04:00:39 +00:00
|
|
|
$out = array();
|
|
|
|
foreach ($records as $record) {
|
|
|
|
$row = array();
|
|
|
|
foreach ($record[$modelObject->alias] as $field => $value) {
|
2011-08-30 14:44:45 +00:00
|
|
|
if ($schema[$field]['type'] === 'boolean') {
|
|
|
|
$value = (int)(bool)$value;
|
|
|
|
}
|
2011-08-21 00:08:45 +00:00
|
|
|
$row[$field] = $value;
|
2009-07-21 04:00:39 +00:00
|
|
|
}
|
|
|
|
$out[] = $row;
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-29 00:32:15 +00:00
|
|
|
}
|