Debian Codegraph @

tests/phpunit/unit/includes/db/MWLBFactoryTest.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 use MediaWiki\Config\ServiceOptions;
22 use Wikimedia\ObjectCache\EmptyBagOStuff;
23 use Wikimedia\ObjectCache\WANObjectCache;
24 use Wikimedia\Rdbms\ChronologyProtector;
25 use Wikimedia\Rdbms\ConfiguredReadOnlyMode;
26 use Wikimedia\Rdbms\DatabaseDomain;
27 use Wikimedia\Rdbms\LBFactorySimple;
28 use Wikimedia\RequestTimeout\CriticalSectionProvider;
29 use Wikimedia\RequestTimeout\RequestTimeout;
30 use Wikimedia\Stats\NullStatsdDataFactory;
31
32 /**
33 * @covers \Wikimedia\Rdbms\LBFactory
34 * @covers \Wikimedia\Rdbms\LBFactorySimple
35 * @covers \Wikimedia\Rdbms\LBFactoryMulti
36 * @covers \MWLBFactory
37 */
38 class MWLBFactoryTest extends MediaWikiUnitTestCase {
39
40 private function newMWLBFactory() {
41 return new MWLBFactory(
42 new ServiceOptions( [], [] ),
43 new ConfiguredReadOnlyMode( 'Test' ),
44 new ChronologyProtector(),
45 new EmptyBagOStuff(),
46 new WANObjectCache( [ 'cache' => new EmptyBagOStuff() ] ),
47 new CriticalSectionProvider( RequestTimeout::singleton(), 1, null, null ),
48 new NullStatsdDataFactory(),
49 []
50 );
51 }
52
53 /**
54 * @dataProvider getLBFactoryClassProvider
55 */
56 public function testGetLBFactoryClass( $config, $expected ) {
57 $this->assertEquals(
58 $expected,
59 $this->newMWLBFactory()->getLBFactoryClass( $config )
60 );
61 }
62
63 public static function getLBFactoryClassProvider() {
64 yield 'undercore alias default' => [
65 [ 'class' => 'LBFactory_Simple' ],
66 Wikimedia\Rdbms\LBFactorySimple::class,
67 ];
68 yield 'short alias multi' => [
69 [ 'class' => 'LBFactoryMulti' ],
70 Wikimedia\Rdbms\LBFactoryMulti::class,
71 ];
72 }
73
74 /**
75 * @dataProvider setDomainAliasesProvider
76 */
77 public function testDomainAliases( $dbname, $prefix, $expectedDomain ) {
78 $servers = [ [
79 'type' => 'sqlite',
80 'dbname' => 'defaultdb',
81 'tablePrefix' => 'defaultprefix_',
82 'dbDirectory' => '~/sqldatadir/',
83 'load' => 0,
84 ] ];
85 $lbFactory = new LBFactorySimple( [
86 'servers' => $servers,
87 'localDomain' => new DatabaseDomain( $dbname, null, $prefix )
88 ] );
89 $this->newMWLBFactory()->setDomainAliases( $lbFactory );
90
91 $rawDomain = rtrim( "$dbname-$prefix", '-' );
92 $this->assertEquals(
93 $expectedDomain,
94 $lbFactory->getMainLB()->resolveDomainID( $rawDomain ),
95 'Domain aliases set'
96 );
97 }
98
99 public function setDomainAliasesProvider() {
100 return [
101 [ 'enwiki', '', 'enwiki' ],
102 [ 'wikipedia', 'fr_', 'wikipedia-fr_' ],
103 [ 'wikipedia', 'zh', 'wikipedia-zh' ],
104 [ 'wiki-pedia', '', 'wiki?hpedia' ],
105 [ 'wiki-pedia', 'es_', 'wiki?hpedia-es_' ],
106 [ 'wiki-pedia', 'ru', 'wiki?hpedia-ru' ]
107 ];
108 }
109 }