Debian Codegraph @

tests/phpunit/unit/includes/filebackend/FileBackendGroupTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Config\ServiceOptions;
4 use MediaWiki\FileBackend\FileBackendGroup;
5 use MediaWiki\FileBackend\FSFile\TempFSFileFactory;
6 use MediaWiki\FileBackend\LockManager\LockManagerGroupFactory;
7 use MediaWiki\Tests\Unit\DummyServicesTrait;
8 use Wikimedia\Mime\MimeAnalyzer;
9 use Wikimedia\ObjectCache\BagOStuff;
10 use Wikimedia\ObjectCache\EmptyBagOStuff;
11 use Wikimedia\ObjectCache\WANObjectCache;
12
13 /**
14 * @coversDefaultClass \MediaWiki\FileBackend\FileBackendGroup
15 */
16 class FileBackendGroupTest extends MediaWikiUnitTestCase {
17 use DummyServicesTrait;
18 use FileBackendGroupTestTrait;
19
20 protected function setUp(): void {
21 parent::setUp();
22 // This config var is not yet dependency-injected.
23 // FileBackendGroup has a default option 'profiler', that holds a closure
24 // that calls Profiler::instance(), which doesn't use service wiring yet.
25 // Without this, the variable would be undefined there.
26 // TODO: Remove this once Profiler uses service wiring and is injected
27 // into FileBackendGroup.
28 $GLOBALS['wgProfiler'] = [];
29 }
30
31 private static function getWikiID() {
32 return 'mywiki';
33 }
34
35 private function getLocalServerCache(): BagOStuff {
36 if ( !$this->srvCache ) {
37 $this->srvCache = new EmptyBagOStuff;
38 }
39 return $this->srvCache;
40 }
41
42 private function getWANObjectCache(): WANObjectCache {
43 if ( !$this->wanCache ) {
44 $this->wanCache = $this->createNoOpMock( WANObjectCache::class );
45 }
46 return $this->wanCache;
47 }
48
49 /**
50 * @param string $domain Expected argument that LockManagerGroupFactory::getLockManagerGroup
51 * will receive
52 * @return LockManagerGroupFactory
53 */
54 private function getLockManagerGroupFactory( $domain = 'mywiki' ): LockManagerGroupFactory {
55 if ( !$this->lmgFactory ) {
56 $mockLmg = $this->createNoOpMock( LockManagerGroup::class, [ 'get' ] );
57 $mockLmg->method( 'get' )->with( 'fsLockManager' )->willReturn( 'string lock manager' );
58
59 $this->lmgFactory = $this->createNoOpMock( LockManagerGroupFactory::class,
60 [ 'getLockManagerGroup' ] );
61 $this->lmgFactory->method( 'getLockManagerGroup' )->with( $domain )
62 ->willReturn( $mockLmg );
63 }
64 return $this->lmgFactory;
65 }
66
67 private function getTempFSFileFactory(): TempFSFileFactory {
68 if ( !$this->tmpFileFactory ) {
69 $this->tmpFileFactory = $this->createNoOpMock( TempFSFileFactory::class );
70 }
71 return $this->tmpFileFactory;
72 }
73
74 /**
75 * @param array $options Dictionary to use as a source for ServiceOptions before defaults, plus
76 * the following options are available to override other arguments:
77 * * 'readOnlyMode'
78 * * 'lmgFactory'
79 * * 'mimeAnalyzer'
80 * * 'tmpFileFactory'
81 * @return FileBackendGroup
82 */
83 private function newObj( array $options = [] ): FileBackendGroup {
84 return new FileBackendGroup(
85 new ServiceOptions(
86 FileBackendGroup::CONSTRUCTOR_OPTIONS, $options, self::getDefaultOptions() ),
87 $this->getDummyReadOnlyMode( $options['readOnlyMode'] ?? false ),
88 $this->getLocalServerCache(),
89 $this->getWANObjectCache(),
90 $options['mimeAnalyzer'] ?? $this->createNoOpMock( MimeAnalyzer::class ),
91 $options['lmgFactory'] ?? $this->getLockManagerGroupFactory(),
92 $options['tmpFileFactory'] ?? $this->getTempFSFileFactory(),
93 $this->getDummyObjectFactory()
94 );
95 }
96
97 }