Search by

mwx / nextcloud-testing

metaworx

Test doubles for a Nextcloud app's unit tests: an in-memory IAppConfig, system config and ICache, an IL10N that answers in English, a MIME type detector, the Emitter stub, and a bootstrap that finds the server or falls back to nextcloud/ocp

0.1.0 2026-11-03 03:48 UTC

This package is not auto-updated.

Last update: 2026-11-03 02:53:45 UTC


README

Test doubles for a Nextcloud app's unit tests. Owns the mwx\Nextcloud\Testing\ namespace prefix. MIT, requires php alone: the OCP interfaces come from the app under test (nextcloud/ocp), and the traits call createMock() on the PHPUnit TestCase that uses them, 9.6 and 10.5 alike.

composer require --dev mwx/nextcloud-testing

The doubles

Each is a trait for a class extending TestCase; the values it answers with are a protected array of the test, to assert on or to fill first.

TraitMethodStands in forOver
MemoryAppConfigmemoryAppConfig()OCP\IAppConfig$this->appValues, one app's keys; getValueType() from the PHP type
MemorySystemConfigsystemConfig()OCP\IConfig, system values$this->systemValues
MemoryCachememoryCache(), memoryCacheFactory()OCP\ICache, OCP\ICacheFactory$this->cacheValues, each key's TTL in $this->cacheTtls
EnglishL10nenglishL10n(), markingL10n()OCP\IL10Nthe text itself, %s and %n filled as Nextcloud fills them; marked «text» by the second
KnownMimeTypesmimeDetector()OCP\Files\IMimeTypeDetector$this->knownMimeTypes, extension to type

An app composes them with its own helpers in one trait of its own:

trait AppDoubles
{
    use \mwx\Nextcloud\Testing\MemoryAppConfig;
    use \mwx\Nextcloud\Testing\MemorySystemConfig;
    use \mwx\Nextcloud\Testing\EnglishL10n;

    protected function settings(): ConfigurationService
    {
        return new ConfigurationService( $this->memoryAppConfig(), $this->englishL10n(), $this->systemConfig() );
    }
}

The bootstrap

tests/bootstrap.php of an app becomes:

<?php

require_once dirname( __DIR__ ) . '/vendor/autoload.php';

\mwx\Nextcloud\Testing\Bootstrap::nextcloud( dirname( __DIR__ ), 'MYAPP_NC_ROOT' );

It takes the first of three that answers: a server the app is deployed into, booted through lib/base.php; a server source tree beside the app (nextcloud-v33 and up, highest first) or the one the environment variable names, autoloaders only; or no server at all, the OCP API from nextcloud/ocp and this package's stub of OC\Hooks\Emitter, which OCP\Files\IRootFolder extends and nextcloud/ocp does not ship. The stub is registered on that third path alone, never by the package's autoload: inside a server the real interface must win. Psalm may read the same stub, vendor/mwx/nextcloud-testing/stubs/OC/Hooks/Emitter.php.

Development

In the monorepo, vendor/bin/phpunit --testsuite nextcloud-testing runs this package's tests, against nextcloud/ocp in the root's require-dev.