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

PublicationPageValidatorTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 69
dl 0
loc 110
rs 10
c 0
b 0
f 0
wmc 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Publications\Testing\Feature;
6
7
use Hyde\Publications\Actions\PublicationPageValidator;
8
use Hyde\Publications\Models\PublicationType;
9
use Hyde\Testing\TestCase;
10
use Illuminate\Validation\ValidationException;
11
12
/**
13
 * @covers \Hyde\Publications\Actions\PublicationPageValidator
14
 */
15
class PublicationPageValidatorTest extends TestCase
16
{
17
    public function testValidatePageFile()
18
    {
19
        $this->directory('test-publication');
20
        $publicationType = new PublicationType('test-publication', fields: [
21
            ['name' => 'myField', 'type' => 'string'],
22
            ['name' => 'myNumber', 'type' => 'integer'],
23
        ]);
24
        $publicationType->save();
25
26
        $this->file('test-publication/my-page.md', <<<'MD'
27
            ---
28
            myField: foo
29
            myNumber: 123
30
            ---
31
            
32
            # My Page
33
            MD
34
        );
35
36
        $validator = PublicationPageValidator::call($publicationType, 'my-page');
37
        $validator->validate();
38
39
        $this->assertTrue(true);
40
    }
41
42
    public function testValidatePageFileWithInvalidFields()
43
    {
44
        $this->directory('test-publication');
45
        $publicationType = new PublicationType('test-publication', fields: [
46
            ['name' => 'myField', 'type' => 'string'],
47
            ['name' => 'myNumber', 'type' => 'integer'],
48
        ]);
49
        $publicationType->save();
50
51
        $this->file('test-publication/my-page.md', <<<'MD'
52
            ---
53
            myField: false
54
            ---
55
            
56
            # My Page
57
            MD
58
        );
59
60
        $validator = PublicationPageValidator::call($publicationType, 'my-page');
61
62
        $this->expectException(ValidationException::class);
63
        $validator->validate();
64
    }
65
66
    public function testValidatePageFileWithInvalidDataBuffered()
67
    {
68
        $this->directory('test-publication');
69
        $publicationType = new PublicationType('test-publication', fields: [
70
            ['name' => 'myField', 'type' => 'string'],
71
            ['name' => 'myNumber', 'type' => 'integer'],
72
        ]);
73
        $publicationType->save();
74
75
        $this->file('test-publication/my-page.md', <<<'MD'
76
            ---
77
            myField: false
78
            ---
79
            
80
            # My Page
81
            MD
82
        );
83
84
        $validator = PublicationPageValidator::call($publicationType, 'my-page');
85
86
        $this->assertSame([
87
            'myField' => 'The my field must be a string.',
88
            'myNumber' => 'The my number must be an integer.',
89
        ], $validator->errors());
90
    }
91
92
    public function testWarningsWithWarnings()
93
    {
94
        $this->directory('test-publication');
95
        $publicationType = new PublicationType('test-publication');
96
        $publicationType->save();
97
98
        $this->file('test-publication/my-page.md', <<<'MD'
99
            ---
100
            extra: field
101
            ---
102
            
103
            # My Page
104
            MD
105
        );
106
107
        $validator = PublicationPageValidator::call($publicationType, 'my-page');
108
109
        $this->assertSame([
110
            'extra' => 'The extra field is not defined in the publication type.',
111
        ], $validator->warnings());
112
    }
113
114
    public function testWarningsWithoutWarnings()
115
    {
116
        $this->directory('test-publication');
117
        $publicationType = new PublicationType('test-publication');
118
        $publicationType->save();
119
120
        $this->file('test-publication/my-page.md');
121
122
        $validator = PublicationPageValidator::call($publicationType, 'my-page');
123
124
        $this->assertSame([], $validator->warnings());
125
    }
126
}
127