2013-02-04 3 views
3

필자는 아래와 같이 php 함수에 const 제한이 있습니다.Const In PHPUnit 테스트 케이스

const limit = 5 ; 
function limit_check($no_of_page) 
{ 
     if($no_of_page < const limit) 
     return true;  else return false; 
} 

지금 나는이 사용 phpunit을위한 단위 케이스를 작성하고 싶지만 단위의 경우 누군가가 한계를 재설정하면 내 테스트 케이스가 실패하지 않도록 제한을 재설정합니다. 내 단위 테스트 함수에서 PHP const를 설정하는 방법?

+2

. 정적 속성을 대신 사용하지 않는 이유는 무엇입니까? –

답변

2

일반적으로 코딩 제한 때문에이 제한을 설정 했으므로이 제한을 검사하고 적용해야하는 이유가있을 수 있으므로이 제한을 적용해야합니다. 그런 다음

class FOO 
{ 
    const limit = 5; 
    private $PageNumberLimit; 

    public function __construct($PageLimit = self::limit) 
    { 
     $this->SetPageLimit($PageLimit); 
    } 

    public function SetPageLimit($PageLimit) 
    { 
     $this->PageNumberLimit = $PageLimit; 
    } 

    public function limit_check($no_of_page) 
    { 
     if($no_of_page < $this->PageNumberLimit) 
      return true; 
     else 
      return false; 
    } 
} 

시험 :없는 경우에는, 당신은 더 다음과 같은 일을 할 수

이 제한 * 일정 * 할 수 있도록 설계되지 않은 것 같습니다
class FOO_TEST extends PHPUnit_Framework_TestCase 
{ 
    protected $FooClass; 

    protected function setUp() 
    { 
     $this->FooClass = new FOO(); 
    } 

    public function testConstantValue() 
    { 
     $ReflectObject = new ReflectionClass('FOO'); 
     $this->assertEquals(5, $ReflectObject->getConstant('limit'), 'Test that the default Page Limit of 5 was not changed'); 
    } 

    public function testDefaultLimitUsed() 
    { 
     $ReflectObject = new ReflectionClass('FOO'); 
     $this->assertEquals($ReflectObject->getConstant('limit'), $this->FooClass->PageNumberLimit, 'Test that the default Page Limit is used by matching value to constant.'); 
    } 

    public function testlimit_check() 
    { 
     $this->assertTrue($this->FooClass->limit_check(4), 'Page Number is less than Limit'); 
     $this->assertFalse($this->FooClass->limit_check(5), 'Page Number is equal to Limit'); 
     $this->assertFalse($this->FooClass->limit_check(6), 'Page Number is greater than Limit'); 
    } 

    public static function PageNumberDataProvider() 
    { 
     return array(
      array(4), 
      array(5), 
      array(6), 
      ); 
    } 

    /** 
    * @dataProvider PageNumberDataProvider 
    */ 
    public function testSetPageLimitWithConstructor($NumberOfPages) 
    { 
     $Foo = new FOO($NumberOfPages);   // Create the class using the constructor 

     $this->assertTrue($Foo->limit_check($NumberOfPages - 1), 'Page Number is less than Limit'); 
     $this->assertFalse($Foo->limit_check($NumberOfPages), 'Page Number is equal to Limit'); 
     $this->assertFalse($Foo->limit_check($NumberOfPages + 1), 'Page Number is greater than Limit'); 
    } 

    /** 
    * @dataProvider PageNumberDataProvider 
    */ 
    public function testSetPageLimitWithSetPageLimit($NumberOfPages) 
    { 
     $this->FooClass->SetPageLimit($NumberOfPages);   // Set the number using the public function 

     $this->assertTrue($Foo->limit_check($NumberOfPages - 1), 'Page Number is less than Limit'); 
     $this->assertFalse($Foo->limit_check($NumberOfPages), 'Page Number is equal to Limit'); 
     $this->assertFalse($Foo->limit_check($NumberOfPages + 1), 'Page Number is greater than Limit'); 
    } 
}