CakePHP ShellをUnitTestでテストする方法を探してもあまり情報が無かったのでメモ書き。
ポイントとしては
\app\Test\Case\Console\Command
というフォルダを作ります。
HogeShell.phpのテストソースを作る場合
\app\Test\Case\Console\Command\HogeShellTest.php
という名前で中身のテンプレートとしては
< ?php
App::uses('ConsoleOutput', 'Console');
App::uses('ShellDispatcher', 'Console');
App::uses('Shell', 'Console');
App::uses('Folder', 'Utility');
App::uses('HogeShell', 'Console/Command');
class HogeShellTest extends CakeTestCase {
public $target;
public function setUp() {
parent::setUp();
$output = $this->getMock('ConsoleOutput', array(), array(), '', false);
$error = $this->getMock('ConsoleOutput', array(), array(), '', false);
$in = $this->getMock('ConsoleInput', array(), array(), '', false);
$this->target = new HogeShell($output, $error, $in);
$this->target->initialize();
}
public function tearDown() {
parent::tearDown();
}
public function testCommon(){
//ここにテストを書く
}
}
こんな感じです。後はコントローラーやモデルでの書き方と同じでOKのようです。
起動方法は
./app/Console/cake test app Console/Command/HogeShell
問う感じで指定すればいいようです。

