Skip to content
This repository was archived by the owner on Oct 25, 2022. It is now read-only.

jaceju-tutorial-examples/phpunit-native-function

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lab3 - 測試 Native Function

目標:學習如何測試 native function 的邏輯?

讓 shell_exec 可以被 mock

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);// 註解拿掉。

執行測試。

改用 Carbon

在 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();

執行測試。

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages