/
ConnectionTest.php
133 lines (108 loc) · 3.65 KB
/
ConnectionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
declare(strict_types=1);
namespace Yiisoft\Db\Oracle\Tests;
use PDO;
use Throwable;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
use Yiisoft\Db\Tests\Common\CommonConnectionTest;
use Yiisoft\Db\Transaction\TransactionInterface;
/**
* @group oracle
*
* @psalm-suppress PropertyNotSetInConstructor
*/
final class ConnectionTest extends CommonConnectionTest
{
use TestTrait;
/**
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
public function testSerialize(): void
{
$db = $this->getConnection();
$db->open();
$serialized = serialize($db);
$unserialized = unserialize($serialized);
$this->assertInstanceOf(ConnectionInterface::class, $unserialized);
$this->assertSame('123', $unserialized->createCommand('SELECT 123 FROM DUAL')->queryScalar());
$db->close();
}
/**
* @throws Exception
* @throws InvalidConfigException
*/
public function testSettingDefaultAttributes(): void
{
$db = $this->getConnection();
$this->assertSame(PDO::ERRMODE_EXCEPTION, $db->getActivePDO()?->getAttribute(PDO::ATTR_ERRMODE));
$db->close();
}
/**
* @throws Exception
* @throws InvalidConfigException
* @throws NotSupportedException
* @throws Throwable
*/
public function testTransactionIsolation(): void
{
$db = $this->getConnection();
$transaction = $db->beginTransaction(TransactionInterface::READ_COMMITTED);
$transaction->commit();
/* should not be any exception so far */
$this->assertTrue(true);
$transaction = $db->beginTransaction(TransactionInterface::SERIALIZABLE);
$transaction->commit();
/* should not be any exception so far */
$this->assertTrue(true);
$db->close();
}
/**
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
public function testTransactionShortcutCustom(): void
{
$db = $this->getConnection(true);
$command = $db->createCommand();
$this->assertTrue(
$db->transaction(
static function (ConnectionInterface $db) {
$db->createCommand()->insert('profile', ['description' => 'test transaction shortcut'])->execute();
return true;
},
TransactionInterface::READ_COMMITTED,
),
'transaction shortcut valid value should be returned from callback',
);
$this->assertSame(
'1',
$command->setSql(
<<<SQL
SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction shortcut'
SQL,
)->queryScalar(),
'profile should be inserted in transaction shortcut',
);
$db->close();
}
public function testSerialized()
{
$connection = $this->getConnection();
$connection->open();
$serialized = serialize($connection);
$this->assertNotNull($connection->getPDO());
$unserialized = unserialize($serialized);
$this->assertInstanceOf(ConnectionPDOInterface::class, $unserialized);
$this->assertNull($unserialized->getPDO());
$this->assertEquals(123, $unserialized->createCommand('SELECT 123 FROM DUAL')->queryScalar());
$this->assertNotNull($connection->getPDO());
}
}