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

StaticSiteBuilderPublicationModuleTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 126
dl 0
loc 229
rs 10
c 1
b 0
f 0
wmc 17
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Publications\Testing\Feature;
6
7
use Hyde\Hyde;
8
use Hyde\Testing\TestCase;
9
use Hyde\Facades\Filesystem;
10
use Illuminate\Support\Collection;
11
use Hyde\Publications\Models\PublicationType;
12
use Hyde\Publications\Actions\SeedsPublicationFiles;
13
use Hyde\Publications\Concerns\PublicationFieldTypes;
14
use Hyde\Publications\Actions\CreatesNewPublicationType;
15
16
use function range;
17
use function collect;
18
19
/**
20
 * Tests that publication pages are compiled properly when building the static site.
21
 *
22
 * These tests provide a high level overview of the entire publications feature.
23
 */
24
class StaticSiteBuilderPublicationModuleTest extends TestCase
25
{
26
    protected function setUp(): void
27
    {
28
        parent::setUp();
29
30
        $this->throwOnConsoleException();
31
    }
32
33
    public function testCompilingWithPublicationTypeWithSeededFilesContainingAllFieldTypes()
34
    {
35
        // Setup
36
37
        $this->directory('test-publication');
38
39
        $creator = new CreatesNewPublicationType('Test Publication', $this->getAllFields());
40
        $creator->create();
41
42
        $this->assertCount(3, Filesystem::files('test-publication'));
43
44
        $this->assertFileExists('test-publication/schema.json');
45
        $this->assertFileExists('test-publication/detail.blade.php');
46
        $this->assertFileExists('test-publication/list.blade.php');
47
48
        // Test site build without any publications
49
50
        $this->artisan('build')->assertSuccessful();
51
52
        $this->assertCount(1, Filesystem::files('_site/test-publication'));
53
        $this->assertFileExists('_site/test-publication/index.html');
54
55
        $this->resetSite();
56
57
        // Test site build with publications
58
59
        $seeder = new SeedsPublicationFiles(PublicationType::get('test-publication'), 5);
60
        $seeder->create();
61
62
        $this->assertCount(3 + 5, Filesystem::files('test-publication'));
63
64
        Hyde::boot(); // Reboot the kernel to discover the new publications
65
66
        $this->artisan('build')->assertSuccessful();
67
68
        $this->assertCount(1 + 5, Filesystem::files('_site/test-publication'));
69
70
        $this->resetSite();
71
    }
72
73
    public function testCompilingWithPublicationTypeThatUsesThePublishedViews()
74
    {
75
        $this->directory('test-publication');
76
77
        (new CreatesNewPublicationType('Test Publication', collect([])))->create();
78
        $this->assertCount(3, Filesystem::files('test-publication'));
79
80
        foreach (range(1, 5) as $i) {
81
            $this->file("test-publication/publication-$i.md", "## Test publication $i");
82
        }
83
84
        $this->artisan('build')->assertSuccessful();
85
86
        $this->assertSame([
87
            'index.html',
88
            'publication-1.html',
89
            'publication-2.html',
90
            'publication-3.html',
91
            'publication-4.html',
92
            'publication-5.html',
93
        ], $this->getFilenamesInDirectory('_site/test-publication'));
94
95
        $this->resetSite();
96
    }
97
98
    public function testCompilingWithPublicationTypeThatUsesTheVendorViews()
99
    {
100
        $this->directory('test-publication');
101
102
        (new CreatesNewPublicationType('Test Publication', collect([])))->create();
103
        $type = PublicationType::get('test-publication');
104
        $type->detailTemplate = 'hyde-publications::detail';
105
        $type->listTemplate = 'hyde-publications::list';
106
        $type->save();
107
108
        foreach (range(1, 5) as $i) {
109
            $this->file("test-publication/publication-$i.md", "## Test publication $i");
110
        }
111
112
        $this->artisan('build')->assertSuccessful();
113
114
        $this->assertSame([
115
            'index.html',
116
            'publication-1.html',
117
            'publication-2.html',
118
            'publication-3.html',
119
            'publication-4.html',
120
            'publication-5.html',
121
        ], $this->getFilenamesInDirectory('_site/test-publication'));
122
123
        $this->resetSite();
124
    }
125
126
    public function testCompilingWithPublicationTypeThatUsesThePublishedPaginatedViews()
127
    {
128
        $this->directory('test-publication');
129
130
        (new CreatesNewPublicationType('Test Publication', collect([]), pageSize: 10))->create();
131
132
        $type = PublicationType::get('test-publication');
133
        $type->pageSize = 2;
134
        $type->save();
135
136
        foreach (range(1, 5) as $i) {
137
            $this->file("test-publication/publication-$i.md", "## Test publication $i");
138
        }
139
140
        $this->artisan('build')->assertSuccessful();
141
142
        $this->assertSame([
143
            'index.html',
144
            'page-1.html',
145
            'page-2.html',
146
            'page-3.html',
147
            'publication-1.html',
148
            'publication-2.html',
149
            'publication-3.html',
150
            'publication-4.html',
151
            'publication-5.html',
152
        ], $this->getFilenamesInDirectory('_site/test-publication'));
153
154
        $this->assertThePaginationLinksAreCorrect();
155
156
        $this->resetSite();
157
    }
158
159
    public function testCompilingWithPublicationTypeThatUsesThePaginatedVendorViews()
160
    {
161
        $this->directory('test-publication');
162
163
        (new CreatesNewPublicationType('Test Publication', collect([])))->create();
164
165
        $type = PublicationType::get('test-publication');
166
        $type->listTemplate = 'hyde-publications::paginated_list';
167
        $type->pageSize = 2;
168
        $type->save();
169
170
        foreach (range(1, 5) as $i) {
171
            $this->file("test-publication/publication-$i.md", "## Test publication $i");
172
        }
173
174
        $this->artisan('build')->assertSuccessful();
175
176
        $this->assertSame([
177
            'index.html',
178
            'page-1.html',
179
            'page-2.html',
180
            'page-3.html',
181
            'publication-1.html',
182
            'publication-2.html',
183
            'publication-3.html',
184
            'publication-4.html',
185
            'publication-5.html',
186
        ], $this->getFilenamesInDirectory('_site/test-publication'));
187
188
        $this->assertThePaginationLinksAreCorrect();
189
190
        $this->resetSite();
191
    }
192
193
    protected function getAllFields(): Collection
194
    {
195
        $types = PublicationFieldTypes::collect();
196
197
        $array = [];
198
        foreach ($types as $index => $type) {
199
            $array[$index] = [
200
                'name' => "{$type->name}Field",
201
                'type' => $type->value,
202
            ];
203
        }
204
205
        return collect($array);
206
    }
207
208
    protected function getFilenamesInDirectory(string $directory): array
209
    {
210
        return collect(Filesystem::files($directory))->map(fn ($file) => $file->getFilename())->toArray();
211
    }
212
213
    /** @noinspection HtmlUnknownTarget */
214
    protected function assertThePaginationLinksAreCorrect(): void
215
    {
216
        $this->assertHtmlHas(<<<'HTML'
217
            <div class="px-2">
218
            <strong>1</strong>
219
            <a href="../test-publication/page-2.html">2</a>                                                <a href="../test-publication/page-3.html">3</a>                        </div>
220
            
221
            <a href="../test-publication/page-2.html">&#8250;</a>    </nav>        </div>
222
            HTML, Filesystem::get('_site/test-publication/page-1.html')
223
        );
224
225
        $this->assertHtmlHas(<<<'HTML'
226
            <div class="px-2">
227
            <a href="../test-publication/page-1.html">1</a>                                                <strong>2</strong>
228
            <a href="../test-publication/page-3.html">3</a>                        </div>
229
            HTML, Filesystem::get('_site/test-publication/page-2.html')
230
        );
231
232
        $this->assertHtmlHas(<<<'HTML'
233
            <div class="px-2">
234
            <a href="../test-publication/page-1.html">1</a>                                                <a href="../test-publication/page-2.html">2</a>                                                <strong>3</strong>
235
            </div>
236
            HTML, Filesystem::get('_site/test-publication/page-3.html')
237
        );
238
    }
239
240
    protected function assertHtmlHas(string $expected, string $html): void
241
    {
242
        $this->assertStringContainsString($this->stripIndentationForEachLine($expected), $this->stripIndentationForEachLine($html));
243
    }
244
245
    protected function stripIndentationForEachLine(string $string): string
246
    {
247
        $array = explode("\n", $string);
248
        foreach ($array as $index => $line) {
249
            $array[$index] = ltrim($line);
250
        }
251
252
        return implode("\n", $array);
253
    }
254
}
255