|
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; |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function logger(): LoggerInterface |
|
104
|
|
|
{ |
|
105
|
|
|
if ($this->logger === null) { |
|
106
|
|
|
$this->logger = new Logger(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $this->logger; |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|