Passed
Push — master ( 5fa21f...f08728 )
by Caen
03:34 queued 15s
created

TestCase   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Testing;
6
7
use function config;
8
use function file_get_contents;
9
10
use Hyde\Facades\Features;
11
use Hyde\Hyde;
12
13
use function Hyde\normalize_newlines;
14
15
use Illuminate\View\Component;
16
use LaravelZero\Framework\Testing\TestCase as BaseTestCase;
17
18
abstract class TestCase extends BaseTestCase
19
{
20
    use CreatesApplication;
21
    use ResetsApplication;
22
    use CreatesTemporaryFiles;
23
    use InteractsWithPages;
24
25
    protected static bool $booted = false;
26
27
    protected function setUp(): void
28
    {
29
        parent::setUp();
30
31
        if (! static::$booted) {
32
            $this->resetApplication();
33
34
            static::$booted = true;
35
        }
36
    }
37
38
    protected function tearDown(): void
39
    {
40
        $this->cleanUpFilesystem();
41
42
        if (method_exists(Component::class, 'flushCache')) {
43
            /** Until https://github.com/laravel/framework/pull/44648 makes its way into Laravel Zero, we need to clear the view cache ourselves */
44
            Component::flushCache();
45
            Component::forgetComponentsResolver();
46
            Component::forgetFactory();
47
        }
48
49
        Features::clearMockedInstances();
50
51
        parent::tearDown();
52
    }
53
54
    protected function assertFileEqualsString(string $string, string $path, bool $strict = false): void
55
    {
56
        if ($strict) {
57
            $this->assertSame($string, file_get_contents(Hyde::path($path)));
58
        } else {
59
            $this->assertEquals(normalize_newlines($string), normalize_newlines(file_get_contents(Hyde::path($path))));
60
        }
61
    }
62
63
    /**
64
     * Disable the throwing of exceptions on console commands for the duration of the test.
65
     *
66
     * Note that this only affects commands using the {@see \Hyde\Console\Concerns\Command::safeHandle()} method.
67
     */
68
    protected function throwOnConsoleException(bool $throw = true): void
69
    {
70
        config(['app.throw_on_console_exception' => $throw]);
71
    }
72
}
73