schemator-php

Форк
0
/
NestedAccessorUpdateTest.php 
195 строк · 4.8 Кб
1
<?php
2

3
declare(strict_types=1);
4

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

7
use Smoren\Schemator\Components\NestedAccessor;
8
use Smoren\Schemator\Exceptions\PathNotExistException;
9
use Smoren\Schemator\Exceptions\PathNotWritableException;
10
use Smoren\Schemator\Tests\Unit\Fixtures\ClassWithAccessibleProperties;
11

12
class NestedAccessorUpdateTest extends \Codeception\Test\Unit
13
{
14
    /**
15
     * @dataProvider dataProviderForArray
16
     * @dataProvider dataProviderForArrayAccess
17
     * @dataProvider dataProviderForStdClass
18
     */
19
    public function testSuccess($source, $path, $value, $expected)
20
    {
21
        // Given
22
        $accessor = new NestedAccessor($source);
23

24
        // When
25
        $accessor->update($path, $value);
26

27
        // Then
28
        $this->assertSame($value, $accessor->get($path));
29
        $this->assertEquals($expected, $source);
30
    }
31

32
    public function dataProviderForArray(): array
33
    {
34
        return [
35
            [
36
                ['a' => 1],
37
                'a',
38
                2,
39
                ['a' => 2],
40
            ],
41
            [
42
                ['a' => 1],
43
                ['a'],
44
                2,
45
                ['a' => 2],
46
            ],
47
            [
48
                ['a' => ['b' => ['c' => [0]]]],
49
                ['a', 'b', 'c'],
50
                'value',
51
                ['a' => ['b' => ['c' => 'value']]],
52
            ],
53
        ];
54
    }
55

56
    public function dataProviderForArrayAccess(): array
57
    {
58
        return [
59
            [
60
                new \ArrayObject(['a' => 1]),
61
                'a',
62
                2,
63
                new \ArrayObject(['a' => 2]),
64
            ],
65
            [
66
                new \ArrayObject(['a' => 1]),
67
                ['a'],
68
                2,
69
                new \ArrayObject(['a' => 2]),
70
            ],
71
            [
72
                ['a' => new \ArrayObject(['b' => ['c' => [0]]])],
73
                ['a', 'b', 'c'],
74
                'value',
75
                ['a' => new \ArrayObject(['b' => ['c' => 'value']])],
76
            ],
77
        ];
78
    }
79

80
    public function dataProviderForStdClass(): array
81
    {
82
        return [
83
            [
84
                (object)['a' => 1],
85
                'a',
86
                2,
87
                (object)['a' => 2],
88
            ],
89
            [
90
                (object)['a' => 1],
91
                ['a'],
92
                2,
93
                (object)['a' => 2],
94
            ],
95
            [
96
                ['a' => (object)['b' => ['c' => [0]]]],
97
                ['a', 'b', 'c'],
98
                'value',
99
                ['a' => (object)['b' => ['c' => 'value']]],
100
            ],
101
        ];
102
    }
103

104
    /**
105
     * @dataProvider dataProviderForObject
106
     */
107
    public function testObject($source, $path, $value)
108
    {
109
        // Given
110
        $accessor = new NestedAccessor($source);
111

112
        // When
113
        $accessor->update($path, $value);
114

115
        // Then
116
        $this->assertEquals($value, $accessor->get($path));
117
    }
118

119
    public function dataProviderForObject(): array
120
    {
121
        return [
122
            [
123
                new ClassWithAccessibleProperties(),
124
                'protectedPropertyWithMethodsAccess',
125
                22,
126
            ],
127
            [
128
                ['a' => new ClassWithAccessibleProperties()],
129
                'a.protectedPropertyWithMethodsAccess',
130
                23,
131
            ],
132
            [
133
                ['a' => new ClassWithAccessibleProperties()],
134
                'a.privatePropertyWithMethodsAccess',
135
                24,
136
            ],
137
        ];
138
    }
139

140
    /**
141
     * @dataProvider dataProviderForPathNotExistError
142
     */
143
    public function testPathNotExistError($source, $path, $value)
144
    {
145
        // Given
146
        $accessor = new NestedAccessor($source);
147

148
        // Then
149
        $this->expectException(PathNotExistException::class);
150

151
        // When
152
        $accessor->update($path, $value);
153
    }
154

155
    public function dataProviderForPathNotExistError(): array
156
    {
157
        return [
158
            [
159
                ['a' => 1],
160
                'b',
161
                2,
162
            ],
163
            [
164
                ['a' => 1],
165
                ['b'],
166
                2,
167
            ],
168
            [
169
                ['a' => ['b' => ['c' => [0]]]],
170
                ['a', 'b', 'd'],
171
                'value',
172
            ],
173
            [
174
                ['a' => ['b' => ['c' => [0]]]],
175
                ['a', 'a', 'c'],
176
                'value',
177
            ],
178
            [
179
                new ClassWithAccessibleProperties(),
180
                'protectedProperty',
181
                2,
182
            ],
183
            [
184
                ['a' => new ClassWithAccessibleProperties()],
185
                'a.protectedProperty',
186
                2,
187
            ],
188
            [
189
                ['a' => new ClassWithAccessibleProperties()],
190
                'a.privateProperty',
191
                2,
192
            ],
193
        ];
194
    }
195
}
196

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

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

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

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