schemator-php

Форк
0
/
NestedAccessorAppendTest.php 
136 строк · 3.1 Кб
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\PathNotArrayAccessibleException;
9
use Smoren\Schemator\Exceptions\PathNotExistException;
10

11
class NestedAccessorAppendTest extends \Codeception\Test\Unit
12
{
13
    /**
14
     * @dataProvider dataProviderForSuccess
15
     */
16
    public 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

28
    public function dataProviderForSuccess(): array
29
    {
30
        return [
31
            [
32
                [],
33
                null,
34
                1,
35
                [1],
36
            ],
37
            [
38
                ['a' => []],
39
                'a',
40
                1,
41
                ['a' => [1]],
42
            ],
43
            [
44
                ['a' => [1]],
45
                'a',
46
                2,
47
                ['a' => [1, 2]],
48
            ],
49
            [
50
                ['a' => ['b' => ['c' => [1, 2, 3, 4]]]],
51
                'a.b.c',
52
                5,
53
                ['a' => ['b' => ['c' => [1, 2, 3, 4, 5]]]],
54
            ],
55
            [
56
                ['a' => ['b' => ['c' => [1, 2, 3, 4]]]],
57
                'a',
58
                5,
59
                ['a' => ['b' => ['c' => [1, 2, 3, 4]], 5]],
60
            ],
61
            [
62
                ['a' => ['b' => ['c' => [1, 2, 3, 4]]]],
63
                'a.b',
64
                5,
65
                ['a' => ['b' => ['c' => [1, 2, 3, 4], 5]]],
66
            ],
67
        ];
68
    }
69

70
    /**
71
     * @dataProvider dataProviderForNotExistError
72
     */
73
    public 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

85
    public function dataProviderForNotExistError(): array
86
    {
87
        return [
88
            [
89
                [],
90
                'a',
91
                1,
92
            ],
93
            [
94
                ['a' => [1]],
95
                'b',
96
                2,
97
            ],
98
            [
99
                ['a' => ['b' => ['c' => [1, 2, 3, 4]]]],
100
                'a.b.d',
101
                5,
102
            ],
103
        ];
104
    }
105

106
    /**
107
     * @dataProvider dataProviderForNotArrayAccessibleError
108
     */
109
    public 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

121
    public function dataProviderForNotArrayAccessibleError(): array
122
    {
123
        return [
124
            [
125
                ['a' => (object)[1]],
126
                'a',
127
                2,
128
            ],
129
            [
130
                ['a' => ['b' => ['c' => (object)[1, 2, 3, 4]]]],
131
                'a.b.c',
132
                5,
133
            ],
134
        ];
135
    }
136
}
137

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

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

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

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