set accessible 在使用它时不会使 private 属性 可访问它嘲弄

set accessible does not make the private propery accessible when used it mockery

我想模拟一个 class 来测试它。它有一个由网络请求设置的 private property detail 。我试图通过模拟 json file 来设置 property,然后再测试其他 methods。一切正常,除了,我似乎无法设置 属性 当它是私有 属性 但当它是受保护的 属性.

时工作
$mockedClass = \Mockery::mock( Myclass::class )->makePartial();
$reflection = new \ReflectionClass($mockedClass);
$property = $reflection->getProperty( 'detail' );
$property->setAccessible(true);
$property->setValue($mockedClass, $jsonData ); 

所以当 detailprivate property 时,Mockery 抛出 Property detail does not exist 但是当我使 detail 受保护时,它起作用了。

我不想让 detail 成为 protected property 因为它不需要,但我需要这样做来测试它。

我在某处阅读 "Like you mother said, do not expose your privates"。我不想暴露我的隐私。

尝试进行以下细微更改:

$mockedClass = \Mockery::mock( Myclass::class )->makePartial();
$reflection = new \ReflectionClass(Myclass::class); // Pass the class name, not the actual mock object
$property = $reflection->getProperty( 'detail' );
$property->setAccessible(true);
$property->setValue($mockedClass, $jsonData );