Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: PHPUnit

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest

services:
mysql:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: test
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
postgres:
image: postgres
env:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: test
ports:
- 5432:5432
options:
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5

strategy:
fail-fast: true
matrix:
php: [ 8.0, 8.1, 8.2, 8.3, 8.4 ]

name: PHP ${{ matrix.php }}

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2
coverage: xdebug

- name: Validate composer.json and composer.lock
run: composer validate

- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run test suite
run: ./vendor/bin/phpunit -v

- name: Run Coveralls
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COVERALLS_PARALLEL: true
COVERALLS_FLAG_NAME: ${{ runner.os }} - ${{ matrix.php }}
run: ./vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v

upload-coverage:
runs-on: ubuntu-latest
needs: [ test ]
steps:
- name: Coveralls Finished
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel-finished: true

semantic-release:
runs-on: ubuntu-latest
needs: [ test, upload-coverage ]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 'lts/*'

- name: Run semantic-release
if: github.repository == 'leeqvip/database' && github.event_name == 'push'
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
run: npx semantic-release
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/vendor/

composer.lock

.vscode
.idea
*.iml

# coverage report
/build

.phpunit.*
phpunit.xml
4 changes: 4 additions & 0 deletions .releaserc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
plugins:
- "@semantic-release/commit-analyzer"
- "@semantic-release/release-notes-generator"
- "@semantic-release/github"
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Database library for PHP

[![PHPUnit](https://github.com/leeqvip/database/actions/workflows/phpunit.yml/badge.svg)](https://github.com/leeqvip/database/actions/workflows/phpunit.yml)
[![Coverage Status](https://coveralls.io/repos/github/leeqvip/database/badge.svg)](https://coveralls.io/github/leeqvip/database)
[![Latest Stable Version](https://poser.pugx.org/leeqvip/database/v/stable)](https://packagist.org/packages/leeqvip/database)
[![Total Downloads](https://poser.pugx.org/leeqvip/database/downloads)](https://packagist.org/packages/leeqvip/database)
[![License](https://poser.pugx.org/leeqvip/database/license)](https://packagist.org/packages/leeqvip/database)

PDO database library for PHP.

the current supported databases are:
Expand Down
52 changes: 33 additions & 19 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
{
"name": "leeqvip/database",
"keywords": ["pdo", "database", "orm", "framework"],
"description": "PDO database library",
"authors": [
{
"name": "leeqvip",
"email": "leeqvip@gmail.com"
}
],
"license": "Apache-2.0",
"require": {
"php": ">=5.4.0",
"ext-pdo": "*"
},
"autoload": {
"psr-4": {
"Leeqvip\\Database\\": "src/"
}
}
"name": "leeqvip/database",
"keywords": [
"pdo",
"database",
"orm",
"framework"
],
"description": "PDO database library",
"authors": [
{
"name": "leeqvip",
"email": "leeqvip@gmail.com"
}
],
"license": "Apache-2.0",
"require": {
"php": ">=8.0",
"ext-pdo": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.6",
"php-coveralls/php-coveralls": "^2.7"
},
"autoload": {
"psr-4": {
"Leeqvip\\Database\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
}
}
34 changes: 34 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false"
bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true"
convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false"
stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/html"/>
</report>
</coverage>
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<logging/>
<php>
<env name="DB_HOST" value="127.0.0.1"/>
<env name="DB_PORT" value="3306"/>
<env name="DB_DATABASE" value="test"/>
<env name="DB_USERNAME" value="root"/>
<env name="DB_PASSWORD" value=""/>

<env name="PG_HOST" value="127.0.0.1"/>
<env name="PG_PORT" value="5432"/>
<env name="PG_DATABASE" value="test"/>
<env name="PG_USERNAME" value="postgres"/>
<env name="PG_PASSWORD" value="postgres"/>
</php>
</phpunit>
2 changes: 1 addition & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(array $config = [])
$this->connector = new $connector();
}

public function getPdo()
public function getPdo(): PDO
{
if (is_null($this->pdo)) {
$this->connect();
Expand Down
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.db
85 changes: 85 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Tests;

use Leeqvip\Database\Connection;
use PHPUnit\Framework\TestCase;

abstract class ConnectionTest extends TestCase
{

protected $config = [];

abstract protected function initConfig();

protected function initDatabase()
{
$pdo = $this->getPDO();
$pdo->exec("DROP TABLE IF EXISTS users");
$pdo->exec(<<<EOT
CREATE TABLE users (
id int NOT NULL,
name varchar(191) DEFAULT NULL,
nickname varchar(255) DEFAULT NULL,
created_at timestamp NULL DEFAULT NULL
);
EOT
);
}

protected function init()
{
$this->initConfig();
$this->initDatabase();
}

protected function getConnection()
{
return new Connection($this->config);
}

protected function getPDO(): \PDO
{
return $this->getConnection()->getPdo();
}

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

$users = $this->getConnection()->query("SELECT * FROM users");
$this->assertCount(1, $users);

$users = $this->getConnection()->query("SELECT * FROM users WHERE id = :id", ['id' => 100]);
$this->assertCount(1, $users);

$this->assertEquals('100', $users[0]['id']);
$this->assertEquals('alice', $users[0]['name']);
$this->assertEquals('Small Flower', $users[0]['nickname']);
}

public function testExecute()
{
$this->init();
$conn = $this->getConnection();

$users = $conn->query("SELECT * FROM users");
$this->assertCount(0, $users);
$conn->execute(
"INSERT INTO users (id, name, nickname, created_at) VALUES (:id, :name, :nickname, '2025-05-17 13:28:56')",
[
'id' => 200,
'name' => 'bob',
'nickname' => 'Small Angel',
],
);

$users = $conn->query("SELECT * FROM users");
$this->assertCount(1, $users);

$this->assertEquals('200', $users[0]['id']);
$this->assertEquals('bob', $users[0]['name']);
$this->assertEquals('Small Angel', $users[0]['nickname']);
}
}
22 changes: 22 additions & 0 deletions tests/MysqlConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Tests;


class MysqlConnectionTest extends ConnectionTest
{

protected $config = [];

protected function initConfig()
{
$this->config = [
'type' => 'mysql', // mysql,pgsql,sqlite,sqlsrv
'hostname' => getenv('DB_HOST'),
'database' => getenv('DB_DATABASE'),
'username' => getenv('DB_USERNAME'),
'password' => getenv('DB_PASSWORD'),
'hostport' => getenv('DB_PORT'),
];
}
}
22 changes: 22 additions & 0 deletions tests/PostgresConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Tests;


class PostgresConnectionTest extends ConnectionTest
{

protected $config = [];

protected function initConfig()
{
$this->config = [
'type' => 'pgsql', // mysql,pgsql,sqlite,sqlsrv
'hostname' => getenv('PG_HOST'),
'database' => getenv('PG_DATABASE'),
'username' => getenv('PG_USERNAME'),
'password' => getenv('PG_PASSWORD'),
'hostport' => getenv('PG_PORT'),
];
}
}
19 changes: 19 additions & 0 deletions tests/SqliteConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Tests;


class SqliteConnectionTest extends ConnectionTest
{

protected $config = [];

protected function initConfig()
{
$this->config = [
'type' => 'sqlite', // mysql,pgsql,sqlite,sqlsrv
'database' => __DIR__.'/test.db',
];
}
}

Loading