<?php
use Composer\Semver\Semver;
+use Wikimedia\ScopedCallback;
/**
* ExtensionRegistry class
*/
protected $attributes = [];
+ /**
+ * Attributes for testing
+ *
+ * @var array
+ */
+ protected $testAttributes = [];
+
/**
* @var ExtensionRegistry
*/
* @return array
*/
public function getAttribute( $name ) {
- return $this->attributes[$name] ?? [];
+ return $this->testAttributes[$name] ??
+ $this->attributes[$name] ?? [];
+ }
+
+ /**
+ * Force override the value of an attribute during tests
+ *
+ * @param string $name Name of attribute to override
+ * @param array $value Value to set
+ * @return ScopedCallback to reset
+ * @since 1.33
+ */
+ public function setAttributeForTest( $name, array $value ) {
+ if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
+ throw new RuntimeException( __METHOD__ . ' can only be used in tests' );
+ }
+ if ( isset( $this->testAttributes[$name] ) ) {
+ throw new Exception( "The attribute '$name' has already been overridden" );
+ }
+ $this->testAttributes[$name] = $value;
+ return new ScopedCallback( function () use ( $name ) {
+ unset( $this->testAttributes[$name] );
+ } );
}
/**
<?php
+use Wikimedia\ScopedCallback;
+
/**
* @covers ExtensionRegistry
*/
],
];
}
+
+ public function testSetAttributeForTest() {
+ $registry = new ExtensionRegistry();
+ $registry->queue( "{$this->dataDir}/good.json" );
+ $registry->loadFromQueue();
+ // Sanity check that it worked
+ $this->assertSame( [ 'test' ], $registry->getAttribute( 'FooBarAttr' ) );
+ $reset = $registry->setAttributeForTest( 'FooBarAttr', [ 'override' ] );
+ // overridden properly
+ $this->assertSame( [ 'override' ], $registry->getAttribute( 'FooBarAttr' ) );
+ ScopedCallback::consume( $reset );
+ // reset properly
+ $this->assertSame( [ 'test' ], $registry->getAttribute( 'FooBarAttr' ) );
+ }
+
+ /**
+ * @expectedException Exception
+ * @expectedExceptionMessage The attribute 'foo' has already been overridden
+ */
+ public function testSetAttributeForTestDuplicate() {
+ $registry = new ExtensionRegistry();
+ $reset1 = $registry->setAttributeForTest( 'foo', [ 'val1' ] );
+ $reset2 = $registry->setAttributeForTest( 'foo', [ 'val2' ] );
+ }
}