#CakePHP2でBakeでFixtureをディレクトリ分けする方法
BakeでFixtureを実行するとapp/Test/Fixture
に出力される。テストごとに分けたい場合にデフォルトではできないので、bakeを変更し、fixuteの指定を変更することで対応可能であった。
Bakeでの出力のしかた
###ファイルのコピー
/lib/Cake/Console/Command/Task/FixtureTask.php
↓
/app/Console/Command/Task/FixtureTask.php
###以下のメソッドにオプションを追加しパラメータを追加する
-d で入力を受け付けるようになる
public function getOptionParser() {
$parser = parent::getOptionParser();
$parser->description(
__d('cake_console', 'Generate fixtures for use with the test suite. You can use `bake fixture all` to bake all fixtures.')
)->addArgument('name', array(
'help' => __d('cake_console', 'Name of the fixture to bake. Can use Plugin.name to bake plugin fixtures.')
))->addOption('count', array(
'help' => __d('cake_console', 'When using generated data, the number of records to include in the fixture(s).'),
'short' => 'n',
'default' => 1
))->addOption('connection', array(
'help' => __d('cake_console', 'Which database configuration to use for baking.'),
'short' => 'c',
'default' => 'default'
))->addOption('plugin', array(
'help' => __d('cake_console', 'CamelCased name of the plugin to bake fixtures for.'),
'short' => 'p'
))->addOption('schema', array(
'help' => __d('cake_console', 'Importing schema for fixtures rather than hardcoding it.'),
'short' => 's',
'boolean' => true
))->addOption('theme', array(
'short' => 't',
'help' => __d('cake_console', 'Theme to use when baking code.')
))->addOption('force', array(
'short' => 'f',
'help' => __d('cake_console', 'Force overwriting existing files without prompting.')
// 追加
))->addOption('records', array(
'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.'),
'short' => 'r',
'boolean' => true
))->epilog(
__d('cake_console', 'Omitting all arguments and options will enter into an interactive mode.')
);
return $parser;
}
###オプション値を利用できるようにする
public function getPath() {
public function getPath() {
$path = $this->path;
if (isset($this->plugin)) {
$path = $this->_pluginPath($this->plugin) . 'Test' . DS . 'Fixture' . DS;
}
// 追加
if(isset($this->params["dir"])){
$path .= $this->params["dir"]. DS;
}
return $path;
}
##コマンドの実行
cake.php bake fixture modelname -d Test01;
以下にFixtureができる
/app/Test/Fixture/Test01/ModelnameFixture.php
#利用方法
class ModelnameControllerTest extends CakeFixtureManager{
public $fixtures = array(
'/Test01/modelname',
)l
}