Passed
Branch master (1e39e8)
by Caen
03:01
created

ValidationServiceTest   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 75
dl 0
loc 186
rs 10
c 0
b 0
f 0
wmc 27
1
<?php
2
3
namespace Hyde\Framework\Testing\Feature\Services;
4
5
use Hyde\Framework\Hyde;
6
use Hyde\Framework\Models\ValidationResult;
7
use Hyde\Framework\Services\ValidationService;
8
use Hyde\Testing\TestCase;
9
10
/**
11
 * Class ValidationServiceTest.
12
 *
13
 * @covers \Hyde\Framework\Services\ValidationService
14
 * @covers \Hyde\Framework\Models\ValidationResult
15
 *
16
 * @see \Hyde\Framework\Testing\Feature\Commands\HydeValidateCommandTest
17
 */
18
class ValidationServiceTest extends TestCase
19
{
20
    protected ValidationService $service;
21
22
    public function __construct()
23
    {
24
        parent::__construct();
25
26
        $this->service = new ValidationService();
27
    }
28
29
    // Rather meta, but lets us know that the method assertions are correct, and gives us test coverage
30
    protected function test(string $method, int $expectedStatusCode)
31
    {
32
        $result = $this->service->run($method);
33
        $this->assertInstanceOf(ValidationResult::class, $result);
34
        $this->assertEquals($expectedStatusCode, $result->statusCode());
35
    }
36
37
    public function test_checks_returns_an_array_of_validation_check_methods()
38
    {
39
        $checks = ValidationService::checks();
40
        $this->assertIsArray($checks);
41
42
        // Assert each key starts with 'check_' and is a valid class method name
43
        foreach ($checks as $check) {
44
            $this->assertStringStartsWith('check_', $check);
45
            $this->assertTrue(method_exists(ValidationService::class, $check));
46
        }
47
    }
48
49
    public function test_check_validators_can_run()
50
    {
51
        $this->test('check_validators_can_run', 0);
52
    }
53
54
    public function test_check_site_has_a_404_page_can_pass()
55
    {
56
        $this->test('check_site_has_a_404_page', 0);
57
    }
58
59
    public function test_check_site_has_a_404_page_can_fail()
60
    {
61
        rename(Hyde::path('_pages/404.blade.php'), Hyde::path('_pages/404.blade.php.bak'));
62
        $this->test('check_site_has_a_404_page', 2);
63
        rename(Hyde::path('_pages/404.blade.php.bak'), Hyde::path('_pages/404.blade.php'));
64
    }
65
66
    public function test_check_documentation_site_has_an_index_page_can_pass()
67
    {
68
        touch('_docs/index.md');
69
        $this->test('check_documentation_site_has_an_index_page', 0);
70
        unlink('_docs/index.md');
71
    }
72
73
    public function test_check_documentation_site_has_an_index_page_can_pass_with_warning_when_only_finding_readme()
74
    {
75
        touch('_docs/README.md');
76
        $this->test('check_documentation_site_has_an_index_page', 2);
77
        $this->assertStringContainsString('a _docs/readme.md file was found',
78
            $this->service->run('check_documentation_site_has_an_index_page')->tip());
79
        unlink('_docs/README.md');
80
    }
81
82
    public function test_check_documentation_site_has_an_index_page_can_fail()
83
    {
84
        touch('_docs/foo.md');
85
        $this->test('check_documentation_site_has_an_index_page', 2);
86
        unlink('_docs/foo.md');
87
    }
88
89
    public function test_check_documentation_site_has_an_index_page_be_skipped()
90
    {
91
        $this->test('check_documentation_site_has_an_index_page', 1);
92
    }
93
94
    public function test_check_site_has_an_index_page_can_pass()
95
    {
96
        $this->test('check_site_has_an_index_page', 0);
97
    }
98
99
    public function test_check_site_has_an_index_page_can_fail()
100
    {
101
        rename(Hyde::path('_pages/index.blade.php'), Hyde::path('_pages/index.blade.php.bak'));
102
        $this->test('check_site_has_an_index_page', 2);
103
        rename(Hyde::path('_pages/index.blade.php.bak'), Hyde::path('_pages/index.blade.php'));
104
    }
105
106
    public function test_check_site_has_an_app_css_stylesheet_can_pass()
107
    {
108
        $this->test('check_site_has_an_app_css_stylesheet', 0);
109
    }
110
111
    public function test_check_site_has_an_app_css_stylesheet_can_fail()
112
    {
113
        rename(Hyde::path('_media/app.css'), Hyde::path('_media/app.css.bak'));
114
        unlinkIfExists(Hyde::path('_site/media/app.css'));
115
        $this->test('check_site_has_an_app_css_stylesheet', 2);
116
        rename(Hyde::path('_media/app.css.bak'), Hyde::path('_media/app.css'));
117
    }
118
119
    public function test_check_site_has_a_base_url_set_can_pass()
120
    {
121
        config(['site.url' => 'https://example.com']);
122
        $this->test('check_site_has_a_base_url_set', 0);
123
    }
124
125
    public function test_check_site_has_a_base_url_set_can_fail()
126
    {
127
        config(['site.url' => null]);
128
        $this->test('check_site_has_a_base_url_set', 2);
129
    }
130
131
    public function test_check_a_torchlight_api_token_is_set_can_skip()
132
    {
133
        config(['hyde.features' => []]);
134
        $this->test('check_a_torchlight_api_token_is_set', 1);
135
    }
136
137
    public function test_check_a_torchlight_api_token_is_set_can_pass()
138
    {
139
        config(['torchlight.token' => '12345']);
140
        $this->test('check_a_torchlight_api_token_is_set', 0);
141
    }
142
143
    public function test_check_a_torchlight_api_token_is_set_can_fail()
144
    {
145
        config(['torchlight.token' => null]);
146
        $this->test('check_a_torchlight_api_token_is_set', 2);
147
    }
148
149
    public function test_check_for_conflicts_between_blade_and_markdown_pages_can_pass()
150
    {
151
        $this->test('check_for_conflicts_between_blade_and_markdown_pages', 0);
152
    }
153
154
    public function test_check_for_conflicts_between_blade_and_markdown_pages_can_fail()
155
    {
156
        Hyde::touch(('_pages/index.md'));
157
        $this->test('check_for_conflicts_between_blade_and_markdown_pages', 2);
158
        unlink(Hyde::path('_pages/index.md'));
159
    }
160
161
    // Some unit tests
162
163
    public function test_validation_result_message_returns_message()
164
    {
165
        $result = new ValidationResult();
166
        $result->message = 'foo';
167
        $this->assertEquals('foo', $result->message());
168
    }
169
170
    public function test_validation_result_passed_returns_true_when_passed_is_true()
171
    {
172
        $result = new ValidationResult();
173
        $result->passed = true;
174
        $this->assertTrue($result->passed());
175
        $result->passed = false;
176
        $this->assertFalse($result->passed());
177
    }
178
179
    public function test_validation_result_failed_returns_true_when_passed_is_false()
180
    {
181
        $result = new ValidationResult();
182
        $result->passed = true;
183
        $this->assertFalse($result->failed());
184
        $result->passed = false;
185
        $this->assertTrue($result->failed());
186
    }
187
188
    public function test_validation_result_skipped_returns_true_when_skipped_is_true()
189
    {
190
        $result = new ValidationResult();
191
        $this->assertFalse($result->skipped());
192
        $result->skipped = true;
193
        $this->assertTrue($result->skipped());
194
        $result->skipped = false;
195
        $this->assertFalse($result->skipped());
196
    }
197
198
    public function test_validation_result_tip_returns_message_when_set()
199
    {
200
        $result = new ValidationResult();
201
        $this->assertFalse($result->tip());
202
        $result->tip = 'foo';
203
        $this->assertEquals('foo', $result->tip());
204
    }
205
}
206