1 回答
TA贡献1821条经验 获得超6个赞
这是一个基于 xmike 的评论和此处的 Phpunit 文档的解决方案:https ://phpunit.readthedocs.io/en/9.0/fixtures.html 。
像这样制作一个夹具类:
class GetSettingsFixture
{
public array $one = [
"oneOne" => "1.1",
"oneTwo" => "1.2",
"oneThree" => [
"oneThreeOne" => "1.3.1",
"oneThreeTwo" => "1.3.2",
]
];
public array $two = [
"twoOne" => "2.1",
"twoTwo" => "2.2",
"twoThree" => [
"twoThreeOne" => "1.3.1",
"twoThreeTwo" => "1.3.2",
]
];
public array $three = [
"threeOne" => "3.1",
"threeTwo" => "3.2"
];
public string $four = "a string";
private array $five = [ // this should be ignored in the output.
"fiveOne" => "5.1",
"fiveTwo" => "5.2"
];
protected array $six = [ // this should be ignored in the output.
"sixOne" => "6.1",
"sixTwo" => "6.2"
];
public function testFunction() { // this should be ignored in the output.
return "something";
}
}
这个测试通过:
class GetSettingsTest extends TestCase
{
private GetSettingsFixture $given;
public function setUp(): void {
// this function is executed before test.
$this->given = new GetSettingsFixture(); // this call the fixture class.
}
public function tearDown(): void {
// this function is executed after the test.
unset($this->given);
}
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" => "a string"
// five, six are not here : it is protected or private.
// testFunction is hot here too, it's not a property.
];
}
public function testGetSettings() {
$expected = $this->getExpected();
$getSettings = new GetSettings($this->given);
$value = $getSettings->getSettings();
$this->assertEquals($expected, $value);
}
}
- 1 回答
- 0 关注
- 68 浏览
添加回答
举报