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

UnitTestCase   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 36
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 Hyde\Foundation\HydeKernel;
8
use Illuminate\Config\Repository;
9
use Illuminate\Support\Facades\Config;
10
use PHPUnit\Framework\TestCase as BaseTestCase;
11
12
abstract class UnitTestCase extends BaseTestCase
13
{
14
    protected static bool $hasSetUpKernel = false;
15
16
    protected static bool $needsKernel = false;
17
    protected static bool $needsConfig = false;
18
19
    protected static function needsKernel(): void
20
    {
21
        if (! self::$hasSetUpKernel) {
22
            self::setupKernel();
23
        }
24
    }
25
26
    public static function setUpBeforeClass(): void
27
    {
28
        if (static::$needsKernel) {
29
            self::needsKernel();
30
        }
31
32
        if (static::$needsConfig) {
33
            self::mockConfig();
34
        }
35
    }
36
37
    protected static function setupKernel(): void
38
    {
39
        HydeKernel::setInstance(new HydeKernel());
40
        self::$hasSetUpKernel = true;
41
    }
42
43
    protected static function mockConfig(array $items = []): void
44
    {
45
        app()->bind('config', fn (): Repository => new Repository($items));
46
47
        Config::swap(app('config'));
48
    }
49
}
50