Skip to content

Commit 6c2a82a

Browse files
authored
Merge pull request #1798 from IrAlfred/refactor-test-db-config-env-based
feat(other): make database configuration for tests environment-based
2 parents 43ebfd0 + 416a633 commit 6c2a82a

File tree

9 files changed

+88
-8
lines changed

9 files changed

+88
-8
lines changed

.env.test.example

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Test Environment Configuration Template
2+
#
3+
# REQUIRED: Copy this file to .env.test before running tests
4+
# cp .env.test.example .env.test
5+
#
6+
# Then edit .env.test with your database credentials.
7+
# The .env.test file is gitignored and safe for local credentials.
8+
#
9+
# Setup test database:
10+
# php scripts/setup_database.php --env=.env.test
11+
#
12+
# Seed test data (required for tests to pass):
13+
# For MySQL: mysql -u DB_USER -pDB_PASS DB_NAME < tests/phpunit/data/seed_mysql.sql
14+
# For SQLite: sqlite3 DB_SOCKET < tests/phpunit/data/seed_sqlite.sql
15+
# For PostgreSQL: PGPASSWORD=DB_PASS psql -h localhost -U DB_USER -d DB_NAME < tests/phpunit/data/seed_pgsql.sql
16+
#
17+
18+
# Database Configuration for Tests
19+
DB_CONNECTION_TYPE=host
20+
DB_DRIVER=mysql
21+
DB_HOST=127.0.0.1
22+
DB_PORT=3306
23+
DB_NAME=cypht_test
24+
DB_USER=cypht_test
25+
DB_PASS=cypht_test
26+
DB_SOCKET=/var/lib/mysqld/mysqld.sock
27+
28+
# Session Configuration for Tests
29+
SESSION_TYPE=PHP
30+
AUTH_TYPE=DB
31+
32+
# User Configuration for Tests
33+
USER_CONFIG_TYPE=file
34+
USER_SETTINGS_DIR=tests/phpunit/data
35+
36+
# Debug Mode
37+
DEBUG_LOG=true
38+
39+
# Other test-specific settings
40+
DEFAULT_LANGUAGE=en
41+
ALLOW_SESSION_CACHE=false

.github/workflows/coveralls.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
run: |
4949
FILE=/tmp/test.db
5050
cat tests/phpunit/data/schema_sqlite.sql | sqlite3 ${FILE}
51-
cat tests/phpunit/data/seed.sql | sqlite3 ${FILE}
51+
cat tests/phpunit/data/seed_sqlite.sql | sqlite3 ${FILE}
5252
env:
5353
DB_DRIVER: sqlite
5454
DB_NAME: cypht_test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
/.env
1111
/.env.*
1212
!.env.example
13+
!.env.test.example
1314
apigen4.sh
1415
testuser.txt
1516
website/docs

scripts/setup_database.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,21 @@
99
require VENDOR_PATH.'autoload.php';
1010
require APP_PATH.'lib/framework.php';
1111

12+
// Allow specifying environment file via --env argument
13+
// Usage: php setup_database.php --env=.env.test
14+
$envFile = '.env';
15+
$options = getopt('', ['env:']);
16+
if (isset($options['env'])) {
17+
$envFile = $options['env'];
18+
}
19+
20+
if (!file_exists(APP_PATH . $envFile)) {
21+
echo "Environment file {$envFile} not found. Please create it from the example file.\n";
22+
exit(1);
23+
}
24+
1225
$environment = Hm_Environment::getInstance();
13-
$environment->load();
26+
$environment->load($envFile);
1427

1528
/* get config object */
1629
$config = new Hm_Site_Config_File();

tests/phpunit/bootstrap.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
Hm_Tags_Wrapper::init($user_config, $session);
5454

5555
$environment = Hm_Environment::getInstance();
56-
$environment->load('.env.example');
56+
// Load test environment configuration from .env.test
57+
// Create this file by copying .env.test.example: cp .env.test.example .env.test
58+
$environment->load('.env.test');
5759
/* set the default since and per_source values */
5860
$environment->define_default_constants($mock_config);
5961

