Completed
Push — develop ( 0660be...71868c )
by Freddie
04:14
created

testItSchemaFromArrayInvalidArgumentThrowException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace FlexPHP\Schema\Tests;
4
5
use FlexPHP\Schema\Constants\Keyword;
6
use FlexPHP\Schema\Schema;
7
use Symfony\Component\Yaml\Yaml;
8
9
class SchemaTest extends TestCase
10
{
11
    public function testItSchemaFromArrayEmptyThrowException()
12
    {
13
        $this->expectException(\ArgumentCountError::class);
14
15
        $schema = new Schema();
16
        $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

16
        $schema->/** @scrutinizer ignore-call */ 
17
                 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...
17
    }
18
19
    public function testItSchemaFromArrayInvalidArgumentThrowException()
20
    {
21
        $this->expectException(\TypeError::class);
22
23
        $schema = new Schema();
24
        $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

24
        $schema->fromArray(/** @scrutinizer ignore-type */ null);
Loading history...
25
    }
26
27
    public function testItSchemaFromArrayEmptyNotThrowException()
28
    {
29
        $schema = new Schema();
30
        $schema->fromArray([]);
31
32
        $this->assertTrue(true);
33
    }
34
35
    public function testItSchemaFromArrayEmptyValidateThrowException()
36
    {
37
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
38
39
        $schema = new Schema();
40
        $schema->fromArray([]);
41
        $schema->validate();
42
    }
43
44
    public function testItSchemaFromArrayWithoutTableNameThrowException()
45
    {
46
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
47
        $this->expectExceptionMessage(':title must');
48
49
        $array = (new Yaml())->parseFile(sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
50
        unset($array['table'][Keyword::TITLE]);
51
52
        $schema = new Schema();
53
        $schema->fromArray($array);
54
        $schema->validate();
55
    }
56
57
    public function testItSchemaFromArrayWithoutTableAttributesThrowException()
58
    {
59
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
60
        $this->expectExceptionMessage(':attributes must');
61
62
        $array = (new Yaml())->parseFile(sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
63
        unset($array['table'][Keyword::ATTRIBUTES]);
64
65
        $schema = new Schema();
66
        $schema->fromArray($array);
67
        $schema->validate();
68
    }
69
70
    public function testItSchemaFromArrayWithTableAttributesInvalidThrowException()
71
    {
72
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
73
        $this->expectExceptionMessage(':attributes are invalid');
74
75
        $array = (new Yaml())->parseFile(sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
76
        unset($array['table'][Keyword::ATTRIBUTES]['column3'][Keyword::DATATYPE]);
77
78
        $schema = new Schema();
79
        $schema->fromArray($array);
80
        $schema->validate();
81
    }
82
83
    public function testItSchemaFromArrayOk()
84
    {
85
        $array = (new Yaml())->parseFile(sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
86
87
        $schema = new Schema();
88
        $schema->fromArray($array);
89
        $schema->validate();
90
91
        $this->assertEquals('table', $schema->name());
92
        $this->assertEquals('Table Name', $schema->title());
93
        $this->assertIsArray($schema->attributes());
94
    }
95
96
    public function testItSchemaFromFileEmptyThrowException()
97
    {
98
        $this->expectException(\ArgumentCountError::class);
99
100
        $schema = new Schema();
101
        $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

101
        $schema->/** @scrutinizer ignore-call */ 
102
                 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...
102
    }
103
104
    public function testItSchemaFromFileInvalidArgumentThrowException()
105
    {
106
        $this->expectException(\TypeError::class);
107
108
        $schema = new Schema();
109
        $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

109
        $schema->fromFile(/** @scrutinizer ignore-type */ null);
Loading history...
110
    }
111
112
    public function testItSchemaFromFileNotExistsThrowException()
113
    {
114
        $this->expectException(\FlexPHP\Schema\Exception\InvalidFileSchemaException::class);
115
116
        $schema = new Schema();
117
        $schema->fromFile('/path/error');
118
    }
119
120
    public function testItSchemaFromFileFormatErrorThrowException()
121
    {
122
        $this->expectException(\FlexPHP\Schema\Exception\InvalidSchemaException::class);
123
        $this->expectExceptionMessage(':attributes are invalid');
124
125
        $schema = new Schema();
126
        $schema->fromFile(sprintf('%s/../Mocks/yaml/error.yaml', __DIR__));
127
        $schema->validate();
128
    }
129
130
    public function testItSchemaFromFileOk()
131
    {
132
        $schema = new Schema();
133
        $schema->fromFile(sprintf('%s/../Mocks/yaml/table.yaml', __DIR__));
134
        $schema->validate();
135
136
        $this->assertEquals('table', $schema->name());
137
        $this->assertEquals('Table Name', $schema->title());
138
        $this->assertIsArray($schema->attributes());
139
    }
140
}
141