2013-02-04 128 views
3

我在我的php函數中有一個const限制,如下所示。Const在PHPUnit測試用例

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

現在我想寫的單位的情況下該使用PHPUnit的,但在單位情況下,我想重置的限制,使我的測試情況下,如果有人復位上限不會失敗。如何在我的單元測試函數中設置php常量?

+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'); 
    } 
}