Passed
Pull Request — master (#380)
by Wilmer
02:34
created

Mock::schema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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