Passed
Push — master ( 8492cc...15f9ee )
by Caen
05:33 queued 02:01
created

test_action_can_generate_nested_pages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Hyde\Framework\Testing\Feature\Actions;
4
5
use Hyde\Framework\Actions\CreatesNewPageSourceFile;
6
use Hyde\Framework\Exceptions\FileConflictException;
7
use Hyde\Framework\Exceptions\UnsupportedPageTypeException;
8
use Hyde\Framework\Hyde;
9
use Hyde\Framework\Models\Pages\BladePage;
10
use Hyde\Framework\Models\Pages\DocumentationPage;
11
use Hyde\Testing\TestCase;
12
13
/**
14
 * @covers \Hyde\Framework\Actions\CreatesNewPageSourceFile
15
 */
16
class CreatesNewPageSourceFileTest extends TestCase
17
{
18
    protected function tearDown(): void
19
    {
20
        if (file_exists(Hyde::path('_pages/682072b-test-page.md'))) {
21
            unlink(Hyde::path('_pages/682072b-test-page.md'));
22
        }
23
24
        if (file_exists(Hyde::path('_pages/682072b-test-page.blade.php'))) {
25
            unlink(Hyde::path('_pages/682072b-test-page.blade.php'));
26
        }
27
28
        parent::tearDown();
29
    }
30
31
    public function test_class_can_be_instantiated()
32
    {
33
        $this->assertInstanceOf(
34
            CreatesNewPageSourceFile::class,
35
            new CreatesNewPageSourceFile('682072b Test Page')
36
        );
37
    }
38
39
    public function test_that_a_slug_is_generated_from_the_title()
40
    {
41
        $this->assertEquals(
42
            '682072b-test-page',
43
            (new CreatesNewPageSourceFile('682072b Test Page'))->slug
44
        );
45
    }
46
47
    public function test_that_an_exception_is_thrown_for_invalid_page_type()
48
    {
49
        $this->expectException(UnsupportedPageTypeException::class);
50
        $this->expectExceptionMessage('The page type must be either "markdown", "blade", or "documentation"');
51
52
        (new CreatesNewPageSourceFile('682072b Test Page', 'invalid'));
53
    }
54
55
    public function test_that_an_exception_is_thrown_if_file_already_exists_and_overwrite_is_false()
56
    {
57
        $path = Hyde::path('_pages/foo.md');
58
        file_put_contents($path, 'foo');
59
60
        $this->expectException(FileConflictException::class);
61
        $this->expectExceptionMessage("File already exists: $path");
62
        $this->expectExceptionCode(409);
63
64
        new CreatesNewPageSourceFile('foo');
65
66
        unlink($path);
67
    }
68
69
    public function test_that_can_save_file_returns_true_if_file_already_exists_and_overwrite_is_true()
70
    {
71
        $path = Hyde::path('_pages/foo.md');
72
        file_put_contents($path, 'foo');
73
74
        new CreatesNewPageSourceFile('foo', force: true);
75
76
        $this->assertTrue(true);
77
        unlink($path);
78
    }
79
80
    public function test_that_a_markdown_file_can_be_created_and_contains_expected_content()
81
    {
82
        (new CreatesNewPageSourceFile('682072b Test Page'));
83
84
        $this->assertFileExists(
85
            Hyde::path('_pages/682072b-test-page.md')
86
        );
87
88
        $this->assertEquals(
89
            "---\ntitle: 682072b Test Page\n---\n\n# 682072b Test Page\n",
90
            file_get_contents(Hyde::path('_pages/682072b-test-page.md'))
91
        );
92
    }
93
94
    public function test_that_a_blade_file_can_be_created_and_contains_expected_content()
95
    {
96
        (new CreatesNewPageSourceFile('682072b Test Page', BladePage::class));
97
98
        $this->assertFileExists(
99
            Hyde::path('_pages/682072b-test-page.blade.php')
100
        );
101
102
        $fileContent = file_get_contents(Hyde::path('_pages/682072b-test-page.blade.php'));
103
        $this->assertStringContainsString(
104
            '@extends(\'hyde::layouts.app\')',
105
            $fileContent
106
        );
107
        $this->assertStringContainsString(
108
            '@php($title = "682072b Test Page")',
109
            $fileContent
110
        );
111
        $this->assertStringContainsString(
112
            '<h1 class="text-center text-3xl font-bold">682072b Test Page</h1>',
113
            $fileContent
114
        );
115
    }
116
117
    public function test_that_a_documentation_file_can_be_created_and_contains_expected_content()
118
    {
119
        (new CreatesNewPageSourceFile('682072b Test Page', DocumentationPage::class));
120
121
        $this->assertFileExists(
122
            Hyde::path('_docs/682072b-test-page.md')
123
        );
124
125
        $this->assertEquals(
126
            "# 682072b Test Page\n",
127
            file_get_contents(Hyde::path('_docs/682072b-test-page.md'))
128
        );
129
130
        Hyde::unlink('_docs/682072b-test-page.md');
0 ignored issues
show
Bug introduced by
The method unlink() does not exist on Hyde\Framework\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

130
        Hyde::/** @scrutinizer ignore-call */ 
131
              unlink('_docs/682072b-test-page.md');
Loading history...
131
    }
132
133
    public function test_that_the_file_path_can_be_returned()
134
    {
135
        $this->assertEquals(
136
            Hyde::path('_pages/682072b-test-page.md'),
137
            (new CreatesNewPageSourceFile('682072b Test Page'))->outputPath
138
        );
139
140
        $this->assertEquals(
141
            Hyde::path('_pages/682072b-test-page.blade.php'),
142
            (new CreatesNewPageSourceFile('682072b Test Page', BladePage::class))->outputPath
143
        );
144
    }
145
146
    public function test_parse_slug_returns_slug_generated_from_title()
147
    {
148
        $action = new CreatesNewPageSourceFile('Foo Bar');
149
        $this->assertEquals('foo-bar', $action->parseSlug('Foo Bar'));
150
        Hyde::unlink('_pages/foo-bar.md');
151
    }
152
153
    public function test_parse_slug_does_not_include_path_information()
154
    {
155
        $action = new CreatesNewPageSourceFile('Foo Bar');
156
        $this->assertEquals('foo-bar', $action->parseSlug('/foo/bar/Foo Bar'));
157
        Hyde::unlink('_pages/foo-bar.md');
158
    }
159
160
    public function test_parse_slug_sets_sub_dir_property_for_nested_pages()
161
    {
162
        $action = new CreatesNewPageSourceFile('foo');
163
        $this->assertEquals('bar', $action->parseSlug('/foo/bar'));
164
        $this->assertEquals('/foo/', $action->subDir);
165
        Hyde::unlink('_pages/foo.md');
166
    }
167
168
    public function test_action_can_generate_nested_pages()
169
    {
170
        new CreatesNewPageSourceFile('foo/bar');
171
        $this->assertFileExists(Hyde::path('_pages/foo/bar.md'));
172
        Hyde::unlink('_pages/foo/bar.md');
173
        rmdir(Hyde::path('_pages/foo'));
174
    }
175
}
176