aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/MediaWikiUnitTestCase.php
diff options
context:
space:
mode:
authordaniel <dkinzler@wikimedia.org>2024-04-24 17:48:19 +0200
committerGergő Tisza <gtisza@wikimedia.org>2024-04-25 13:00:14 +0000
commitc5d29b849a6b79c467328e80b61f0554343cacb1 (patch)
tree7bdb2c5f7023516eed3c9c55694e61b305bf3df8 /tests/phpunit/MediaWikiUnitTestCase.php
parent6182df7d1b9228c28d32b4452845e634b5c1ec9d (diff)
downloadmediawikicore-c5d29b849a6b79c467328e80b61f0554343cacb1.tar.gz
mediawikicore-c5d29b849a6b79c467328e80b61f0554343cacb1.zip
Tests: enable non-integration tests to use TitleFactory service.
This change implements two things that can be combined to allow the usage of services like the TitleFactory in subclasses of MediaWikiUnitTestCase: 1) This adds getServiceContainer() to the MediaWikiUnitTestCase base class, to mirror the method of the same name on MediaWikiIntegrationtest case, along with a a setService() method for defining service objects for use in tests. 2) This adds makeMockTitleFactory() to the MockTitleTrait, to allow tests to esaily define the TitleFactory service. Making services available through a service container primarily benefits code that relies on ObjectFactory, like REST handlers. Needed-By: Iac113a9e766e38b2d19ae99b7e448548a515469e Change-Id: Ida6c37c7133b9e3887268025daf6e5b7c1340e52
Diffstat (limited to 'tests/phpunit/MediaWikiUnitTestCase.php')
-rw-r--r--tests/phpunit/MediaWikiUnitTestCase.php72
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/phpunit/MediaWikiUnitTestCase.php b/tests/phpunit/MediaWikiUnitTestCase.php
index d91a0d6b4abe..a4b438c997c0 100644
--- a/tests/phpunit/MediaWikiUnitTestCase.php
+++ b/tests/phpunit/MediaWikiUnitTestCase.php
@@ -30,6 +30,7 @@ use MediaWiki\Settings\SettingsBuilder;
use PHPUnit\Framework\Exception;
use PHPUnit\Framework\TestCase;
use Wikimedia\ObjectFactory\ObjectFactory;
+use Wikimedia\Services\NoSuchServiceException;
/**
* Base class for unit tests.
@@ -47,6 +48,13 @@ abstract class MediaWikiUnitTestCase extends TestCase {
private static $originalGlobals;
private static $unitGlobals;
+ private ?MediaWikiServices $serviceContainer = null;
+
+ /**
+ * @var array<string,object>
+ */
+ private array $services = [];
+
/**
* List of allowed globals to allow in MediaWikiUnitTestCase.
*
@@ -174,4 +182,68 @@ abstract class MediaWikiUnitTestCase extends TestCase {
SettingsBuilder::enableAccessAfterUnitTests();
}
+ /**
+ * Returns a mock service container.
+ * To populate the service container with service objects, use setService().
+ *
+ * @since 1.43
+ */
+ protected function getServiceContainer(): MediaWikiServices {
+ if ( !$this->serviceContainer ) {
+ $this->serviceContainer = $this->getMockBuilder( MediaWikiServices::class )
+ ->setConstructorArgs( [ new HashConfig() ] )
+ ->onlyMethods( [
+ 'getService',
+ 'disableStorage',
+ 'isStorageDisabled',
+ 'redefineService',
+ 'resetServiceForTesting',
+ 'resetChildProcessServices',
+ 'peekService'
+ ] )
+ ->getMock();
+
+ $this->serviceContainer
+ ->method( 'getService' )
+ ->willReturnCallback( function ( $name ) {
+ return $this->getService( $name );
+ } );
+ }
+
+ return $this->serviceContainer;
+ }
+
+ /**
+ * Returns a service previously defined with setService().
+ *
+ * @param string $name
+ *
+ * @return mixed The service instance
+ */
+ protected function getService( string $name ) {
+ if ( !isset( $this->services[$name] ) ) {
+ throw new NoSuchServiceException( $name );
+ }
+
+ if ( is_callable( $this->services[$name] ) ) {
+ $func = $this->services[$name];
+ $this->services[$name] = $func( $this->serviceContainer );
+ }
+
+ return $this->services[$name];
+ }
+
+ /**
+ * Register a service object with the service container returned by
+ * getServiceContainer().
+ *
+ * @param string $name
+ * @param mixed $service
+ *
+ * @since 1.43
+ */
+ protected function setService( string $name, $service ) {
+ $this->services[$name] = $service;
+ }
+
}