PHPUnit测试用例中的Const
我的php函数中有一个常量限制,如下所示PHPUnit测试用例中的Const,php,unit-testing,phpunit,Php,Unit Testing,Phpunit,我的php函数中有一个常量限制,如下所示 const limit = 5 ; function limit_check($no_of_page) { if($no_of_page < const limit) return true; else return false; } const limit=5; 功能限制检查($no\u of\u页) { 如果($第页无
const limit = 5 ;
function limit_check($no_of_page)
{
if($no_of_page < const limit)
return true; else return false;
}
const limit=5;
功能限制检查($no\u of\u页)
{
如果($第页无<常数限制)
返回true;否则返回false;
}
现在我想用PHPUnit来编写单元用例,但是在单元用例中,我想重置限制,这样当有人重置限制时,我的测试用例就不会失败。如何在我的单元测试函数中设置php常量?通常,您设置此限制是出于编码原因,因此,您应该检查并强制执行此限制,因为它可能有存在的原因。但是,如果没有,那么您可以有以下内容:
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');
}
}