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

CreatesNewPublicationPageTest::testItCreatesValidYaml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 34
nc 1
nop 0
dl 0
loc 43
rs 9.376
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\Hyde;
8
use RuntimeException;
9
use Hyde\Testing\TestCase;
10
use Illuminate\Support\Str;
11
use Hyde\Facades\Filesystem;
12
use Illuminate\Support\Carbon;
13
use Symfony\Component\Yaml\Yaml;
14
use Illuminate\Support\Collection;
15
use Hyde\Publications\Models\PublicationType;
16
use Hyde\Publications\Models\PublicationFieldValue;
17
use Hyde\Publications\Concerns\PublicationFieldTypes;
18
use Hyde\Publications\Actions\CreatesNewPublicationPage;
19
20
use function file_get_contents;
21
22
/**
23
 * @covers \Hyde\Publications\Actions\CreatesNewPublicationPage
24
 */
25
class CreatesNewPublicationPageTest extends TestCase
26
{
27
    protected function setUp(): void
28
    {
29
        parent::setUp();
30
31
        Carbon::setTestNow(Carbon::create(2022));
32
    }
33
34
    protected function tearDown(): void
35
    {
36
        Filesystem::deleteDirectory('test-publication');
37
38
        parent::tearDown();
39
    }
40
41
    public function testCreate()
42
    {
43
        $pubType = new PublicationType(
44
            'Test Publication',
45
            'title',
46
            fields: [['type' => 'string', 'name' => 'title']],
47
        );
48
49
        $fieldData = Collection::make([
50
            'title' => new PublicationFieldValue(PublicationFieldTypes::String, 'Hello World'),
51
        ]);
52
53
        (new CreatesNewPublicationPage($pubType, $fieldData))->create();
54
55
        $this->assertFileExists(Hyde::path('test-publication/hello-world.md'));
56
        $this->assertEquals(<<<'MARKDOWN'
57
            ---
58
            __createdAt: 2022-01-01T00:00:00+00:00
59
            title: 'Hello World'
60
            ---
61
            
62
            ## Write something awesome.
63
            
64
            
65
            MARKDOWN, file_get_contents(Hyde::path('test-publication/hello-world.md')));
66
    }
67
68
    public function testWithTextType()
69
    {
70
        $pubType = $this->makePublicationType([[
71
            'type' => 'text',
72
            'name' => 'description',
73
        ]]);
74
75
        $fieldData = Collection::make(['description' => new PublicationFieldValue(PublicationFieldTypes::Text, <<<'TEXT'
76
            This is a description
77
            It can be multiple lines.
78
            TEXT),
79
        ]);
80
81
        (new CreatesNewPublicationPage($pubType, $fieldData))->create();
82
83
        $this->assertFileExists(Hyde::path('test-publication/2022-01-01-000000.md'));
84
        $this->assertEquals(<<<'MARKDOWN'
85
            ---
86
            __createdAt: 2022-01-01T00:00:00+00:00
87
            description: |
88
                This is a description
89
                It can be multiple lines.
90
            ---
91
            
92
            ## Write something awesome.
93
            
94
            
95
            MARKDOWN, file_get_contents(Hyde::path('test-publication/2022-01-01-000000.md')));
96
    }
97
98
    public function testWithArrayType()
99
    {
100
        $pubType = $this->makePublicationType([[
101
            'type' => 'array',
102
            'name' => 'tags',
103
        ]]);
104
105
        $fieldData = Collection::make([
106
            'tags' => new PublicationFieldValue(PublicationFieldTypes::Tag, ['tag1', 'tag2', 'foo bar']),
107
        ]);
108
109
        (new CreatesNewPublicationPage($pubType, $fieldData))->create();
110
111
        $this->assertFileExists(Hyde::path('test-publication/2022-01-01-000000.md'));
112
        $this->assertEquals(<<<'MARKDOWN'
113
            ---
114
            __createdAt: 2022-01-01T00:00:00+00:00
115
            tags:
116
                - tag1
117
                - tag2
118
                - 'foo bar'
119
            ---
120
            
121
            ## Write something awesome.
122
            
123
            
124
            MARKDOWN, file_get_contents(Hyde::path('test-publication/2022-01-01-000000.md')));
125
    }
126
127
    public function testCreateWithoutSupplyingCanonicalField()
128
    {
129
        $pubType = new PublicationType(
130
            'Test Publication',
131
            'title',
132
            fields: [['type' => 'string', 'name' => 'title']],
133
        );
134
135
        $fieldData = Collection::make();
136
137
        $this->expectException(RuntimeException::class);
138
        $this->expectExceptionMessage("Could not find field value for 'title' which is required as it's the type's canonical field");
139
        (new CreatesNewPublicationPage($pubType, $fieldData))->create();
140
    }
141
142
    public function testCreateWithoutSupplyingRequiredField()
143
    {
144
        $pubType = $this->makePublicationType([['type' => 'string', 'name' => 'title']]);
145
146
        $fieldData = Collection::make();
147
148
        (new CreatesNewPublicationPage($pubType, $fieldData))->create();
149
150
        // Since the inputs are collected by the command, with the shipped code this should never happen.
151
        // If a developer is using the action directly, it's their responsibility to ensure the data is valid.
152
153
        $this->assertFileExists(Hyde::path('test-publication/2022-01-01-000000.md'));
154
        $this->assertEquals(<<<'MARKDOWN'
155
            ---
156
            __createdAt: 2022-01-01T00:00:00+00:00
157
            ---
158
            
159
            ## Write something awesome.
160
            
161
            
162
            MARKDOWN, file_get_contents(Hyde::path('test-publication/2022-01-01-000000.md')));
163
    }
164
165
    public function testItCreatesValidYaml()
166
    {
167
        $pubType = $this->makePublicationType([
168
            ['type' => 'string', 'name' => 'title'],
169
            ['type' => 'text', 'name' => 'description'],
170
            ['type' => 'array', 'name' => 'tags'],
171
        ]);
172
173
        $fieldData = Collection::make([
174
            'title' => new PublicationFieldValue(PublicationFieldTypes::String, 'Hello World'),
175
            'description' => new PublicationFieldValue(PublicationFieldTypes::Text, "This is a description.\nIt can be multiple lines.\n"),
176
            'tags' => new PublicationFieldValue(PublicationFieldTypes::Tag, ['tag1', 'tag2', 'foo bar']),
177
        ]);
178
179
        (new CreatesNewPublicationPage($pubType, $fieldData))->create();
180
181
        $this->assertFileExists(Hyde::path('test-publication/2022-01-01-000000.md'));
182
        $contents = file_get_contents(Hyde::path('test-publication/2022-01-01-000000.md'));
183
        $this->assertEquals(<<<'MARKDOWN'
184
            ---
185
            __createdAt: 2022-01-01T00:00:00+00:00
186
            title: 'Hello World'
187
            description: |
188
                This is a description.
189
                It can be multiple lines.
190
            tags:
191
                - tag1
192
                - tag2
193
                - 'foo bar'
194
            ---
195
            
196
            ## Write something awesome.
197
            
198
            
199
            MARKDOWN, $contents
200
        );
201
202
        $this->assertSame([
203
            '__createdAt' => 1640995200,
204
            'title' => 'Hello World',
205
            'description' => "This is a description.\nIt can be multiple lines.\n",
206
            'tags' =>  ['tag1', 'tag2', 'foo bar'],
207
        ], Yaml::parse(Str::between($contents, '---', '---')));
208
    }
209
210
    protected function makePublicationType(array $fields): PublicationType
211
    {
212
        return new PublicationType('Test Publication', '__createdAt', fields: $fields);
213
    }
214
}
215