Completed
Push — master ( 32dcda...951284 )
by
unknown
06:45 queued 10s
created

GlobalStateFactoryMethodsResourceTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\Lib\Tests;
4
5
use IBufferingStatsdDataFactory;
6
use MediaWiki\Http\HttpRequestFactory;
7
use MediaWikiIntegrationTestCase;
8
use Psr\Log\LoggerInterface;
9
use Wikibase\Lib\StatsdRecordingSimpleCache;
10
use Wikibase\Lib\TermFallbackCache\TermFallbackCacheServiceFactory;
11
use Wikibase\Lib\TermFallbackCacheFactory;
12
use Wikibase\Lib\WikibaseContentLanguages;
13
use Wikibase\Lib\WikibaseSettings;
14
use Wikimedia\Rdbms\ILoadBalancer;
15
use Wikimedia\Rdbms\LBFactory;
16
17
/**
18
 * Test to assert that factory methods of hook service classes (and similar services)
19
 * don't access the database or do http requests (which would be a performance issue).
20
 *
21
 * @group Wikibase
22
 *
23
 * @license GPL-2.0-or-later
24
 * @author Marius Hoch
25
 */
26
class GlobalStateFactoryMethodsResourceTest extends MediaWikiIntegrationTestCase {
27
28
	protected function setUp(): void {
29
		parent::setUp();
30
31
		// Factory methods should never access the database or do http requests
32
		// https://phabricator.wikimedia.org/T243729
33
		$this->disallowDBAccess();
34
		$this->disallowHttpAccess();
35
	}
36
37
	public function testWikibaseContentLanguages(): void {
38
		WikibaseContentLanguages::getDefaultInstance();
39
		WikibaseContentLanguages::getDefaultMonolingualTextLanguages();
40
		WikibaseContentLanguages::getDefaultTermsLanguages();
41
		$this->assertTrue( true );
42
	}
43
44
	public function testWikibaseSettings_clientSettings(): void {
45
		if ( !WikibaseSettings::isClientEnabled() ) {
46
			$this->markTestSkipped(
47
				'Can only get client settings, if client is enabled'
48
			);
49
		}
50
		WikibaseSettings::getClientSettings();
51
		$this->assertTrue( true );
52
	}
53
54
	public function testWikibaseSettings_repoSettings(): void {
55
		if ( !WikibaseSettings::isRepoEnabled() ) {
56
			$this->markTestSkipped(
57
				'Can only get repo settings, if repo is enabled'
58
			);
59
		}
60
		WikibaseSettings::getRepoSettings();
61
		$this->assertTrue( true );
62
	}
63
64
	/**
65
	 * @dataProvider cacheTypeProvider
66
	 */
67
	public function testTermFallbackCacheFactory( $sharedCacheType ): void {
68
		$logger = $this->createMock( LoggerInterface::class );
69
		$factory = new TermFallbackCacheFactory(
70
			$sharedCacheType,
71
			$logger,
72
			$this->createMock( IBufferingStatsdDataFactory::class ),
73
			'secret',
74
			new TermFallbackCacheServiceFactory(),
75
			null
76
		);
77
		$this->assertInstanceOf( StatsdRecordingSimpleCache::class, $factory->getTermFallbackCache() );
78
	}
79
80
	public function cacheTypeProvider(): array {
81
		return [
82
			[ CACHE_ANYTHING ],
83
			[ CACHE_NONE ],
84
			[ CACHE_DB ],
85
			[ CACHE_MEMCACHED ],
86
			[ CACHE_ACCEL ],
87
		];
88
	}
89
90
	private function disallowDBAccess() {
91
		$this->setService(
92
			'DBLoadBalancerFactory',
93
			function() {
94
				$lb = $this->createMock( ILoadBalancer::class );
95
				$lb->expects( $this->never() )
96
					->method( 'getConnection' );
97
				$lb->expects( $this->never() )
98
					->method( 'getConnectionRef' );
99
				$lb->expects( $this->never() )
100
					->method( 'getMaintenanceConnectionRef' );
101
				$lb->expects( $this->any() )
102
					->method( 'getLocalDomainID' )
103
					->willReturn( 'banana' );
104
105
				$lbFactory = $this->createMock( LBFactory::class );
106
				$lbFactory->expects( $this->any() )
107
					->method( 'getMainLB' )
108
					->willReturn( $lb );
109
110
				return $lbFactory;
111
			}
112
		);
113
	}
114
115
	private function disallowHttpAccess() {
116
		$this->setService(
117
			'HttpRequestFactory',
118
			function() {
119
				$factory = $this->createMock( HttpRequestFactory::class );
120
				$factory->expects( $this->never() )
121
					->method( 'create' );
122
				$factory->expects( $this->never() )
123
					->method( 'request' );
124
				$factory->expects( $this->never() )
125
					->method( 'get' );
126
				$factory->expects( $this->never() )
127
					->method( 'post' );
128
				return $factory;
129
			}
130
		);
131
	}
132
133
}
134