您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
phpunit断言assertClassHasStaticAttribute
发布时间:2021-10-01 13:30:08编辑:雪饮阅读()
Phpunit断言中的assertClassHasStaticAttribute方法主要用于断言一个类中是否拥有某个静态变量,若拥有这个静态变量则断言为真,否则断言为false。
ClassHasStaticAttributeTest.php:
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class ClassHasStaticAttributeTest extends TestCase
{
static $a="b";
public $b="c";
public function testFailure(): void
{
$this->assertClassHasStaticAttribute('foo', stdClass::class);
}
public function testSuccess(): void
{
$this->assertClassHasStaticAttribute('a', ClassHasStaticAttributeTest::class);
}
public function testFailureP(): void
{
$this->assertClassHasStaticAttribute('b', ClassHasStaticAttributeTest::class);
}
}
use PHPUnit\Framework\TestCase;
final class ClassHasStaticAttributeTest extends TestCase
{
static $a="b";
public $b="c";
public function testFailure(): void
{
$this->assertClassHasStaticAttribute('foo', stdClass::class);
}
public function testSuccess(): void
{
$this->assertClassHasStaticAttribute('a', ClassHasStaticAttributeTest::class);
}
public function testFailureP(): void
{
$this->assertClassHasStaticAttribute('b', ClassHasStaticAttributeTest::class);
}
}
运行结果:
C:\Users\Administrator>D:\phpstudy_pro\Extensions\php\php7.3.4nts\php.exe C:\Users\Administrator\PhpstormProjects\untitled\vendor\phpunit\phpunit\phpunit -c C:\Users\Administrator\PhpstormProjects\untitled\organizing\phpunit.xml C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\ClassHasStaticAttributeTest.php
PHPUnit 9.5.8 by Sebastian Bergmann and contributors.
F.F 3 / 3 (100%)
Time: 00:00.008, Memory: 6.00 MB
There were 2 failures:
1) ClassHasStaticAttributeTest::testFailure
Failed asserting that class "stdClass" has static attribute "foo".
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\ClassHasStaticAttributeTest.php:10
2) ClassHasStaticAttributeTest::testFailureP
Failed asserting that class "ClassHasStaticAttributeTest" has static attribute "b".
C:\Users\Administrator\PhpstormProjects\untitled\organizing\tests\ClassHasStaticAttributeTest.php:18
FAILURES!
Tests: 3, Assertions: 3, Failures: 2.
这里有3个断言,第一个stdclass中肯定不存在静态的foo属性
第二个断言当前类自身存在静态属性a,这个很明显是ok的
第三个断言当前类自身存在静态属性b,这个就错了,当前类存在属性b,但是并非静态。
关键字词:phpunit,assertClassHasStaticAttribute