[英]how to test private / protected class variable when set to default type
这是一个简化的例子,理解问题。
代码有效我想测试 phpUnit 时遇到问题
IBase.php
namespace interfaces;
interface IBase {}
FileNavigate.php
class FileNavigate {
private IBase $f3; // <= line: 5 #RED
public function __construct(IBase $f3, $file = '') { // <= #GREEN use IBase
$this->f3 = $f3;
}
}
FileNavigateTest.php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
class FileNavigateTest extends TestCase {
public function testInterface() {
$mock = $this->createMock(\interfaces\IBase::class);
$f3_get_FileNavigate = new FileNavigate($mock); // <= line: 10
$this->assertTrue(true);
}
}
cmd:
PHPUnit 8.5.21 by Sebastian Bergmann and contributors.
..E... 1 / 1 (100%)
Time: 8 ms, Memory: 4.00 MB
There was 1 error:
1) FileNavigateTest::testInterface
ParseError: syntax error, unexpected 'IBase' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)
/html/app/v2/FileNavigate.php:5
/html/tests/FileNavigateTest.php:10
ERRORS!
Tests: 1, Assertions: 1, Errors: 1.
Script ./vendor/bin/phpunit tests handling the test event returned with error code 2
Script @test was called via t
我知道错误出现在第 5 行的文件“FileNavigate.php”中
当我使用这个符号时,只有我的 IDE 效果更好。
有谁知道如何测试它?
class是为了测试它收到的object是否有这个接口,mock替换一个已经有这个接口的class。
################################################# ######
为子孙后代
################################################# ######
在FileNavigate.php文件中使用 phpdoc: 重要注释采用以下形式:
/** @var IBase */
private $f3;
或者
/** @param IBase $f3 */
private $f3;
不是
/* <= bad form
* @var IBase
*/
private $f3;
也许这就是你的 IDE 看不到类型的原因
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.