|
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
|
|
|
|
|
22
|
|
|
final class Mock extends TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
private Cache|null $cache = null; |
|
25
|
|
|
private QueryCache|null $queryCache = null; |
|
26
|
|
|
private SchemaCache|null $schemaCache = null; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct() |
|
29
|
|
|
{ |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function connection(bool $prepareDatabase = false): ConnectionInterface |
|
33
|
|
|
{ |
|
34
|
|
|
$db = new Connection(); |
|
35
|
|
|
|
|
36
|
|
|
if ($prepareDatabase) { |
|
37
|
|
|
$this->prepareDatabase($db); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $db; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getDriverName(): string |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->connection()->getDriver()->getDriverName(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getQueryCache(): QueryCache |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->queryCache(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getSchemaCache(): SchemaCache |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->schemaCache(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function query(): QueryInterface |
|
59
|
|
|
{ |
|
60
|
|
|
return new Query($this->connection()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function queryBuilder(): QueryBuilderInterface |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->connection()->getQueryBuilder(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function quoter(): QuoterInterface |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->connection()->getQuoter(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function schema(): SchemaInterface |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->connection()->getSchema(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function cache(): CacheInterface |
|
79
|
|
|
{ |
|
80
|
|
|
if ($this->cache === null) { |
|
81
|
|
|
$this->cache = new Cache(new ArrayCache()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this->cache; |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
private function prepareDatabase(ConnectionPDOInterface $db, string $fixture = __DIR__ . '/Fixture/sqlite.sql'): void |
|
88
|
|
|
{ |
|
89
|
|
|
$db->open(); |
|
90
|
|
|
$lines = explode(';', file_get_contents($fixture)); |
|
91
|
|
|
|
|
92
|
|
|
foreach ($lines as $line) { |
|
93
|
|
|
if (trim($line) !== '') { |
|
94
|
|
|
$db->getPDO()?->exec($line); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private function queryCache(): QueryCache |
|
100
|
|
|
{ |
|
101
|
|
|
if ($this->queryCache === null) { |
|
102
|
|
|
$this->queryCache = new QueryCache($this->cache()); |
|
103
|
|
|
} |
|
104
|
|
|
return $this->queryCache; |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
private function schemaCache(): SchemaCache |
|
108
|
|
|
{ |
|
109
|
|
|
if ($this->schemaCache === null) { |
|
110
|
|
|
$this->schemaCache = new SchemaCache($this->cache()); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $this->schemaCache; |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|