Passed
Push — master ( fd4a88...7449e0 )
by Caen
04:01 queued 12s
created

UnitTestCase::setUpBeforeClass()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 4
nop 0
dl 0
loc 8
rs 10
c 1
b 0
f 0
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