Completed
Push — master ( 54a6ce...5c7adb )
by Harry
47:33 queued 45:24
created

ArrayValidatorTest::testChildBuilders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of graze/config-validation.
4
 *
5
 * Copyright (c) 2017 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/config-validation/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/config-validation
12
 */
13
14
namespace Graze\ConfigValidation\Test\Unit;
15
16
use Graze\ConfigValidation\ArrayValidator;
17
use Graze\ConfigValidation\Test\TestCase;
18
use Respect\Validation\Validator as v;
19
20
class ArrayValidatorTest extends TestCase
21
{
22
    public function testDefaultValidator()
23
    {
24
        $validator = new ArrayValidator();
25
26
        $this->assertTrue($validator->isAllowUnspecified());
27
        $this->assertEquals('.', $validator->getSeparator());
28
    }
29
30
    public function testSetAllowUnspecified()
31
    {
32
        $validator = new ArrayValidator();
33
34
        $this->assertTrue($validator->isAllowUnspecified());
35
        $this->assertSame($validator, $validator->setAllowUnspecified(false));
36
        $this->assertFalse($validator->isAllowUnspecified());
37
    }
38
39
    public function testSetSeparator()
40
    {
41
        $validator = new ArrayValidator();
42
43
        $this->assertEquals('.', $validator->getSeparator());
44
        $this->assertSame($validator, $validator->setSeparator('->'));
45
        $this->assertEquals('->', $validator->getSeparator());
46
    }
47
48
    /**
49
     * @dataProvider defaultsDataProvider
50
     *
51
     * @param array $input
52
     * @param array $expected
53
     *
54
     * @throws \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException
55
     */
56
    public function testSimpleValidation(array $input, array $expected)
57
    {
58
        $validator = (new ArrayValidator())
59
            ->optional('defaults.path', v::stringType(), '/some/path')
60
            ->optional('defaults.group', v::stringType(), 'group')
61
            ->required('must', v::stringType()->equals('be here'));
62
63
        $this->assertTrue($validator->isValid($input));
64
        $actual = $validator->validate($input);
65
66
        $this->assertEquals($expected, $actual);
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    public function defaultsDataProvider()
73
    {
74
        return [
75
            [
76
                ['must' => 'be here'],
77
                ['must' => 'be here', 'defaults' => ['path' => '/some/path', 'group' => 'group']],
78
            ],
79
            [
80
                ['must' => 'be here', 'defaults' => ['path' => '/a/path']],
81
                ['must' => 'be here', 'defaults' => ['path' => '/a/path', 'group' => 'group']],
82
            ],
83
            [
84
                ['must' => 'be here', 'other' => 'cake'],
85
                ['must' => 'be here', 'defaults' => ['path' => '/some/path', 'group' => 'group'], 'other' => 'cake'],
86
            ],
87
        ];
88
    }
89
90
    /**
91
     * @dataProvider invalidDataProvider
92
     *
93
     * @param array $input
94
     *
95
     * @throws \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException
96
     * @expectedException \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException
97
     */
98
    public function testFailedValidation(array $input)
99
    {
100
        $validator = (new ArrayValidator())
101
            ->optional('defaults.path', v::stringType(), '/some/path')
102
            ->optional('defaults.group', v::stringType(), 'group')
103
            ->required('must', v::stringType()->equals('be here'));
104
105
        $this->assertFalse($validator->isValid($input));
106
        $validator->validate($input);
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    public function invalidDataProvider()
113
    {
114
        return [
115
            [['must' => 'be here', 'defaults' => ['path' => 1]]],
116
            [['must' => 'be here', 'defaults' => ['group' => 2]]],
117
            [['must' => 'be here', 'defaults' => 'monkey']],
118
            [['must' => 'be here', 'defaults' => ['path' => ['idea' => 'poop']]]],
119
            [[]],
120
        ];
121
    }
122
123
    /**
124
     * @dataProvider doNotAllowUnspecifiedData
125
     *
126
     * @param array $input
127
     *
128
     * @throws \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException
129
     * @expectedException \Graze\ConfigValidation\Exceptions\ConfigValidationFailedException
130
     */
131
    public function testDoNotAllowUnspecified(array $input)
132
    {
133
        $validator = (new ArrayValidator(false))
134
            ->required('stuff')
135
            ->optional('cake', v::intType());
136
137
        $this->assertFalse($validator->isAllowUnspecified());
138
139
        $validator->validate($input);
140
    }
141
142
    /**
143
     * @return array
144
     */
145
    public function doNotAllowUnspecifiedData()
146
    {
147
        return [
148
            [['stuff', 'monkey']],
149
            [['stuff', 'cake' => 4, 'poop']],
150
        ];
151
    }
152
153
    public function testDefaultValidations()
154
    {
155
        $validator = (new ArrayValidator())
156
            ->required('stuff')
157
            ->optional('cake');
158
159
        $this->assertTrue($validator->isValid(['stuff' => 'yup']));
160
    }
161
162
    public function testChildRequireDependencies()
163
    {
164
        $validator = (new ArrayValidator())
165
            ->required('default.stuff.option')
166
            ->optional('default.thing');
167
168
        $this->assertFalse($validator->isValid(['default' => 'nope']));
169
        $this->assertFalse($validator->isValid([]));
170
    }
171
172
    public function testChildBuilders()
173
    {
174
        $validator = (new ArrayValidator())
175
            ->required('default.stuff')
176
            ->addChild(
177
                'default.thing',
178
                (new ArrayValidator())
179
                    ->required('cake')
180
                    ->optional('moon')
181
            );
182
183
        $this->assertFalse($validator->isValid(['default' => ['stuff' => 1]]));
184
        $this->assertTrue($validator->isValid(['default' => ['stuff' => 1, 'thing' => ['cake' => 'yup']]]));
185
    }
186
}
187