aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/structure/BundleSizeTestBase.php
blob: 0057a747fce2dca1eaed7673e8a612a8b649cbff (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php

namespace MediaWiki\Tests\Structure;

use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Request\FauxRequest;
use MediaWiki\ResourceLoader\Context;
use MediaWiki\ResourceLoader\DerivativeContext;
use MediaWiki\ResourceLoader\Module;
use MediaWikiIntegrationTestCase;
use Wikimedia\DependencyStore\KeyValueDependencyStore;
use Wikimedia\ObjectCache\HashBagOStuff;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\LBFactory;

/**
 * Compare bundle sizes from each skin/extension bundlesize.config.json with ResourceLoader output.
 *
 * Extensions and skins can subclass this and override getTestCases with just their own bundlesize
 * file. This allows one to run that test suite by its own, for faster CLI feedback.
 */
abstract class BundleSizeTestBase extends MediaWikiIntegrationTestCase {
	protected function setUp(): void {
		parent::setUp();
		$db = $this->createMock( IDatabase::class );
		$db->method( 'getSessionLagStatus' )->willReturn( [ 'lag' => 0, 'since' => 0 ] );
		$lbFactory = $this->createMock( LBFactory::class );
		$lbFactory->method( 'getReplicaDatabase' )->willReturn( $db );
		$this->setService( 'DBLoadBalancerFactory', $lbFactory );
	}

	/**
	 * Adjustments for bundle size increases caused by core, to avoid breaking
	 * previously introduced extension tests.
	 */
	private const CORE_SIZE_ADJUSTMENTS = [
		'mw.loader.impl' => 17
	];

	public function provideBundleSize() {
		foreach ( json_decode( file_get_contents( $this->getBundleSizeConfig() ), true ) as $testCase ) {
			yield $testCase['resourceModule'] => [ $testCase ];
		}
	}

	/**
	 * @dataProvider provideBundleSize
	 * @coversNothing
	 */
	public function testBundleSize( $testCase ) {
		$maxSize = $testCase['maxSize'];
		$projectName = $testCase['projectName'] ?? '';
		$moduleName = $testCase['resourceModule'];
		if ( is_string( $maxSize ) ) {
			if ( str_contains( $maxSize, 'KB' ) || str_contains( $maxSize, 'kB' ) ) {
				$maxSize = (float)str_replace( [ 'KB', 'kB', ' KB', ' kB' ], '', $maxSize );
				$maxSize = $maxSize * 1024;
			} elseif ( str_contains( $maxSize, 'B' ) ) {
				$maxSize = (float)str_replace( [ ' B', 'B' ], '', $maxSize );
			}
		}
		$resourceLoader = MediaWikiServices::getInstance()->getResourceLoader();
		$resourceLoader->setDependencyStore( new KeyValueDependencyStore( new HashBagOStuff() ) );
		$request = new FauxRequest(
			[
				'lang' => 'en',
				'modules' => $moduleName,
				'skin' => $this->getSkinName(),
			]
		);

		$context = new Context( $resourceLoader, $request );
		$module = $resourceLoader->getModule( $moduleName );
		$contentContext = new DerivativeContext( $context );
		$contentContext->setOnly(
			$module->getType() === Module::LOAD_STYLES
				? Module::TYPE_STYLES
				: Module::TYPE_COMBINED
		);
		$content = $resourceLoader->makeModuleResponse( $context, [ $moduleName => $module ] );
		$contentTransferSize = strlen( gzencode( $content, 9 ) );
		$contentTransferSize -= array_sum( self::CORE_SIZE_ADJUSTMENTS );
		$message = $projectName ?
			"$projectName: $moduleName is less than $maxSize" :
			"$moduleName is less than $maxSize";
		$this->assertLessThan( $maxSize, $contentTransferSize, $message );
	}

	/**
	 * @return string Path to bundlesize.config.json
	 */
	abstract public function getBundleSizeConfig(): string;

	/**
	 * @return string Skin name
	 */
	public function getSkinName(): string {
		return MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::DefaultSkin );
	}

}

// @deprecated since 1.42
class_alias( BundleSizeTestBase::class, 'MediaWiki\\Tests\\Structure\\BundleSizeTest' );