Passed
Push — master ( 8fb1fc...598c15 )
by Caen
03:49 queued 11s
created

TestCase::mockRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Hyde\Testing;
4
5
use Hyde\Framework\Contracts\PageContract;
6
use Hyde\Framework\Hyde;
7
use Hyde\Framework\Models\Pages\MarkdownPage;
8
use Hyde\Framework\Models\Route;
9
use LaravelZero\Framework\Testing\TestCase as BaseTestCase;
10
11
require_once __DIR__.'/helpers.php';
12
13
abstract class TestCase extends BaseTestCase
14
{
15
    use CreatesApplication;
16
    use ResetsApplication;
17
18
    protected static bool $booted = false;
19
20
    /**
21
     * Setup the test environment.
22
     *
23
     * @return void
24
     */
25
    protected function setUp(): void
26
    {
27
        parent::setUp();
28
29
        if (! static::$booted) {
30
            $this->resetApplication();
31
32
            Hyde::macro('touch', function (string|array $path) {
33
                if (is_array($path)) {
0 ignored issues
show
introduced by
The condition is_array($path) is always true.
Loading history...
34
                    foreach ($path as $p) {
35
                        touch(Hyde::path($p));
36
                    }
37
                } else {
38
                    return touch(Hyde::path($path));
39
                }
40
            });
41
42
            Hyde::macro('unlink', function (string|array $path) {
43
                if (is_array($path)) {
0 ignored issues
show
introduced by
The condition is_array($path) is always true.
Loading history...
44
                    foreach ($path as $p) {
45
                        unlink(Hyde::path($p));
46
                    }
47
                } else {
48
                    return unlink(Hyde::path($path));
49
                }
50
            });
51
52
            static::$booted = true;
53
        }
54
    }
55
56
    /**
57
     * Clean up the testing environment before the next test.
58
     *
59
     * @return void
60
     */
61
    protected function tearDown(): void
62
    {
63
        parent::tearDown();
64
    }
65
66
    /** @internal */
67
    protected function mockRoute(?Route $route = null)
68
    {
69
        view()->share('currentRoute', $route ?? (new Route(new MarkdownPage())));
70
    }
71
72
    /** @internal */
73
    protected function mockPage(?PageContract $page = null, ?string $currentPage = null)
74
    {
75
        view()->share('page', $page ?? new MarkdownPage());
76
        view()->share('currentPage', $currentPage ?? 'PHPUnit');
77
    }
78
79
    /** @internal */
80
    protected function mockCurrentPage(string $currentPage)
81
    {
82
        view()->share('currentPage', $currentPage);
83
    }
84
}
85