Skip to content

Commit 57725fc

Browse files
authored
Merge pull request #13 from davebarnwell/chore-modernize-php83-postgres
Modernize PHP 8.3+ support and add PostgreSQL
2 parents 4826b1c + a74a209 commit 57725fc

15 files changed

Lines changed: 4091 additions & 772 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: ["main", "master"]
6+
pull_request:
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
php: ["8.3", "8.4"]
15+
db: ["mysql", "postgres"]
16+
17+
services:
18+
mysql:
19+
image: mariadb:10.11
20+
ports:
21+
- "3306:3306"
22+
env:
23+
MYSQL_ALLOW_EMPTY_PASSWORD: "1"
24+
postgres:
25+
image: postgres:16
26+
ports:
27+
- "5432:5432"
28+
env:
29+
POSTGRES_DB: categorytest
30+
POSTGRES_USER: postgres
31+
POSTGRES_PASSWORD: postgres
32+
33+
steps:
34+
- uses: actions/checkout@v4
35+
36+
- name: Set up PHP
37+
uses: shivammathur/setup-php@v2
38+
with:
39+
php-version: ${{ matrix.php }}
40+
extensions: pdo, pdo_mysql, pdo_pgsql
41+
42+
- name: Install dependencies
43+
run: composer install --no-interaction --no-progress
44+
45+
- name: Static analysis
46+
run: vendor/bin/phpstan analyse -c phpstan.neon
47+
48+
- name: Format check
49+
run: vendor/bin/php-cs-fixer fix --dry-run --diff
50+
51+
- name: Configure DB env
52+
run: |
53+
if [ "${{ matrix.db }}" = "mysql" ]; then
54+
echo "MODEL_ORM_TEST_DSN=mysql:host=127.0.0.1;port=3306" >> "$GITHUB_ENV"
55+
echo "MODEL_ORM_TEST_USER=root" >> "$GITHUB_ENV"
56+
echo "MODEL_ORM_TEST_PASS=" >> "$GITHUB_ENV"
57+
else
58+
echo "MODEL_ORM_TEST_DSN=pgsql:host=127.0.0.1;port=5432;dbname=categorytest" >> "$GITHUB_ENV"
59+
echo "MODEL_ORM_TEST_USER=postgres" >> "$GITHUB_ENV"
60+
echo "MODEL_ORM_TEST_PASS=postgres" >> "$GITHUB_ENV"
61+
fi
62+
63+
- name: Wait for database
64+
run: |
65+
if [ "${{ matrix.db }}" = "mysql" ]; then
66+
for i in {1..30}; do
67+
nc -z 127.0.0.1 3306 && break
68+
sleep 1
69+
done
70+
else
71+
for i in {1..30}; do
72+
nc -z 127.0.0.1 5432 && break
73+
sleep 1
74+
done
75+
fi
76+
77+
- name: Run tests
78+
run: vendor/bin/phpunit -c phpunit.xml.dist

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
### Composer template
33
composer.phar
44
/vendor/
5+
.phpunit.cache
6+
.php-cs-fixer.cache
57

68
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
79
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
@@ -60,4 +62,3 @@ composer.phar
6062
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
6163
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
6264
# composer.lock
63-

‎.php-cs-fixer.dist.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in([
5+
__DIR__ . '/src',
6+
__DIR__ . '/tests',
7+
__DIR__ . '/test-src',
8+
])
9+
->name('*.php')
10+
->ignoreDotFiles(true)
11+
->ignoreVCS(true);
12+
13+
return (new PhpCsFixer\Config())
14+
->setRiskyAllowed(false)
15+
->setRules([
16+
'@PSR12' => true,
17+
])
18+
->setFinder($finder);

