schemator-php
195 строк · 4.8 Кб
1<?php
2
3declare(strict_types=1);
4
5namespace Smoren\Schemator\Tests\Unit\NestedAccessor;
6
7use Smoren\Schemator\Components\NestedAccessor;
8use Smoren\Schemator\Exceptions\PathNotExistException;
9use Smoren\Schemator\Exceptions\PathNotWritableException;
10use Smoren\Schemator\Tests\Unit\Fixtures\ClassWithAccessibleProperties;
11
12class NestedAccessorUpdateTest extends \Codeception\Test\Unit
13{
14/**
15* @dataProvider dataProviderForArray
16* @dataProvider dataProviderForArrayAccess
17* @dataProvider dataProviderForStdClass
18*/
19public 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
32public function dataProviderForArray(): array
33{
34return [
35[
36['a' => 1],
37'a',
382,
39['a' => 2],
40],
41[
42['a' => 1],
43['a'],
442,
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
56public function dataProviderForArrayAccess(): array
57{
58return [
59[
60new \ArrayObject(['a' => 1]),
61'a',
622,
63new \ArrayObject(['a' => 2]),
64],
65[
66new \ArrayObject(['a' => 1]),
67['a'],
682,
69new \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
80public function dataProviderForStdClass(): array
81{
82return [
83[
84(object)['a' => 1],
85'a',
862,
87(object)['a' => 2],
88],
89[
90(object)['a' => 1],
91['a'],
922,
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*/
107public 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
119public function dataProviderForObject(): array
120{
121return [
122[
123new ClassWithAccessibleProperties(),
124'protectedPropertyWithMethodsAccess',
12522,
126],
127[
128['a' => new ClassWithAccessibleProperties()],
129'a.protectedPropertyWithMethodsAccess',
13023,
131],
132[
133['a' => new ClassWithAccessibleProperties()],
134'a.privatePropertyWithMethodsAccess',
13524,
136],
137];
138}
139
140/**
141* @dataProvider dataProviderForPathNotExistError
142*/
143public 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
155public function dataProviderForPathNotExistError(): array
156{
157return [
158[
159['a' => 1],
160'b',
1612,
162],
163[
164['a' => 1],
165['b'],
1662,
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[
179new ClassWithAccessibleProperties(),
180'protectedProperty',
1812,
182],
183[
184['a' => new ClassWithAccessibleProperties()],
185'a.protectedProperty',
1862,
187],
188[
189['a' => new ClassWithAccessibleProperties()],
190'a.privateProperty',
1912,
192],
193];
194}
195}
196