Exemplo n.º 1
0
    /**
     * Builds the tests fixtures for the model and create the file
     *
     * @param string $model the name of the model
     * @param string $useTable table name
     * @return array $records, used in ModelTask::bakeTest() to create $expected
     * @todo move this to a task
     */
    function fixture($model, $useTable = null)
    {
        if (!class_exists('CakeSchema')) {
            App::import('Model', 'Schema');
        }
        $out = "\nclass {$model}Fixture extends CakeTestFixture {\n";
        $out .= "\tvar \$name = '{$model}';\n";
        if (!$useTable) {
            $useTable = Inflector::tableize($model);
        } else {
            $out .= "\tvar \$table = '{$useTable}';\n";
        }
        $schema = new CakeSchema();
        $data = $schema->read(array('models' => false));
        if (!isset($data['tables'][$useTable])) {
            return false;
        }
        $tables[$model] = $data['tables'][$useTable];
        foreach ($tables as $table => $fields) {
            if (!is_numeric($table) && $table !== 'missing') {
                $out .= "\tvar \$fields = array(\n";
                $records = array();
                if (is_array($fields)) {
                    $cols = array();
                    foreach ($fields as $field => $value) {
                        if ($field != 'indexes') {
                            if (is_string($value)) {
                                $type = $value;
                                $value = array('type' => $type);
                            }
                            $col = "\t\t\t'{$field}' => array('type'=>'" . $value['type'] . "', ";
                            switch ($value['type']) {
                                case 'integer':
                                    $insert = 1;
                                    break;
                                case 'string':
                                    $insert = "Lorem ipsum dolor sit amet";
                                    if (!empty($value['length'])) {
                                        $insert = substr($insert, 0, (int) $value['length'] - 2);
                                    }
                                    $insert = "'{$insert}'";
                                    break;
                                case 'datetime':
                                    $ts = date('Y-m-d H:i:s');
                                    $insert = "'{$ts}'";
                                    break;
                                case 'date':
                                    $ts = date('Y-m-d');
                                    $insert = "'{$ts}'";
                                    break;
                                case 'time':
                                    $ts = date('H:i:s');
                                    $insert = "'{$ts}'";
                                    break;
                                case 'boolean':
                                    $insert = 1;
                                    break;
                                case 'text':
                                    $insert = '\'Lorem ipsum dolor sit amet, aliquet feugiat. Convallis morbi fringilla gravida,
									phasellus feugiat dapibus velit nunc, pulvinar eget sollicitudin venenatis cum nullam,
									vivamus ut a sed, mollitia lectus. Nulla vestibulum massa neque ut et, id hendrerit sit,
									feugiat in taciti enim proin nibh, tempor dignissim, rhoncus duis vestibulum nunc mattis convallis.
									Orci aliquet, in lorem et velit maecenas luctus, wisi nulla at, mauris nam ut a, lorem et et elit eu.
									Sed dui facilisi, adipiscing mollis lacus congue integer, faucibus consectetuer eros amet sit sit,
									magna dolor posuere. Placeat et, ac occaecat rutrum ante ut fusce. Sit velit sit porttitor non enim purus,
									id semper consectetuer justo enim, nulla etiam quis justo condimentum vel, malesuada ligula arcu. Nisl neque,
									ligula cras suscipit nunc eget, et tellus in varius urna odio est. Fuga urna dis metus euismod laoreet orci,
									litora luctus suspendisse sed id luctus ut. Pede volutpat quam vitae, ut ornare wisi. Velit dis tincidunt,
									pede vel eleifend nec curabitur dui pellentesque, volutpat taciti aliquet vivamus viverra, eget tellus ut
									feugiat lacinia mauris sed, lacinia et felis.\'';
                                    break;
                            }
                            $records[] = "\t\t\t'{$field}'  => {$insert}";
                            unset($value['type']);
                            $col .= join(', ', $schema->__values($value));
                        } else {
                            $col = "\t\t\t'indexes' => array(";
                            $props = array();
                            foreach ((array) $value as $key => $index) {
                                $props[] = "'{$key}' => array(" . join(', ', $schema->__values($index)) . ")";
                            }
                            $col .= join(', ', $props);
                        }
                        $col .= ")";
                        $cols[] = $col;
                    }
                    $out .= join(",\n", $cols);
                }
                $out .= "\n\t\t\t);\n";
            }
        }
        $records = join(",\n", $records);
        $out .= "\tvar \$records = array(array(\n{$records}\n\t\t\t));\n";
        $out .= "}\n";
        $path = TESTS . DS . 'fixtures' . DS;
        if (isset($this->plugin)) {
            $pluginPath = 'plugins' . DS . Inflector::underscore($this->plugin) . DS;
            $path = APP . $pluginPath . 'tests' . DS . 'fixtures' . DS;
        }
        $filename = Inflector::underscore($model) . '_fixture.php';
        $header = '$Id';
        $content = "<?php \n/* SVN FILE: {$header}\$ */\n/* " . $model . " Fixture generated on: " . date('Y-m-d H:m:s') . " : " . time() . "*/\n{$out}?>";
        $this->out("\nBaking test fixture for {$model}...");
        if ($this->createFile($path . $filename, $content)) {
            return $records;
        }
        return false;
    }