tests/phpunit/data/seed_pgsql.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- Test Data Seed for PHPUnit Tests (PostgreSQL)
2+
-- Password for 'unittestuser' is 'unittestpass'
3+
-- Password for 'testuser' is hashed with argon2id
4+
5+
INSERT INTO hm_user (username, hash) VALUES
6+
('unittestuser', 'sha512:86000:xfEgf7NIUQ2XkeU5tnIcA+HsN8pUllMVdzpJxCSwmbsZAE8Hze3Zs+MeIqepwocYteJ92vhq7pjOfrVThg/p1voELkDdPenU8i2PgG9UTI0IJTGhMN7rsUILgT6XlMAKLp/u2OD13sukUFcQNTdZNFqMsuTVTYw/Me2tAnFwgO4=:rfyUhYsWBCknx6EmbeswN0fy0hAC0N3puXzwWyDRquA='),
7+
('testuser', '$argon2id$v=19$m=65536,t=2,p=1$dw4pTU24zRKHCEkLcloU/A$9NJm6ALQhVpB2HTHmVHjOai912VhURUDAPsut5lrEa0')
8+
ON CONFLICT (username) DO UPDATE SET hash = EXCLUDED.hash;
9+
10+
INSERT INTO hm_user_settings (username, settings) VALUES
11+
('testuser', 'sFpVPU/hPvmfeiEKUBs4w1EizmbW/Ze2BALZf6kdJrIU3KVZrsqIhKaWTNNFRm3p51ssRAH2mpbxBMhsdpOAqIZMXFHjLttRu9t5WZWOkN7qwEh2LRq6imbkMkfqXg//K294QDLyWjE0Lsc/HSGqnguBF0YUVLVmWmdeqq7/OrXUo4HNbU88i4s2gkukKobJA2hjcOEq/rLOXr3t4LnLlcISnUbt4ptalSbeRrOnx4ehZV8hweQf1E+ID7s/a+8HHx1Qo713JDzReoLEKUsxRQ==')
12+
ON CONFLICT (username) DO UPDATE SET settings = EXCLUDED.settings;

tests/phpunit/db.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ public function setUp(): void {
1818
public function test_build_dsn() {
1919
setup_db($this->config);
2020
$this->assertEquals('object', gettype(Hm_DB::connect($this->config)));
21+
$db_name = $this->config->get('db_name', 'cypht_test');
2122
if ($this->config->data['db_driver'] == 'mysql') {
22-
$this->assertEquals('mysql:host=127.0.0.1;dbname=cypht_test', Hm_DB::build_dsn());
23+
$this->assertEquals('mysql:host=127.0.0.1;dbname='.$db_name, Hm_DB::build_dsn());
2324
}
2425
if ($this->config->data['db_driver'] == 'pgsql') {
25-
$this->assertEquals('pgsql:host=127.0.0.1;dbname=cypht_test', Hm_DB::build_dsn());
26+
$this->assertEquals('pgsql:host=127.0.0.1;dbname='.$db_name, Hm_DB::build_dsn());
2627
}
2728
$this->config->data['db_driver'] = 'sqlite';
2829
$this->config->data['db_socket'] = '/tmp/test.db';
@@ -33,7 +34,7 @@ public function test_build_dsn() {
3334
$this->config->data['db_connection_type'] = 'socket';
3435
$this->config->data['db_socket'] = '/test';
3536
$this->assertEquals('boolean', gettype(Hm_DB::connect($this->config)));
36-
$this->assertEquals('mysql:unix_socket=/test;dbname=cypht_test', Hm_DB::build_dsn());
37+
$this->assertEquals('mysql:unix_socket=/test;dbname='.$db_name, Hm_DB::build_dsn());
3738
}
3839
/**
3940
* @preserveGlobalState disabled

tests/phpunit/run.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
set -e
66

77
SCRIPT_DIR=$(dirname $(realpath "$0"))
8+
PROJECT_ROOT=$(dirname $(dirname "$SCRIPT_DIR"))
9+
10+
# Change to project root to ensure correct paths work
11+
cd "$PROJECT_ROOT"
12+
13+
# Create .env.test if it doesn't exist (for CI environments)
14+
if [ ! -f .env.test ]; then
15+
echo "Creating .env.test from .env.test.example"
16+
cp .env.test.example .env.test
17+
fi
818

919
DB="${DB:-sqlite}"
1020

@@ -21,7 +31,7 @@ if [ "$DB" = "sqlite" ]; then
2131
export DB_SOCKET=${FILE}
2232

2333
cat ${SCRIPT_DIR}/data/schema_sqlite.sql | sqlite3 ${FILE}
24-
cat ${SCRIPT_DIR}/data/seed.sql | sqlite3 ${FILE}
34+
cat ${SCRIPT_DIR}/data/seed_sqlite.sql | sqlite3 ${FILE}
2535

2636
elif [ "$DB" = "mysql" ]; then
2737
# Load schema.sql
@@ -42,4 +52,4 @@ else
4252
exit 1
4353
fi
4454

45-
phpunit --configuration ${SCRIPT_DIR}/phpunit.xml --testdox $@
55+
${PROJECT_ROOT}/vendor/bin/phpunit --configuration ${SCRIPT_DIR}/phpunit.xml --testdox $@

0 commit comments

Comments
 (0)