-
Notifications
You must be signed in to change notification settings - Fork 196
Running local Tests in Cypht
Related issue: https://github.com/cypht-org/cypht/issues/1011
This guide covers how to set up and run PHPUnit tests for the Cypht webmail application.
# 1. Create test environment configuration
cp .env.test.example .env.test
# 2. Edit .env.test with your database credentials
nano .env.test
# 3. Setup database structure
php scripts/setup_database.php --env=.env.test
# 4. Seed test data (required!)
mysql -u YOUR_USER -pYOUR_PASS YOUR_DB < tests/phpunit/data/seed_mysql.sql
# 5. Run all tests
vendor/bin/phpunit --config=tests/phpunit/phpunit.xml- PHP 8.1 or higher
- Composer
- One of the following databases:
- MySQL 5.7+ / MariaDB 10.3+
- PostgreSQL 12+
- SQLite 3.8+
- pdo
- pdo_mysql (for MySQL)
- pdo_pgsql (for PostgreSQL)
- pdo_sqlite (for SQLite)
- sodium
- mbstring
- xdebug (for test coverage)
Note: This guide assumes you have already run composer install to install Cypht dependencies (including PHPUnit).
All test database configuration is managed through the .env.test file:
cp .env.test.example .env.testThe .env.test file is gitignored, so your local credentials are safe.
mysql -u root -pCREATE DATABASE cypht_test;
CREATE USER 'cypht_test'@'localhost' IDENTIFIED BY 'cypht_test';
GRANT ALL PRIVILEGES ON cypht_test.* TO 'cypht_test'@'localhost';
FLUSH PRIVILEGES;
EXIT;DB_DRIVER=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_NAME=cypht_test
DB_USER=cypht_test
DB_PASS=cypht_test
DB_CONNECTION_TYPE=hostphp scripts/setup_database.php --env=.env.testmysql -u cypht_test -pcypht_test cypht_test < tests/phpunit/data/seed_mysql.sqlNote: No space between -p and the password!
If your MySQL runs on a different port (e.g., 3307):
DB_HOST=127.0.0.1:3307sudo -u postgres psqlCREATE DATABASE cypht_test;
CREATE USER cypht_test WITH PASSWORD 'cypht_test';
GRANT ALL PRIVILEGES ON DATABASE cypht_test TO cypht_test;
\qDB_DRIVER=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_NAME=cypht_test
DB_USER=cypht_test
DB_PASS=cypht_test
DB_CONNECTION_TYPE=hostphp scripts/setup_database.php --env=.env.testUsing password authentication:
PGPASSWORD=cypht_test psql -h localhost -U cypht_test -d cypht_test < tests/phpunit/data/seed_pgsql.sqlOr using sudo (peer authentication):
sudo -u postgres psql -d cypht_test < tests/phpunit/data/seed_pgsql.sqlDB_DRIVER=sqlite
DB_CONNECTION_TYPE=socket
DB_SOCKET=/tmp/cypht_test.dbphp scripts/setup_database.php --env=.env.testsqlite3 /tmp/cypht_test.db < tests/phpunit/data/seed_sqlite.sqlvendor/bin/phpunit --config=tests/phpunit/phpunit.xmlOr using the full path:
php vendor/phpunit/phpunit/phpunit --configuration tests/phpunit/phpunit.xml# Run only session tests
vendor/bin/phpunit --config=tests/phpunit/phpunit.xml --testsuite=session
# Run only database tests
vendor/bin/phpunit --config=tests/phpunit/phpunit.xml --testsuite=db
# Run only cache tests
vendor/bin/phpunit --config=tests/phpunit/phpunit.xml --testsuite=cachevendor/bin/phpunit tests/phpunit/session.php --config=tests/phpunit/phpunit.xml
vendor/bin/phpunit tests/phpunit/db.php --config=tests/phpunit/phpunit.xmlvendor/bin/phpunit --config=tests/phpunit/phpunit.xml --filter=test_checkvendor/bin/phpunit --config=tests/phpunit/phpunit.xml --testdoxexport XDEBUG_MODE=coverage
vendor/bin/phpunit --config=tests/phpunit/phpunit.xml --coverage-html build/coverageThen open build/coverage/index.html in your browser.
Problem: Tests fail with errors about missing users or authentication failures.
Solution: Make sure you seeded the test data:
# For MySQL
mysql -u cypht_test -pcypht_test cypht_test < tests/phpunit/data/seed_mysql.sql
# For PostgreSQL
sudo -u postgres psql -d cypht_test < tests/phpunit/data/seed_pgsql.sql
# For SQLite
sqlite3 /tmp/cypht_test.db < tests/phpunit/data/seed_sqlite.sqlProblem: Unable to connect to database
Solutions:
-
Verify database is running:
# MySQL systemctl status mysql # PostgreSQL systemctl status postgresql
-
Check credentials in
.env.test:cat .env.test
-
Test connection manually:
# MySQL mysql -u cypht_test -pcypht_test cypht_test -e "SELECT 1;" # PostgreSQL psql -h localhost -U cypht_test -d cypht_test -c "SELECT 1;"
Problem: Test test_build_dsn fails with database name mismatch.
Solution: This is normal if you changed the database name in .env.test. The test now uses the actual database name from your configuration.
Problem: Access denied or FATAL: password authentication failed
Solutions:
-
For MySQL, verify user privileges:
SHOW GRANTS FOR 'cypht_test'@'localhost';
-
For PostgreSQL, check
pg_hba.confauthentication settings. -
Recreate the user with correct permissions (see Database Setup sections above).
Problem: FATAL: Peer authentication failed for user "postgres"
Solution: Use -h localhost to force password authentication:
PGPASSWORD=cypht_test psql -h localhost -U cypht_test -d cypht_test < tests/phpunit/data/seed_pgsql.sqlOr use sudo with peer authentication:
sudo -u postgres psql -d cypht_test < tests/phpunit/data/seed_pgsql.sqlIf tests behave unexpectedly after configuration changes:
rm -rf tests/phpunit/.phpunit.cacheThe seed files are located in tests/phpunit/data/:
-
seed_mysql.sql- MySQL/MariaDB seed data -
seed_pgsql.sql- PostgreSQL seed data -
seed_sqlite.sql- SQLite seed data
Test Users Included:
-
unittestuser/unittestpass- Used for authentication tests -
testuser- Used for configuration tests
If you need to modify test data, edit the appropriate seed file and re-seed:
mysql -u cypht_test -pcypht_test cypht_test < tests/phpunit/data/seed_mysql.sqlRun vendor/bin/phpunit --list-suites --config=tests/phpunit/phpunit.xml to see all available test suites, including:
-
session- Session management tests -
db- Database connection and query tests -
cache- Cache implementation tests -
config- Configuration handling tests -
auth- Authentication tests -
module- Module system tests - And many more...
The test database schema is automatically created by setup_database.php from:
-
database/mysql_schema.sql(for MySQL) -
database/pgsql_schema.sql(for PostgreSQL) -
database/sqlite_schema.sql(for SQLite)
| Username | Password | Purpose | Hash Type |
|---|---|---|---|
unittestuser |
unittestpass |
Authentication & session tests | SHA512 with salt |
testuser |
(hashed) | Configuration & settings tests | Argon2id |
Important: These users MUST exist for tests to pass. They are created by running the seed SQL files.
If you encounter issues:
- Check this documentation
- Verify your
.env.testconfiguration - Ensure test data is seeded
- Check the GitHub Issues
- Join our Community Chat
When adding new tests that require database setup:
- Document any new test users needed
- Update the seed files for all database types
- Update this documentation
- Ensure tests work on fresh database setup