Passed
Push — master ( a80842...bbbee0 )
by Caen
03:37 queued 13s
created

ValidatePublicationsCommandTest::createFullRangeTestFixtures()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 40
nc 1
nop 0
dl 0
loc 47
rs 9.28
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Publications\Testing\Feature;
6
7
use function file_put_contents;
8
9
use Hyde\Hyde;
10
use Hyde\Publications\Models\PublicationType;
11
use Hyde\Testing\TestCase;
12
13
/**
14
 * @covers \Hyde\Publications\Commands\ValidatePublicationsCommand
15
 * @covers \Hyde\Publications\Actions\PublicationPageValidator
16
 */
17
class ValidatePublicationsCommandTest extends TestCase
18
{
19
    protected function setUp(): void
20
    {
21
        parent::setUp();
22
23
        $this->throwOnConsoleException();
24
    }
25
26
    public function testWithNoPublicationTypes()
27
    {
28
        $this->throwOnConsoleException(false);
29
30
        $this->artisan('validate:publications')
31
            ->expectsOutput('Error: No publication types to validate!')
32
            ->assertExitCode(1);
33
    }
34
35
    public function testWithInvalidPublicationType()
36
    {
37
        $this->throwOnConsoleException(false);
38
39
        $this->artisan('validate:publications', ['publicationType' => 'invalid'])
40
            ->expectsOutput('Error: Publication type [invalid] does not exist')
41
            ->assertExitCode(1);
42
    }
43
44
    public function testWithPublicationType()
45
    {
46
        $this->directory('test-publication');
47
        $this->copyTestPublicationFixture();
48
        $this->file('test-publication/test.md', '---
49
__createdAt: 2022-11-27 21:07:37
50
title: My Title
51
---
52
53
## Write something awesome.
54
55
');
56
57
        $this->artisan('validate:publications')
58
            ->expectsOutputToContain('Validated 1 publication types, 1 publications, 1 fields')
59
            ->expectsOutput('Found 0 Warnings')
60
            ->expectsOutput('Found 0 Errors')
61
            ->assertExitCode(0);
62
    }
63
64
    public function testWithPublicationTypeAndVerboseOutput()
65
    {
66
        $this->directory('test-publication');
67
        $this->copyTestPublicationFixture();
68
        $this->file('test-publication/test.md', '---
69
__createdAt: 2022-11-27 21:07:37
70
title: My Title
71
---
72
73
## Write something awesome.
74
75
');
76
77
        $this->artisan('validate:publications', ['--verbose' => true])
78
             ->expectsOutputToContain('Validated 1 publication types, 1 publications, 1 fields')
79
             ->expectsOutput('Found 0 Warnings')
80
             ->expectsOutput('Found 0 Errors')
81
             ->assertExitCode(0);
82
    }
83
84
    public function testWithInvalidPublication()
85
    {
86
        $this->directory('test-publication');
87
        $this->copyTestPublicationFixture();
88
        file_put_contents(Hyde::path('test-publication/test.md'), '---
89
---
90
91
Hello World
92
');
93
94
        $this->artisan('validate:publications')
95
             ->expectsOutputToContain('Validated 1 publication types, 1 publications, 1 fields')
96
             ->expectsOutput('Found 0 Warnings')
97
             ->expectsOutput('Found 1 Errors')
98
             ->assertExitCode(1);
99
    }
100
101
    public function testWithWarnedPublication()
102
    {
103
        $this->directory('test-publication');
104
        $this->copyTestPublicationFixture();
105
        file_put_contents(Hyde::path('test-publication/test.md'), '---
106
title: foo
107
extra: field
108
---
109
110
Hello World
111
');
112
113
        $this->artisan('validate:publications')
114
            ->expectsOutputToContain('Validated 1 publication types, 1 publications, 2 fields')
115
            ->expectsOutput('Found 1 Warnings')
116
            ->expectsOutput('Found 0 Errors')
117
            ->assertExitCode(0);
118
    }
119
120
    public function testWithMultiplePublicationTypes()
121
    {
122
        $this->directory('test-publication');
123
        $this->directory('test-publication-two');
124
        $this->savePublication('Test Publication');
125
        $this->savePublication('Test Publication Two');
126
127
        $this->artisan('validate:publications')
128
            ->assertExitCode(0);
129
    }
130
131
    public function testWithMultipleInvalidFields()
132
    {
133
        $this->directory('test-publication');
134
        $publicationType = new PublicationType('test-publication', fields: [
135
            ['name' => 'myField', 'type' => 'string'],
136
            ['name' => 'myNumber', 'type' => 'integer'],
137
        ]);
138
        $publicationType->save();
139
140
        $this->file('test-publication/my-page.md', <<<'MD'
141
            ---
142
            myField: false
143
            ---
144
            
145
            # My Page
146
            MD
147
        );
148
149
        $this->artisan('validate:publications')
150
            ->expectsOutputToContain('Validated 1 publication types, 1 publications, 2 fields')
151
            ->expectsOutput('Found 0 Warnings')
152
            ->expectsOutput('Found 2 Errors')
153
            ->assertExitCode(1);
154
    }
155
156
    public function testOnlySpecifiedTypeIsValidatedWhenUsingArgument()
157
    {
158
        $this->directory('test-publication');
159
        $this->directory('test-publication-two');
160
        $this->savePublication('Test Publication');
161
        $this->savePublication('Test Publication Two');
162
163
        $this->artisan('validate:publications test-publication-two')
164
            ->assertExitCode(0);
165
    }
166
167
    public function testOutput()
168
    {
169
        $this->createFullRangeTestFixtures();
170
171
        $this->artisan('validate:publications')
172
            ->expectsOutputToContain('Validating publications!')
173
            ->expectsOutput('Validating publication type [test-publication]')
174
            ->expectsOutput('  ! extra-field.md')
175
            ->expectsOutput('    ! The extra field is not defined in the publication type.')
176
            ->expectsOutput('  ⨯ invalid-field-and-extra-field.md')
177
            ->expectsOutput('    ⨯ The title must be a string.')
178
            ->expectsOutput('    ! The extra field is not defined in the publication type.')
179
            ->expectsOutput('  ⨯ invalid-field.md')
180
            ->expectsOutput('    ⨯ The title must be a string.')
181
            ->expectsOutput('  ⨯ missing-field.md')
182
            ->expectsOutput('    ⨯ The title must be a string.')
183
            ->expectsOutput('  ✓ valid.md')
184
            ->expectsOutputToContain('Summary:')
185
            ->expectsOutputToContain('Validated 1 publication types, 5 publications, 7 fields')
186
            ->expectsOutput('Found 2 Warnings')
187
            ->expectsOutput('Found 3 Errors')
188
            ->assertExitCode(1);
189
    }
190
191
    public function testWithVerboseOutput()
192
    {
193
        $this->createFullRangeTestFixtures();
194
195
        $this->artisan('validate:publications --verbose')
196
            ->expectsOutputToContain('Validating publications!')
197
            ->expectsOutput('Validating publication type [test-publication]')
198
            ->expectsOutput('  ! extra-field.md')
199
            ->expectsOutput('    ✓ Field title passed.')
200
            ->expectsOutput('    ! The extra field is not defined in the publication type.')
201
            ->expectsOutput('  ⨯ invalid-field-and-extra-field.md')
202
            ->expectsOutput('    ⨯ The title must be a string.')
203
            ->expectsOutput('    ! The extra field is not defined in the publication type.')
204
            ->expectsOutput('  ⨯ invalid-field.md')
205
            ->expectsOutput('    ⨯ The title must be a string.')
206
            ->expectsOutput('  ⨯ missing-field.md')
207
            ->expectsOutput('    ⨯ The title must be a string.')
208
            ->expectsOutput('  ✓ valid.md')
209
            ->expectsOutput('    ✓ Field title passed.')
210
            ->expectsOutputToContain('Summary:')
211
            ->expectsOutputToContain('Validated 1 publication types, 5 publications, 7 fields')
212
            ->expectsOutput('Found 2 Warnings')
213
            ->expectsOutput('Found 3 Errors')
214
            ->assertExitCode(1);
215
    }
216
217
    public function testWithJsonOutput()
218
    {
219
        $this->createFullRangeTestFixtures();
220
221
        $this->artisan('validate:publications --json')
222
            ->expectsOutput(<<<'JSON'
223
                {
224
                    "test-publication": {
225
                        "extra-field": {
226
                            "title": "Field title passed.",
227
                            "extra": "Warning: The extra field is not defined in the publication type."
228
                        },
229
                        "invalid-field-and-extra-field": {
230
                            "title": "Error: The title must be a string.",
231
                            "extra": "Warning: The extra field is not defined in the publication type."
232
                        },
233
                        "invalid-field": {
234
                            "title": "Error: The title must be a string."
235
                        },
236
                        "missing-field": {
237
                            "title": "Error: The title must be a string."
238
                        },
239
                        "valid": {
240
                            "title": "Field title passed."
241
                        }
242
                    }
243
                }
244
                JSON)
245
            ->assertExitCode(1);
246
    }
247
248
    public function testWithJsonOutputWithNoPublications()
249
    {
250
        $this->directory('test-publication');
251
        $this->copyTestPublicationFixture();
252
253
        $this->artisan('validate:publications --json')
254
            ->expectsOutput(<<<'JSON'
255
                {
256
                    "test-publication": []
257
                }
258
                JSON
259
            )
260
            ->assertExitCode(0);
261
    }
262
263
    protected function copyTestPublicationFixture(): void
264
    {
265
        file_put_contents(Hyde::path('test-publication/schema.json'), <<<'JSON'
266
            {
267
                "name": "Test Publication",
268
                "canonicalField": "title",
269
                "detailTemplate": "detail.blade.php",
270
                "listTemplate": "list.blade.php",
271
                "sortField": "__createdAt",
272
                "sortAscending": true,
273
                "pageSize": 25,
274
                "fields": [
275
                    {
276
                        "name": "title",
277
                        "type": "string"
278
                    }
279
                ]
280
            }
281
            JSON
282
        );
283
    }
284
285
    protected function savePublication(string $name): void
286
    {
287
        (new PublicationType($name))->save();
288
    }
289
290
    protected function createFullRangeTestFixtures(): void
291
    {
292
        $this->directory('test-publication');
293
294
        $publicationType = new PublicationType('test-publication', fields: [
295
            ['name' => 'title', 'type' => 'string'],
296
        ]);
297
        $publicationType->save();
298
299
        $this->file('test-publication/extra-field.md', <<<'MD'
300
            ---
301
            title: foo
302
            extra: field
303
            ---
304
            
305
            # My Page
306
            MD
307
        );
308
309
        $this->file('test-publication/invalid-field-and-extra-field.md', <<<'MD'
310
            ---
311
            title: false
312
            extra: field
313
            ---
314
            
315
            # My Page
316
            MD
317
        );
318
319
        $this->file('test-publication/missing-field.md', <<<'MD'
320
            ---
321
            ---
322
            
323
            # My Page
324
            MD
325
        );
326
327
        $this->file('test-publication/invalid-field.md', <<<'MD'
328
            ---
329
            title: false
330
            ---
331
            
332
            # My Page
333
            MD
334
        );
335
336
        $this->file('test-publication/valid.md', <<<'MD'
337
            ---
338
            title: foo
339
            ---
340
            
341
            # My Page
342
            MD
343
        );
344
    }
345
}
346