schemator-php

Форк
0
/
GetPropertyRefTest.php 
159 строк · 4.7 Кб
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Smoren\Schemator\Tests\Unit\ObjectAccessHelper;
6

7
use Codeception\Test\Unit;
8
use Smoren\Schemator\Helpers\ObjectAccessHelper;
9
use Smoren\Schemator\Tests\Unit\Fixtures\ClassWithAccessibleProperties;
10
use stdClass;
11

12
class GetPropertyRefTest extends Unit
13
{
14
    /**
15
     * @param stdClass $input
16
     * @param string $key
17
     * @param mixed $expected
18
     * @return void
19
     * @dataProvider fromStdClassSuccessDataProvider
20
     */
21
    public function testFromStdClassSuccess(stdClass $input, string $key, $expected): void
22
    {
23
        // When
24
        $result = &ObjectAccessHelper::getPropertyRef($input, $key);
25

26
        // Then
27
        $this->assertSame($expected, $result);
28

29
        // And when
30
        $result = 'new result';
31

32
        // Then
33
        $this->assertEquals('new result', ObjectAccessHelper::getPropertyValue($input, $key));
34
    }
35

36
    public function fromStdClassSuccessDataProvider(): array
37
    {
38
        $wrap = fn (array $input) => (object)$input;
39

40
        return [
41
            [$wrap(['a' => 1, 'b' => 2]), 'a', 1],
42
            [$wrap(['a' => 1, 'b' => 2]), 'b', 2],
43
            [$wrap([]), 'b', null],
44
            [$wrap(['a' => 1]), 'b', null],
45
        ];
46
    }
47

48
    /**
49
     * @param object $input
50
     * @param string $key
51
     * @param mixed $expected
52
     * @return void
53
     * @dataProvider fromObjectSuccessDataProvider
54
     */
55
    public function testFromObjectSuccess(object $input, string $key, $expected): void
56
    {
57
        // When
58
        $result = &ObjectAccessHelper::getPropertyRef($input, $key);
59

60
        // Then
61
        $this->assertSame($expected, $result);
62

63
        // And when
64
        $result = 123;
65

66
        // Then
67
        $this->assertEquals(123, ObjectAccessHelper::getPropertyValue($input, $key));
68
    }
69

70
    public function fromObjectSuccessDataProvider(): array
71
    {
72
        return [
73
            [new ClassWithAccessibleProperties(), 'publicProperty', 1],
74
            [new ClassWithAccessibleProperties(), 'publicProperty', 1],
75
            [new ClassWithAccessibleProperties(), 'publicPropertyWithMethodsAccess', 2],
76
            [new ClassWithAccessibleProperties(), 'publicPropertyWithMethodsAccess', 2],
77
        ];
78
    }
79

80
    /**
81
     * @param object $input
82
     * @param string $key
83
     * @return void
84
     * @dataProvider fromObjectFailDataProvider
85
     */
86
    public function testFromObjectGetFail(object $input, string $key): void
87
    {
88
        // When
89
        $proxy = ObjectAccessHelper::getPropertyRef($input, $key);
90

91
        try {
92
            $proxy->getValue();
93
            $this->fail();
94
        } catch (\BadMethodCallException $e) {
95
            // Then
96
            $this->assertSame("Property '" . get_class($input) . "::{$key}' is not readable", $e->getMessage());
97
        }
98
    }
99

100
    /**
101
     * @param object $input
102
     * @param string $key
103
     * @return void
104
     * @dataProvider fromObjectFailDataProvider
105
     */
106
    public function testFromObjectSetFail(object $input, string $key): void
107
    {
108
        // When
109
        $proxy = ObjectAccessHelper::getPropertyRef($input, $key);
110

111
        try {
112
            $proxy->setValue(150);
113
            $this->fail();
114
        } catch (\BadMethodCallException $e) {
115
            // Then
116
            $this->assertSame("Property '" . get_class($input) . "::{$key}' is not writable", $e->getMessage());
117
        }
118
    }
119

120
    public function fromObjectFailDataProvider(): array
121
    {
122
        return [
123
            [new ClassWithAccessibleProperties(), ''],
124
            [new ClassWithAccessibleProperties(), ''],
125
            [new ClassWithAccessibleProperties(), '0'],
126
            [new ClassWithAccessibleProperties(), '0'],
127
            [new ClassWithAccessibleProperties(), 'unknownProperty'],
128
            [new ClassWithAccessibleProperties(), 'unknownProperty'],
129
            [new ClassWithAccessibleProperties(), 'protectedProperty'],
130
            [new ClassWithAccessibleProperties(), 'protectedProperty'],
131
            [new ClassWithAccessibleProperties(), 'privateProperty'],
132
            [new ClassWithAccessibleProperties(), 'privateProperty'],
133
        ];
134
    }
135

136
    /**
137
     * @param object $input
138
     * @param string $key
139
     * @param $expected
140
     * @return void
141
     * @dataProvider fromObjectProxySuccessDataProvider
142
     */
143
    public function testFromObjectProxySuccess(object $input, string $key, $expected): void
144
    {
145
        // When
146
        $proxy = ObjectAccessHelper::getPropertyRef($input, $key);
147

148
        // Then
149
        $this->assertEquals($expected, $proxy->getValue());
150
    }
151

152
    public function fromObjectProxySuccessDataProvider(): array
153
    {
154
        return [
155
            [new ClassWithAccessibleProperties(), 'protectedPropertyWithMethodsAccess', 4],
156
            [new ClassWithAccessibleProperties(), 'privatePropertyWithMethodsAccess', 6],
157
        ];
158
    }
159
}
160

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.