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

AbstractConnectionTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
dl 0
loc 94
rs 10
c 1
b 0
f 0
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 testTransactionShortcutException() 0 20 1
A testCreateCommand() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests;
6
7
use PHPUnit\Framework\TestCase;
8
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface;
9
use Yiisoft\Db\Exception\Exception;
10
use Yiisoft\Db\Query\BatchQueryResult;
11
use Yiisoft\Db\Query\Query;
12
use Yiisoft\Db\Tests\Support\Assert;
13
use Yiisoft\Db\Tests\Support\TestTrait;
14
15
abstract class AbstractConnectionTest extends TestCase
16
{
17
    use TestTrait;
18
19
    public function testCacheKey(): void
20
    {
21
        $db = $this->getConnection();
22
23
        $driver = $db->getDriver();
24
25
        $this->assertEquals([$driver->getDsn(), $driver->getUsername()], $db->getCacheKey());
26
    }
27
28
    /**
29
     * @throws \Exception
30
     */
31
    public function testConnection(): void
32
    {
33
        $this->assertInstanceOf(ConnectionPDOInterface::class, $this->getConnection());
34
    }
35
36
    public function testCreateBatchQueryResult(): void
37
    {
38
        $db = $this->getConnection();
39
40
        $query = (new Query($db))->from('customer');
41
42
        $this->assertInstanceOf(BatchQueryResult::class, $db->createBatchQueryResult($query));
43
    }
44
45
    public function testCreateCommand(): void
46
    {
47
        $db = $this->getConnection();
48
49
        $sql = <<<SQL
50
        SELECT * FROM customer
51
        SQL;
52
53
        $params = ['id' => 1];
54
        $command = $db->createCommand($sql, $params);
55
56
        $this->assertSame($sql, $command->getSql());
57
        $this->assertSame($params, $command->getParams());
58
    }
59
60
    public function testGetName(): void
61
    {
62
        $db = $this->getConnection();
63
64
        $this->assertSame('sqlite', $db->getName());
65
    }
66
67
    public function testNotProfiler(): void
68
    {
69
        $db = $this->getConnection();
70
71
        $profiler = $this->getProfiler();
72
73
        $this->assertNull(Assert::getInaccessibleProperty($db, 'profiler'));
74
75
        $db->setProfiler($profiler);
76
77
        $this->assertSame($profiler, Assert::getInaccessibleProperty($db, 'profiler'));
78
    }
79
80
    public function testSetTablePrefix(): void
81
    {
82
        $db = $this->getConnection();
83
84
        $db->setTablePrefix('pre_');
85
86
        $this->assertSame('pre_', $db->getTablePrefix());
87
    }
88
89
    public function testTransactionShortcutException(): void
90
    {
91
        $db = $this->getConnectionWithData();
92
93
        $this->expectException(Exception::class);
94
95
        $db->transaction(
96
            static function () use ($db) {
97
                $db->createCommand()->insert('profile', ['description' => 'test transaction shortcut'])->execute();
98
99
                throw new Exception('Exception in transaction shortcut');
100
            }
101
        );
102
        $profilesCount = $db->createCommand(
103
            <<<SQL
104
            SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction shortcut'
105
            SQL
106
        )->queryScalar();
107
108
        $this->assertSame(0, $profilesCount, 'profile should not be inserted in transaction shortcut');
109
    }
110
}
111