aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/MediaWikiServicesTest.php
blob: 188957569bf551caeafaafcf6ab935fb45711aa4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
use Liuggio\StatsdClient\Factory\StatsdDataFactory;
use MediaWiki\MediaWikiServices;

/**
 * @covers MediaWiki\MediaWikiServices
 *
 * @group MediaWiki
 */
class MediaWikiServicesTest extends PHPUnit_Framework_TestCase {

	public function testGetInstance() {
		$services = MediaWikiServices::getInstance();
		$this->assertInstanceOf( 'MediaWiki\\MediaWikiServices', $services );
	}

	public function provideGetters() {
		// NOTE: This should list all service getters defined in MediaWikiServices.
		// NOTE: For every test case defined here there should be a corresponding
		// test case defined in provideGetService().
		return [
			'BootstrapConfig' => [ 'getBootstrapConfig', Config::class ],
			'ConfigFactory' => [ 'getConfigFactory', ConfigFactory::class ],
			'MainConfig' => [ 'getMainConfig', Config::class ],
			'SiteStore' => [ 'getSiteStore', SiteStore::class ],
			'SiteLookup' => [ 'getSiteLookup', SiteLookup::class ],
			'StatsdDataFactory' => [ 'getStatsdDataFactory', StatsdDataFactory::class ],
		];
	}

	/**
	 * @dataProvider provideGetters
	 */
	public function testGetters( $getter, $type ) {
		// Test against the default instance, since the dummy will not know the default services.
		$services = MediaWikiServices::getInstance();
		$service = $services->$getter();
		$this->assertInstanceOf( $type, $service );
	}

	public function provideGetService() {
		// NOTE: This should list all service getters defined in ServiceWiring.php.
		// NOTE: For every test case defined here there should be a corresponding
		// test case defined in provideGetters().
		return [
			'BootstrapConfig' => [ 'BootstrapConfig', Config::class ],
			'ConfigFactory' => [ 'ConfigFactory', ConfigFactory::class ],
			'MainConfig' => [ 'MainConfig', Config::class ],
			'SiteStore' => [ 'SiteStore', SiteStore::class ],
			'SiteLookup' => [ 'SiteLookup', SiteLookup::class ],
			'StatsdDataFactory' => [ 'StatsdDataFactory', StatsdDataFactory::class ],
		];
	}

	/**
	 * @dataProvider provideGetService
	 */
	public function testGetService( $name, $type ) {
		// Test against the default instance, since the dummy will not know the default services.
		$services = MediaWikiServices::getInstance();

		$service = $services->getService( $name );
		$this->assertInstanceOf( $type, $service );
	}

	public function testDefaultServiceInstantiation() {
		// Check all services in the default instance, not a dummy instance!
		// Note that we instantiate all services here, including any that
		// were registered by extensions.
		$services = MediaWikiServices::getInstance();
		$names = $services->getServiceNames();

		foreach ( $names as $name ) {
			$this->assertTrue( $services->hasService( $name ) );
			$service = $services->getService( $name );
			$this->assertInternalType( 'object', $service );
		}
	}

}