Completed
Push — develop ( 71868c...88717d )
by Freddie
03:24
created

SchemaTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 71
dl 0
loc 152
rs 10
c 0
b 0
f 0
wmc 15

15 Methods

Rating   Name   Duplication   Size   Complexity  
A testItSchemaFromArrayInvalidArgumentThrowException() 0 6 1
A testItSchemaFromArrayWithoutTableAttributesThrowException() 0 11 1
A testItSchemaFromFileEmptyThrowException() 0 6 1
A getTitleInvalid() 0 6 1
A testItSchemaFromFileFormatErrorThrowException() 0 7 1
A testItSchemaFromArrayWithTableAttributesInvalidThrowException() 0 10 1
A testItSchemaFromArrayTitleInvalidThrowException() 0 11 1
A testItSchemaFromArrayEmptyThrowException() 0 6 1
A testItSchemaFromFileOk() 0 9 1
A testItSchemaFromFileInvalidArgumentThrowException() 0 6 1
A testItSchemaFromArrayEmptyValidateThrowException() 0 7 1
A testItSchemaFromArrayEmptyNotThrowException() 0 6 1
A testItSchemaFromFileNotExistsThrowException() 0 6 1
A testItSchemaFromArrayOk() 0 11 1
A testItSchemaFromArrayWithoutTitleThrowException() 0 11 1
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
    public function testItSchemaFromArrayWithoutTitleThrowException(): void
52
    {
53
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
54
        $this->expectExceptionMessage(':title');
55
56
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
57
        unset($array['table'][Keyword::TITLE]);
58
59
        $schema = new Schema();
60
        $schema->fromArray($array);
61
        $schema->load();
62
    }
63
64
    /**
65
     * @dataProvider getTitleInvalid
66
     */
67
    public function testItSchemaFromArrayTitleInvalidThrowException($title): void
68
    {
69
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
70
        $this->expectExceptionMessage(':title');
71
72
        $array                          = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
73
        $array['table'][Keyword::TITLE] = $title;
74
75
        $schema = new Schema();
76
        $schema->fromArray($array);
77
        $schema->load();
78
    }
79
80
    public function testItSchemaFromArrayWithoutTableAttributesThrowException(): void
81
    {
82
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
83
        $this->expectExceptionMessage(':attributes must');
84
85
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
86
        unset($array['table'][Keyword::ATTRIBUTES]);
87
88
        $schema = new Schema();
89
        $schema->fromArray($array);
90
        $schema->load();
91
    }
92
93
    public function testItSchemaFromArrayWithTableAttributesInvalidThrowException(): void
94
    {
95
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class);
96
97
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
98
        unset($array['table'][Keyword::ATTRIBUTES]['column3'][Keyword::DATATYPE]);
99
100
        $schema = new Schema();
101
        $schema->fromArray($array);
102
        $schema->load();
103
    }
104
105
    public function testItSchemaFromArrayOk(): void
106
    {
107
        $array = (new Yaml())->parseFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
108
109
        $schema = new Schema();
110
        $schema->fromArray($array);
111
        $schema->load();
112
113
        $this->assertEquals('table', $schema->name());
114
        $this->assertEquals('Table Name', $schema->title());
115
        $this->assertIsArray($schema->attributes());
116
    }
117
118
    public function testItSchemaFromFileEmptyThrowException(): void
119
    {
120
        $this->expectException(\ArgumentCountError::class);
121
122
        $schema = new Schema();
123
        $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

123
        $schema->/** @scrutinizer ignore-call */ 
124
                 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...
124
    }
125
126
    public function testItSchemaFromFileInvalidArgumentThrowException(): void
127
    {
128
        $this->expectException(\TypeError::class);
129
130
        $schema = new Schema();
131
        $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

131
        $schema->fromFile(/** @scrutinizer ignore-type */ null);
Loading history...
132
    }
133
134
    public function testItSchemaFromFileNotExistsThrowException(): void
135
    {
136
        $this->expectException(\FlexPHP\Schema\Exception\InvalidFileSchemaException::class);
137
138
        $schema = new Schema();
139
        $schema->fromFile('/path/error');
140
    }
141
142
    public function testItSchemaFromFileFormatErrorThrowException(): void
143
    {
144
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaAttributeException::class);
145
146
        $schema = new Schema();
147
        $schema->fromFile(\sprintf('%s/../Mocks/yaml/error.yaml', __DIR__));
148
        $schema->load();
149
    }
150
151
    public function testItSchemaFromFileOk(): void
152
    {
153
        $schema = new Schema();
154
        $schema->fromFile(\sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
155
        $schema->load();
156
157
        $this->assertEquals('table', $schema->name());
158
        $this->assertEquals('Table Name', $schema->title());
159
        $this->assertIsArray($schema->attributes());
160
    }
161
162
    public function getTitleInvalid(): array
163
    {
164
        return [
165
            [null],
166
            [''],
167
            [' '],
168
        ];
169
    }
170
}
171