2017-03-02 62 views
1

好的,所以我正在看一個代碼,我正在做一個phpunit覆蓋測試。在該測試,我只得到2行未適當地工作:我getEarliestDateorNull內PHPUnit覆蓋測試elseif錯誤

private function getEarliestDateOrNull($snapshotDate = null, $preexistingEpisodeDate = null) 
{ 
    $date = null; 

    if ($snapshotDate instanceof DateTime && $preexistingEpisodeDate instanceof DateTime) { 
     $date = $snapshotDate < $preexistingEpisodeDate ? $snapshotDate : $preexistingEpisodeDate; 
    } elseif ($snapshotDate instanceof DateTime) { 
     $date = $snapshotDate; 
    } elseif ($preexistingEpisodeDate instanceof DateTime) { 
     $date = $preexistingEpisodeDate; 
    } 

    return ($date instanceof DateTime) ? $date->format('m/d/Y') : null; 
} 

的問題是,這兩個ELSEIF語句是測試出於某種原因不能讀取唯一的。有沒有辦法從elseif語句中改變它們?我的意思是說,如果可能有SWITCH的實施。該測試在PHP 7

+0

你說你有沒有適當工作兩行,但你貼103線... –

+0

@JoeC好的,我修改了它。我非常感激知道錯誤是什麼。我如何實現兩個elseif語句的替代方案? – OZTZ3

+0

我也遇到了這個問題elseif被PHPUnit 5.6.2 –

回答

0

運行得試錯後,如果您將其設置爲else ifelseif它應該工作:)

+0

覆蓋是的,我試過切換它,它的工作! 雖然我不知道爲什麼它不承認它?如果我從php 7切換到PHP 5.6,它應該可以在兩個版本上運行,對吧? – OZTZ3

+0

嘿安迪!我有一個後續問題,當我運行代碼時...因爲它進入if語句,它應該只讀取第一個if和忽略第二個else if,否則讀取第二個else if和忽略第一個if第二如果,對吧?在代碼覆蓋測試中,它讀取所有3條語句。你知道爲了挑選3個陳述中的一個可以實施什麼嗎?謝謝! – OZTZ3