diff options
author | Daimona Eaytoy <daimona.wiki@gmail.com> | 2023-07-17 00:14:38 +0200 |
---|---|---|
committer | Daimona Eaytoy <daimona.wiki@gmail.com> | 2023-07-25 12:12:43 +0000 |
commit | 4c72c161d87b6b1f02e131157ba43a56b3f650ea (patch) | |
tree | 927662878620f10811aa255472b27be5ae95370f /tests/phpunit/includes/api/ApiTestCase.php | |
parent | 67a7658e4378c76f6b4d49a09887a07255ca4179 (diff) | |
download | mediawikicore-4c72c161d87b6b1f02e131157ba43a56b3f650ea.tar.gz mediawikicore-4c72c161d87b6b1f02e131157ba43a56b3f650ea.zip |
Deprecate MediaWikiIntegrationTestCase::$users
This property shouldn't be needed in modern code, where most things can
use Authority. So much so because TestUser adds a dependency on the
database, but many tests that use TestUser don't even need the database.
ApiTestCase, in particular, sets this property in setUp, thus adding a
database dependency to all API tests, including those that don't need
users or the database at all.
Deprecate the property and replace existing usages in core. The one in
ApiTestCase is much harder to migrate, but this patch replaces the array
with an anonymous ArrayAccess class to allow lazy initialization and
remove DB dependencies.
Bug: T155147
Change-Id: I59c4ed1f6a7572d3a92387b15b8e56625bc376a2
Diffstat (limited to 'tests/phpunit/includes/api/ApiTestCase.php')
-rw-r--r-- | tests/phpunit/includes/api/ApiTestCase.php | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/tests/phpunit/includes/api/ApiTestCase.php b/tests/phpunit/includes/api/ApiTestCase.php index 6db118737fa4..569ebfe1ba11 100644 --- a/tests/phpunit/includes/api/ApiTestCase.php +++ b/tests/phpunit/includes/api/ApiTestCase.php @@ -23,10 +23,45 @@ abstract class ApiTestCase extends MediaWikiLangTestCase { parent::setUp(); self::$apiUrl = $wgServer . wfScript( 'api' ); - self::$users = [ - 'sysop' => static::getTestSysop(), - 'uploader' => static::getTestUser(), + // HACK: Avoid creating test users in the DB if the test may not need them. + $getters = [ + 'sysop' => fn () => $this->getTestSysop(), + 'uploader' => fn () => $this->getTestUser(), ]; + $fakeUserArray = new class ( $getters ) implements ArrayAccess { + private array $getters; + private array $extraUsers = []; + + public function __construct( array $getters ) { + $this->getters = $getters; + } + + public function offsetExists( $offset ): bool { + return isset( $this->getters[$offset] ) || isset( $this->extraUsers[$offset] ); + } + + #[\ReturnTypeWillChange] + public function offsetGet( $offset ) { + if ( isset( $this->getters[$offset] ) ) { + return ( $this->getters[$offset] )(); + } + if ( isset( $this->extraUsers[$offset] ) ) { + return $this->extraUsers[$offset]; + } + throw new LogicException( "Requested unknown user $offset" ); + } + + public function offsetSet( $offset, $value ): void { + $this->extraUsers[$offset] = $value; + } + + public function offsetUnset( $offset ): void { + unset( $this->getters[$offset] ); + unset( $this->extraUsers[$offset] ); + } + }; + + self::$users = $fakeUserArray; $this->setRequest( new FauxRequest( [] ) ); |