From caf05dae9d01139d7a0280f1dfc6715543dd4a17 Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Fri, 21 Nov 2025 22:13:45 +0200 Subject: [PATCH] chore(test): improve local testing --- Makefile | 31 ++++++++++++++++++++++++++++--- tests/bootstrap.php | 37 +++++++++++++++++++++++++++++++++---- tests/phpunit.local.xml | 18 ++++++++++++++++++ 3 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 tests/phpunit.local.xml diff --git a/Makefile b/Makefile index d9f197e..96d233b 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,7 @@ composer_phar=$(build_tools_directory)/composer.phar composer_bin := $(if $(composer),$(composer),php $(composer_phar)) pnpm_wrapper=$(build_tools_directory)/pnpm.sh pnpm_cmd=$(if $(pnpm),$(pnpm),$(pnpm_wrapper)) +NEXTCLOUD_ROOT= # Default target: install deps & build JS (and PHP if composer.json exists) all: build @@ -198,11 +199,35 @@ appstore: tar czf $(appstore_package_name).tar.gz $(app_name) # test: -# - Run PHP unit tests (standard + optional integration config) +# - Run PHP unit tests locally with a configured Nextcloud installation +# - Requires: A fully configured and installed Nextcloud instance with database +# - Auto-detects Nextcloud installation or uses NEXTCLOUD_ROOT env var +# - RECOMMENDED: Use 'make test-docker' instead (works in any environment) .PHONY: test test: composer - $(CURDIR)/vendor/phpunit/phpunit/phpunit -c tests/phpunit.xml - ( test ! -f tests/phpunit.integration.xml ) || $(CURDIR)/vendor/phpunit/phpunit/phpunit -c tests/phpunit.integration.xml + @NC_ROOT="$$NEXTCLOUD_ROOT"; \ + if [ -z "$$NC_ROOT" ]; then \ + if [ -d "$(CURDIR)/../../../tests/bootstrap.php" ]; then \ + NC_ROOT="$(CURDIR)/../../.."; \ + fi; \ + fi; \ + if [ -z "$$NC_ROOT" ]; then \ + echo "\x1b[33mCould not find Nextcloud installation.\x1b[0m"; \ + echo ""; \ + echo "Local testing requires a fully configured Nextcloud instance."; \ + echo ""; \ + echo "Options:"; \ + echo " 1. Use Docker tests (recommended): \x1b[32mmake test-docker\x1b[0m"; \ + echo " 2. Set NEXTCLOUD_ROOT to your Nextcloud installation:"; \ + echo " \x1b[32mNEXTCLOUD_ROOT=/path/to/nextcloud make test\x1b[0m"; \ + echo ""; \ + exit 1; \ + fi; \ + echo "\x1b[32mUsing Nextcloud root: $$NC_ROOT\x1b[0m"; \ + NEXTCLOUD_ROOT="$$NC_ROOT" $(CURDIR)/vendor/phpunit/phpunit/phpunit -c tests/phpunit.local.xml; \ + if [ -f tests/phpunit.integration.xml ]; then \ + NEXTCLOUD_ROOT="$$NC_ROOT" $(CURDIR)/vendor/phpunit/phpunit/phpunit -c tests/phpunit.integration.xml; \ + fi # test-docker: # - Run PHP unit tests inside a Nextcloud Docker container diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d12087b..85b2040 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -2,8 +2,37 @@ declare(strict_types=1); -require_once __DIR__ . '/../../../tests/bootstrap.php'; -require_once __DIR__ . '/../vendor/autoload.php'; +// Detect Nextcloud bootstrap location +// Priority: 1) NEXTCLOUD_ROOT env var, 2) Docker location, 3) Standard location +$nextcloudBootstrap = null; -\OC_App::loadApp(OCA\Forum\AppInfo\Application::APP_ID); -OC_Hook::clear(); +// Check both $_ENV and getenv() as they can differ depending on PHP configuration +$nextcloudRoot = $_ENV['NEXTCLOUD_ROOT'] ?? getenv('NEXTCLOUD_ROOT'); + +if (!empty($nextcloudRoot)) { + // Use NEXTCLOUD_ROOT environment variable (set by Makefile for local testing) + $nextcloudBootstrap = $nextcloudRoot . '/tests/bootstrap.php'; +} elseif (file_exists(__DIR__ . '/../../../tests/bootstrap.php')) { + // Standard location (Docker/installed in Nextcloud apps directory) + $nextcloudBootstrap = __DIR__ . '/../../../tests/bootstrap.php'; +} + +if ($nextcloudBootstrap && file_exists($nextcloudBootstrap)) { + // Running with full Nextcloud environment + // Define OC_CONSOLE to bypass installation check during tests + if (!defined('OC_CONSOLE')) { + define('OC_CONSOLE', 1); + } + require_once $nextcloudBootstrap; + require_once __DIR__ . '/../vendor/autoload.php'; + \OC_App::loadApp(OCA\Forum\AppInfo\Application::APP_ID); + OC_Hook::clear(); +} else { + // Cannot find Nextcloud bootstrap + echo "\n\033[31mError: Nextcloud bootstrap not found.\033[0m\n"; + echo "For local testing, set NEXTCLOUD_ROOT environment variable:\n"; + echo " NEXTCLOUD_ROOT=~/Dev/nextcloud-docker-dev/workspace/server make test\n"; + echo "\nOr run tests in Docker:\n"; + echo " make test-docker\n\n"; + exit(1); +} diff --git a/tests/phpunit.local.xml b/tests/phpunit.local.xml new file mode 100644 index 0000000..4ae32a2 --- /dev/null +++ b/tests/phpunit.local.xml @@ -0,0 +1,18 @@ + + + + + . + + + + + ../appinfo + ../lib + + +