‎AGENTS.md‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Repository Guidelines
2+
3+
## Project Structure & Module Organization
4+
- `src/` holds the library source (primary entry: `src/Model/Model.php`) under the `Freshsauce\\` namespace.
5+
- `tests/` contains PHPUnit tests (e.g., `tests/Model/CategoryTest.php`).
6+
- `test-src/` provides example models used by tests (`App\\` namespace).
7+
- Root config files: `composer.json`, `phpunit.xml.dist`, and `docker-compose.yml`.
8+
9+
## Build, Test, and Development Commands
10+
- `composer install` installs dependencies (PHP >= 8.3, `ext-pdo`, PHPUnit 10).
11+
- `vendor/bin/phpunit -c phpunit.xml.dist` runs the test suite.
12+
- `vendor/bin/phpstan analyse -c phpstan.neon` runs static analysis.
13+
- `vendor/bin/php-cs-fixer fix` formats the codebase (use `--dry-run --diff` in CI).
14+
- `docker-compose up -d` starts local MariaDB and PostgreSQL instances (ports 3306 and 5432).
15+
16+
## Coding Style & Naming Conventions
17+
- PHP code uses 4-space indentation and namespaces.
18+
- Class names are `StudlyCaps` (e.g., `Model`, `CategoryTest`); methods are `camelCase`.
19+
- Table config is set via static properties like `static protected $_tableName`.
20+
- Follow PSR-4 autoloading (`Freshsauce\\` -> `src/`, `App\\` -> `test-src/`).
21+
22+
## Testing Guidelines
23+
- PHPUnit 10 is the test runner; tests live in `tests/`.
24+
- Test classes are named `*Test` and extend `PHPUnit\Framework\TestCase`.
25+
- Tests can run against MySQL/MariaDB or PostgreSQL; configure via env vars:
26+
- `MODEL_ORM_TEST_DSN` (e.g., `mysql:host=127.0.0.1;port=3306` or `pgsql:host=127.0.0.1;port=5432;dbname=categorytest`)
27+
- `MODEL_ORM_TEST_USER`, `MODEL_ORM_TEST_PASS`
28+
- MySQL tests create/drop `categorytest`; PostgreSQL tests create/drop the `categories` table.
29+
30+
## Commit & Pull Request Guidelines
31+
- Commit messages in this repo are short, descriptive sentences (e.g., “Fix test”, “Scrutinizer adjustments”).
32+
- Prefer imperative or present-tense summaries without a required prefix.
33+
- PRs should include a concise description, testing notes (`vendor/bin/phpunit -c phpunit.xml.dist`), and any DB/setup changes.
34+
35+
## Configuration & Local Setup Tips
36+
- Default test connection assumes MySQL on `127.0.0.1:3306` with `root`/empty password.
37+
- For PostgreSQL, use the Docker defaults (`postgres`/`postgres`) and `dbname=categorytest`.
38+
- Ensure ports `3306` and/or `5432` are free and reachable from PHP.

‎CHANGELOG.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Changelog
2+
3+
## Unreleased
4+
- Require PHP 8.3+ and modernize PHPUnit to 10.x.
5+
- Add PostgreSQL compatibility alongside MySQL/MariaDB.
6+
- Improve identifier quoting and driver-specific schema discovery.
7+
- Update tests to support configurable DB backends.
8+
- Add GitHub Actions CI matrix for PHP and database drivers.

‎CONTRIBUTING.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Contributing
2+
3+
Thanks for contributing to `model-orm-php`.
4+
5+
## Setup
6+
- PHP 8.3+ with PDO and the driver you intend to test (`pdo_mysql` or `pdo_pgsql`).
7+
- Install dependencies: `composer install`.
8+
- Start local DBs (optional): `docker-compose up -d`.
9+
10+
## Tests
11+
- Run: `vendor/bin/phpunit -c phpunit.xml.dist`.
12+
- Use env overrides to target a DB:
13+
- `MODEL_ORM_TEST_DSN` (e.g., `mysql:host=127.0.0.1;port=3306` or `pgsql:host=127.0.0.1;port=5432;dbname=categorytest`)
14+
- `MODEL_ORM_TEST_USER`, `MODEL_ORM_TEST_PASS`
15+
16+
## Static Analysis
17+
- Run: `vendor/bin/phpstan analyse -c phpstan.neon` (use `--debug` if your environment blocks parallel workers).
18+
19+
## Formatting
20+
- Format: `vendor/bin/php-cs-fixer fix`.
21+
- Check only: `vendor/bin/php-cs-fixer fix --dry-run --diff`.
22+
23+
## Code Style
24+
- Keep changes minimal and consistent with existing conventions (4-space indent, `StudlyCaps` classes, `camelCase` methods).
25+
- Update or add tests for behavior changes.
26+
27+
## Pull Requests
28+
- Explain the change and include test results.
29+
- Note any DB/schema assumptions and compatibility impacts.

