Repository navigation
Expand file tree
/
Copy pathConnectionTest.php
More file actions
154 lines (125 loc) · 4.97 KB
/
Copy pathConnectionTest.php
File metadata and controls
154 lines (125 loc) · 4.97 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Database\PgSQL\DBAL;
use Hyperf\Database\PgSQL\DBAL\Connection;
use Hyperf\Database\PgSQL\DBAL\Result;
use Hyperf\Database\PgSQL\DBAL\Statement;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\TestCase;
use Swoole\Coroutine\PostgreSQL;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
#[RequiresPhpExtension('swoole', '< 6.0')]
class ConnectionTest extends TestCase
{
protected Connection $connection;
public function setUp(): void
{
$pgsql = new PostgreSQL();
$connected = $pgsql->connect('host=127.0.0.1 port=5432 dbname=postgres user=postgres password=postgres');
if (! $connected) {
$this->fail('Failed to connect to PostgreSQL server.');
}
$this->connection = new Connection($pgsql);
$pgsql->query('CREATE TABLE IF NOT EXISTS connection_tests_for_hyperf( id SERIAL constraint id primary key, val1 varchar(255) not null, val2 varchar(255) not null);');
}
public function tearDown(): void
{
$this->connection->exec('DROP TABLE IF EXISTS connection_tests_for_hyperf');
}
public function testExec()
{
$sql = sprintf('insert into "connection_tests_for_hyperf" ("val1", "val2") values (\'%s\', \'%s\');', uniqid(), uniqid());
$this->assertGreaterThan(0, $this->connection->exec($sql));
}
public function testPrepare()
{
$stmt = $this->connection->prepare('select * from "connection_tests_for_hyperf" where "val1" = $1');
$this->assertInstanceOf(Statement::class, $stmt);
}
public function testQuery()
{
$this->insertOneRow();
$result = $this->connection->query('select * from "connection_tests_for_hyperf"');
$this->assertInstanceOf(Result::class, $result);
$this->assertGreaterThan(0, $result->rowCount());
}
public function testLastInsertId()
{
$this->insertOneRow();
$one = $this->connection->lastInsertId();
$this->assertIsNumeric($one);
$this->assertGreaterThan(0, $one);
$this->insertOneRow();
$two = $this->connection->lastInsertId();
$this->assertIsNumeric($two);
$this->assertGreaterThan($one, $two);
$this->assertEquals($two, $this->connection->lastInsertId('connection_tests_for_hyperf_id_seq'));
}
public function testTransactions()
{
$count = $this->getRowCount();
$this->connection->beginTransaction();
$this->insertOneRow();
$this->connection->rollBack();
$this->assertEquals($count, $this->getRowCount());
$this->connection->beginTransaction();
$this->insertOneRow();
$this->connection->commit();
$this->assertEquals($count + 1, $this->getRowCount());
}
public function testQuote()
{
$this->assertSame("'hyperf'", $this->connection->quote('hyperf'));
}
public function testGetServerVersion()
{
$this->assertMatchesRegularExpression('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $this->connection->getServerVersion());
}
public function testGetNativeConnection()
{
$this->assertInstanceOf(PostgreSQL::class, $this->connection->getNativeConnection());
}
public function testResult()
{
$insertResult = $this->connection
->prepare('insert into "connection_tests_for_hyperf" ("val1", "val2") values ($1, $2)')
->execute([$val1 = uniqid(), $val2 = uniqid()]);
$this->assertEquals(1, $insertResult->rowCount());
$result = $this->connection
->prepare('select val1, val2, id from "connection_tests_for_hyperf" where "val1" = $1')
->execute([$val1]);
$row = $result->fetchNumeric();
$id = $row[2];
$this->assertSame([$val1, $val2, $id], $row);
$this->assertSame(['val1' => $val1, 'val2' => $val2, 'id' => $id], $result->fetchAssociative());
$this->assertSame($val1, $result->fetchOne());
$this->assertSame([[$val1, $val2, $id]], $result->fetchAllNumeric());
$this->assertSame([['val1' => $val1, 'val2' => $val2, 'id' => $id]], $result->fetchAllAssociative());
$this->assertSame([$val1], $result->fetchFirstColumn());
$this->assertEquals(1, $result->rowCount());
$this->assertEquals(3, $result->columnCount());
}
private function insertOneRow()
{
$stmt = $this->connection->prepare('insert into "connection_tests_for_hyperf" ("val1", "val2") values ($1, $2)');
$stmt->execute([uniqid(), uniqid()]);
}
private function getRowCount()
{
$result = $this->connection->query('select count(*) from "connection_tests_for_hyperf"');
return $result->fetchOne();
}
}