Skip to content

Commit 8e9ba41

Browse files
authored
Merge pull request #1 from leeqvip/develop
Develop
2 parents 7912762 + af26dc5 commit 8e9ba41

9 files changed

Lines changed: 205 additions & 40 deletions

File tree

.github/workflows/phpunit.yml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
name: PHPUnit
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
9+
services:
10+
mysql:
11+
image: mysql:5.7
12+
env:
13+
MYSQL_ALLOW_EMPTY_PASSWORD: yes
14+
MYSQL_DATABASE: test
15+
ports:
16+
- 3306:3306
17+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
18+
postgres:
19+
image: postgres
20+
env:
21+
POSTGRES_PASSWORD: postgres
22+
POSTGRES_USER: postgres
23+
POSTGRES_DB: test
24+
ports:
25+
- 5432:5432
26+
options:
27+
--health-cmd pg_isready
28+
--health-interval 10s
29+
--health-timeout 5s
30+
--health-retries 5
31+
32+
strategy:
33+
fail-fast: true
34+
matrix:
35+
php: [ 8.0, 8.1, 8.2, 8.3, 8.4 ]
36+
37+
name: PHP ${{ matrix.php }}
38+
39+
steps:
40+
- name: Checkout code
41+
uses: actions/checkout@v3
42+
43+
- name: Setup PHP
44+
uses: shivammathur/setup-php@v2
45+
with:
46+
php-version: ${{ matrix.php }}
47+
tools: composer:v2
48+
coverage: xdebug
49+
50+
- name: Validate composer.json and composer.lock
51+
run: composer validate
52+
53+
- name: Install dependencies
54+
if: steps.composer-cache.outputs.cache-hit != 'true'
55+
run: composer install --prefer-dist --no-progress --no-suggest
56+
57+
- name: Run test suite
58+
run: ./vendor/bin/phpunit -v
59+
60+
- name: Run Coveralls
61+
env:
62+
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
63+
COVERALLS_PARALLEL: true
64+
COVERALLS_FLAG_NAME: ${{ runner.os }} - ${{ matrix.php }}
65+
run: ./vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v
66+
67+
upload-coverage:
68+
runs-on: ubuntu-latest
69+
needs: [ test ]
70+
steps:
71+
- name: Coveralls Finished
72+
uses: coverallsapp/github-action@master
73+
with:
74+
github-token: ${{ secrets.GITHUB_TOKEN }}
75+
parallel-finished: true
76+
77+
semantic-release:
78+
runs-on: ubuntu-latest
79+
needs: [ test, upload-coverage ]
80+
steps:
81+
- uses: actions/checkout@v3
82+
- uses: actions/setup-node@v3
83+
with:
84+
node-version: 'lts/*'
85+
86+
- name: Run semantic-release
87+
if: github.repository == 'leeqvip/database' && github.event_name == 'push'
88+
env:
89+
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
90+
run: npx semantic-release

.releaserc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
plugins:
2+
- "@semantic-release/commit-analyzer"
3+
- "@semantic-release/release-notes-generator"
4+
- "@semantic-release/github"

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Database library for PHP
22

3+
[![PHPUnit](https://github.com/leeqvip/database/actions/workflows/phpunit.yml/badge.svg)](https://github.com/leeqvip/database/actions/workflows/phpunit.yml)
4+
[![Coverage Status](https://coveralls.io/repos/github/leeqvip/database/badge.svg)](https://coveralls.io/github/leeqvip/database)
5+
[![Latest Stable Version](https://poser.pugx.org/leeqvip/database/v/stable)](https://packagist.org/packages/leeqvip/database)
6+
[![Total Downloads](https://poser.pugx.org/leeqvip/database/downloads)](https://packagist.org/packages/leeqvip/database)
7+
[![License](https://poser.pugx.org/leeqvip/database/license)](https://packagist.org/packages/leeqvip/database)
8+
39
PDO database library for PHP.
410

511
the current supported databases are:

phpunit.xml.dist

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
bootstrap="vendor/autoload.php"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnFailure="false">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false"
3+
bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true"
4+
convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false"
5+
stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
6+
<coverage processUncoveredFiles="true">
7+
<include>
8+
<directory suffix=".php">./src</directory>
9+
</include>
10+
<report>
11+
<clover outputFile="build/logs/clover.xml"/>
12+
<html outputDirectory="build/html"/>
13+
</report>
14+
</coverage>
1115
<testsuites>
1216
<testsuite name="Application Test Suite">
1317
<directory>./tests/</directory>
1418
</testsuite>
1519
</testsuites>
16-
<filter>
17-
<whitelist processUncoveredFilesFromWhitelist="true">
18-
<directory suffix=".php">./src</directory>
19-
</whitelist>
20-
</filter>
21-
<logging>
22-
<log type="coverage-clover" target="build/logs/clover.xml"/>
23-
<log type="coverage-html" target="build/html"/>
24-
</logging>
20+
<logging/>
2521
<php>
2622
<env name="DB_HOST" value="127.0.0.1"/>
2723
<env name="DB_PORT" value="3306"/>
2824
<env name="DB_DATABASE" value="test"/>
2925
<env name="DB_USERNAME" value="root"/>
3026
<env name="DB_PASSWORD" value=""/>
27+
28+
<env name="PG_HOST" value="127.0.0.1"/>
29+
<env name="PG_PORT" value="5432"/>
30+
<env name="PG_DATABASE" value="test"/>
31+
<env name="PG_USERNAME" value="postgres"/>
32+
<env name="PG_PASSWORD" value="postgres"/>
3133
</php>
32-
</phpunit>
34+
</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 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

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)