55use Leeqvip \Database \Connection ;
66use 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 int NOT NULL,
22+ name varchar(191) DEFAULT NULL,
23+ nickname varchar(255) DEFAULT NULL,
24+ created_at timestamp 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
0 commit comments