第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 PHPUnit 在模擬類中添加私有屬性

如何使用 PHPUnit 在模擬類中添加私有屬性

PHP
冉冉說 2022-11-12 13:35:44
我正在嘗試測試一個遍歷一個類、獲取公共屬性并用它創(chuàng)建一個對象的函數(shù)。非公共屬性在輸出中被忽略。因此,我模擬將要處理的類并為其添加一些屬性。這是我的代碼: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"        ];    }該函數(shù)與 var_dump 配合使用效果很好,它會按預(yù)期忽略非公開值。該測試在沒有非公共部分的情況下有效,但我想用非公共部分對其進行測試。我不知道如何測試 PhHPUnit 中的非公開部分。可能是通過在 getSettingMock 函數(shù)中設(shè)置一個受保護的值,但我該怎么做呢?
查看完整描述

1 回答

?
達令說

TA貢獻1821條經(jīng)驗 獲得超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);

    }


}


查看完整回答
反對 回復(fù) 2022-11-12
  • 1 回答
  • 0 關(guān)注
  • 112 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號