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

AbstractConnectionPDOTest::testClone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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