wiki.techinc.nl/tests/phpunit/includes/config/LoggedServiceOptions.php
Aryeh Gregor b058a0e562 Test that classes use all their ServiceOptions
Classes that use ServiceOptions need to declare a list of keys that they
use, typically in self::$constructorOptions. If keys are missing from
that list that are supposed to be present, an exception will be thrown
when someone tries to access them. If keys are present on the list that
are never used, no error is flagged. This means if a dependency on a
given configuration option is removed and nobody updates the list, the
service will keep thinking it depends on that option when it doesn't.
This is messy at best, like an unused variable.

A new and easy-to-use TestAllServiceOptionsUsed trait fixes that
problem. It will log all the ServiceOptions accesses while the test
class runs and raise an error if there are any keys that were never
accessed.

In retrospect, it was probably not worth the time to write this, but
it's a sunk cost now.

Change-Id: Idcaf9a0e2f687069869e6f8057908ffee7dd5f11
2019-08-19 20:42:28 +03:00

36 lines
770 B
PHP

<?php
use MediaWiki\Config\ServiceOptions;
/**
* Helper for TestAllServiceOptionsUsed.
*/
class LoggedServiceOptions extends ServiceOptions {
/** @var array */
private $accessLog;
/**
* @param array &$accessLog Pass self::$serviceOptionsAccessLog from the class implementing
* TestAllServiceOptionsUsed.
* @param string[] $keys
* @param mixed ...$args Forwarded to parent as-is.
*/
public function __construct( array &$accessLog, array $keys, ...$args ) {
$this->accessLog = &$accessLog;
if ( !$accessLog ) {
$accessLog = [ $keys, [] ];
}
parent::__construct( $keys, ...$args );
}
/**
* @param string $key
* @return mixed
*/
public function get( $key ) {
$this->accessLog[1][$key] = true;
return parent::get( $key );
}
}