Skip to content

Commit bf95929

Browse files
authored
PostgreSQL and Upgrade PHPUnit to 13 (#27)
* PostgreSQL * phpunit 10 * phpunit 11 * phpunit 13 * Scrutinizer * php 8.5 * make sure xdebug is found * php 8.4.11 * make db.dev.php that CI writes match the real test one more * adjust tests * .gitignore .phpunit.cache
1 parent f4e5bd6 commit bf95929

29 files changed

Lines changed: 968 additions & 138 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ docs
1010
composer.lock
1111
.phpunit.result.cache
1212
clover.xml
13-
config/db.dev.php
13+
config/db.dev.php
14+
.phpunit.cache

.scrutinizer.yml

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ checks:
66
build:
77
image: default-jammy
88
environment:
9-
php: 8.4.0
9+
php: 8.4.11
1010
nodes:
1111
coverage:
1212
services:
@@ -18,13 +18,97 @@ build:
1818
- 3306
1919
ramdisks:
2020
- /var/run/mysqld
21+
postgres: 10
2122
dependencies:
2223
before:
24+
- printf "\n" | pecl install xdebug-3.4.7
2325
- mysql -u root -e "CREATE DATABASE IF NOT EXISTS test CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
26+
- PGPASSWORD=scrutinizer psql -h 127.0.0.1 -U scrutinizer -d scrutinizer -tc "SELECT 1 FROM pg_database WHERE datname = 'divergence'" | grep -q 1 || PGPASSWORD=scrutinizer createdb -h 127.0.0.1 -U scrutinizer divergence
27+
- PGPASSWORD=scrutinizer psql -h 127.0.0.1 -U scrutinizer -d scrutinizer -tc "SELECT 1 FROM pg_database WHERE datname = 'test'" | grep -q 1 || PGPASSWORD=scrutinizer createdb -h 127.0.0.1 -U scrutinizer test
28+
- |
29+
cat > config/db.dev.php <<'PHP'
30+
<?php
31+
return [
32+
'mysql' => [
33+
'host' => '127.0.0.1',
34+
'database' => 'divergence',
35+
'username' => 'divergence',
36+
'password' => 'not-a-secret',
37+
],
38+
'dev-mysql' => [
39+
'host' => '127.0.0.1',
40+
'database' => 'divergence',
41+
'username' => 'divergence',
42+
'password' => 'not-a-secret',
43+
],
44+
'tests-mysql' => [
45+
'host' => '127.0.0.1',
46+
'database' => 'test',
47+
'username' => 'root',
48+
'password' => '',
49+
],
50+
'tests-mysql-socket' => [
51+
'socket' => '/var/run/mysqld/mysqld.sock',
52+
'database' => 'test',
53+
'username' => 'root',
54+
'password' => '',
55+
],
56+
'pgsql' => [
57+
'driver' => 'pgsql',
58+
'host' => '127.0.0.1',
59+
'port' => 5432,
60+
'database' => 'divergence',
61+
'username' => 'scrutinizer',
62+
'password' => 'scrutinizer',
63+
],
64+
'dev-pgsql' => [
65+
'driver' => 'pgsql',
66+
'host' => '127.0.0.1',
67+
'port' => 5432,
68+
'database' => 'divergence',
69+
'username' => 'scrutinizer',
70+
'password' => 'scrutinizer',
71+
],
72+
'tests-sqlite-memory' => [
73+
'path' => ':memory:',
74+
'foreign_keys' => true,
75+
'busy_timeout' => 5000,
76+
],
77+
'sqlite' => [
78+
'path' => __DIR__ . '/../var/sqlite/app.sqlite',
79+
'foreign_keys' => true,
80+
'busy_timeout' => 5000,
81+
],
82+
'dev-sqlite' => [
83+
'path' => __DIR__ . '/../var/sqlite/dev.sqlite',
84+
'foreign_keys' => true,
85+
'busy_timeout' => 5000,
86+
],
87+
'tests-sqlite-files' => [
88+
'path' => __DIR__ . '/../var/sqlite/tests.sqlite',
89+
'foreign_keys' => true,
90+
'busy_timeout' => 5000,
91+
],
92+
'tests-pgsql' => [
93+
'driver' => 'pgsql',
94+
'host' => '127.0.0.1',
95+
'port' => 5432,
96+
'database' => 'test',
97+
'username' => 'scrutinizer',
98+
'password' => 'scrutinizer',
99+
],
100+
];
101+
PHP
24102
- sudo apt install -y ffmpeg exiftool
25103
tests:
26104
override:
27-
- command: mkdir -p build/logs && DIVERGENCE_TEST_DB=tests-mysql phpdbg -qrr ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml
105+
- command: mkdir -p build/coverage build/logs && DIVERGENCE_TEST_DB=tests-mysql XDEBUG_MODE=coverage php -d zend_extension=$(php-config --extension-dir)/xdebug.so -d xdebug.mode=coverage ./vendor/bin/phpunit --coverage-php build/coverage/mysql.cov
106+
idle_timeout: 300
107+
- command: mkdir -p build/coverage build/logs && DIVERGENCE_TEST_DB=tests-sqlite-memory XDEBUG_MODE=coverage php -d zend_extension=$(php-config --extension-dir)/xdebug.so -d xdebug.mode=coverage ./vendor/bin/phpunit --coverage-php build/coverage/sqlite.cov
108+
idle_timeout: 300
109+
- command: mkdir -p build/coverage build/logs && DIVERGENCE_TEST_DB=tests-pgsql XDEBUG_MODE=coverage php -d zend_extension=$(php-config --extension-dir)/xdebug.so -d xdebug.mode=coverage ./vendor/bin/phpunit --coverage-php build/coverage/pgsql.cov
110+
idle_timeout: 300
111+
- command: vendor/bin/phpcov merge --clover build/logs/clover.xml build/coverage
28112
idle_timeout: 300
29113
coverage:
30114
file: 'build/logs/clover.xml'

composer.json

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@
3939
}
4040
},
4141
"require-dev": {
42-
"phpunit/phpunit": "^9.5",
42+
"phpunit/phpunit": "^13.0",
43+
"phpunit/phpcov": "^12.0",
4344
"friendsofphp/php-cs-fixer": "*",
4445
"mikey179/vfsstream": "^1.6",
45-
"fakerphp/faker": "^1.20",
46-
"dms/phpunit-arraysubset-asserts": "^0.4.0"
46+
"fakerphp/faker": "^1.20"
4747
},
4848
"suggest": {
4949
"divergence/cli": "Lets you initialize a new project as well as create, edit, and test database configurations via CLI."
@@ -52,10 +52,22 @@
5252
"fix-code": "php-cs-fixer fix",
5353
"test": [
5454
"@test:mysql",
55-
"@test:sqlite"
55+
"@test:sqlite",
56+
"@test:pgsql"
5657
],
57-
"test:mysql": "DIVERGENCE_TEST_DB=tests-mysql vendor/bin/phpunit --coverage-clover build/logs/clover.xml",
58-
"test:sqlite": "DIVERGENCE_TEST_DB=tests-sqlite-memory vendor/bin/phpunit"
58+
"test:coverage": [
59+
"@test:mysql:coverage",
60+
"@test:sqlite:coverage",
61+
"@test:pgsql:coverage",
62+
"@coverage:merge"
63+
],
64+
"test:mysql": "DIVERGENCE_TEST_DB=tests-mysql vendor/bin/phpunit",
65+
"test:sqlite": "DIVERGENCE_TEST_DB=tests-sqlite-memory vendor/bin/phpunit",
66+
"test:pgsql": "DIVERGENCE_TEST_DB=tests-pgsql vendor/bin/phpunit",
67+
"test:mysql:coverage": "mkdir -p build/coverage build/logs && XDEBUG_MODE=coverage DIVERGENCE_TEST_DB=tests-mysql vendor/bin/phpunit --coverage-php build/coverage/mysql.cov",
68+
"test:sqlite:coverage": "mkdir -p build/coverage build/logs && XDEBUG_MODE=coverage DIVERGENCE_TEST_DB=tests-sqlite-memory vendor/bin/phpunit --coverage-php build/coverage/sqlite.cov",
69+
"test:pgsql:coverage": "mkdir -p build/coverage build/logs && XDEBUG_MODE=coverage DIVERGENCE_TEST_DB=tests-pgsql vendor/bin/phpunit --coverage-php build/coverage/pgsql.cov",
70+
"coverage:merge": "vendor/bin/phpcov merge --clover build/logs/clover.xml build/coverage"
5971
},
6072
"support": {
6173
"issues": "https://github.com/Divergence/framework/issues"

config/db.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10-
$devConfig = __DIR__ . '/db.dev.php';
11-
12-
if (file_exists($devConfig)) {
13-
return require $devConfig;
10+
if (file_exists(__DIR__ . '/db.dev.php')) {
11+
return require __DIR__ . '/db.dev.php';
1412
}
1513

1614
return [
@@ -58,6 +56,33 @@
5856
'username' => 'root',
5957
'password' => '',
6058
],
59+
/*
60+
* PostgreSQL database configuration
61+
*/
62+
'pgsql' => [
63+
'driver' => 'pgsql',
64+
'host' => '127.0.0.1',
65+
'port' => 5432,
66+
'database' => 'divergence',
67+
'username' => 'divergence',
68+
'password' => 'abc123',
69+
],
70+
'dev-pgsql' => [
71+
'driver' => 'pgsql',
72+
'host' => '127.0.0.1',
73+
'port' => 5432,
74+
'database' => 'divergence',
75+
'username' => 'divergence',
76+
'password' => 'abc123',
77+
],
78+
'tests-pgsql' => [
79+
'driver' => 'pgsql',
80+
'host' => '127.0.0.1',
81+
'port' => 5432,
82+
'database' => 'test',
83+
'username' => 'divergence',
84+
'password' => 'abc123',
85+
],
6186
/*
6287
* SQLite database configuration
6388
*/

phpunit.xml

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" verbose="true" colors="true" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3-
<coverage>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" cacheDirectory=".phpunit.cache" colors="true" bootstrap="tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/13.0/phpunit.xsd">
3+
<testsuites>
4+
<testsuite name="tests-mysql">
5+
<directory>tests/Divergence</directory>
6+
</testsuite>
7+
<!--<testsuite name="tests-sqlite-memory">
8+
<file>tests/DivergenceSQLite/SQLiteSuiteLoader.php</file>
9+
</testsuite>-->
10+
</testsuites>
11+
<source>
412
<include>
513
<directory>./src</directory>
614
</include>
@@ -10,19 +18,5 @@
1018
<directory>./vendor</directory>
1119
<directory>./tests</directory>
1220
</exclude>
13-
</coverage>
14-
<testsuites>
15-
<testsuite name="tests-mysql">
16-
<directory>tests/Divergence</directory>
17-
</testsuite>
18-
<!--<testsuite name="tests-sqlite-memory">
19-
<file>tests/DivergenceSQLite/SQLiteSuiteLoader.php</file>
20-
</testsuite>-->
21-
</testsuites>
22-
<listeners>
23-
<listener class="Divergence\Tests\TestListener" file="./tests/Divergence/TestListener.php"/>
24-
</listeners>
25-
<php>
26-
<const name="PHPUNIT_TESTSUITE" value="true"/>
27-
</php>
21+
</source>
2822
</phpunit>

src/IO/Database/Connections.php

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ protected static function getConnectionTypeForLabel(?string $label): string
209209
return SQLite::class;
210210
}
211211

212+
if (($connectionConfig['driver'] ?? null) === 'pgsql') {
213+
return PostgreSQL::class;
214+
}
215+
212216
return MySQL::class;
213217
}
214218

