Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / structure / BundleSizeTestBase.php
blobdd5670640ee3ccc605e1efabf22be150664614f0
1 <?php
3 namespace MediaWiki\Tests\Structure;
5 use MediaWiki\MainConfigNames;
6 use MediaWiki\MediaWikiServices;
7 use MediaWiki\Request\FauxRequest;
8 use MediaWiki\ResourceLoader\Context;
9 use MediaWiki\ResourceLoader\DerivativeContext;
10 use MediaWiki\ResourceLoader\Module;
11 use MediaWikiIntegrationTestCase;
12 use Wikimedia\DependencyStore\DependencyStore;
13 use Wikimedia\ObjectCache\HashBagOStuff;
14 use Wikimedia\Rdbms\IDatabase;
15 use Wikimedia\Rdbms\LBFactory;
17 /**
18 * Compare bundle sizes from each skin/extension bundlesize.config.json with ResourceLoader output.
20 * Extensions and skins can subclass this and override getTestCases with just their own bundlesize
21 * file. This allows one to run that test suite by its own, for faster CLI feedback.
23 abstract class BundleSizeTestBase extends MediaWikiIntegrationTestCase {
24 protected function setUp(): void {
25 parent::setUp();
26 $db = $this->createMock( IDatabase::class );
27 $db->method( 'getSessionLagStatus' )->willReturn( [ 'lag' => 0, 'since' => 0 ] );
28 $lbFactory = $this->createMock( LBFactory::class );
29 $lbFactory->method( 'getReplicaDatabase' )->willReturn( $db );
30 $this->setService( 'DBLoadBalancerFactory', $lbFactory );
33 /**
34 * Adjustments for bundle size increases caused by core, to avoid breaking
35 * previously introduced extension tests.
37 private const CORE_SIZE_ADJUSTMENTS = [
38 'mw.loader.impl' => 17
41 public function provideBundleSize() {
42 foreach ( json_decode( file_get_contents( $this->getBundleSizeConfig() ), true ) as $testCase ) {
43 yield $testCase['resourceModule'] => [ $testCase ];
47 /**
48 * @dataProvider provideBundleSize
49 * @coversNothing
51 public function testBundleSize( $testCase ) {
52 $maxSize = $testCase['maxSize'];
53 $projectName = $testCase['projectName'] ?? '';
54 $moduleName = $testCase['resourceModule'];
55 if ( $maxSize === null ) {
56 $this->markTestSkipped( "The module $moduleName has opted out of bundle size testing." );
58 if ( is_string( $maxSize ) ) {
59 if ( str_contains( $maxSize, 'KB' ) || str_contains( $maxSize, 'kB' ) ) {
60 $maxSize = (float)str_replace( [ 'KB', 'kB', ' KB', ' kB' ], '', $maxSize );
61 $maxSize = $maxSize * 1024;
62 } elseif ( str_contains( $maxSize, 'B' ) ) {
63 $maxSize = (float)str_replace( [ ' B', 'B' ], '', $maxSize );
66 $resourceLoader = MediaWikiServices::getInstance()->getResourceLoader();
67 $resourceLoader->setDependencyStore( new DependencyStore( new HashBagOStuff() ) );
68 $request = new FauxRequest(
70 'lang' => 'en',
71 'modules' => $moduleName,
72 'skin' => $this->getSkinName(),
76 $context = new Context( $resourceLoader, $request );
77 $module = $resourceLoader->getModule( $moduleName );
78 $contentContext = new DerivativeContext( $context );
79 $contentContext->setOnly(
80 $module->getType() === Module::LOAD_STYLES
81 ? Module::TYPE_STYLES
82 : Module::TYPE_COMBINED
84 $content = $resourceLoader->makeModuleResponse( $context, [ $moduleName => $module ] );
85 $contentTransferSize = strlen( gzencode( $content, 9 ) );
86 $contentTransferSize -= array_sum( self::CORE_SIZE_ADJUSTMENTS );
87 $message = $projectName ?
88 "$projectName: $moduleName is less than $maxSize" :
89 "$moduleName is less than $maxSize";
90 $this->assertLessThan( $maxSize, $contentTransferSize, $message );
93 /**
94 * @return string Path to bundlesize.config.json
96 abstract public function getBundleSizeConfig(): string;
98 /**
99 * @return string Skin name
101 public function getSkinName(): string {
102 return $this->getConfVar( MainConfigNames::DefaultSkin );