我正在尝试测试一个遍历一个类、获取公共属性并用它创建一个对象的函数。非公共属性在输出中被忽略。因此,我模拟将要处理的类并为其添加一些属性。这是我的代码:
class GetSettingsTest extends TestCase
{
public function getExpected() {
return (object) [
"one" => (object) [
"oneOne" => "1.1",
"oneTwo" => "1.2",
"oneThree" => (object) [
"oneThreeOne" => "1.3.1",
"oneThreeTwo" => "1.3.2",
]
],
"two" => (object) [
"twoOne" => "2.1",
"twoTwo" => "2.2",
"twoThree" => (object) [
"twoThreeOne" => "1.3.1",
"twoThreeTwo" => "1.3.2",
]
],
"three" => (object) [
"threeOne" => "3.1",
"threeTwo" => "3.2"
]
// four is not here : it is protected or private.
];
}
public function getSettingsMock() {
$stub = $this->getMockBuilder('FakeSettingsClass')
->disableOriginalConstructor()
->getMock();
$stub->one = (array) [
"oneOne" => "1.1",
"oneTwo" => "1.2",
"oneThree" => (array) [
"oneThreeOne" => "1.3.1",
"oneThreeTwo" => "1.3.2",
]
];
$stub->two = (array) [// provide an array, must return an object
"twoOne" => "2.1",
"twoTwo" => "2.2",
"twoThree" => (object) [// provide an object, must return an object
"twoThreeOne" => "1.3.1",
"twoThreeTwo" => "1.3.2",
]
];
$stub->three = (array) [
"threeOne" => "3.1",
"threeTwo" => "3.2"
];
}
该函数与 var_dump 配合使用效果很好,它会按预期忽略非公开值。该测试在没有非公共部分的情况下有效,但我想用非公共部分对其进行测试。我不知道如何测试 PhHPUnit 中的非公开部分。可能是通过在 getSettingMock 函数中设置一个受保护的值,但我该怎么做呢?
达令说