Passed
Pull Request — master (#380)
by Wilmer
04:47 queued 01:51
created

Mock::getConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Support;
6
7
use PHPUnit\Framework\TestCase;
8
use Psr\Log\LoggerInterface;
9
use Yiisoft\Cache\ArrayCache;
10
use Yiisoft\Cache\Cache;
11
use Yiisoft\Cache\CacheInterface;
12
use Yiisoft\Db\Cache\QueryCache;
13
use Yiisoft\Db\Cache\SchemaCache;
14
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface;
15
use Yiisoft\Db\Exception\Exception;
16
use Yiisoft\Db\Exception\InvalidConfigException;
17
use Yiisoft\Db\Tests\Support\Stubs\Connection;
18
use Yiisoft\Log\Logger;
19
use Yiisoft\Profiler\Profiler;
20
use Yiisoft\Profiler\ProfilerInterface;
21
22
/**
23
 * @psalm-suppress PropertyNotSetInConstructor
24
 */
25
final class Mock extends TestCase
26
{
27
    private static Cache|null $cache = null;
28
    private static Logger|null $logger = null;
29
    private static Profiler|null $profiler = null;
30
    private static QueryCache|null $queryCache = null;
31
    private static SchemaCache|null $schemaCache = null;
32
33
    public static function getConnection(
34
        bool $prepareDatabase = false,
35
        string $dsn = 'sqlite::memory:'
36
    ): ConnectionPDOInterface {
37
        $db = new Connection($dsn);
38
39
        if ($prepareDatabase) {
40
            self::prepareDatabase($db, __DIR__ . '/Fixture/sqlite.sql');
41
        }
42
43
        return $db;
44
    }
45
46
    public static function getCache(): CacheInterface
47
    {
48
        return self::cache();
49
    }
50
51
    public static function getLogger(): LoggerInterface
52
    {
53
        return self::logger();
54
    }
55
56
    public static function getProfiler(): ProfilerInterface
57
    {
58
        return self::profiler();
59
    }
60
61
    public static function getQueryCache(): QueryCache
62
    {
63
        return self::queryCache();
64
    }
65
66
    public static function getSchemaCache(): SchemaCache
67
    {
68
        return self::schemaCache();
69
    }
70
71
    /**
72
     * @throws Exception
73
     * @throws InvalidConfigException
74
     */
75
    public static function prepareDatabase(ConnectionPDOInterface $db, string $fixture): void
76
    {
77
        $db->open();
78
        $lines = explode(';', file_get_contents($fixture));
79
80
        foreach ($lines as $line) {
81
            if (trim($line) !== '') {
82
                $db->getPDO()?->exec($line);
83
            }
84
        }
85
    }
86
87
    private static function cache(): CacheInterface
88
    {
89
        if (self::$cache === null) {
90
            self::$cache = new Cache(new ArrayCache());
91
        }
92
93
        return self::$cache;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::cache could return the type null which is incompatible with the type-hinted return Yiisoft\Cache\CacheInterface. Consider adding an additional type-check to rule them out.
Loading history...
94
    }
95
96
    private static function logger(): LoggerInterface
97
    {
98
        if (self::$logger === null) {
99
            self::$logger = new Logger();
100
        }
101
102
        return self::$logger;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::logger could return the type null which is incompatible with the type-hinted return Psr\Log\LoggerInterface. Consider adding an additional type-check to rule them out.
Loading history...
103
    }
104
105
    private static function profiler(): ProfilerInterface
106
    {
107
        if (self::$profiler === null) {
108
            self::$profiler = new Profiler(self::logger());
109
        }
110
111
        return self::$profiler;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::profiler could return the type null which is incompatible with the type-hinted return Yiisoft\Profiler\ProfilerInterface. Consider adding an additional type-check to rule them out.
Loading history...
112
    }
113
114
    private static function queryCache(): QueryCache
115
    {
116
        if (self::$queryCache === null) {
117
            self::$queryCache = new QueryCache(self::cache());
118
        }
119
120
        return self::$queryCache;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::queryCache could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Cache\QueryCache. Consider adding an additional type-check to rule them out.
Loading history...
121
    }
122
123
    private static function schemaCache(): SchemaCache
124
    {
125
        if (self::$schemaCache === null) {
126
            self::$schemaCache = new SchemaCache(self::cache());
127
        }
128
129
        return self::$schemaCache;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::schemaCache could return the type null which is incompatible with the type-hinted return Yiisoft\Db\Cache\SchemaCache. Consider adding an additional type-check to rule them out.
Loading history...
130
    }
131
}
132