Passed
Pull Request — master (#380)
by Wilmer
05:31 queued 02:47
created

Mock::prepareDatabase()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 8
rs 10
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\Connection\ConnectionInterface;
15
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface;
16
use Yiisoft\Db\Query\Query;
17
use Yiisoft\Db\Query\QueryInterface;
18
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
19
use Yiisoft\Db\Schema\QuoterInterface;
20
use Yiisoft\Db\Schema\SchemaInterface;
21
use Yiisoft\Db\Tests\Support\Stubs\Connection;
22
use Yiisoft\Log\Logger;
23
use Yiisoft\Profiler\Profiler;
24
use Yiisoft\Profiler\ProfilerInterface;
25
26
final class Mock extends TestCase
27
{
28
    private Cache|null $cache = null;
29
    private Logger|null $logger = null;
30
    private Profiler|null $profiler = null;
31
    private QueryCache|null $queryCache = null;
32
    private SchemaCache|null $schemaCache = null;
33
34
    public function __construct()
35
    {
36
    }
37
38
    public function connection(bool $prepareDatabase = false): ConnectionInterface
39
    {
40
        $db = new Connection();
41
42
        if ($prepareDatabase) {
43
            $this->prepareDatabase($db);
44
        }
45
46
        return $db;
47
    }
48
49
    public function getDriverName(): string
50
    {
51
        return $this->connection()->getDriver()->getDriverName();
52
    }
53
54
    public function getLogger(): Logger
55
    {
56
        return $this->logger();
57
    }
58
59
    public function getProfiler(): ProfilerInterface
60
    {
61
        return $this->profiler();
62
    }
63
64
    public function getQueryCache(): QueryCache
65
    {
66
        return $this->queryCache();
67
    }
68
69
    public function getSchemaCache(): SchemaCache
70
    {
71
        return $this->schemaCache();
72
    }
73
74
    public function query(): QueryInterface
75
    {
76
        return new Query($this->connection());
77
    }
78
79
    public function queryBuilder(): QueryBuilderInterface
80
    {
81
        return $this->connection()->getQueryBuilder();
82
    }
83
84
    public function quoter(): QuoterInterface
85
    {
86
        return $this->connection()->getQuoter();
87
    }
88
89
    public function schema(): SchemaInterface
90
    {
91
        return $this->connection()->getSchema();
92
    }
93
94
    private function cache(): CacheInterface
95
    {
96
        if ($this->cache === null) {
97
            $this->cache = new Cache(new ArrayCache());
98
        }
99
100
        return $this->cache;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->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...
101
    }
102
103
    private function logger(): LoggerInterface
104
    {
105
        if ($this->logger === null) {
106
            $this->logger = new Logger();
107
        }
108
109
        return $this->logger;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->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...
110
    }
111
112
    private function prepareDatabase(ConnectionPDOInterface $db, string $fixture = __DIR__ . '/Fixture/sqlite.sql'): void
113
    {
114
        $db->open();
115
        $lines = explode(';', file_get_contents($fixture));
116
117
        foreach ($lines as $line) {
118
            if (trim($line) !== '') {
119
                $db->getPDO()?->exec($line);
120
            }
121
        }
122
    }
123
124
    private function profiler(): ProfilerInterface
125
    {
126
        if ($this->profiler === null) {
127
            $this->profiler = new Profiler($this->logger());
128
        }
129
130
        return $this->profiler;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->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...
131
    }
132
133
    private function queryCache(): QueryCache
134
    {
135
        if ($this->queryCache === null) {
136
            $this->queryCache = new QueryCache($this->cache());
137
        }
138
139
        return $this->queryCache;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->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...
140
    }
141
142
    private function schemaCache(): SchemaCache
143
    {
144
        if ($this->schemaCache === null) {
145
            $this->schemaCache = new SchemaCache($this->cache());
146
        }
147
148
        return $this->schemaCache;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->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...
149
    }
150
}
151