Passed
Pull Request — master (#380)
by Alexander
03:34 queued 01:02
created

Mock   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 88
rs 10
wmc 18

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A query() 0 3 1
A connection() 0 3 1
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 5 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\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(): ConnectionInterface
36
    {
37
        return new Connection();
38
    }
39
40
    public function getDriverName(): string
41
    {
42
        return $this->connection()->getDriver()->getDriverName();
43
    }
44
45
    public function getQueryCache(): QueryCache
46
    {
47
        return $this->queryCache();
48
    }
49
50
    public function getSchemaCache(): SchemaCache
51
    {
52
        return $this->schemaCache();
53
    }
54
55
    public function query(): QueryInterface
56
    {
57
        return new Query($this->connection());
58
    }
59
60
    public function queryBuilder(
61
        array|string $columnQuoteCharacter = '',
62
        array|string $tableQuoteCharacter = ''
63
    ): QueryBuilderInterface {
64
        return new QueryBuilder($this->quoter($columnQuoteCharacter, $tableQuoteCharacter), $this->schema());
65
    }
66
67
    public function quoter(array|string $columnQuoteCharacter, array|string $tableQuoteCharacter): QuoterInterface
68
    {
69
        return new Quoter($columnQuoteCharacter, $tableQuoteCharacter);
70
    }
71
72
    public function schema(): SchemaInterface
73
    {
74
        return new Schema($this->connection(), $this->schemaCache());
75
    }
76
77
    private function cache(): CacheInterface
78
    {
79
        if ($this->cache === null) {
80
            $this->cache = new Cache(new ArrayCache());
81
        }
82
83
        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...
84
    }
85
86
    public function prepareDatabase(ConnectionPDOInterface $db, string $fixture = __DIR__ . '/Fixture/sqlite.sql'): void
87
    {
88
        $db->open();
89
        $lines = explode(';', file_get_contents($fixture));
90
91
        foreach ($lines as $line) {
92
            if (trim($line) !== '') {
93
                $db->getPDO()?->exec($line);
94
            }
95
        }
96
    }
97
98
    private function queryCache(): QueryCache
99
    {
100
        if ($this->queryCache === null) {
101
            $this->queryCache = new QueryCache($this->cache());
102
        }
103
        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...
104
    }
105
106
    private function schemaCache(): SchemaCache
107
    {
108
        if ($this->schemaCache === null) {
109
            $this->schemaCache = new SchemaCache($this->cache());
110
        }
111
112
        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...
113
    }
114
}
115