2017-01-16 2 views
0

필자는 PHPUnit의 코드 커버리지 도구를 가져올 수 없거나 다음 줄을 포함 할 수 없더라도이 else 문을 덮음으로 표시 할 수 없습니다. 다른 클래스의 동일한 클래스에서 } else { 만 포함 된 다른 행은 올바르게 표시되어 있습니다. 약간 수정 된 소스와PHPUnit의 코드 커버리지를 단순한 '} else {'라인으로 변경하기

enter image description here

if (is_string($externalId) && $externalId != '') { 
     $sitesIds[] = $externalId; 
    } else if ($regionName != null && $regionName != '') { 
     $sitesIds = $this->sitesService->getSites($regionName); 
     if (!is_array($sitesIds) || count($sitesIds) == 0) { 
      throw new \Exception(self::NO_MATCHING_REGION, '404'); 
     } 
    } else { 
     throw new \Exception(self::BAD_REQUEST.'. Should specify station or region', '400'); 
    } 
+1

테스트에서 한 줄을 녹색으로 표시하면 코드가 향상되지 않지만 내 OCD는 필자에게 다음과 같은 내용의 마지막 줄을 표시하도록 강요합니다 :-) – Craig

+0

'else {'를 줄 바꿈 장물? – Furgas

+0

@Furgas 아니요.}는 덮개가없는 것으로 표시되어 있습니다. 반면에 else {는 흰색으로 덮어 쓰거나 검사 할 의도가 아닙니다. – Craig

답변

1

실제로 원 (그것은 단지 라벨 간주 될 수 있습니다) 아무것도하지 않습니다 덮어 쓰지 마라.

귀하의 문제는 ($regionName != null && $regionName != '')true하고 (!is_array($sitesIds) || count($sitesIds) == 0)false이며, (is_string($externalId) && $externalId != '')false 어디 테스트를하지 않아도됩니다. 당신은 적어도 하나 개의 요소 배열을 반환하는 $sitesIds = $this->sitesService->getSites($regionName);을 얻을 수있는 경우

, 당신의 빨간색 선이 될 것입니다 : (($externalId !== '') & ($regionName !== null && $regionName !== '') 당신은 !== 정확하게 일치하지 사용하는 대신에 동일하지 !=에보다 구체적으로 할 수 있습니다) 덮고 녹색으로 변합니다.

else 앞에 닫기 중괄호 }이 기술적으로 도달 할 수 있지만 테스트에는 적용되지 않는 것이 빨간색 줄에 나와 있습니다.

+0

그랬습니다. 이전 테스트는 '던지기'까지만 진행되었습니다. – Craig

0

: enter image description here

:

class A 
{ 
    const NO_MATCHING_REGION = 1; 
    const BAD_REQUEST  = 2; 

    private $sitesService = ['a' => ['AA'], 'b'=>12]; 

    public function a($externalId, $regionName) 
    { 
     $sitesIds = []; 
     if (is_string($externalId) && $externalId != '') { 
      $sitesIds[] = $externalId; 
     } else { 
      if ($regionName != null && $regionName != '') { 
       $sitesIds = $this->sitesService[$regionName]; 
       if (!is_array($sitesIds) || count($sitesIds) == 0) { 
        throw new \Exception(self::NO_MATCHING_REGION, '404'); 
       } 
      } else { 
       throw new \Exception(self::BAD_REQUEST.'. Should specify station or region', '400'); 
      } 
     } 
     return $sitesIds; 
    } 
} 

시험

class ATest extends \PHPUnit_Framework_TestCase 
{ 

    /** 
    * @dataProvider data 
    */ 
    public function testOk($id, $reg, $res) 
    { 
     $a = new A; 
     $r = $a->a($id, $reg); 
     $this->assertEquals($res, $r); 
    } 

    public function data() 
    { 
     return [ 
      ['a', 1, ['a']], 
      [1,'a', ['AA']] 
     ]; 
    } 

    /** 
    * @dataProvider error 
    * @expectedException \Exception 
    */ 
    public function testNotOK($id, $reg) 
    { 
     $a = new A; 
     $a->a($id, $reg); 
    } 

    public function error() 
    { 
     return [ 
      [1,'b'], 
      [1,null] 
     ]; 
    } 
} 

else 라인 커버

PHP 5.6.15-1 + deb.sury.org ~ 믿음직한 + 1

phpunit을 4.8.21 else 이후