Passed
Push — master ( b0ca68...10680f )
by Wilmer
27:02 queued 23:37
created

AbstractConnectionPDOTest::testGetServerVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests;
6
7
use PDO;
8
use PHPUnit\Framework\TestCase;
9
use Yiisoft\Db\Driver\PDO\PDODriverInterface;
10
use Yiisoft\Db\Exception\Exception;
11
use Yiisoft\Db\Exception\InvalidConfigException;
12
use Yiisoft\Db\Tests\Support\Assert;
13
use Yiisoft\Db\Tests\Support\DbHelper;
14
use Yiisoft\Db\Tests\Support\TestTrait;
15
16
abstract class AbstractConnectionPDOTest extends TestCase
17
{
18
    use TestTrait;
19
20
    public function testClone(): void
21
    {
22
        $db = $this->getConnection();
23
24
        $db2 = clone $db;
25
26
        $this->assertNotSame($db, $db2);
27
        $this->assertNull($db2->getTransaction());
28
        $this->assertNull($db2->getPDO());
29
    }
30
31
    public function testGetDriver(): void
32
    {
33
        $driver = $this->getConnection()->getDriver();
34
35
        $this->assertInstanceOf(PDODriverInterface::class, $driver);
36
    }
37
38
    public function testGetServerVersion(): void
39
    {
40
        $db = $this->getConnection();
41
42
        $this->assertIsString($db->getServerVersion());
43
    }
44
45
    /**
46
     * @throws Exception
47
     * @throws InvalidConfigException
48
     */
49
    public function testOpenClose(): void
50
    {
51
        $db = $this->getConnection();
52
53
        $this->assertFalse($db->isActive());
54
        $this->assertNull($db->getPDO());
55
56
        $db->open();
57
58
        $this->assertTrue($db->isActive());
59
        $this->assertInstanceOf(PDO::class, $db->getPDO());
60
61
        $db->close();
62
63
        $this->assertFalse($db->isActive());
64
        $this->assertNull($db->getPDO());
65
66
        $this->setDsn('unknown::memory:');
67
68
        $db = $this->getConnection();
69
70
        $this->expectException(Exception::class);
71
        $this->expectExceptionMessage('could not find driver');
72
73
        $db->open();
74
    }
75
76
    /**
77
     * @throws Exception
78
     * @throws InvalidConfigException
79
     */
80
    public function testOpenCloseWithLogger(): void
81
    {
82
        $db = $this->getConnection();
83
84
        $this->assertFalse($db->isActive());
85
        $this->assertNull($db->getPDO());
86
87
        $db->open();
88
89
        $this->assertTrue($db->isActive());
90
        $this->assertInstanceOf(PDO::class, $db->getPDO());
91
92
        $logger = DbHelper::getLogger();
93
        $db->setLogger($logger);
94
        $logger->flush();
95
        $db->close();
96
97
        $this->assertCount(1, Assert::getInaccessibleProperty($logger, 'messages'));
98
        $this->assertFalse($db->isActive());
99
        $this->assertNull($db->getPDO());
100
101
        $this->setDsn('unknown::memory:');
102
103
        $db = $this->getConnection();
104
105
        $this->expectException(Exception::class);
106
        $this->expectExceptionMessage('could not find driver');
107
108
        $db->open();
109
    }
110
111
    public function testQuoteValueNotString(): void
112
    {
113
        $db = $this->getConnection();
114
115
        $value = $db->quoteValue(1);
116
117
        $this->assertSame(1, $value);
118
    }
119
120
    public function testSetEmulatePrepare(): void
121
    {
122
        $db = $this->getConnection();
123
124
        $this->assertNull($db->getEmulatePrepare());
125
126
        $db->setEmulatePrepare(true);
127
128
        $this->assertTrue($db->getEmulatePrepare());
129
    }
130
}
131