Completed
Push — develop ( c90588...babbbc )
by Freddie
05:37
created

testItSchemaFromArrayNameInvalidThrowException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 10
rs 10
c 2
b 0
f 0
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of FlexPHP.
4
 *
5
 * (c) Freddie Gar <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace FlexPHP\Schema\Tests;
11
12
use FlexPHP\Schema\Constants\Keyword;
13
use FlexPHP\Schema\Schema;
14
use FlexPHP\Schema\SchemaAttributeInterface;
15
use Symfony\Component\Yaml\Yaml;
16
17
class SchemaTest extends TestCase
18
{
19
    /**
20
     * @dataProvider getNameInvalid
21
     *
22
     * @param mixed $name
23
     */
24
    public function testItSchemaNameInvalidThrowException($name): void
25
    {
26
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
27
        $this->expectExceptionMessage('name is');
28
29
        new Schema($name, 'title', []);
30
    }
31
32
    /**
33
     * @dataProvider getTitleInvalid
34
     *
35
     * @param mixed $title
36
     */
37
    public function testItSchemaTitleInvalidThrowException($title): void
38
    {
39
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
40
        $this->expectExceptionMessage(':title');
41
42
        new Schema('name', $title, []);
43
    }
44
45
    /**
46
     * @dataProvider getAttributesInvalid
47
     *
48
     * @param mixed $attributes
49
     */
50
    public function testItSchemaAttributesInvalidThrowException($attributes): void
51
    {
52
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
53
        $this->expectExceptionMessage(':attributes are');
54
55
        new Schema('name', 'title', $attributes);
56
    }
57
58
    public function testItSchemaSetOk(): void
59
    {
60
        $name = 'name';
61
        $title = 'title';
62
        $attributes = [
63
            [
64
                Keyword::NAME => 'foo',
65
                Keyword::DATATYPE => 'string',
66
                Keyword::CONSTRAINTS => 'required:true',
67
            ],
68
            [
69
                Keyword::NAME => 'bar',
70
                Keyword::DATATYPE => 'integer',
71
                Keyword::CONSTRAINTS => 'required:false|min:8|max:10',
72
            ],
73
        ];
74
75
        $schema = new Schema($name, $title, $attributes);
76
77
        $this->assertEquals($name, $schema->name());
78
        $this->assertEquals($title, $schema->title());
79
        $this->assertIsArray($schema->attributes());
80
        $this->assertSame(2, count($schema->attributes()));
81
82
        foreach ($schema->attributes() as $attribute) {
83
            $this->assertInstanceOf(SchemaAttributeInterface::class, $attribute);
84
        }
85
86
        $this->assertSame(false, $attribute->isRequired());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $attribute seems to be defined by a foreach iteration on line 82. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
87
        $this->assertSame(8, $attribute->min());
88
        $this->assertSame(10, $attribute->max());
89
    }
90
91
    /**
92
     * @dataProvider getNameInvalid
93
     *
94
     * @param mixed $name
95
     */
96
    public function testItSchemaFromArrayNameInvalidThrowException($name): void
97
    {
98
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
99
        $this->expectExceptionMessage('name is');
100
101
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
102
        $array[$name] = $array['table'];
103
        unset($array['table']);
104
105
        Schema::fromArray($array);
106
    }
107
108
    public function testItSchemaFromArrayWithoutTitleThrowException(): void
109
    {
110
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
111
        $this->expectExceptionMessage(':title');
112
113
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
114
        unset($array['table'][Keyword::TITLE]);
115
116
        Schema::fromArray($array);
117
    }
118
119
    /**
120
     * @dataProvider getTitleInvalid
121
     *
122
     * @param mixed $title
123
     */
124
    public function testItSchemaFromArrayTitleInvalidThrowException($title): void
125
    {
126
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
127
        $this->expectExceptionMessage(':title');
128
129
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
130
        $array['table'][Keyword::TITLE] = $title;
131
132
        Schema::fromArray($array);
133
    }
134
135
    public function testItSchemaFromArrayWithoutTableAttributesThrowException(): void
136
    {
137
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
138
        $this->expectExceptionMessage(':attributes are');
139
140
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
141
        unset($array['table'][Keyword::ATTRIBUTES]);
142
143
        Schema::fromArray($array);
144
    }
145
146
    /**
147
     * @dataProvider getAttributesInvalid
148
     *
149
     * @param mixed $attributes
150
     */
151
    public function testItSchemaFromArrayAttributesInvalidThrowException($attributes): void
152
    {
153
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
154
        $this->expectExceptionMessage(':attributes are');
155
156
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
157
        $array['table'][Keyword::ATTRIBUTES] = $attributes;
158
159
        Schema::fromArray($array);
160
    }
161
162
    public function testItSchemaFromArrayWithTableAttributesInvalidThrowException(): void
163
    {
164
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class);
165
166
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
167
        unset($array['table'][Keyword::ATTRIBUTES]['column3'][Keyword::DATATYPE]);
168
169
        Schema::fromArray($array);
170
    }
171
172
    public function testItSchemaFromArrayOk(): void
173
    {
174
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
175
176
        $schema = Schema::fromArray($array);
177
178
        $this->assertEquals('table', $schema->name());
179
        $this->assertEquals('Table Name', $schema->title());
180
        $this->assertIsArray($schema->attributes());
181
182
        foreach ($schema->attributes() as $attribute) {
183
            $this->assertInstanceOf(SchemaAttributeInterface::class, $attribute);
184
        }
185
    }
186
187
    public function testItSchemaFromFileNotExistsThrowException(): void
188
    {
189
        $this->expectException(\FlexPHP\Schema\Exception\InvalidFileSchemaException::class);
190
191
        Schema::fromFile('/path/error');
192
    }
193
194
    public function testItSchemaFromFileFormatErrorThrowException(): void
195
    {
196
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class);
197
198
        Schema::fromFile(\sprintf('%s/../Mocks/yaml/error.yaml', __DIR__));
199
    }
200
201
    public function testItSchemaFromFileOk(): void
202
    {
203
        $schema = Schema::fromFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
204
205
        $this->assertEquals('table', $schema->name());
206
        $this->assertEquals('Table Name', $schema->title());
207
        $this->assertIsArray($schema->attributes());
208
209
        foreach ($schema->attributes() as $attribute) {
210
            $this->assertInstanceOf(SchemaAttributeInterface::class, $attribute);
211
        }
212
    }
213
214
    public function getNameInvalid(): array
215
    {
216
        return [
217
            [''],
218
            [' '],
219
        ];
220
    }
221
222
    public function getTitleInvalid(): array
223
    {
224
        return [
225
            [''],
226
            [' '],
227
        ];
228
    }
229
230
    public function getAttributesInvalid(): array
231
    {
232
        return [
233
            [[]],
234
        ];
235
    }
236
}
237