【问题标题】:how to test private / protected class variable when set to default type设置为默认类型时如何测试私有/受保护的类变量
【发布时间】:2022-01-08 21:39:21
【问题描述】:

这是一个简化的例子,理解问题。

代码有效我想测试phpUnit时遇到问题

IBase.php

namespace interfaces;
interface IBase {}

文件导航.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);
 }

}

命令:

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 工作得更好。

有人知道怎么测试吗?

该类是为了测试它接收到的对象是否有这个接口, mock 替换了一个已经有这个接口的类。

【问题讨论】:

  • 这是一个简化的例子,明白问题所在。类是为了测试对象是否有这个接口,这里没有mock类
  • 试过你的代码 - 工作正常。我认为命名空间存在问题 - 尝试遵循 PSR 标准。
  • @Maksim 代码工作正常,它是第 2 版,我正在为 php 7 重写它,并且想使用变量类型,它工作正常。我想测试phpUnit时遇到问题

标签: php unit-testing phpunit typeerror php-7.2


【解决方案1】:

FileNavigate.php 文件中使用 phpdoc: 重要评论采用以下形式:

/** @var IBase */
private $f3; 

/** @param IBase $f3 */
private $f3;

不是

/* <= bad form
* @var IBase
*/
private $f3;

也许这就是你的 IDE 看不到类型的原因

【讨论】:

    猜你喜欢
    • 2019-04-04
    • 2011-03-04
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 2018-07-16
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多