Passed
Pull Request — master (#395)
by Wilmer
04:09 queued 01:23
created

TestTrait::getConnection()   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 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Support;
6
7
use Yiisoft\Cache\ArrayCache;
8
use Yiisoft\Cache\Cache;
9
use Yiisoft\Cache\CacheInterface;
10
use Yiisoft\Db\Cache\QueryCache;
11
use Yiisoft\Db\Cache\SchemaCache;
12
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface;
13
use Yiisoft\Db\Tests\Support\Stub\PDODriver;
14
15
trait TestTrait
16
{
17
    private CacheInterface|null $cache = null;
18
    private QueryCache|null $queryCache = null;
19
    private SchemaCache|null $schemaCache = null;
20
21
    protected function getConnection(string $dsn = 'sqlite::memory:'): ConnectionPDOInterface
22
    {
23
        return new Stub\Connection(new PDODriver($dsn), $this->getQueryCache(), $this->getSchemaCache());
24
    }
25
26
    private function getCache(): CacheInterface
27
    {
28
        if ($this->cache === null) {
29
            $this->cache = new Cache(new ArrayCache());
30
        }
31
32
        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...
33
    }
34
35
    private function getQueryCache(): QueryCache
36
    {
37
        if ($this->queryCache === null) {
38
            $this->queryCache = new QueryCache($this->getCache());
39
        }
40
41
        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...
42
    }
43
44
    private function getSchemaCache(): SchemaCache
45
    {
46
        if ($this->schemaCache === null) {
47
            $this->schemaCache = new SchemaCache($this->getCache());
48
        }
49
50
        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...
51
    }
52
}
53