Как добавить свойство pivate в mocked-класс с помощью Phpunit - PullRequest
0 голосов
/ 23 марта 2020

Я пытаюсь протестировать функцию, которая проходит через класс, берет свойства publi c и создает с ним объект. Не публикуемые c свойства игнорируются в выводе. Итак, я высмеиваю класс, который будет обрабатываться, и добавляю в него некоторые свойства. Это мой код:

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"
        ];
        $stub->four = (array) [
        // I want this to be protected or private to be not present in the output.
            "fourOne" => "4.1",
            "fourTwo" => "4.2"
        ];
        return $stub;
    }

    public function testGetSettings() {
        $expected = $this->getExpected();
        $getSettings = new GetSettings($this->getSettingsMock());
        $value = $getSettings->getSettings();
        $this->assertEquals($expected, $value);
    }
}

Функция хорошо работает с var_dump, она игнорирует неопубликованные c значения, как и ожидалось. Тест работает без неопубликованной c части, но я хочу протестировать ее с неопубликованной c частью. Я не могу понять, как проверить неопубликованную c часть в Phpunit. Возможно, установив защищенное значение в функции getSettingMock, но как я могу это сделать?

1 Ответ

0 голосов
/ 24 марта 2020

Вот решение, основанное на комментарии xmike и с Phpunit: c здесь: 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);
    }

}

...