如何使用phpunit测试函数中是否存在变量
how to test if variables exists in function with phpunit
我正在为 Class 编写单元测试,如下所示:
class example {
public function __construct($param1, $param2) {
$this->param1 = $param1
$this->param2 = $param2
}
}
是否可以在构造函数执行后测试$this->param1和$this->param2是否存在?我已经用谷歌搜索了这个但没有找到有效的答案。我用 Assertion contain 试过了,但这也没用。
如果要查看结果对象中的属性是否已分配指定值,请使用 Reflection class。在您的示例中,如果您的属性是 public:
public function testInitialParams()
{
$value1 = 'foo';
$value2 = 'bar';
$example = new Example($value1, $value2); // note that Example is using 'Standing CamelCase'
$sut = new \ReflectionClass($example);
$prop1 = $sut->getProperty('param1');
$prop1->setAccessible(true); // Needs to be done to access protected and private properties
$this->assertEquals($prop2->getValue($example), $value1, 'param1 got assigned the correct value');
$prop2 = $sut->getProperty('param2');
$prop2->setAccessible(true);
$this->assertEquals($prop2->getValue($example), $value2, 'param2 got assigned the correct value');
}
您尚未在 $this->param1
或 $this->param2
上声明任何可见性,因此默认情况下它们将是 public。
考虑到这一点,您应该能够像下面这样进行测试:
public function testConstructorSetsParams()
{
$param1 = 'testVal1';
$param2 = 'testVal2';
$object = new example($param1, $param2);
$this->assertEquals($param1, $object->param1);
$this->assertEquals($param2, $object->param2);
}
有一个断言 assertAttributeSame()
允许您查看 public、class 的受保护和私有属性。
看例子:
class ColorTest extends PHPUnit_Framework_TestCase {
public function test_assertAttributeSame() {
$hasColor = new Color();
$this->assertAttributeSame("red","publicColor",$hasColor);
$this->assertAttributeSame("green","protectedColor",$hasColor);
$this->assertAttributeSame("blue","privateColor",$hasColor);
$this->assertAttributeNotSame("wrong","privateColor",$hasColor);
}
}
我正在为 Class 编写单元测试,如下所示:
class example {
public function __construct($param1, $param2) {
$this->param1 = $param1
$this->param2 = $param2
}
}
是否可以在构造函数执行后测试$this->param1和$this->param2是否存在?我已经用谷歌搜索了这个但没有找到有效的答案。我用 Assertion contain 试过了,但这也没用。
如果要查看结果对象中的属性是否已分配指定值,请使用 Reflection class。在您的示例中,如果您的属性是 public:
public function testInitialParams()
{
$value1 = 'foo';
$value2 = 'bar';
$example = new Example($value1, $value2); // note that Example is using 'Standing CamelCase'
$sut = new \ReflectionClass($example);
$prop1 = $sut->getProperty('param1');
$prop1->setAccessible(true); // Needs to be done to access protected and private properties
$this->assertEquals($prop2->getValue($example), $value1, 'param1 got assigned the correct value');
$prop2 = $sut->getProperty('param2');
$prop2->setAccessible(true);
$this->assertEquals($prop2->getValue($example), $value2, 'param2 got assigned the correct value');
}
您尚未在 $this->param1
或 $this->param2
上声明任何可见性,因此默认情况下它们将是 public。
考虑到这一点,您应该能够像下面这样进行测试:
public function testConstructorSetsParams()
{
$param1 = 'testVal1';
$param2 = 'testVal2';
$object = new example($param1, $param2);
$this->assertEquals($param1, $object->param1);
$this->assertEquals($param2, $object->param2);
}
有一个断言 assertAttributeSame()
允许您查看 public、class 的受保护和私有属性。
看例子:
class ColorTest extends PHPUnit_Framework_TestCase {
public function test_assertAttributeSame() {
$hasColor = new Color();
$this->assertAttributeSame("red","publicColor",$hasColor);
$this->assertAttributeSame("green","protectedColor",$hasColor);
$this->assertAttributeSame("blue","privateColor",$hasColor);
$this->assertAttributeNotSame("wrong","privateColor",$hasColor);
}
}