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

PublicationPageCompilerTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 107
rs 10
c 0
b 0
f 0
wmc 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Publications\Testing\Feature;
6
7
use Hyde\Framework\Exceptions\FileNotFoundException;
8
use Hyde\Hyde;
9
use Hyde\Publications\Actions\PublicationPageCompiler;
10
use Hyde\Publications\Models\PublicationType;
11
use Hyde\Publications\Pages\PublicationPage;
12
use Hyde\Support\Facades\Render;
13
use Hyde\Testing\TestCase;
14
15
use function file_get_contents;
16
use function file_put_contents;
17
use function json_decode;
18
use function json_encode;
19
20
/**
21
 * @covers \Hyde\Publications\Actions\PublicationPageCompiler
22
 */
23
class PublicationPageCompilerTest extends TestCase
24
{
25
    public function test_can_compile_publication_pages()
26
    {
27
        $this->setupPublicationType();
28
29
        file_put_contents(Hyde::path('test-publication/detail.blade.php'), 'Detail: {{ $publication->title }}');
30
31
        $string = PublicationPageCompiler::call(new PublicationPage('my-publication', type: PublicationType::get('test-publication')));
32
33
        $this->assertEquals('Detail: My Publication', $string);
34
    }
35
36
    public function test_can_compile_list_pages()
37
    {
38
        $this->setupPublicationType();
39
40
        file_put_contents(Hyde::path('test-publication/my-publication.md'), 'Foo');
41
        file_put_contents(Hyde::path('test-publication/list.blade.php'), 'List: {{ $publicationType->getPublications()->first()->title }}');
42
43
        $string = PublicationPageCompiler::call(PublicationType::get('test-publication')->getListPage());
44
45
        $this->assertEquals('List: My Publication', $string);
46
    }
47
48
    public function test_can_compile_publication_pages_with_registered_view()
49
    {
50
        $this->setupPublicationType();
51
52
        $schema = json_decode(file_get_contents(Hyde::path('test-publication/schema.json')));
53
        $schema->detailTemplate = 'foo';
54
        file_put_contents(Hyde::path('test-publication/schema.json'), json_encode($schema));
55
        $this->directory('resources/views');
56
        $this->file('resources/views/foo.blade.php', 'Registered detail view');
57
58
        $publicationPage = new PublicationPage('my-publication', type: PublicationType::get('test-publication'));
59
        Render::setPage($publicationPage);
60
        $this->assertEquals('Registered detail view', PublicationPageCompiler::call($publicationPage));
61
    }
62
63
    public function test_can_compile_list_pages_with_registered_view()
64
    {
65
        $this->setupPublicationType();
66
67
        $schema = json_decode(file_get_contents(Hyde::path('test-publication/schema.json')));
68
        $schema->listTemplate = 'foo';
69
        file_put_contents(Hyde::path('test-publication/schema.json'), json_encode($schema));
70
        $this->directory('resources/views');
71
        $this->file('resources/views/foo.blade.php', 'Registered list view');
72
73
        $publicationType = PublicationType::get('test-publication');
74
        $publicationPage = $publicationType->getListPage();
75
        $this->assertEquals('Registered list view', PublicationPageCompiler::call($publicationPage));
76
    }
77
78
    public function test_can_compile_publication_pages_with_registered_namespaced_view()
79
    {
80
        $this->setupPublicationType();
81
82
        $schema = json_decode(file_get_contents(Hyde::path('test-publication/schema.json')));
83
        $schema->detailTemplate = 'hyde-publications::detail';
84
        file_put_contents(Hyde::path('test-publication/schema.json'), json_encode($schema));
85
86
        $publicationPage = new PublicationPage('my-publication', type: PublicationType::get('test-publication'));
87
        Render::setPage($publicationPage);
88
        $this->assertStringContainsString('My Publication', PublicationPageCompiler::call($publicationPage));
89
    }
90
91
    public function test_can_compile_list_pages_with_registered_namespaced_view()
92
    {
93
        $this->setupPublicationType();
94
        $this->file('vendor/hyde/framework/resources/views/layouts/test.blade.php', 'Registered list view');
95
96
        $schema = json_decode(file_get_contents(Hyde::path('test-publication/schema.json')));
97
        $schema->listTemplate = 'hyde::layouts.test';
98
        file_put_contents(Hyde::path('test-publication/schema.json'), json_encode($schema));
99
        file_put_contents(Hyde::path('test-publication/my-publication.md'), 'Foo');
100
101
        $publicationType = PublicationType::get('test-publication');
102
        $publicationPage = $publicationType->getListPage();
103
        $this->assertEquals('Registered list view', PublicationPageCompiler::call($publicationPage));
104
    }
105
106
    public function test_with_missing_detail_blade_view()
107
    {
108
        $this->setupPublicationType();
109
110
        $this->expectException(FileNotFoundException::class);
111
        $this->expectExceptionMessage('File [test-publication/detail.blade.php] not found.');
112
113
        PublicationPageCompiler::call(new PublicationPage('my-publication', type: PublicationType::get('test-publication')));
114
    }
115
116
    public function test_with_missing_list_blade_view()
117
    {
118
        $this->setupPublicationType();
119
120
        $this->expectException(FileNotFoundException::class);
121
        $this->expectExceptionMessage('File [test-publication/list.blade.php] not found.');
122
123
        PublicationPageCompiler::call(PublicationType::get('test-publication')->getListPage());
124
    }
125
126
    protected function setupPublicationType()
127
    {
128
        $this->directory('test-publication');
129
        (new PublicationType('Test Publication'))->save();
130
    }
131
}
132