From c0751e113568d4b597e568a3d9da4a02a60d7b15 Mon Sep 17 00:00:00 2001 From: Jono Date: Wed, 22 May 2024 13:49:31 -0700 Subject: [PATCH] Creating script to locally run unit tests in sqlite --- tests/phpunit/data/schema.sql | 6 ++++++ tests/phpunit/data/seed.sql | 6 ++++++ tests/phpunit/mocks.php | 14 +++++++------- tests/phpunit/run.sh | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 tests/phpunit/data/schema.sql create mode 100644 tests/phpunit/data/seed.sql create mode 100755 tests/phpunit/run.sh diff --git a/tests/phpunit/data/schema.sql b/tests/phpunit/data/schema.sql new file mode 100644 index 0000000000..75ad172e7e --- /dev/null +++ b/tests/phpunit/data/schema.sql @@ -0,0 +1,6 @@ + +CREATE TABLE IF NOT EXISTS hm_user (username varchar(255), hash varchar(255), primary key (username)); + +CREATE TABLE IF NOT EXISTS hm_user_session (hm_id varchar(255), data longblob, date timestamp, primary key (hm_id)); + +CREATE TABLE IF NOT EXISTS hm_user_settings(username varchar(255), settings longblob, primary key (username)); diff --git a/tests/phpunit/data/seed.sql b/tests/phpunit/data/seed.sql new file mode 100644 index 0000000000..810696dd3e --- /dev/null +++ b/tests/phpunit/data/seed.sql @@ -0,0 +1,6 @@ + +INSERT OR REPLACE INTO hm_user values('unittestuser', 'sha512:86000:xfEgf7NIUQ2XkeU5tnIcA+HsN8pUllMVdzpJxCSwmbsZAE8Hze3Zs+MeIqepwocYteJ92vhq7pjOfrVThg/p1voELkDdPenU8i2PgG9UTI0IJTGhMN7rsUILgT6XlMAKLp/u2OD13sukUFcQNTdZNFqMsuTVTYw/Me2tAnFwgO4=:rfyUhYsWBCknx6EmbeswN0fy0hAC0N3puXzwWyDRquA='); + +INSERT OR REPLACE INTO hm_user values('testuser', '\$argon2id\$v=19\$m=65536,t=2,p=1\$dw4pTU24zRKHCEkLcloU/A\$9NJm6ALQhVpB2HTHmVHjOai912VhURUDAPsut5lrEa0'); + +INSERT OR REPLACE INTO hm_user_settings values('testuser', 'sFpVPU/hPvmfeiEKUBs4w1EizmbW/Ze2BALZf6kdJrIU3KVZrsqIhKaWTNNFRm3p51ssRAH2mpbxBMhsdpOAqIZMXFHjLttRu9t5WZWOkN7qwEh2LRq6imbkMkfqXg//K294QDLyWjE0Lsc/HSGqnguBF0YUVLVmWmdeqq7/OrXUo4HNbU88i4s2gkukKobJA2hjcOEq/rLOXr3t4LnLlcISnUbt4ptalSbeRrOnx4ehZV8hweQf1E+ID7s/a+8HHx1Qo713JDzReoLEKUsxRQ=='); diff --git a/tests/phpunit/mocks.php b/tests/phpunit/mocks.php index 34d649408d..6af4755737 100644 --- a/tests/phpunit/mocks.php +++ b/tests/phpunit/mocks.php @@ -325,13 +325,13 @@ public static function stream_socket_enable_crypto($socket, $type) { } } function setup_db($config) { - $config->set('db_connection_type', 'host'); - $config->set('db_socket', '/tmp/test.db'); - $config->set('db_driver', 'mysql'); - $config->set('db_host', '127.0.0.1'); - $config->set('db_name', 'cypht_test'); - $config->set('db_user', 'cypht_test'); - $config->set('db_pass', 'cypht_test'); + $config->set('db_connection_type', env('DB_CONNECTION_TYPE', 'host')); + $config->set('db_socket', env('DB_SOCKET', '/tmp/test.db')); + $config->set('db_driver', env('DB_DRIVER', 'mysql')); + $config->set('db_host', env('DB_HOST', '127.0.0.1')); + $config->set('db_name', env('DB_NAME', 'cypht_test')); + $config->set('db_user', env('DB_USER', 'cypht_test')); + $config->set('db_pass', env('DB_PASS', 'cypht_test')); } function flatten($str) { return strtolower(str_replace(array("\n", "\t", "\r", " "), '', $str)); diff --git a/tests/phpunit/run.sh b/tests/phpunit/run.sh new file mode 100755 index 0000000000..764f27c3a4 --- /dev/null +++ b/tests/phpunit/run.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +# example run: ./run.sh --filter=Hm_Test_Core_Message_Functions + +set -e + +SCRIPT_DIR=$(dirname $(realpath "$0")) + +DB="${DB:-sqlite}" + +echo "SETTING UP DB $DB" + +export DB_DRIVER=$DB +export DB_NAME=cypht_test + +if [ "$DB" = "sqlite" ]; then + + FILE=/tmp/test.db + + export DB_CONNECTION_TYPE=socket + export DB_SOCKET=${FILE} + + cat ${SCRIPT_DIR}/data/schema.sql | sqlite3 ${FILE} + cat ${SCRIPT_DIR}/data/seed.sql | sqlite3 ${FILE} + +# elif [ "$DB" = "mysql" ]; then # TODO + +# elif [ "$DB" = "pgsql" ]; then # TODO + +else + echo "Database not supported in test: ${DB}" + exit 1 +fi + +phpunit --configuration ${SCRIPT_DIR}/phpunit.xml $@