Passed
Push — master ( 3684ce...bd7ad1 )
by Caen
03:24 queued 12s
created

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