schemator-php
136 строк · 3.1 Кб
1<?php
2
3declare(strict_types=1);
4
5namespace Smoren\Schemator\Tests\Unit\NestedAccessor;
6
7use Smoren\Schemator\Components\NestedAccessor;
8use Smoren\Schemator\Exceptions\PathNotArrayAccessibleException;
9use Smoren\Schemator\Exceptions\PathNotExistException;
10
11class NestedAccessorAppendTest extends \Codeception\Test\Unit
12{
13/**
14* @dataProvider dataProviderForSuccess
15*/
16public function testSuccess($source, $path, $value, $expected)
17{
18// Given
19$accessor = new NestedAccessor($source);
20
21// When
22$accessor->append($path, $value);
23
24// Then
25$this->assertEquals($expected, $source);
26}
27
28public function dataProviderForSuccess(): array
29{
30return [
31[
32[],
33null,
341,
35[1],
36],
37[
38['a' => []],
39'a',
401,
41['a' => [1]],
42],
43[
44['a' => [1]],
45'a',
462,
47['a' => [1, 2]],
48],
49[
50['a' => ['b' => ['c' => [1, 2, 3, 4]]]],
51'a.b.c',
525,
53['a' => ['b' => ['c' => [1, 2, 3, 4, 5]]]],
54],
55[
56['a' => ['b' => ['c' => [1, 2, 3, 4]]]],
57'a',
585,
59['a' => ['b' => ['c' => [1, 2, 3, 4]], 5]],
60],
61[
62['a' => ['b' => ['c' => [1, 2, 3, 4]]]],
63'a.b',
645,
65['a' => ['b' => ['c' => [1, 2, 3, 4], 5]]],
66],
67];
68}
69
70/**
71* @dataProvider dataProviderForNotExistError
72*/
73public function testNotExistError($source, $path, $value)
74{
75// Given
76$accessor = new NestedAccessor($source);
77
78// Then
79$this->expectException(PathNotExistException::class);
80
81// When
82$accessor->append($path, $value);
83}
84
85public function dataProviderForNotExistError(): array
86{
87return [
88[
89[],
90'a',
911,
92],
93[
94['a' => [1]],
95'b',
962,
97],
98[
99['a' => ['b' => ['c' => [1, 2, 3, 4]]]],
100'a.b.d',
1015,
102],
103];
104}
105
106/**
107* @dataProvider dataProviderForNotArrayAccessibleError
108*/
109public function testNotArrayAccessibleError($source, $path, $value)
110{
111// Given
112$accessor = new NestedAccessor($source);
113
114// Then
115$this->expectException(PathNotArrayAccessibleException::class);
116
117// When
118$accessor->append($path, $value);
119}
120
121public function dataProviderForNotArrayAccessibleError(): array
122{
123return [
124[
125['a' => (object)[1]],
126'a',
1272,
128],
129[
130['a' => ['b' => ['c' => (object)[1, 2, 3, 4]]]],
131'a.b.c',
1325,
133],
134];
135}
136}
137