Skip to content

Commit 25b6a39

Browse files
committed
test: update tests
1 parent 303fa4b commit 25b6a39

6 files changed

Lines changed: 90 additions & 21 deletions

File tree

phpunit.xml.dist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,11 @@
2828
<env name="DB_DATABASE" value="test"/>
2929
<env name="DB_USERNAME" value="root"/>
3030
<env name="DB_PASSWORD" value=""/>
31+
32+
<env name="PG_HOST" value="127.0.0.1"/>
33+
<env name="PG_PORT" value="5432"/>
34+
<env name="PG_DATABASE" value="test"/>
35+
<env name="PG_USERNAME" value="postgres"/>
36+
<env name="PG_PASSWORD" value="postgres"/>
3137
</php>
3238
</phpunit>

tests/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.db

tests/ConnectionTest.php

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,32 @@
55
use Leeqvip\Database\Connection;
66
use PHPUnit\Framework\TestCase;
77

8-
class ConnectionTest extends TestCase
8+
abstract class ConnectionTest extends TestCase
99
{
1010

1111
protected $config = [];
1212

13-
protected function initConfig()
14-
{
15-
$this->config = [
16-
'type' => 'mysql', // mysql,pgsql,sqlite,sqlsrv
17-
'hostname' => getenv('DB_HOST'),
18-
'database' => getenv('DB_DATABASE'),
19-
'username' => getenv('DB_USERNAME'),
20-
'password' => getenv('DB_PASSWORD'),
21-
'hostport' => getenv('DB_PORT'),
22-
];
13+
abstract protected function initConfig();
2314

15+
protected function initDatabase()
16+
{
2417
$pdo = $this->getPDO();
2518
$pdo->exec("DROP TABLE IF EXISTS users");
2619
$pdo->exec(<<<EOT
2720
CREATE TABLE users (
28-
`id` bigint unsigned NOT NULL,
29-
`name` varchar(191) DEFAULT NULL,
30-
`nickname` varchar(255) DEFAULT NULL,
31-
`created_at` timestamp NULL DEFAULT NULL
21+
id bigint unsigned NOT NULL,
22+
name varchar(191) DEFAULT NULL,
23+
nickname varchar(255) DEFAULT NULL,
24+
created_at datetime NULL DEFAULT NULL
3225
);
33-
EOT);
26+
EOT
27+
);
28+
}
29+
30+
protected function init()
31+
{
32+
$this->initConfig();
33+
$this->initDatabase();
3434
}
3535

3636
protected function getConnection()
@@ -45,8 +45,8 @@ protected function getPDO(): \PDO
4545

4646
public function testQuery()
4747
{
48-
$this->initConfig();
49-
$this->getPDO()->exec("INSERT INTO users (id, name, nickname, created_at) VALUES (100, 'alice', 'Small Flower', now())");
48+
$this->init();
49+
$this->getPDO()->exec("INSERT INTO users (id, name, nickname, created_at) VALUES (100, 'alice', 'Small Flower', '2025-05-17 13:28:56')");
5050

5151
$users = $this->getConnection()->query("SELECT * FROM users");
5252
$this->assertCount(1, $users);
@@ -61,18 +61,17 @@ public function testQuery()
6161

6262
public function testExecute()
6363
{
64-
$this->initConfig();
64+
$this->init();
6565
$conn = $this->getConnection();
6666

6767
$users = $conn->query("SELECT * FROM users");
6868
$this->assertCount(0, $users);
6969
$conn->execute(
70-
"INSERT INTO users (id, name, nickname, created_at) VALUES (:id, :name, :nickname, :created_at)",
70+
"INSERT INTO users (id, name, nickname, created_at) VALUES (:id, :name, :nickname, '2025-05-17 13:28:56')",
7171
[
7272
'id' => 200,
7373
'name' => 'bob',
7474
'nickname' => 'Small Angel',
75-
'created_at' => time(),
7675
],
7776
);
7877

tests/MysqlConnectionTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
6+
class MysqlConnectionTest extends ConnectionTest
7+
{
8+
9+
protected $config = [];
10+
11+
protected function initConfig()
12+
{
13+
$this->config = [
14+
'type' => 'mysql', // mysql,pgsql,sqlite,sqlsrv
15+
'hostname' => getenv('DB_HOST'),
16+
'database' => getenv('DB_DATABASE'),
17+
'username' => getenv('DB_USERNAME'),
18+
'password' => getenv('DB_PASSWORD'),
19+
'hostport' => getenv('DB_PORT'),
20+
];
21+
}
22+
}

tests/PostgresConnectionTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
6+
class PostgresConnectionTest extends ConnectionTest
7+
{
8+
9+
protected $config = [];
10+
11+
protected function initConfig()
12+
{
13+
$this->config = [
14+
'type' => 'pgsql', // mysql,pgsql,sqlite,sqlsrv
15+
'hostname' => getenv('PG_HOST'),
16+
'database' => getenv('PG_DATABASE'),
17+
'username' => getenv('PG_USERNAME'),
18+
'password' => getenv('PG_PASSWORD'),
19+
'hostport' => getenv('PG_PORT'),
20+
];
21+
}
22+
}

tests/SqliteConnectionTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
6+
class SqliteConnectionTest extends ConnectionTest
7+
{
8+
9+
protected $config = [];
10+
11+
protected function initConfig()
12+
{
13+
$this->config = [
14+
'type' => 'sqlite', // mysql,pgsql,sqlite,sqlsrv
15+
'database' => __DIR__.'/test.db',
16+
];
17+
}
18+
}
19+

0 commit comments

Comments
 (0)