Exemplo n.º 2
0
 /**
  * Builds the tests fixtures for the model and create the file
  *
  * @param string $model the name of the model
  * @param string $useTable table name
  * @return array $records, used in ModelTask::bakeTest() to create $expected
  * @todo move this to a task
  */
 function fixture($model, $useTable = null)
 {
     if (!class_exists('CakeSchema')) {
         App::import('Model', 'Schema');
     }
     $out = "\nclass {$model}Fixture extends CakeTestFixture {\n";
     $out .= "\tvar \$name = '{$model}';\n";
     if (!$useTable) {
         $useTable = Inflector::tableize($model);
     } else {
         $out .= "\tvar \$table = '{$useTable}';\n";
     }
     $schema = new CakeSchema();
     $data = $schema->read(array('models' => false, 'connection' => $this->useDbConfig));
     if (!isset($data['tables'][$useTable])) {
         return false;
     }
     $tables[$model] = $data['tables'][$useTable];
     foreach ($tables as $table => $fields) {
         if (!is_numeric($table) && $table !== 'missing') {
             $out .= "\tvar \$fields = array(\n";
             $records = array();
             if (is_array($fields)) {
                 $cols = array();
                 foreach ($fields as $field => $value) {
                     if ($field != 'indexes') {
                         if (is_string($value)) {
                             $type = $value;
                             $value = array('type' => $type);
                         }
                         $col = "\t\t'{$field}' => array('type'=>'" . $value['type'] . "', ";
                         switch ($value['type']) {
                             case 'float':
                             case 'integer':
                                 $insert = 1;
                                 break;
                             case 'binary':
                             case 'string':
                                 $insert = "Lorem ipsum dolor sit amet";
                                 if (!empty($value['length'])) {
                                     $insert = substr($insert, 0, (int) $value['length'] - 2);
                                 }
                                 $insert = "'{$insert}'";
                                 break;
                             case 'datetime':
                                 $ts = date('Y-m-d H:i:s');
                                 $insert = "'{$ts}'";
                                 break;
                             case 'date':
                                 $ts = date('Y-m-d');
                                 $insert = "'{$ts}'";
                                 break;
                             case 'time':
                                 $ts = date('H:i:s');
                                 $insert = "'{$ts}'";
                                 break;
                             case 'boolean':
                                 $insert = 1;
                                 break;
                             case 'text':
                                 $insert = "'Lorem ipsum dolor sit amet, aliquet feugiat. Convallis morbi fringilla gravida,";
                                 $insert .= "phasellus feugiat dapibus velit nunc, pulvinar eget sollicitudin venenatis cum nullam,";
                                 $insert .= "vivamus ut a sed, mollitia lectus. Nulla vestibulum massa neque ut et, id hendrerit sit,";
                                 $insert .= "feugiat in taciti enim proin nibh, tempor dignissim, rhoncus duis vestibulum nunc mattis convallis.'";
                                 break;
                         }
                         $records[] = "\t\t'{$field}' => {$insert}";
                         unset($value['type']);
                         $col .= implode(', ', $schema->__values($value));
                     } else {
                         $col = "\t\t'indexes' => array(";
                         $props = array();
                         foreach ((array) $value as $key => $index) {
                             $props[] = "'{$key}' => array(" . implode(', ', $schema->__values($index)) . ")";
                         }
                         $col .= implode(', ', $props);
                     }
                     $col .= ")";
                     $cols[] = $col;
                 }
                 $out .= implode(",\n", $cols);
             }
             $out .= "\n\t);\n";
         }
     }
     $records = implode(",\n", $records);
     $out .= "\tvar \$records = array(array(\n{$records}\n\t));\n";
     $out .= "}\n";
     $path = TESTS . DS . 'fixtures' . DS;
     if (isset($this->plugin)) {
         $pluginPath = 'plugins' . DS . Inflector::underscore($this->plugin) . DS;
         $path = APP . $pluginPath . 'tests' . DS . 'fixtures' . DS;
     }
     $filename = Inflector::underscore($model) . '_fixture.php';
     $header = '$Id';
     $content = "<?php \n/* SVN FILE: {$header}\$ */\n/* " . $model . " Fixture generated on: " . date('Y-m-d H:i:s') . " : " . time() . "*/\n{$out}?>";
     $this->out("\nBaking test fixture for {$model}...");
     if ($this->createFile($path . $filename, $content)) {
         return str_replace("\t\t", "\t\t\t", $records);
     }
     return false;
 }