‎README.md‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ PHP Model class which provides
1212
* raw database queries with escaped parameters
1313
* helpers for finding one or more records returning rows as instances of the Model class
1414
* throws exception on query error
15-
* Requires PHP >= 5.3
16-
* Currenlty only tested with MySQL through the PDO drivers
15+
* Requires PHP >= 8.3
16+
* Supports MySQL/MariaDB and PostgreSQL via PDO
1717

1818
Usage
1919
=====
2020

21-
With a mysql database as such on your localhost
21+
With a MySQL database as such on your localhost
2222

2323
CREATE DATABASE categorytest;
2424
CREATE TABLE `categories` (
@@ -30,13 +30,35 @@ With a mysql database as such on your localhost
3030
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
3131

3232
require_once('vendor/autoload.php');
33-
Db\Model::connectDb('mysql:dbname=categorytest;host=127.0.0.1','root',''); // db connection for all sub-classes
33+
Freshsauce\Model\Model::connectDb('mysql:dbname=categorytest;host=127.0.0.1','root',''); // db connection for all sub-classes
3434

3535
// minimum model definition
36-
class Category extends db\Model {
36+
class Category extends Freshsauce\Model\Model {
3737
static protected $_tableName = 'categories'; // database table name
3838
}
3939

40+
PostgreSQL example schema and connection
41+
42+
CREATE TABLE categories (
43+
id SERIAL PRIMARY KEY,
44+
name VARCHAR(120) NULL,
45+
updated_at TIMESTAMP NULL,
46+
created_at TIMESTAMP NULL
47+
);
48+
49+
Freshsauce\Model\Model::connectDb('pgsql:host=127.0.0.1;port=5432;dbname=categorytest','postgres','postgres');
50+
51+
Testing
52+
=======
53+
54+
Run PHPUnit with optional environment overrides for the DB connection:
55+
56+
MODEL_ORM_TEST_DSN=mysql:host=127.0.0.1;port=3306
57+
MODEL_ORM_TEST_USER=root
58+
MODEL_ORM_TEST_PASS=
59+
60+
vendor/bin/phpunit -c phpunit.xml.dist
61+
4062
Save & Update records
4163
=====================
4264

@@ -176,4 +198,4 @@ date strings are parsed into a unix date via PHPs incredibly flexible strtotime(
176198
db\Model::datetimeToMysqldatetime('2012 Sept 13th 12:00'); // returns '2012-09-13 12:00:00'
177199
db\Model::datetimeToMysqldatetime('next Monday'); // returns next monday midnight in the format 'YYYY-MM-DD HH:MM:SS'
178200
db\Model::datetimeToMysqldatetime(gmdate()); // returns the current date time in the format 'YYYY-MM-DD HH:MM:SS'
179-
201+

‎composer.json‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "Freshsauce/Model",
2+
"name": "freshsauce/model",
33
"type": "library",
44
"description": "PHP Model class",
55
"keywords": [
@@ -23,7 +23,7 @@
2323
},
2424
"require": {
2525
"ext-pdo": "*",
26-
"php" : ">=5.4"
26+
"php" : ">=8.3"
2727
},
2828
"autoload": {
2929
"psr-4": {
@@ -32,6 +32,17 @@
3232
}
3333
},
3434
"require-dev": {
35-
"phpunit/phpunit": "^5.4"
35+
"phpunit/phpunit": "^10.5",
36+
"phpstan/phpstan": "^1.11",
37+
"friendsofphp/php-cs-fixer": "^3.50"
38+
},
39+
"suggest": {
40+
"ext-pdo_mysql": "Required for MySQL/MariaDB support",
41+
"ext-pdo_pgsql": "Required for PostgreSQL support"
42+
},
43+
"config": {
44+
"platform": {
45+
"php": "8.3.0"
46+
}
3647
}
3748
}

0 commit comments

Comments
 (0)