Passed
Push — master ( c70df2...3b77f2 )
by Caen
03:41 queued 11s
created

HydeMakePageCommandTest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Testing\Feature\Commands;
4
5
use Exception;
6
use Hyde\Framework\Exceptions\FileConflictException;
7
use Hyde\Framework\Hyde;
8
use Hyde\Testing\TestCase;
9
10
/**
11
 * @covers \Hyde\Framework\Commands\HydeMakePageCommand
12
 */
13
class HydeMakePageCommandTest extends TestCase
14
{
15
    protected string $markdownPath;
16
    protected string $bladePath;
17
18
    public function setUp(): void
19
    {
20
        parent::setUp();
21
22
        $this->markdownPath = Hyde::path('_pages/foo-test-page.md');
23
        $this->bladePath = Hyde::path('_pages/foo-test-page.blade.php');
24
    }
25
26
    protected function tearDown(): void
27
    {
28
        if (file_exists($this->markdownPath)) {
29
            unlink($this->markdownPath);
30
        }
31
32
        if (file_exists($this->bladePath)) {
33
            unlink($this->bladePath);
34
        }
35
36
        parent::tearDown();
37
    }
38
39
    // Assert the command can run
40
    public function test_command_can_run()
41
    {
42
        $this->artisan('make:page "foo test page"')->assertExitCode(0);
43
    }
44
45
    // Assert the command contains expected output
46
    public function test_command_output()
47
    {
48
        $this->artisan('make:page "foo test page"')
49
            ->expectsOutputToContain('Creating a new page!')
50
            ->expectsOutputToContain('Created file '.$this->markdownPath);
51
    }
52
53
    // Assert the command allows the user to specify a page type
54
    public function test_command_allows_user_to_specify_page_type()
55
    {
56
        $this->artisan('make:page "foo test page" --type=markdown')->assertExitCode(0);
57
        $this->artisan('make:page "foo test page" --type=blade')->assertExitCode(0);
58
    }
59
60
    // Assert the type option is case-insensitive
61
    public function test_type_option_is_case_insensitive()
62
    {
63
        $this->artisan('make:page "foo test page" --type=Markdown')->assertExitCode(0);
64
        $this->artisan('make:page "foo test page" --type=Blade')->assertExitCode(0);
65
    }
66
67
    // Assert that the command fails if the user specifies an invalid page type
68
    public function test_command_fails_if_user_specifies_invalid_page_type()
69
    {
70
        $this->expectException(Exception::class);
71
        $this->expectExceptionMessage('Invalid page type: invalid');
72
        $this->expectExceptionCode(400);
73
        $this->artisan('make:page "foo test page" --type=invalid')->assertExitCode(400);
74
    }
75
76
    // Assert the command creates the markdown file
77
    public function test_command_creates_markdown_file()
78
    {
79
        $this->artisan('make:page "foo test page"')->assertExitCode(0);
80
81
        $this->assertFileExists($this->markdownPath);
82
    }
83
84
    // Assert the command creates the blade file
85
    public function test_command_creates_blade_file()
86
    {
87
        $this->artisan('make:page "foo test page" --type="blade"')->assertExitCode(0);
88
89
        $this->assertFileExists($this->bladePath);
90
    }
91
92
    // Assert the command creates the documentation file
93
    public function test_command_creates_documentation_file()
94
    {
95
        $this->artisan('make:page "foo test page" --type="documentation"')->assertExitCode(0);
96
97
        $this->assertFileExists(Hyde::path('_docs/foo-test-page.md'));
98
        unlink(Hyde::path('_docs/foo-test-page.md'));
99
    }
100
101
    // Assert the command fails if the file already exists
102
    public function test_command_fails_if_file_already_exists()
103
    {
104
        file_put_contents($this->markdownPath, 'This should not be overwritten');
105
106
        $this->expectException(FileConflictException::class);
107
        $this->expectExceptionMessage("File already exists: $this->markdownPath");
108
        $this->expectExceptionCode(409);
109
        $this->artisan('make:page "foo test page"')->assertExitCode(409);
110
111
        $this->assertEquals('This should not be overwritten', file_get_contents($this->markdownPath));
112
    }
113
114
    // Assert the command overwrites existing files when the force option is used
115
    public function test_command_overwrites_existing_files_when_force_option_is_used()
116
    {
117
        file_put_contents($this->markdownPath, 'This should be overwritten');
118
119
        $this->artisan('make:page "foo test page" --force')->assertExitCode(0);
120
121
        $this->assertNotEquals('This should be overwritten', file_get_contents($this->markdownPath));
122
    }
123
124
    // Assert the command prompts for title if it was not specified
125
    public function test_command_prompts_for_title_if_it_was_not_specified()
126
    {
127
        $this->artisan('make:page')
128
            ->expectsQuestion('What is the title of the page?', 'Test Page')
129
            ->expectsOutput("Creating page with title: Test Page\n")
130
            ->assertExitCode(0);
131
132
        unlink(Hyde::path('_pages/test-page.md'));
133
    }
134
135
    // Assert the command falls back to default title if the user enters nothing
136
    public function test_command_falls_back_to_default_title_if_user_enters_nothing()
137
    {
138
        $this->artisan('make:page')
139
            ->expectsQuestion('What is the title of the page?', null)
140
            ->expectsOutput("Creating page with title: My New Page\n")
141
            ->assertExitCode(0);
142
143
        unlink(Hyde::path('_pages/my-new-page.md'));
144
    }
145
}
146