Passed
Pull Request — master (#377)
by Alexander
04:31 queued 01:54
created

Mock::getDriverName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
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\SchemaCache;
12
use Yiisoft\Db\Connection\ConnectionInterface;
13
use Yiisoft\Db\Query\Query;
14
use Yiisoft\Db\Query\QueryInterface;
15
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
16
use Yiisoft\Db\Schema\Quoter;
17
use Yiisoft\Db\Schema\QuoterInterface;
18
use Yiisoft\Db\Schema\SchemaInterface;
19
use Yiisoft\Db\Tests\Support\Stubs\QueryBuilder;
20
use Yiisoft\Db\Tests\Support\Stubs\Schema;
21
22
final class Mock extends TestCase
23
{
24
    private Cache|null $cache = null;
25
    private SchemaCache|null $schemaCache = null;
26
27
    public function __construct(private string $driverName = '')
28
    {
29
    }
30
31
    public function connection(): ConnectionInterface
32
    {
33
        return $this->createMock(ConnectionInterface::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createMock...ectionInterface::class) returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return Yiisoft\Db\Connection\ConnectionInterface.
Loading history...
34
    }
35
36
    public function getDriverName(): string
37
    {
38
        return $this->driverName;
39
    }
40
41
    public function query(): QueryInterface
42
    {
43
        return new Query($this->connection());
44
    }
45
46
    public function queryBuilder(
47
        array|string $columnQuoteCharacter = '',
48
        array|string $tableQuoteCharacter = ''
49
    ): QueryBuilderInterface {
50
        return new QueryBuilder($this->quoter($columnQuoteCharacter, $tableQuoteCharacter), $this->schema());
51
    }
52
53
    public function quoter(array|string $columnQuoteCharacter, array|string $tableQuoteCharacter): QuoterInterface
54
    {
55
        return new Quoter($columnQuoteCharacter, $tableQuoteCharacter);
56
    }
57
58
    public function schema(): SchemaInterface
59
    {
60
        return new Schema($this->connection(), $this->schemaCache());
61
    }
62
63
    private function cache(): CacheInterface
64
    {
65
        if ($this->cache === null) {
66
            $this->cache = new Cache(new ArrayCache());
67
        }
68
69
        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...
70
    }
71
72
    private function schemaCache(): SchemaCache
73
    {
74
        if ($this->schemaCache === null) {
75
            $this->schemaCache = new SchemaCache($this->cache());
76
        }
77
78
        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...
79
    }
80
}
81