Passed
Push — master ( 2a3d19...5e9e4e )
by Caen
05:36 queued 13s
created

testValidateSchemaFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Publications\Testing\Feature;
6
7
use Hyde\Publications\Models\PublicationType;
8
use Hyde\Testing\TestCase;
9
use Illuminate\Validation\ValidationException;
10
11
/**
12
 * @covers \Hyde\Publications\Actions\PublicationSchemaValidator
13
 */
14
class PublicationSchemaValidatorTest extends TestCase
15
{
16
    public function testValidateSchemaFile()
17
    {
18
        $this->directory('test-publication');
19
        $publicationType = new PublicationType('test-publication', fields: [
20
            ['name' => 'myField', 'type' => 'string'],
21
        ]);
22
        $publicationType->save();
23
24
        $publicationType->validateSchemaFile();
25
26
        $this->assertTrue(true);
27
    }
28
29
    public function testValidateSchemaFileWithInvalidSchema()
30
    {
31
        $this->directory('test-publication');
32
        $publicationType = new PublicationType('test-publication');
33
        $publicationType->save();
34
35
        $this->file('test-publication/schema.json', <<<'JSON'
36
            {
37
                "name": 123,
38
                "canonicalField": 123,
39
                "detailTemplate": 123,
40
                "listTemplate": 123,
41
                "sortField": 123,
42
                "sortAscending": 123,
43
                "pageSize": "123",
44
                "fields": 123,
45
                "directory": "foo"
46
            }
47
            JSON
48
        );
49
50
        $this->expectException(ValidationException::class);
51
        $publicationType->validateSchemaFile();
52
    }
53
54
    public function testValidateSchemaFileWithInvalidFields()
55
    {
56
        $this->directory('test-publication');
57
        $publicationType = new PublicationType('test-publication');
58
        $publicationType->save();
59
60
        $this->file('test-publication/schema.json', <<<'JSON'
61
            {
62
                "name": "test-publication",
63
                "canonicalField": "__createdAt",
64
                "detailTemplate": "detail.blade.php",
65
                "listTemplate": "list.blade.php",
66
                "sortField": "__createdAt",
67
                "sortAscending": true,
68
                "pageSize": 0,
69
                "fields": [
70
                    {
71
                        "name": 123,
72
                        "type": 123
73
                    },
74
                    {
75
                        "noName": "myField",
76
                        "noType": "string"
77
                    }
78
                ]
79
            }
80
            JSON
81
        );
82
83
        $this->expectException(ValidationException::class);
84
        $publicationType->validateSchemaFile();
85
    }
86
87
    public function testValidateSchemaFileWithInvalidDataBuffered()
88
    {
89
        $this->directory('test-publication');
90
        $publicationType = new PublicationType('test-publication');
91
92
        $this->file('test-publication/schema.json', <<<'JSON'
93
            {
94
                "name": 123,
95
                "canonicalField": 123,
96
                "detailTemplate": 123,
97
                "listTemplate": 123,
98
                "sortField": 123,
99
                "sortAscending": 123,
100
                "pageSize": "123",
101
                 "fields": [
102
                    {
103
                        "name": 123,
104
                        "type": 123
105
                    },
106
                    {
107
                        "noName": "myField",
108
                        "noType": "string"
109
                    }
110
                ],
111
                "directory": "foo"
112
            }
113
            JSON
114
        );
115
116
        $errors = $publicationType->validateSchemaFile(false);
117
118
        $this->assertSame([
119
            'schema' => [
120
                'The name must be a string.',
121
                'The canonical field must be a string.',
122
                'The detail template must be a string.',
123
                'The list template must be a string.',
124
                'The sort field must be a string.',
125
                'The sort ascending field must be true or false.',
126
                'The directory field is prohibited.',
127
            ],
128
            'fields' => [[
129
                'The type must be a string.',
130
                'The name must be a string.',
131
            ], [
132
                'The type field is required.',
133
                'The name field is required.',
134
            ]],
135
        ], $errors);
136
    }
137
}
138