我认为 app/lib/lib/Cake/Console/Command/TestShellTest.php 可以作为参考。
1、在app/Test/Case/Console/Command/yourfile.php中创建文件,使用App::uses('your class', 'relative path').
例如:
App::uses('yourShellClass', 'Console/Command');
App::uses('ShellDispatcher', 'Console');
App::uses('Shell', 'Console');
2、从你的shell类A编写一个模拟类B,其函数使用A类中的数据返回。例如:
class TestYourShellClass extends YourShellClass {
public function F1 ($parms){
//if you need use database here, you can
//$this->yourModel = ClassRegistry::init('yourModel');
return $this->_F1($parms);
}
}
3、编写A类的测试类,需要在启动时初始化。例如:
class YourShellClassTest extends CakeTestCase {
public function setUp()
{
parent::setUp();
$out = $this->getMock('ConsoleOutput', [], [], '', false);
$in = $this->getMock('ConsoleInput', [], [], '', false);
$this->Shell = $this->getMock(
// this is your class B, which mocks class A.
'TestYourShellClass',
['in', 'out', 'hr', 'help', 'error', 'err', '_stop', 'initialize', '_run', 'clear'],
[$out, $out, $in]
);
$this->Shell->OptionParser = $this->getMock('ConsoleOptionParser', [], [null, false]);
}
/**
* tear down method.
*
* @return void
*/
public function tearDown()
{
parent::tearDown();
unset($this->Dispatch, $this->Shell);
}
}
4、测试功能可以这样。
/**
* test _F1.
*
* @return void
*/
public function testF1()
{
$this->Shell->startup();
$return = $this->Shell->F1();
$expectedSellerCount = 14;
$this->assertSame($expectedSellerCount, $return);
}
然后你可以在http://yourdomain/test.php查看结果