wiki.techinc.nl/tests/phpunit/includes/AutoLoaderTest.php
daniel 37af531721 AutoLoader: provide methods for registering classes
This introduces static methods for registering classes and namespaces,
so we don't have to mess with global variables and static members
fields.

Bug: T240535
Change-Id: I1abb27bd70898d04c9253e0b7467447fa96d15a4
2022-05-10 13:55:53 +10:00

83 lines
2.6 KiB
PHP

<?php
use Wikimedia\TestingAccessWrapper;
/**
* @covers AutoLoader
*/
class AutoLoaderTest extends MediaWikiIntegrationTestCase {
private $oldPsr4;
private $oldClassFiles;
protected function setUp(): void {
parent::setUp();
// Fancy dance to trigger a rebuild of AutoLoader::$autoloadLocalClassesLower
$this->mergeMwGlobalArrayValue( 'wgAutoloadLocalClasses', [
'TestAutoloadedLocalClass' =>
__DIR__ . '/../data/autoloader/TestAutoloadedLocalClass.php',
'TestAutoloadedCamlClass' =>
__DIR__ . '/../data/autoloader/TestAutoloadedCamlClass.php',
'TestAutoloadedSerializedClass' =>
__DIR__ . '/../data/autoloader/TestAutoloadedSerializedClass.php',
] );
AutoLoader::resetAutoloadLocalClassesLower();
$this->mergeMwGlobalArrayValue( 'wgAutoloadClasses', [
'TestAutoloadedClass' => __DIR__ . '/../data/autoloader/TestAutoloadedClass.php',
] );
$access = TestingAccessWrapper::newFromClass( AutoLoader::class );
$this->oldPsr4 = $access->psr4Namespaces;
$this->oldClassFiles = $access->classFiles;
AutoLoader::registerNamespaces( [
'Test\\MediaWiki\\AutoLoader\\' => __DIR__ . '/../data/autoloader/psr4'
] );
AutoLoader::registerClasses( [
'TestAnotherAutoloadedClass' => __DIR__ . '/../data/autoloader/TestAnotherAutoloadedClass.php',
] );
}
protected function tearDown(): void {
$access = TestingAccessWrapper::newFromClass( AutoLoader::class );
$access->psr4Namespaces = $this->oldPsr4;
$access->classFiles = $this->oldClassFiles;
parent::tearDown();
}
public function testFind() {
$path = __DIR__ . '/../data/autoloader/TestAutoloadedLocalClass.php';
$this->assertSame( $path, AutoLoader::find( TestAutoloadedLocalClass::class ) );
}
public function testCoreClass() {
$this->assertTrue( class_exists( 'TestAutoloadedLocalClass' ) );
}
public function testExtensionClass() {
$this->assertTrue( class_exists( 'TestAnotherAutoloadedClass' ) );
}
public function testLegacyExtensionClass() {
$this->assertTrue( class_exists( 'TestAutoloadedClass' ) );
}
public function testWrongCaseClass() {
$this->setMwGlobals( 'wgAutoloadAttemptLowercase', true );
$this->assertTrue( class_exists( 'testautoLoadedcamlCLASS' ) );
}
public function testWrongCaseSerializedClass() {
$this->setMwGlobals( 'wgAutoloadAttemptLowercase', true );
$dummyCereal = 'O:29:"testautoloadedserializedclass":0:{}';
$uncerealized = unserialize( $dummyCereal );
$this->assertFalse( $uncerealized instanceof __PHP_Incomplete_Class,
"unserialize() can load classes case-insensitively." );
}
public function testPsr4() {
$this->assertTrue( class_exists( 'Test\\MediaWiki\\AutoLoader\\TestFooBar' ) );
}
}