[MediaWiki-commits] [Gerrit] mediawiki...WikibaseQualityConstraints[master]: Make maintenance script testable
jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/406036 ) Change subject: Make maintenance script testable .. Make maintenance script testable Use Maintenance::getConfig(), which has a corresponding setter which tests can use, instead of getting the config from the services directly. Move code to get the PropertyInfoLookup, and to instantiate the UpdateConstraintsTableJob, into the constructor, where it is stored in private member variables which tests can then later override (using TestingAccessWrapper to get around the access restrictions). And to make sure we can actually find the class in the tests, register its containing directory in the autoload namespaces. Also removes the use of Maintenance::error()’s deprecated $die parameter. There is a fatalError() function we could use instead, but in that case we could completely terminate the tests, which isn’t what we want. Instead, simply return from execute(). Change-Id: I57f5a102f542db0b59347ec82fe85277ffb95560 --- M extension.json M maintenance/ImportConstraintStatements.php 2 files changed, 29 insertions(+), 6 deletions(-) Approvals: jenkins-bot: Verified Thiemo Kreuz (WMDE): Looks good to me, approved diff --git a/extension.json b/extension.json index bc3d7c8..e60e04d 100644 --- a/extension.json +++ b/extension.json @@ -422,6 +422,7 @@ }, "AutoloadNamespaces": { "WikibaseQuality\\ConstraintReport\\": "src/", + "WikibaseQuality\\ConstraintReport\\Maintenance\\": "maintenance/", "WikibaseQuality\\ConstraintReport\\Tests\\": "tests/phpunit/" }, "manifest_version": 2 diff --git a/maintenance/ImportConstraintStatements.php b/maintenance/ImportConstraintStatements.php index 2a17564..db942c3 100644 --- a/maintenance/ImportConstraintStatements.php +++ b/maintenance/ImportConstraintStatements.php @@ -4,14 +4,16 @@ use Maintenance; use Title; -use MediaWiki\MediaWikiServices; +use Wikibase\Lib\Store\PropertyInfoLookup; use WikibaseQuality\ConstraintReport\UpdateConstraintsTableJob; use Wikibase\Repo\WikibaseRepo; +// @codeCoverageIgnoreStart $basePath = getenv( "MW_INSTALL_PATH" ) !== false ? getenv( "MW_INSTALL_PATH" ) : __DIR__ . "/../../.."; require_once $basePath . "/maintenance/Maintenance.php"; +// @codeCoverageIgnoreEnd /** * Runs {@link UpdateConstraintsTableJob} once for every property. @@ -20,22 +22,42 @@ */ class ImportConstraintStatements extends Maintenance { + /** +* @var PropertyInfoLookup +*/ + private $propertyInfoLookup; + + /** +* @var callable +* @param string $propertyIdSerialization +* @return UpdateConstraintsTableJob +*/ + private $newUpdateConstraintsTableJob; + public function __construct() { parent::__construct(); + $repo = WikibaseRepo::getDefaultInstance(); + $this->propertyInfoLookup = $repo->getStore()->getPropertyInfoLookup(); + $this->newUpdateConstraintsTableJob = function ( $propertyIdSerialization ) { + return UpdateConstraintsTableJob::newFromGlobalState( + Title::newMainPage(), + [ 'propertyId' => $propertyIdSerialization ] + ); + }; $this->addDescription( 'Imports property constraints from statements on properties' ); } public function execute() { - if ( !MediaWikiServices::getInstance()->getMainConfig()->get( 'WBQualityConstraintsEnableConstraintsImportFromStatements' ) ) { - $this->error( 'Constraint statements are not enabled. Aborting.', 1 ); + if ( !$this->getConfig()->get( 'WBQualityConstraintsEnableConstraintsImportFromStatements' ) ) { + $this->error( 'Constraint statements are not enabled. Aborting.' ); + return; } - $propertyInfoLookup = WikibaseRepo::getDefaultInstance()->getStore()->getPropertyInfoLookup(); - foreach ( $propertyInfoLookup->getAllPropertyInfo() as $propertyIdSerialization => $info ) { + foreach ( $this->propertyInfoLookup->getAllPropertyInfo() as $propertyIdSerialization => $info ) { $this->output( sprintf( 'Importing constraint statements for % 6s... ', $propertyIdSerialization ), $propertyIdSerialization ); $startTime = microtime( true ); - $job = UpdateConstraintsTableJob::newFromGlobalState( Title::newMainPage(), [ 'propertyId' => $propertyIdSerialization ] ); + $job = call_user_func( $this->newUpdateConstraintsTableJob, $propertyIdSerialization ); $job->run(); $endTime =
[MediaWiki-commits] [Gerrit] mediawiki...WikibaseQualityConstraints[master]: Make maintenance script testable
Lucas Werkmeister (WMDE) has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/406036 ) Change subject: Make maintenance script testable .. Make maintenance script testable Use Maintenance::getConfig(), which has a corresponding setter which tests can use, instead of getting the config from the services directly. Move code to get the PropertyInfoLookup, and to instantiate the UpdateConstraintsTableJob, into the constructor, where it is stored in private member variables which tests can then later override (using TestingAccessWrapper to get around the access restrictions). And to make sure we can actually find the class in the tests, register its containing directory in the autoload namespaces. Also removes the use of Maintenance::error()’s deprecated $die parameter. There is a fatalError() function we could use instead, but in that case we could completely terminate the tests, which isn’t what we want. Instead, simply return from execute(). Change-Id: I57f5a102f542db0b59347ec82fe85277ffb95560 --- M extension.json M maintenance/ImportConstraintStatements.php 2 files changed, 28 insertions(+), 6 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQualityConstraints refs/changes/36/406036/1 diff --git a/extension.json b/extension.json index bc3d7c8..e60e04d 100644 --- a/extension.json +++ b/extension.json @@ -422,6 +422,7 @@ }, "AutoloadNamespaces": { "WikibaseQuality\\ConstraintReport\\": "src/", + "WikibaseQuality\\ConstraintReport\\Maintenance\\": "maintenance/", "WikibaseQuality\\ConstraintReport\\Tests\\": "tests/phpunit/" }, "manifest_version": 2 diff --git a/maintenance/ImportConstraintStatements.php b/maintenance/ImportConstraintStatements.php index 2a17564..f0d640c 100644 --- a/maintenance/ImportConstraintStatements.php +++ b/maintenance/ImportConstraintStatements.php @@ -4,14 +4,16 @@ use Maintenance; use Title; -use MediaWiki\MediaWikiServices; +use Wikibase\Lib\Store\PropertyInfoLookup; use WikibaseQuality\ConstraintReport\UpdateConstraintsTableJob; use Wikibase\Repo\WikibaseRepo; +// @codeCoverageIgnoreStart $basePath = getenv( "MW_INSTALL_PATH" ) !== false ? getenv( "MW_INSTALL_PATH" ) : __DIR__ . "/../../.."; require_once $basePath . "/maintenance/Maintenance.php"; +// @codeCoverageIgnoreEnd /** * Runs {@link UpdateConstraintsTableJob} once for every property. @@ -20,22 +22,41 @@ */ class ImportConstraintStatements extends Maintenance { + /** +* @var PropertyInfoLookup +*/ + private $propertyInfoLookup; + + /** +* @var callable +* @return UpdateConstraintsTableJob +*/ + private $newUpdateConstraintsTableJob; + public function __construct() { parent::__construct(); + $repo = WikibaseRepo::getDefaultInstance(); + $this->propertyInfoLookup = $repo->getStore()->getPropertyInfoLookup(); + $this->newUpdateConstraintsTableJob = function ( $propertyIdSerialization ) { + return UpdateConstraintsTableJob::newFromGlobalState( + Title::newMainPage(), + [ 'propertyId' => $propertyIdSerialization ] + ); + }; $this->addDescription( 'Imports property constraints from statements on properties' ); } public function execute() { - if ( !MediaWikiServices::getInstance()->getMainConfig()->get( 'WBQualityConstraintsEnableConstraintsImportFromStatements' ) ) { - $this->error( 'Constraint statements are not enabled. Aborting.', 1 ); + if ( !$this->getConfig()->get( 'WBQualityConstraintsEnableConstraintsImportFromStatements' ) ) { + $this->error( 'Constraint statements are not enabled. Aborting.' ); + return; } - $propertyInfoLookup = WikibaseRepo::getDefaultInstance()->getStore()->getPropertyInfoLookup(); - foreach ( $propertyInfoLookup->getAllPropertyInfo() as $propertyIdSerialization => $info ) { + foreach ( $this->propertyInfoLookup->getAllPropertyInfo() as $propertyIdSerialization => $info ) { $this->output( sprintf( 'Importing constraint statements for % 6s... ', $propertyIdSerialization ), $propertyIdSerialization ); $startTime = microtime( true ); - $job = UpdateConstraintsTableJob::newFromGlobalState( Title::newMainPage(), [ 'propertyId' => $propertyIdSerialization ] ); + $job = call_user_func( $this->newUpdateConstraintsTableJob, $propertyIdSerialization ); $job->run(); $endTime = microtime(