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

AbstractConnectionTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 97
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testSetTablePrefix() 0 7 1
A testCacheKey() 0 7 1
A testCreateBatchQueryResult() 0 7 1
A testNotProfiler() 0 11 1
A testGetName() 0 5 1
A testConnection() 0 3 1
A testNestedTransactionNotSupported() 0 14 1
A testCreateCommand() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests;
6
7
use Exception;
8
use PHPUnit\Framework\TestCase;
9
use Throwable;
10
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface;
11
use Yiisoft\Db\Exception\InvalidConfigException;
12
use Yiisoft\Db\Exception\NotSupportedException;
13
use Yiisoft\Db\Query\BatchQueryResult;
14
use Yiisoft\Db\Query\Query;
15
use Yiisoft\Db\Tests\Support\Assert;
16
use Yiisoft\Db\Tests\Support\DbHelper;
17
use Yiisoft\Db\Tests\Support\TestTrait;
18
19
abstract class AbstractConnectionTest extends TestCase
20
{
21
    use TestTrait;
22
23
    public function testCacheKey(): void
24
    {
25
        $db = $this->getConnection();
26
27
        $driver = $db->getDriver();
28
29
        $this->assertEquals([$driver->getDsn(), $driver->getUsername()], $db->getCacheKey());
30
    }
31
32
    /**
33
     * @throws Exception
34
     */
35
    public function testConnection(): void
36
    {
37
        $this->assertInstanceOf(ConnectionPDOInterface::class, $this->getConnection());
38
    }
39
40
    public function testCreateBatchQueryResult(): void
41
    {
42
        $db = $this->getConnection();
43
44
        $query = (new Query($db))->from('customer');
45
46
        $this->assertInstanceOf(BatchQueryResult::class, $db->createBatchQueryResult($query));
47
    }
48
49
    /**
50
     * @throws InvalidConfigException
51
     * @throws \Yiisoft\Db\Exception\Exception
52
     */
53
    public function testCreateCommand(): void
54
    {
55
        $db = $this->getConnection();
56
57
        $sql = <<<SQL
58
        SELECT * FROM customer
59
        SQL;
60
61
        $params = ['id' => 1];
62
        $command = $db->createCommand($sql, $params);
63
64
        $this->assertSame($sql, $command->getSql());
65
        $this->assertSame($params, $command->getParams());
66
    }
67
68
    public function testGetName(): void
69
    {
70
        $db = $this->getConnection();
71
72
        $this->assertSame($db->getName(), $db->getDriver()->getDriverName());
73
    }
74
75
    /**
76
     * @throws Throwable
77
     */
78
    public function testNestedTransactionNotSupported(): void
79
    {
80
        $db = $this->getConnection();
81
82
        $db->setEnableSavepoint(false);
83
84
        $this->assertFalse($db->isSavepointEnabled());
85
86
        $db->transaction(
87
            function (ConnectionPDOInterface $db) {
88
                $this->assertNotNull($db->getTransaction());
89
                $this->expectException(NotSupportedException::class);
90
91
                $db->beginTransaction();
92
            }
93
        );
94
    }
95
96
    public function testNotProfiler(): void
97
    {
98
        $db = $this->getConnection();
99
100
        $profiler = DbHelper::getProfiler();
101
102
        $this->assertNull(Assert::getInaccessibleProperty($db, 'profiler'));
103
104
        $db->setProfiler($profiler);
105
106
        $this->assertSame($profiler, Assert::getInaccessibleProperty($db, 'profiler'));
107
    }
108
109
    public function testSetTablePrefix(): void
110
    {
111
        $db = $this->getConnection();
112
113
        $db->setTablePrefix('pre_');
114
115
        $this->assertSame('pre_', $db->getTablePrefix());
116
    }
117
}
118