Passed
Push — develop ( 88717d...ee51e8 )
by Freddie
03:18
created

testItSchemaFromArrayAttributesInvalidThrowException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 11
rs 10
c 1
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 Symfony\Component\Yaml\Yaml;
15
16
class SchemaTest extends TestCase
17
{
18
    public function testItSchemaFromArrayEmptyThrowException(): void
19
    {
20
        $this->expectException(\ArgumentCountError::class);
21
22
        $schema = new Schema();
23
        $schema->fromArray();
0 ignored issues
show
Bug introduced by
The call to FlexPHP\Schema\Schema::fromArray() has too few arguments starting with schema. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
        $schema->/** @scrutinizer ignore-call */ 
24
                 fromArray();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
24
    }
25
26
    public function testItSchemaFromArrayInvalidArgumentThrowException(): void
27
    {
28
        $this->expectException(\TypeError::class);
29
30
        $schema = new Schema();
31
        $schema->fromArray(null);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type array expected by parameter $schema of FlexPHP\Schema\Schema::fromArray(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
        $schema->fromArray(/** @scrutinizer ignore-type */ null);
Loading history...
32
    }
33
34
    public function testItSchemaFromArrayEmptyNotThrowException(): void
35
    {
36
        $schema = new Schema();
37
        $schema->fromArray([]);
38
39
        $this->assertTrue(true);
40
    }
41
42
    public function testItSchemaFromArrayEmptyValidateThrowException(): void
43
    {
44
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
45
46
        $schema = new Schema();
47
        $schema->fromArray([]);
48
        $schema->load();
49
    }
50
51
    /**
52
     * @dataProvider getNameInvalid
53
     */
54
    public function testItSchemaFromArrayNameInvalidThrowException($name): void
55
    {
56
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
57
        $this->expectExceptionMessage('name is');
58
59
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
60
        $array[$name] = $array['table'];
61
        unset($array['table']);
62
63
        $schema = new Schema();
64
        $schema->fromArray($array);
65
        $schema->load();
66
    }
67
68
    public function testItSchemaFromArrayWithoutTitleThrowException(): void
69
    {
70
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
71
        $this->expectExceptionMessage(':title');
72
73
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
74
        unset($array['table'][Keyword::TITLE]);
75
76
        $schema = new Schema();
77
        $schema->fromArray($array);
78
        $schema->load();
79
    }
80
81
    /**
82
     * @dataProvider getTitleInvalid
83
     */
84
    public function testItSchemaFromArrayTitleInvalidThrowException($title): void
85
    {
86
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
87
        $this->expectExceptionMessage(':title');
88
89
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
90
        $array['table'][Keyword::TITLE] = $title;
91
92
        $schema = new Schema();
93
        $schema->fromArray($array);
94
        $schema->load();
95
    }
96
97
    public function testItSchemaFromArrayWithoutTableAttributesThrowException(): void
98
    {
99
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
100
        $this->expectExceptionMessage(':attributes are');
101
102
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
103
        unset($array['table'][Keyword::ATTRIBUTES]);
104
105
        $schema = new Schema();
106
        $schema->fromArray($array);
107
        $schema->load();
108
    }
109
110
    /**
111
     * @dataProvider getAttributesInvalid
112
     */
113
    public function testItSchemaFromArrayAttributesInvalidThrowException($attributes): void
114
    {
115
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
116
        $this->expectExceptionMessage(':attributes are');
117
118
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
119
        $array['table'][Keyword::ATTRIBUTES] = $attributes;
120
121
        $schema = new Schema();
122
        $schema->fromArray($array);
123
        $schema->load();
124
    }
125
126
    public function testItSchemaFromArrayWithTableAttributesInvalidThrowException(): void
127
    {
128
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class);
129
130
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
131
        unset($array['table'][Keyword::ATTRIBUTES]['column3'][Keyword::DATATYPE]);
132
133
        $schema = new Schema();
134
        $schema->fromArray($array);
135
        $schema->load();
136
    }
137
138
    public function testItSchemaFromArrayOk(): void
139
    {
140
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
141
142
        $schema = new Schema();
143
        $schema->fromArray($array);
144
        $schema->load();
145
146
        $this->assertEquals('table', $schema->name());
147
        $this->assertEquals('Table Name', $schema->title());
148
        $this->assertIsArray($schema->attributes());
149
    }
150
151
    public function testItSchemaFromFileEmptyThrowException(): void
152
    {
153
        $this->expectException(\ArgumentCountError::class);
154
155
        $schema = new Schema();
156
        $schema->fromFile();
0 ignored issues
show
Bug introduced by
The call to FlexPHP\Schema\Schema::fromFile() has too few arguments starting with filename. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

156
        $schema->/** @scrutinizer ignore-call */ 
157
                 fromFile();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
157
    }
158
159
    public function testItSchemaFromFileInvalidArgumentThrowException(): void
160
    {
161
        $this->expectException(\TypeError::class);
162
163
        $schema = new Schema();
164
        $schema->fromFile(null);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type string expected by parameter $filename of FlexPHP\Schema\Schema::fromFile(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

164
        $schema->fromFile(/** @scrutinizer ignore-type */ null);
Loading history...
165
    }
166
167
    public function testItSchemaFromFileNotExistsThrowException(): void
168
    {
169
        $this->expectException(\FlexPHP\Schema\Exception\InvalidFileSchemaException::class);
170
171
        $schema = new Schema();
172
        $schema->fromFile('/path/error');
173
    }
174
175
    public function testItSchemaFromFileFormatErrorThrowException(): void
176
    {
177
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class);
178
179
        $schema = new Schema();
180
        $schema->fromFile(\sprintf('%s/../Mocks/yaml/error.yaml', __DIR__));
181
        $schema->load();
182
    }
183
184
    public function testItSchemaFromFileOk(): void
185
    {
186
        $schema = new Schema();
187
        $schema->fromFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
188
        $schema->load();
189
190
        $this->assertEquals('table', $schema->name());
191
        $this->assertEquals('Table Name', $schema->title());
192
        $this->assertIsArray($schema->attributes());
193
    }
194
195
    public function getNameInvalid(): array
196
    {
197
        return [
198
            [null],
199
            [''],
200
            [' '],
201
        ];
202
    }
203
204
    public function getTitleInvalid(): array
205
    {
206
        return [
207
            [null],
208
            [''],
209
            [' '],
210
        ];
211
    }
212
213
    public function getAttributesInvalid(): array
214
    {
215
        return [
216
            [null],
217
            [[]],
218
        ];
219
    }
220
}
221