QueryTestCase::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\TestCase\Database;
6
7
use Opulence\Databases\Adapters\Pdo\Connection;
8
use Opulence\Databases\ConnectionPools\ConnectionPool;
9
use Opulence\Databases\IConnection;
10
use PHPUnit\Framework\MockObject\MockObject;
11
use PHPUnit\Framework\TestCase;
12
13
abstract class QueryTestCase extends TestCase
14
{
15
    /** @var IConnection|MockObject */
16
    protected $readConnectionMock;
17
18
    /** @var IConnection|MockObject */
19
    protected $writeConnectionMock;
20
21
    /** @var ConnectionPool|MockObject */
22
    protected $connectionPoolMock;
23
24
    public function setUp(): void
25
    {
26
        parent::setUp();
27
28
        $this->readConnectionMock  = $this->getReadConnectionMock();
29
        $this->writeConnectionMock = $this->getWriteConnectionMock();
30
31
        $this->connectionPoolMock = $this->getConnectionPoolMock($this->readConnectionMock, $this->writeConnectionMock);
32
    }
33
34
    /**
35
     * @return IConnection|MockObject
36
     */
37
    protected function getReadConnectionMock()
38
    {
39
        return $this->createMock(Connection::class);
40
    }
41
42
    /**
43
     * @return IConnection|MockObject
44
     */
45
    protected function getWriteConnectionMock()
46
    {
47
        return $this->createMock(Connection::class);
48
    }
49
50
    /**
51
     * @param IConnection|null $readConnection
52
     * @param IConnection|null $writeConnection
53
     *
54
     * @return ConnectionPool|MockObject
55
     */
56
    protected function getConnectionPoolMock(?IConnection $readConnection, ?IConnection $writeConnection)
57
    {
58
        $connectionPool = $this->createMock(ConnectionPool::class);
59
60
        if ($readConnection) {
61
            $connectionPool->expects($this->any())->method('getReadConnection')->willReturn($readConnection);
62
        }
63
64
        if ($writeConnection) {
65
            $connectionPool->expects($this->any())->method('getWriteConnection')->willReturn($writeConnection);
66
        }
67
68
        return $connectionPool;
69
    }
70
}
71