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

Mock   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 39
c 4
b 1
f 0
dl 0
loc 124
rs 10
wmc 25

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getLogger() 0 3 1
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 7 2
A getSchemaCache() 0 3 1
A logger() 0 7 2
A getDriverName() 0 3 1
A getProfiler() 0 3 1
A profiler() 0 7 2
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
use Yiisoft\Log\Logger;
22
use Yiisoft\Profiler\Profiler;
23
use Yiisoft\Profiler\ProfilerInterface;
24
25
final class Mock extends TestCase
26
{
27
    private Cache|null $cache = null;
28
    private Logger|null $logger = null;
29
    private Profiler|null $profiler = null;
30
    private QueryCache|null $queryCache = null;
31
    private SchemaCache|null $schemaCache = null;
32
33
    public function __construct()
34
    {
35
    }
36
37
    public function connection(bool $prepareDatabase = false): ConnectionInterface
38
    {
39
        $db = new Connection();
40
41
        if ($prepareDatabase) {
42
            $this->prepareDatabase($db);
43
        }
44
45
        return $db;
46
    }
47
48
    public function getDriverName(): string
49
    {
50
        return $this->connection()->getDriver()->getDriverName();
51
    }
52
53
    public function getLogger(): Logger
54
    {
55
        return $this->logger();
56
    }
57
58
    public function getProfiler(): ProfilerInterface
59
    {
60
        return $this->profiler();
61
    }
62
63
    public function getQueryCache(): QueryCache
64
    {
65
        return $this->queryCache();
66
    }
67
68
    public function getSchemaCache(): SchemaCache
69
    {
70
        return $this->schemaCache();
71
    }
72
73
    public function query(): QueryInterface
74
    {
75
        return new Query($this->connection());
76
    }
77
78
    public function queryBuilder(): QueryBuilderInterface
79
    {
80
        return $this->connection()->getQueryBuilder();
81
    }
82
83
    public function quoter(): QuoterInterface
84
    {
85
        return $this->connection()->getQuoter();
86
    }
87
88
    public function schema(): SchemaInterface
89
    {
90
        return $this->connection()->getSchema();
91
    }
92
93
    private function cache(): CacheInterface
94
    {
95
        if ($this->cache === null) {
96
            $this->cache = new Cache(new ArrayCache());
97
        }
98
99
        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...
100
    }
101
102
    private function logger(): LoggerInterface
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Tests\Support\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
103
    {
104
        if ($this->logger === null) {
105
            $this->logger = new Logger();
106
        }
107
108
        return $this->logger;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->logger returns the type Yiisoft\Log\Logger|null which is incompatible with the type-hinted return Yiisoft\Db\Tests\Support\LoggerInterface.
Loading history...
109
    }
110
111
    private function prepareDatabase(ConnectionPDOInterface $db, string $fixture = __DIR__ . '/Fixture/sqlite.sql'): void
112
    {
113
        $db->open();
114
        $lines = explode(';', file_get_contents($fixture));
115
116
        foreach ($lines as $line) {
117
            if (trim($line) !== '') {
118
                $db->getPDO()?->exec($line);
119
            }
120
        }
121
    }
122
123
    private function profiler(): ProfilerInterface
124
    {
125
        if ($this->profiler === null) {
126
            $this->profiler = new Profiler($this->createLogger());
0 ignored issues
show
Bug introduced by
The method createLogger() does not exist on Yiisoft\Db\Tests\Support\Mock. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
            $this->profiler = new Profiler($this->/** @scrutinizer ignore-call */ createLogger());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127
        }
128
129
        return $this->profiler;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->profiler could return the type null which is incompatible with the type-hinted return Yiisoft\Profiler\ProfilerInterface. Consider adding an additional type-check to rule them out.
Loading history...
130
    }
131
132
133
    private function queryCache(): QueryCache
134
    {
135
        if ($this->queryCache === null) {
136
            $this->queryCache = new QueryCache($this->cache());
137
        }
138
139
        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...
140
    }
141
142
    private function schemaCache(): SchemaCache
143
    {
144
        if ($this->schemaCache === null) {
145
            $this->schemaCache = new SchemaCache($this->cache());
146
        }
147
148
        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...
149
    }
150
}
151