为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 PHPUnit 在模拟类中添加私有属性

如何使用 PHPUnit 在模拟类中添加私有属性

PHP
冉冉说 2022-11-12 13:35:44
我正在尝试测试一个遍历一个类、获取公共属性并用它创建一个对象的函数。非公共属性在输出中被忽略。因此,我模拟将要处理的类并为其添加一些属性。这是我的代码: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 函数中设置一个受保护的值,但我该怎么做呢?
查看完整描述

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);

    }


}


查看完整回答
反对 回复 2022-11-12
  • 1 回答
  • 0 关注
  • 68 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信