@@ -262,37 +266,7 @@ protected static function createConnection(array $config, string $label): PDO
262266
*/
263267
protected static function createResolvedConnection(string $driverClass, array $config, string $label): PDO
264268
{
265-
if ($driverClass === SQLite::class) {
266-
if (empty($config['path'])) {
267-
throw new Exception('SQLite configuration requires a "path" value.');
268-
}
269-
270-
$connection = new PDO('sqlite:' . $config['path']);
271-
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
272-
273-
if (!empty($config['foreign_keys'])) {
274-
$connection->exec('PRAGMA foreign_keys = ON');
275-
}
276-
277-
if (!empty($config['busy_timeout'])) {
278-
$connection->exec(sprintf('PRAGMA busy_timeout = %d', (int) $config['busy_timeout']));
279-
}
280-
281-
return $connection;
282-
}
283-
284-
$config = array_merge([
285-
'host' => 'localhost',
286-
'port' => 3306,
287-
], $config);
288-
289-
if (isset($config['socket'])) {
290-
$DSN = 'mysql:unix_socket=' . $config['socket'] . ';dbname=' . $config['database'];
291-
} else {
292-
$DSN = 'mysql:host=' . $config['host'] . ';port=' . $config['port'] . ';dbname=' . $config['database'];
293-
}
294-
295-
return new PDO($DSN, $config['username'], $config['password']);
269+
return $driverClass::createConnection($config, $label);
296270
}
297271

298272
/**
@@ -318,10 +292,6 @@ protected static function configureConnection(PDO $connection): void
318292
*/
319293
protected static function configureResolvedConnection(string $driverClass, PDO $connection): void
320294
{
321-
if ($driverClass === SQLite::class) {
322-
return;
323-
}
324-
325-
self::configureConnection($connection);
295+
$driverClass::configureConnection($connection);
326296
}
327297
}

0 commit comments

Comments
 (0)