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

ValidatePublicationTypesCommandTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 248
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 171
dl 0
loc 248
rs 10
c 0
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testWithMultiplePublicationTypes() 0 56 1
A testWithValidSchemaFile() 0 17 1
A testWithNoFields() 0 13 1
A testWithNoPublicationTypes() 0 8 1
A testJsonOutputOption() 0 16 1
A testWithInvalidSchemaFile() 0 50 1
A setUp() 0 5 1
A testMultipleTypesWithJsonOutput() 0 67 1
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
10
/**
11
 * @covers \Hyde\Publications\Commands\ValidatePublicationTypesCommand
12
 */
13
class ValidatePublicationTypesCommandTest extends TestCase
14
{
15
    protected function setUp(): void
16
    {
17
        parent::setUp();
18
19
        $this->throwOnConsoleException();
20
    }
21
22
    public function testWithNoPublicationTypes()
23
    {
24
        $this->throwOnConsoleException(false);
25
26
        $this->artisan('validate:publicationTypes')
27
            ->expectsOutputToContain('Validating publication schemas!')
28
            ->expectsOutput('Error: No publication types to validate!')
29
            ->assertExitCode(1);
30
    }
31
32
    public function testWithValidSchemaFile()
33
    {
34
        $this->throwOnConsoleException(false);
35
36
        $this->directory('test-publication');
37
        $publicationType = new PublicationType('test-publication', fields: [
38
            ['name' => 'myField', 'type' => 'string'],
39
        ]);
40
        $publicationType->save();
41
42
        $this->artisan('validate:publicationTypes')
43
            ->expectsOutputToContain('Validating publication schemas!')
44
            ->expectsOutput('Validating schema file for [test-publication]')
45
            ->expectsOutput('  No top-level schema errors found')
46
            ->expectsOutput('  No field-level schema errors found')
47
            ->expectsOutputToContain('All done')
48
            ->assertExitCode(0);
49
    }
50
51
    public function testWithInvalidSchemaFile()
52
    {
53
        $this->directory('test-publication');
54
        $publicationType = new PublicationType('test-publication');
55
        $publicationType->save();
56
57
        $this->file('test-publication/schema.json', <<<'JSON'
58
            {
59
                "name": 123,
60
                "canonicalField": 123,
61
                "detailTemplate": 123,
62
                "listTemplate": 123,
63
                "sortField": 123,
64
                "sortAscending": 123,
65
                "pageSize": "123",
66
                 "fields": [
67
                    {
68
                        "name": 123,
69
                        "type": 123
70
                    },
71
                    {
72
                        "noName": "myField",
73
                        "noType": "string"
74
                    }
75
                ],
76
                "directory": "foo"
77
            }
78
            JSON
79
        );
80
81
        $this->artisan('validate:publicationTypes')
82
            ->expectsOutputToContain('Validating publication schemas!')
83
            ->expectsOutput('Validating schema file for [test-publication]')
84
            ->expectsOutput('  Found 7 top-level schema errors:')
85
            ->expectsOutput('    x The name must be a string.')
86
            ->expectsOutput('    x The canonical field must be a string.')
87
            ->expectsOutput('    x The detail template must be a string.')
88
            ->expectsOutput('    x The list template must be a string.')
89
            ->expectsOutput('    x The sort field must be a string.')
90
            ->expectsOutput('    x The sort ascending field must be true or false.')
91
            ->expectsOutput('    x The directory field is prohibited.')
92
            ->expectsOutput('  Found errors in 2 field definitions:')
93
            ->expectsOutput('    Field #1:')
94
            ->expectsOutput('      x The type must be a string.')
95
            ->expectsOutput('      x The name must be a string.')
96
            ->expectsOutput('    Field #2:')
97
            ->expectsOutput('      x The type field is required.')
98
            ->expectsOutput('      x The name field is required.')
99
            ->expectsOutputToContain('All done')
100
            ->assertExitCode(1);
101
    }
102
103
    public function testWithMultiplePublicationTypes()
104
    {
105
        $this->directory('test-publication-1');
106
        $publicationType = new PublicationType('test-publication-1', fields: [
107
            ['name' => 'myField', 'type' => 'string'],
108
        ]);
109
        $publicationType->save();
110
111
        $this->directory('test-publication-2');
112
        $this->file('test-publication-2/schema.json', <<<'JSON'
113
            {
114
                "name": 123,
115
                "canonicalField": 123,
116
                "detailTemplate": 123,
117
                "listTemplate": 123,
118
                "sortField": 123,
119
                "sortAscending": 123,
120
                "pageSize": "123",
121
                 "fields": [
122
                    {
123
                        "name": 123,
124
                        "type": 123
125
                    },
126
                    {
127
                        "noName": "myField",
128
                        "noType": "string"
129
                    }
130
                ],
131
                "directory": "foo"
132
            }
133
            JSON
134
        );
135
136
        $this->artisan('validate:publicationTypes')
137
            ->expectsOutputToContain('Validating publication schemas!')
138
            ->expectsOutput('Validating schema file for [test-publication-1]')
139
            ->expectsOutput('  No top-level schema errors found')
140
            ->expectsOutput('  No field-level schema errors found')
141
            ->expectsOutput('Validating schema file for [test-publication-2]')
142
            ->expectsOutput('  Found 7 top-level schema errors:')
143
            ->expectsOutput('    x The name must be a string.')
144
            ->expectsOutput('    x The canonical field must be a string.')
145
            ->expectsOutput('    x The detail template must be a string.')
146
            ->expectsOutput('    x The list template must be a string.')
147
            ->expectsOutput('    x The sort field must be a string.')
148
            ->expectsOutput('    x The sort ascending field must be true or false.')
149
            ->expectsOutput('    x The directory field is prohibited.')
150
            ->expectsOutput('  Found errors in 2 field definitions:')
151
            ->expectsOutput('    Field #1:')
152
            ->expectsOutput('      x The type must be a string.')
153
            ->expectsOutput('      x The name must be a string.')
154
            ->expectsOutput('    Field #2:')
155
            ->expectsOutput('      x The type field is required.')
156
            ->expectsOutput('      x The name field is required.')
157
            ->expectsOutputToContain('All done')
158
            ->assertExitCode(1);
159
    }
160
161
    public function testWithNoFields()
162
    {
163
        $this->directory('test-publication');
164
        $publicationType = new PublicationType('test-publication');
165
        $publicationType->save();
166
167
        $this->artisan('validate:publicationTypes')
168
            ->expectsOutputToContain('Validating publication schemas!')
169
            ->expectsOutput('Validating schema file for [test-publication]')
170
            ->expectsOutput('  No top-level schema errors found')
171
            ->expectsOutput('  No field-level schema errors found')
172
            ->expectsOutputToContain('All done')
173
            ->assertExitCode(0);
174
    }
175
176
    public function testJsonOutputOption()
177
    {
178
        $this->directory('test-publication');
179
        $publicationType = new PublicationType('test-publication');
180
        $publicationType->save();
181
182
        $this->artisan('validate:publicationTypes --json')
183
            ->expectsOutput(<<<'JSON'
184
                {
185
                    "test-publication": {
186
                        "schema": [],
187
                        "fields": []
188
                    }
189
                }
190
                JSON)
191
            ->assertExitCode(0);
192
    }
193
194
    public function testMultipleTypesWithJsonOutput()
195
    {
196
        $this->directory('test-publication-1');
197
        $publicationType = new PublicationType('test-publication-1', fields: [
198
            ['name' => 'myField', 'type' => 'string'],
199
        ]);
200
        $publicationType->save();
201
202
        $this->directory('test-publication-2');
203
        $this->file('test-publication-2/schema.json', <<<'JSON'
204
            {
205
                "name": 123,
206
                "canonicalField": 123,
207
                "detailTemplate": 123,
208
                "listTemplate": 123,
209
                "sortField": 123,
210
                "sortAscending": 123,
211
                "pageSize": "123",
212
                 "fields": [
213
                    {
214
                        "name": 123,
215
                        "type": 123
216
                    },
217
                    {
218
                        "noName": "myField",
219
                        "noType": "string"
220
                    }
221
                ],
222
                "directory": "foo"
223
            }
224
            JSON
225
        );
226
227
        $this->artisan('validate:publicationTypes --json')
228
            ->expectsOutput(
229
                <<<'JSON'
230
                {
231
                    "test-publication-1": {
232
                        "schema": [],
233
                        "fields": [
234
                            []
235
                        ]
236
                    },
237
                    "test-publication-2": {
238
                        "schema": [
239
                            "The name must be a string.",
240
                            "The canonical field must be a string.",
241
                            "The detail template must be a string.",
242
                            "The list template must be a string.",
243
                            "The sort field must be a string.",
244
                            "The sort ascending field must be true or false.",
245
                            "The directory field is prohibited."
246
                        ],
247
                        "fields": [
248
                            [
249
                                "The type must be a string.",
250
                                "The name must be a string."
251
                            ],
252
                            [
253
                                "The type field is required.",
254
                                "The name field is required."
255
                            ]
256
                        ]
257
                    }
258
                }
259
                JSON
260
            )->assertExitCode(1);
261
    }
262
}
263