tims-todo/tests/generate

146 lines
3.7 KiB
Plaintext
Raw Normal View History

2014-08-08 15:41:59 -04:00
#!/usr/bin/env php
<?php
/*
* fooStack, CIUnit for CodeIgniter
* Copyright (c) 2008-2009 Clemens Gruenberger
* Released under the MIT license, see:
* http://www.opensource.org/licenses/mit-license.php
*/
/**
* generate fixtures from test database
* fixtures are named according to convention and are only
* created if they didin't exist before
*/
require_once (__DIR__ . '/bootstrap.php');
// Set the controller to the default controller. This allows us access to the
// CI framework.
set_controller();
/*
* this function for extraction of command line arguments
* might go back into some foo helper class
*/
function args_arr($arg_arr = false)
{
//extract commandline arguments
if ($arg_arr === false) {
$argv = $_SERVER['argv'];
} else {
$argv = $arg_arr;
}
array_shift($argv);
$flags = array();
$params = array();
$args = array();
foreach ($argv as $arg) {
$param_parts = preg_split('/=/', $arg);
if (count($param_parts) == 2) {
$params[$param_parts[0]] = $param_parts[1];
} else {
if (substr($arg,0,1) == "-") {
$flags[] = substr($arg, 1);
} else {
$args[] = $arg;
}
}
}
return array('params' => $params, 'flags' => $flags, 'args' => $args);
}
class Generate {
function __construct()
{
$this->CI = &get_instance();
$this->CI->spyc = new Spyc();
$this->Ci->fixture = new Fixture();
$this->args_arr = args_arr();
$this->params = $this->args_arr['params'];
$this->args = $this->args_arr['args'];
$this->flags = $this->args_arr['flags'];
if (isset($this->args[0]))
{
$function = $this->args[0];
$this->$function();
}
else
{
echo "
Please specify what task to run:
e.g.: 'php generate fixtures' or
'php generate migration'
flags can by specified by '-flag'
parameters by 'PARAM=VALUE'
e.g.: 'php generate fixtures -verbose'
'php generate fixtures db=different_db_than_default'
To receive more information on a task
type 'php generate help task'
e.g.: 'php generate help migration'
";
}
}
function fixtures()
{
$db = isset($this->params['db'])? $this->params['db']:'db';
$prefix = ($db == 'db2')? "db2_":"";
/**
* Update this controller to your default controller
*/
$this->CI = set_controller('welcome');
echo "generating...\n";
$this->CI->load->database();
$tables = $this->CI->$db->list_tables();
foreach ($tables as $table)
{
echo $table;
$fields = $this->CI->$db->list_fields($table);
$fixt_name= 'fixtures/'.$prefix.$table.'_fixt.yml';
if (file_exists($fixt_name))
{
echo "fixture $fixt_name already exits!\n";
}
else
{
$h = fopen($fixt_name, 'w') or die("can't open file");
//write fixture!
foreach (array('1', '2', '3') as $rowname)
{
fwrite($h, $rowname.":\n");
foreach ($fields as $field)
{
//echo $field;
fwrite($h, ' ' . $field . ": \n");
}
fwrite($h, "\n");
}
echo " *** fixture $fixt_name written.\n";
fclose($h);
}
}
}
}
$gen = new Generate();