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

Mock::query()   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(
67
        array|string $columnQuoteCharacter = '',
68
        array|string $tableQuoteCharacter = ''
69
    ): QueryBuilderInterface {
70
        return new QueryBuilder($this->quoter($columnQuoteCharacter, $tableQuoteCharacter), $this->schema());
71
    }
72
73
    public function quoter(array|string $columnQuoteCharacter, array|string $tableQuoteCharacter): QuoterInterface
74
    {
75
        return new Quoter($columnQuoteCharacter, $tableQuoteCharacter);
76
    }
77
78
    public function schema(): SchemaInterface
79
    {
80
        return new Schema($this->connection(), $this->schemaCache());
81
    }
82
83
    private function cache(): CacheInterface
84
    {
85
        if ($this->cache === null) {
86
            $this->cache = new Cache(new ArrayCache());
87
        }
88
89
        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...
90
    }
91
92
    private function prepareDatabase(ConnectionPDOInterface $db, string $fixture = __DIR__ . '/Fixture/sqlite.sql'): void
93
    {
94
        $db->open();
95
        $lines = explode(';', file_get_contents($fixture));
96
97
        foreach ($lines as $line) {
98
            if (trim($line) !== '') {
99
                $db->getPDO()?->exec($line);
100
            }
101
        }
102
    }
103
104
    private function queryCache(): QueryCache
105
    {
106
        if ($this->queryCache === null) {
107
            $this->queryCache = new QueryCache($this->cache());
108
        }
109
        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...
110
    }
111
112
    private function schemaCache(): SchemaCache
113
    {
114
        if ($this->schemaCache === null) {
115
            $this->schemaCache = new SchemaCache($this->cache());
116
        }
117
118
        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...
119
    }
120
}
121