1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Testing; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Actions\ConvertsArrayToFrontMatter; |
6
|
|
|
use Hyde\Framework\Contracts\PageContract; |
7
|
|
|
use Hyde\Framework\Hyde; |
8
|
|
|
use Hyde\Framework\Models\Pages\MarkdownPage; |
9
|
|
|
use Hyde\Framework\Models\Route; |
10
|
|
|
use LaravelZero\Framework\Testing\TestCase as BaseTestCase; |
11
|
|
|
|
12
|
|
|
require_once __DIR__.'/helpers.php'; |
13
|
|
|
|
14
|
|
|
abstract class TestCase extends BaseTestCase |
15
|
|
|
{ |
16
|
|
|
use CreatesApplication; |
17
|
|
|
use ResetsApplication; |
18
|
|
|
|
19
|
|
|
protected static bool $booted = false; |
20
|
|
|
|
21
|
|
|
protected array $fileMemory = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Setup the test environment. |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
protected function setUp(): void |
29
|
|
|
{ |
30
|
|
|
parent::setUp(); |
31
|
|
|
|
32
|
|
|
if (! static::$booted) { |
33
|
|
|
$this->resetApplication(); |
34
|
|
|
|
35
|
|
|
static::$booted = true; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Clean up the testing environment before the next test. |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
protected function tearDown(): void |
45
|
|
|
{ |
46
|
|
|
if (sizeof($this->fileMemory) > 0) { |
47
|
|
|
Hyde::unlink($this->fileMemory); |
48
|
|
|
$this->fileMemory = []; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
parent::tearDown(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** @internal */ |
55
|
|
|
protected function mockRoute(?Route $route = null) |
56
|
|
|
{ |
57
|
|
|
view()->share('currentRoute', $route ?? (new Route(new MarkdownPage()))); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** @internal */ |
61
|
|
|
protected function mockPage(?PageContract $page = null, ?string $currentPage = null) |
62
|
|
|
{ |
63
|
|
|
view()->share('page', $page ?? new MarkdownPage()); |
64
|
|
|
view()->share('currentPage', $currentPage ?? 'PHPUnit'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** @internal */ |
68
|
|
|
protected function mockCurrentPage(string $currentPage) |
69
|
|
|
{ |
70
|
|
|
view()->share('currentPage', $currentPage); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Create a temporary file in the project directory. |
75
|
|
|
* The TestCase will automatically remove the file when the test is completed. |
76
|
|
|
*/ |
77
|
|
|
protected function file(string $path, ?string $contents = null): void |
78
|
|
|
{ |
79
|
|
|
if ($contents) { |
80
|
|
|
file_put_contents(Hyde::path($path), $contents); |
81
|
|
|
} else { |
82
|
|
|
Hyde::touch($path); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->fileMemory[] = $path; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Create a temporary Markdown+FrontMatter file in the project directory. |
90
|
|
|
*/ |
91
|
|
|
protected function markdown(string $path, string $contents = '', array $matter = []): void |
92
|
|
|
{ |
93
|
|
|
$this->file($path, (new ConvertsArrayToFrontMatter())->execute($matter).$contents); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|