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

Mock   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
eloc 39
c 5
b 2
f 0
dl 0
loc 123
rs 10
wmc 25

17 Methods

Rating   Name   Duplication   Size   Complexity  
A schemaCache() 0 7 2
A getLogger() 0 3 1
A __construct() 0 2 1
A query() 0 3 1
A connection() 0 9 2
A schema() 0 3 1
A prepareDatabase() 0 8 3
A quoter() 0 3 1
A cache() 0 7 2
A queryCache() 0 7 2
A getSchemaCache() 0 3 1
A logger() 0 7 2
A getDriverName() 0 3 1
A getProfiler() 0 3 1
A profiler() 0 7 2
A queryBuilder() 0 3 1
A getQueryCache() 0 3 1
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