mwx / nextcloud-testing
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
Package info
gitlab.com/metaworx/open-source/php-library/nextcloud-testing
pkg:composer/mwx/nextcloud-testing
Requires
- php: ^8.2
Requires (Dev)
None
Suggests
- nextcloud/ocp: The OCP interfaces the doubles stand in for; the app under test has it
- phpunit/phpunit: The traits call createMock() on the TestCase that uses them; 9.6 and 10.5 both do
Provides
None
Conflicts
None
Replaces
None
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.
| Trait | Method | Stands in for | Over |
|---|---|---|---|
MemoryAppConfig | memoryAppConfig() | OCP\IAppConfig | $this->appValues, one app's keys; getValueType() from the PHP type |
MemorySystemConfig | systemConfig() | OCP\IConfig, system values | $this->systemValues |
MemoryCache | memoryCache(), memoryCacheFactory() | OCP\ICache, OCP\ICacheFactory | $this->cacheValues, each key's TTL in $this->cacheTtls |
EnglishL10n | englishL10n(), markingL10n() | OCP\IL10N | the text itself, %s and %n filled as Nextcloud fills them; marked «text» by the second |
KnownMimeTypes | mimeDetector() | 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.