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(); |
|
|
|
|
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function testItSchemaFromArrayInvalidArgumentThrowException() |
20
|
|
|
{ |
21
|
|
|
$this->expectException(\TypeError::class); |
22
|
|
|
|
23
|
|
|
$schema = new Schema(); |
24
|
|
|
$schema->fromArray(null); |
|
|
|
|
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(); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testItSchemaFromFileInvalidArgumentThrowException() |
105
|
|
|
{ |
106
|
|
|
$this->expectException(\TypeError::class); |
107
|
|
|
|
108
|
|
|
$schema = new Schema(); |
109
|
|
|
$schema->fromFile(null); |
|
|
|
|
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
|
|
|
|
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.