Passed
Push — master ( 2a3d19...5e9e4e )
by Caen
05:36 queued 13s
created

PublicationPageTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 83
dl 0
loc 149
rs 10
c 0
b 0
f 0
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A test_publication_pages_are_compilable() 0 8 1
A test_identifier_passed_constructor_is_normalized() 0 12 1
A test_publication_pages_are_parsable() 0 25 1
A test_publication_pages_are_routable() 0 10 1
A createPublicationFiles() 0 8 1
A test_publication_pages_are_discoverable() 0 6 1
A test_identifier_normalizer_does_not_affect_directory_with_same_name_as_identifier() 0 7 1
A createRealPublicationFiles() 0 31 1
A test_publication_pages_are_properly_parsed() 0 12 1
A test_source_path_mappings() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Publications\Testing\Feature;
6
7
use Hyde\Hyde;
8
use Hyde\Publications\Models\PublicationType;
9
use Hyde\Publications\Pages\PublicationPage;
10
use Hyde\Support\Models\Route;
11
use Hyde\Testing\TestCase;
12
13
use function file_put_contents;
14
15
/**
16
 * @covers \Hyde\Publications\Pages\PublicationPage
17
 */
18
class PublicationPageTest extends TestCase
19
{
20
    public function test_source_path_mappings()
21
    {
22
        $this->createPublicationFiles();
23
24
        $page = new PublicationPage('foo', [], '', PublicationType::fromFile('test-publication/schema.json'));
25
26
        $this->assertSame('test-publication/foo', $page->getIdentifier());
27
        $this->assertSame('test-publication/foo', $page->getRouteKey());
28
        $this->assertSame('test-publication/foo.md', $page->getSourcePath());
29
        $this->assertSame('test-publication/foo.html', $page->getOutputPath());
30
    }
31
32
    public function test_publication_pages_are_routable()
33
    {
34
        $this->createPublicationFiles();
35
36
        $page = PublicationPage::get('test-publication/foo');
37
        $this->assertInstanceOf(Route::class, $page->getRoute());
38
        $this->assertEquals(new Route($page), $page->getRoute());
39
        $this->assertSame($page->getRoute()->getLink(), $page->getLink());
40
        $this->assertArrayHasKey($page->getSourcePath(), Hyde::pages());
41
        $this->assertArrayHasKey($page->getRouteKey(), Hyde::routes());
42
    }
43
44
    public function test_publication_pages_are_discoverable()
45
    {
46
        $this->createPublicationFiles();
47
48
        $collection = Hyde::pages()->getPages();
49
        $this->assertInstanceOf(PublicationPage::class, $collection->get('test-publication/foo.md'));
50
    }
51
52
    public function test_publication_pages_are_properly_parsed()
53
    {
54
        $this->createPublicationFiles();
55
56
        $page = Hyde::pages()->getPages()->get('test-publication/foo.md');
57
        $this->assertInstanceOf(PublicationPage::class, $page);
58
        $this->assertEquals('test-publication/foo', $page->getIdentifier());
59
        $this->assertEquals('Foo', $page->title);
60
61
        $this->assertEquals('bar', $page->matter('foo'));
62
        $this->assertEquals('canonical', $page->matter('__canonical'));
63
        $this->assertEquals('Hello World!', $page->markdown()->body());
64
    }
65
66
    public function test_publication_pages_are_parsable()
67
    {
68
        $this->directory('test-publication');
69
70
        (new PublicationType('test-publication'))->save();
71
72
        $this->file('test-publication/foo.md', <<<'MD'
73
            ---
74
            __createdAt: 2022-11-27 21:07:37
75
            title: My Title
76
            ---
77
            
78
            ## Write something awesome.
79
            
80
            
81
            MD
82
        );
83
84
        $page = PublicationPage::parse('test-publication/foo');
85
        $this->assertInstanceOf(PublicationPage::class, $page);
86
        $this->assertEquals('test-publication/foo', $page->identifier);
87
        $this->assertEquals('## Write something awesome.', $page->markdown);
88
        $this->assertEquals('My Title', $page->title);
89
        $this->assertEquals('My Title', $page->matter->get('title'));
90
        $this->assertTrue($page->matter->has('__createdAt'));
91
    }
92
93
    public function test_publication_pages_are_compilable()
94
    {
95
        $this->createRealPublicationFiles();
96
97
        $page = Hyde::pages()->getPages()->get('test-publication/foo.md');
98
99
        Hyde::shareViewData($page);
100
        $this->assertStringContainsString('Hello World!', $page->compile());
101
    }
102
103
    public function test_identifier_passed_constructor_is_normalized()
104
    {
105
        $this->createPublicationFiles();
106
        $type = PublicationType::fromFile('test-publication/schema.json');
107
108
        $page1 = new PublicationPage('foo', [], '', $type);
109
        $page2 = new PublicationPage('test-publication/foo', [], '', $type);
110
111
        $this->assertSame('test-publication/foo', $page1->getIdentifier());
112
        $this->assertSame('test-publication/foo', $page2->getIdentifier());
113
114
        $this->assertEquals($page1, $page2);
115
    }
116
117
    public function test_identifier_normalizer_does_not_affect_directory_with_same_name_as_identifier()
118
    {
119
        $this->createPublicationFiles();
120
        $type = PublicationType::fromFile('test-publication/schema.json');
121
122
        $page = new PublicationPage('test-publication/test-publication', [], '', $type);
123
        $this->assertSame('test-publication/test-publication', $page->getIdentifier());
124
    }
125
126
    protected function createRealPublicationFiles(): void
127
    {
128
        $this->directory('test-publication');
129
        file_put_contents(Hyde::path('test-publication/schema.json'), '{
130
  "name": "test",
131
  "canonicalField": "slug",
132
  "detailTemplate": "test_detail.blade.php",
133
  "listTemplate": "test_list.blade.php",
134
  "sortField": "__createdAt",
135
  "sortAscending": true,
136
  "pageSize": 0,
137
  "fields": [
138
    {
139
      "name": "slug",
140
      "type": "string"
141
    }
142
  ]
143
}');
144
        file_put_contents(
145
            Hyde::path('test-publication/foo.md'),
146
            '---
147
__canonical: canonical
148
__createdAt: 2022-11-16 11:32:52
149
foo: bar
150
---
151
152
Hello World!
153
'
154
        );
155
156
        file_put_contents(Hyde::path('test-publication/test_detail.blade.php'), '{{ ($publication->markdown()->body()) }}');
157
    }
158
159
    protected function createPublicationFiles(): void
160
    {
161
        $this->directory('test-publication');
162
        (new PublicationType('Test Publication'))->save();
163
164
        file_put_contents(
165
            Hyde::path('test-publication/foo.md'),
166
            '---
167
__canonical: canonical
168
__createdAt: 2022-11-16 11:32:52
169
foo: bar
170
---
171
172
Hello World!
173
'
174
        );
175
    }
176
}
177