目標:學習如何測試 native function 的邏輯?
為 HolidayTest
類別加上 namespace Lab3
:
namespace Lab3;
在 namespace 下方覆寫 shell_exec
:
function shell_exec($cmd)
{
HolidayTest::$functions->shell_exec($cmd);
}
在 HolidayTest
類別中,加入:
/**
* @var \Mockery\MockInterface
*/
public static $functions = null;
public function setUp()
{
self::$functions = \Mockery::mock();
self::$functions->shouldReceive('shell_exec')
->withAnyArgs()
->andReturnNull();
}
public function tearDown()
{
\Mockery::close();
}
執行測試。
打開 src/Holiday.php
,瞭解運作方式。
打開 tests/HolidayTest.php
,查看驗證方式。
將 // $this->assertTrue($actual);
及 // $this->assertFalse($actual);
的 //
註解拿掉。
執行測試。
在 Terminal 執行:
composer require nesbot/carbon
編輯 src/Holiday.php
,將:
return '12/25' === date('m/d');
替換成:
$date = Carbon::now();
return '12/25' === $date->format('m/d');
編輯 tests/HolidayTest.php
,將 today_should_be_xmas
方法中的:
$holiday = new Holiday;
$actual = $holiday->isTodayXmas();
替換為:
$holiday = new Holiday;
Carbon::setTestNow(Carbon::create(2015, 12, 25));
$actual = $holiday->isTodayXmas();
Carbon::setTestNow();
執行測試。
將 today_should_be_not_xmas
方法中的:
$holiday = new Holiday;
$actual = $holiday->isTodayXmas();
替換為:
$holiday = new Holiday;
Carbon::setTestNow(Carbon::create(2015, 1, 1));
$actual = $holiday->isTodayXmas();
Carbon::setTestNow();
執行測試。