test: support local tests

This commit is contained in:
2025-11-21 23:03:03 +02:00
parent 2783548ce8
commit 289efc5226
4 changed files with 94 additions and 14 deletions

View File

@@ -1,7 +1,7 @@
# SPDX-FileCopyrightText: Bernhard Posselt <dev@bernhard-posselt.com>
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# Nextcloud App Template — Makefile
# NextcloudAppTemplate — Makefile
# ---------------------------------
# A friendly, batteries-included Makefile for building and packaging a Nextcloud app
# that uses pnpm (JS) and Composer (PHP).
@@ -42,6 +42,10 @@ composer_bin := $(if $(composer),$(composer),php $(composer_phar))
pnpm_wrapper=$(build_tools_directory)/pnpm.sh
pnpm_cmd=$(if $(pnpm),$(pnpm),$(pnpm_wrapper))
# Optional: Set path to Nextcloud installation for local testing
# Can be overridden by environment variable: NEXTCLOUD_ROOT=/path make test
NEXTCLOUD_ROOT ?=
# Default target: install deps & build JS (and PHP if composer.json exists)
all: build
@@ -198,11 +202,38 @@ 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 (Makefile var or 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 [ -n "$$NC_ROOT" ]; then \
NC_ROOT=$$(echo "$$NC_ROOT" | sed "s|^\\\~|$$HOME|" | sed "s|^~|$$HOME|"); \
fi; \
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 in Makefile (line 47) or as env var:"; \
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

View File

@@ -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\NextcloudAppTemplate\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\NextcloudAppTemplate\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);
}

18
tests/phpunit.local.xml Normal file
View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="bootstrap.php"
cacheDirectory=".phpunit.cache"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
colors="true">
<testsuites>
<testsuite name="NextcloudAppTemplate Tests">
<directory suffix="Test.php">.</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">../appinfo</directory>
<directory suffix=".php">../lib</directory>
</include>
</source>
</phpunit>

View File

@@ -1,12 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="bootstrap.php" timeoutForSmallTests="900" timeoutForMediumTests="900" timeoutForLargeTests="900" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache">
<testsuite name="Auto Currency Tests">
<directory suffix="Test.php">.</directory>
</testsuite>
<source>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="bootstrap.php" timeoutForSmallTests="900" timeoutForMediumTests="900" timeoutForLargeTests="900" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">../appinfo</directory>
<directory suffix=".php">../lib</directory>
</include>
</source>
</coverage>
<testsuites>
<testsuite name="NextcloudAppTemplate Tests">
<directory suffix="Test.php">.</directory>
</testsuite>
</testsuites>
</phpunit>