Passed
Pull Request — master (#380)
by Wilmer
03:20 queued 35s
created

Mock   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 29
c 3
b 1
f 0
dl 0
loc 92
rs 10
wmc 19

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A query() 0 3 1
A connection() 0 9 2
A schema() 0 3 1
A prepareDatabase() 0 8 3
A quoter() 0 3 1
A cache() 0 7 2
A queryCache() 0 6 2
A getSchemaCache() 0 3 1
A getDriverName() 0 3 1
A queryBuilder() 0 3 1
A schemaCache() 0 7 2
A getQueryCache() 0 3 1
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;
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...
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;
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...
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;
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...
114
    }
115
}
116