1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Tests\Support; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Yiisoft\Cache\ArrayCache; |
9
|
|
|
use Yiisoft\Cache\Cache; |
10
|
|
|
use Yiisoft\Cache\CacheInterface; |
11
|
|
|
use Yiisoft\Db\Cache\QueryCache; |
12
|
|
|
use Yiisoft\Db\Cache\SchemaCache; |
13
|
|
|
use Yiisoft\Db\Connection\ConnectionInterface; |
14
|
|
|
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface; |
15
|
|
|
use Yiisoft\Db\Query\Query; |
16
|
|
|
use Yiisoft\Db\Query\QueryInterface; |
17
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
18
|
|
|
use Yiisoft\Db\Schema\QuoterInterface; |
19
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
20
|
|
|
use Yiisoft\Db\Tests\Support\Stubs\Connection; |
21
|
|
|
use Yiisoft\Log\Logger; |
22
|
|
|
use Yiisoft\Profiler\Profiler; |
23
|
|
|
use Yiisoft\Profiler\ProfilerInterface; |
24
|
|
|
|
25
|
|
|
final class Mock extends TestCase |
26
|
|
|
{ |
27
|
|
|
private Cache|null $cache = null; |
28
|
|
|
private Logger|null $logger = null; |
29
|
|
|
private Profiler|null $profiler = null; |
30
|
|
|
private QueryCache|null $queryCache = null; |
31
|
|
|
private SchemaCache|null $schemaCache = null; |
32
|
|
|
|
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function connection(bool $prepareDatabase = false): ConnectionInterface |
38
|
|
|
{ |
39
|
|
|
$db = new Connection(); |
40
|
|
|
|
41
|
|
|
if ($prepareDatabase) { |
42
|
|
|
$this->prepareDatabase($db); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $db; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getDriverName(): string |
49
|
|
|
{ |
50
|
|
|
return $this->connection()->getDriver()->getDriverName(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getLogger(): Logger |
54
|
|
|
{ |
55
|
|
|
return $this->logger(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getProfiler(): ProfilerInterface |
59
|
|
|
{ |
60
|
|
|
return $this->profiler(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getQueryCache(): QueryCache |
64
|
|
|
{ |
65
|
|
|
return $this->queryCache(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getSchemaCache(): SchemaCache |
69
|
|
|
{ |
70
|
|
|
return $this->schemaCache(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function query(): QueryInterface |
74
|
|
|
{ |
75
|
|
|
return new Query($this->connection()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function queryBuilder(): QueryBuilderInterface |
79
|
|
|
{ |
80
|
|
|
return $this->connection()->getQueryBuilder(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function quoter(): QuoterInterface |
84
|
|
|
{ |
85
|
|
|
return $this->connection()->getQuoter(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function schema(): SchemaInterface |
89
|
|
|
{ |
90
|
|
|
return $this->connection()->getSchema(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function cache(): CacheInterface |
94
|
|
|
{ |
95
|
|
|
if ($this->cache === null) { |
96
|
|
|
$this->cache = new Cache(new ArrayCache()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->cache; |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function logger(): LoggerInterface |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
if ($this->logger === null) { |
105
|
|
|
$this->logger = new Logger(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->logger; |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function prepareDatabase(ConnectionPDOInterface $db, string $fixture = __DIR__ . '/Fixture/sqlite.sql'): void |
112
|
|
|
{ |
113
|
|
|
$db->open(); |
114
|
|
|
$lines = explode(';', file_get_contents($fixture)); |
115
|
|
|
|
116
|
|
|
foreach ($lines as $line) { |
117
|
|
|
if (trim($line) !== '') { |
118
|
|
|
$db->getPDO()?->exec($line); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
private function profiler(): ProfilerInterface |
124
|
|
|
{ |
125
|
|
|
if ($this->profiler === null) { |
126
|
|
|
$this->profiler = new Profiler($this->createLogger()); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this->profiler; |
|
|
|
|
130
|
|
|
} |
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
|
|
|
|