3 DATABASENAME=${MYSQL_DATABASE:-test}
4 DATABASEUSER=${MYSQL_USERNAME:-friendica}
5 DATABASEHOST=${MYSQL_HOST:-localhost}
8 DBCONFIGS="mysql mariadb"
9 TESTS="REDIS MEMCACHE MEMCACHED APCU NODB"
11 export MYSQL_DATABASE="$DATABASENAME"
12 export MYSQL_USERNAME="$DATABASEUSER"
13 export MYSQL_PASSWORD="friendica"
15 if [ -z "$PHP_EXE" ]; then
18 PHP=$(which "$PHP_EXE")
19 # Use the Friendica internal composer
20 COMPOSER="$BASEDIR/bin/composer.phar"
24 _XDEBUG_CONFIG=$XDEBUG_CONFIG
27 function show_syntax() {
28 echo -e "Syntax: ./autotest.sh [dbconfigname] [testfile]\n" >&2
29 echo -e "\t\"dbconfigname\" can be one of: $DBCONFIGS" >&2
30 echo -e "\t\"testfile\" is the name of a test file, for example lib/template.php" >&2
31 echo -e "\nDatabase environment variables:\n" >&2
32 echo -e "\t\"MYSQL_HOST\" Mysql Hostname (Default: localhost)" >&2
33 echo -e "\t\"MYSQL_USDRNAME\" Mysql Username (Default: friendica)" >&2
34 echo -e "\t\"MYSQL_DATABASE\" Mysql Database (Default: test)" >&2
35 echo -e "\nOther environment variables:\n" >&2
36 echo -e "\t\"TEST_SELECTION\" test a specific group of tests, can be one of: $TESTS" >&2
37 echo -e "\t\"NOINSTALL\" If set to true, skip the db and install process" >&2
38 echo -e "\t\"NOCOVERAGE\" If set to true, don't create a coverage output" >&2
39 echo -e "\t\"USEDOCKER\" If set to true, the DB server will be executed inside a docker container" >&2
40 echo -e "\nExample: NOCOVERAGE=true ./autotest.sh mysql src/Core/Cache/MemcacheTest.php" >&2
41 echo "will run the test suite from \"tests/src/Core/Cache/MemcacheTest.php\" without a Coverage" >&2
42 echo -e "\nIf no arguments are specified, all tests will be run with all database configs" >&2
45 if [ -x "$PHP" ]; then
46 echo "Using PHP executable $PHP"
48 echo "Could not find PHP executable $PHP_EXE" >&2
52 echo "Installing depdendencies"
53 $PHP "$COMPOSER" install
55 PHPUNIT="$BASEDIR/vendor/bin/phpunit"
57 if [ -x "$PHPUNIT" ]; then
58 echo "Using PHPUnit executable $PHPUNIT"
60 echo "Could not find PHPUnit executable after composer $PHPUNIT" >&2
64 if ! [ \( -w config -a ! -f config/local.config.php \) -o \( -f config/local.config.php -a -w config/local.config.php \) ]; then
65 echo "Please enable write permissions on config and config/config.php" >&2
71 for DBCONFIG in $DBCONFIGS; do
72 if [ "$1" = "$DBCONFIG" ]; then
77 if [ $FOUND = 0 ]; then
78 echo -e "Unknown database config name \"$1\"\n" >&2
84 # Back up existing (dev) config if one exists and backup not already there
85 if [ -f config/local.config.php ] && [ ! -f config/local.config-autotest-backup.php ]; then
86 mv config/local.config.php config/local.config-autotest-backup.php
89 function cleanup_config() {
91 if [ -n "$DOCKER_CONTAINER_ID" ]; then
92 echo "Kill the docker $DOCKER_CONTAINER_ID"
93 docker stop "$DOCKER_CONTAINER_ID"
94 docker rm -f "$DOCKER_CONTAINER_ID"
99 # Restore existing config
100 if [ -f config/local.config-autotest-backup.php ]; then
101 mv config/local.config-autotest-backup.php config/local.config.php
105 # restore config on exit
106 trap cleanup_config EXIT
108 function execute_tests() {
110 echo "Setup environment for $DB testing ..."
111 # back to root folder
114 # backup current config
115 if [ -f config/local.config.php ]; then
116 mv config/local.config.php config/local.config-autotest-backup.php
119 if [ -z "$NOINSTALL" ]; then
121 if [ "$DB" == "mysql" ]; then
122 if [ -n "$USEDOCKER" ]; then
123 echo "Fire up the mysql docker"
124 DOCKER_CONTAINER_ID=$(docker run \
125 -e MYSQL_ROOT_PASSWORD=friendica \
126 -e MYSQL_USER="$DATABASEUSER" \
127 -e MYSQL_PASSWORD=friendica \
128 -e MYSQL_DATABASE="$DATABASENAME" \
130 DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
133 if [ -z "$DRONE" ]; then # no need to drop the DB when we are on CI
134 if [ "mysql" != "$(mysql --version | grep -o mysql)" ]; then
135 echo "Your mysql binary is not provided by mysql"
136 echo "To use the docker container set the USEDOCKER environment variable"
139 mysql -u "$DATABASEUSER" -pfriendica -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
140 mysql -u "$DATABASEUSER" -pfriendica -e "CREATE DATABASE $DATABASENAME DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci" -h $DATABASEHOST
146 echo "Waiting for MySQL $DATABASEHOST initialization..."
147 if ! bin/wait-for-connection $DATABASEHOST 3306 300; then
148 echo "[ERROR] Waited 300 seconds, no response" >&2
154 if [ "$DB" == "mariadb" ]; then
155 if [ -n "$USEDOCKER" ]; then
156 echo "Fire up the mariadb docker"
157 DOCKER_CONTAINER_ID=$(docker run \
158 -e MYSQL_ROOT_PASSWORD=friendica \
159 -e MYSQL_USER="$DATABASEUSER" \
160 -e MYSQL_PASSWORD=friendica \
161 -e MYSQL_DATABASE="$DATABASENAME" \
163 DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
166 if [ -z "$DRONE" ]; then # no need to drop the DB when we are on CI
167 if [ "MariaDB" != "$(mysql --version | grep -o MariaDB)" ]; then
168 echo "Your mysql binary is not provided by mysql"
169 echo "To use the docker container set the USEDOCKER environment variable"
172 mysql -u "$DATABASEUSER" -pfriendica -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
173 mysql -u "$DATABASEUSER" -pfriendica -e "CREATE DATABASE $DATABASENAME DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci" -h $DATABASEHOST
179 echo "Waiting for MariaDB $DATABASEHOST initialization..."
180 if ! bin/wait-for-connection $DATABASEHOST 3306 300; then
181 echo "[ERROR] Waited 300 seconds, no response" >&2
185 echo "MariaDB is up."
188 if [ -n "$USEDOCKER" ]; then
189 echo "Initialize database..."
190 docker exec $DOCKER_CONTAINER_ID mysql -u root -pfriendica -e 'CREATE DATABASE IF NOT EXISTS $DATABASENAME;'
193 export MYSQL_HOST="$DATABASEHOST"
196 echo "Installing Friendica..."
197 "$PHP" ./bin/console.php autoinstall --dbuser="$DATABASEUSER" --dbpass=friendica --dbdata="$DATABASENAME" --dbhost="$DATABASEHOST" --url=https://friendica.local --admin=admin@friendica.local
202 rm -fr "coverage-html"
203 mkdir "coverage-html"
204 if [[ "$_XDEBUG_CONFIG" ]]; then
205 export XDEBUG_CONFIG=$_XDEBUG_CONFIG
209 if [ -z "$NOCOVERAGE" ]; then
210 COVER="--coverage-clover tests/autotest-clover.xml"
215 # per default, there is no cache installed
216 GROUP='--exclude-group REDIS,MEMCACHE,MEMCACHED,APCU'
217 if [ "$TEST_SELECTION" == "REDIS" ]; then
218 GROUP="--group REDIS"
220 if [ "$TEST_SELECTION" == "MEMCACHE" ]; then
221 GROUP="--group MEMCACHE"
223 if [ "$TEST_SELECTION" == "MEMCACHED" ]; then
224 GROUP="--group MEMCACHED"
226 if [ "$TEST_SELECTION" == "APCU" ]; then
229 if [ "$TEST_SELECTION" == "NODB" ]; then
230 GROUP="--exclude-group DB,SLOWDB"
233 INPUT="$BASEDIR/tests"
238 echo "${PHPUNIT[@]}" --configuration tests/phpunit.xml $GROUP $COVER --log-junit "autotest-results.xml" "$INPUT" "$3"
239 "${PHPUNIT[@]}" --configuration tests/phpunit.xml $GROUP $COVER --log-junit "autotest-results.xml" "$INPUT" "$3"
242 if [ -n "$DOCKER_CONTAINER_ID" ]; then
243 echo "Kill the docker $DOCKER_CONTAINER_ID"
244 docker stop $DOCKER_CONTAINER_ID
245 docker rm -f $DOCKER_CONTAINER_ID
246 unset $DOCKER_CONTAINER_ID
251 # Start the test execution
253 if [ -z "$1" ] && [ -n "$TEST_SELECTION" ]; then
254 # run all known database configs
255 for DBCONFIG in $DBCONFIGS; do
256 execute_tests "$DBCONFIG"
260 if [ -n "$2" ] && [ ! -f "tests/$FILENAME" ] && [ "${FILENAME:0:2}" != "--" ]; then
261 FILENAME="../$FILENAME"
263 execute_tests "$1" "$FILENAME" "$3"