1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Pgsql\Tests; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Db\Drivers\Connection; |
8
|
|
|
use Yiisoft\Db\Helper\Dsn; |
9
|
|
|
use Yiisoft\Db\Transactions\Transaction; |
10
|
|
|
use Yiisoft\Db\Tests\ConnectionTest as AbstractConnectionTest; |
11
|
|
|
|
12
|
|
|
class ConnectionTest extends AbstractConnectionTest |
13
|
|
|
{ |
14
|
|
|
protected ?string $driverName = 'pgsql'; |
15
|
|
|
|
16
|
|
|
public function testConnection(): void |
17
|
|
|
{ |
18
|
|
|
$this->assertIsObject($this->getConnection(true)); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testDsnHelper(): void |
22
|
|
|
{ |
23
|
|
|
$dsn = new Dsn('pgsql', '127.0.0.1', 'yiitest', '5432'); |
24
|
|
|
|
25
|
|
|
$connection = new Connection($this->cache, $this->logger, $this->profiler, $dsn->getDsn()); |
26
|
|
|
|
27
|
|
|
$this->assertEquals('pgsql:host=127.0.0.1;dbname=yiitest;port=5432', $connection->getDsn()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testQuoteValue(): void |
31
|
|
|
{ |
32
|
|
|
$connection = $this->getConnection(false); |
33
|
|
|
$this->assertEquals(123, $connection->quoteValue(123)); |
34
|
|
|
$this->assertEquals("'string'", $connection->quoteValue('string')); |
35
|
|
|
$this->assertEquals("'It''s interesting'", $connection->quoteValue("It's interesting")); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testQuoteTableName(): void |
39
|
|
|
{ |
40
|
|
|
$connection = $this->getConnection(false); |
41
|
|
|
$this->assertEquals('"table"', $connection->quoteTableName('table')); |
42
|
|
|
$this->assertEquals('"table"', $connection->quoteTableName('"table"')); |
43
|
|
|
$this->assertEquals('"schema"."table"', $connection->quoteTableName('schema.table')); |
44
|
|
|
$this->assertEquals('"schema"."table"', $connection->quoteTableName('schema."table"')); |
45
|
|
|
$this->assertEquals('"schema"."table"', $connection->quoteTableName('"schema"."table"')); |
46
|
|
|
$this->assertEquals('{{table}}', $connection->quoteTableName('{{table}}')); |
47
|
|
|
$this->assertEquals('(table)', $connection->quoteTableName('(table)')); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testQuoteColumnName(): void |
51
|
|
|
{ |
52
|
|
|
$connection = $this->getConnection(false); |
53
|
|
|
$this->assertEquals('"column"', $connection->quoteColumnName('column')); |
54
|
|
|
$this->assertEquals('"column"', $connection->quoteColumnName('"column"')); |
55
|
|
|
$this->assertEquals('[[column]]', $connection->quoteColumnName('[[column]]')); |
56
|
|
|
$this->assertEquals('{{column}}', $connection->quoteColumnName('{{column}}')); |
57
|
|
|
$this->assertEquals('(column)', $connection->quoteColumnName('(column)')); |
58
|
|
|
|
59
|
|
|
$this->assertEquals('"column"', $connection->quoteSql('[[column]]')); |
60
|
|
|
$this->assertEquals('"column"', $connection->quoteSql('{{column}}')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testQuoteFullColumnName(): void |
64
|
|
|
{ |
65
|
|
|
$connection = $this->getConnection(false, false); |
66
|
|
|
$this->assertEquals('"table"."column"', $connection->quoteColumnName('table.column')); |
67
|
|
|
$this->assertEquals('"table"."column"', $connection->quoteColumnName('table."column"')); |
68
|
|
|
$this->assertEquals('"table"."column"', $connection->quoteColumnName('"table".column')); |
69
|
|
|
$this->assertEquals('"table"."column"', $connection->quoteColumnName('"table"."column"')); |
70
|
|
|
|
71
|
|
|
$this->assertEquals('[[table.column]]', $connection->quoteColumnName('[[table.column]]')); |
72
|
|
|
$this->assertEquals('{{table}}."column"', $connection->quoteColumnName('{{table}}.column')); |
73
|
|
|
$this->assertEquals('{{table}}."column"', $connection->quoteColumnName('{{table}}."column"')); |
74
|
|
|
$this->assertEquals('{{table}}.[[column]]', $connection->quoteColumnName('{{table}}.[[column]]')); |
75
|
|
|
$this->assertEquals('{{%table}}."column"', $connection->quoteColumnName('{{%table}}.column')); |
76
|
|
|
$this->assertEquals('{{%table}}."column"', $connection->quoteColumnName('{{%table}}."column"')); |
77
|
|
|
|
78
|
|
|
$this->assertEquals('"table"."column"', $connection->quoteSql('[[table.column]]')); |
79
|
|
|
$this->assertEquals('"table"."column"', $connection->quoteSql('{{table}}.[[column]]')); |
80
|
|
|
$this->assertEquals('"table"."column"', $connection->quoteSql('{{table}}."column"')); |
81
|
|
|
$this->assertEquals('"table"."column"', $connection->quoteSql('{{%table}}.[[column]]')); |
82
|
|
|
$this->assertEquals('"table"."column"', $connection->quoteSql('{{%table}}."column"')); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testTransactionIsolation(): void |
86
|
|
|
{ |
87
|
|
|
$connection = $this->getConnection(true); |
88
|
|
|
|
89
|
|
|
$transaction = $connection->beginTransaction(); |
90
|
|
|
$transaction->setIsolationLevel(Transaction::READ_UNCOMMITTED); |
91
|
|
|
$transaction->commit(); |
92
|
|
|
|
93
|
|
|
$transaction = $connection->beginTransaction(); |
94
|
|
|
$transaction->setIsolationLevel(Transaction::READ_COMMITTED); |
95
|
|
|
$transaction->commit(); |
96
|
|
|
|
97
|
|
|
$transaction = $connection->beginTransaction(); |
98
|
|
|
$transaction->setIsolationLevel(Transaction::REPEATABLE_READ); |
99
|
|
|
$transaction->commit(); |
100
|
|
|
|
101
|
|
|
$transaction = $connection->beginTransaction(); |
102
|
|
|
$transaction->setIsolationLevel(Transaction::SERIALIZABLE); |
103
|
|
|
$transaction->commit(); |
104
|
|
|
|
105
|
|
|
$transaction = $connection->beginTransaction(); |
106
|
|
|
$transaction->setIsolationLevel(Transaction::SERIALIZABLE . ' READ ONLY DEFERRABLE'); |
107
|
|
|
$transaction->commit(); |
108
|
|
|
|
109
|
|
|
$this->assertTrue(true); // No error occurred – assert passed. |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|