wiki.techinc.nl/tests/phpunit/includes/session/TestUtils.php
Tim Starling 317b460500 Fix even more PHPStorm inspections (#3)
* Inappropriate @inheritDoc usage. Arguably all @inheritDoc is
  inappropriate but these are the ones PHPStorm flags as misleading
  due to the method not being inherited.
* Doc comment type does not match actual argument/return type.
* I replaced "@return void|never" with "@return void" since never means
  never, it doesn't make sense for it to be conditional. If a method
  can return (even if that is unlikely) then @return contains the type
  that it returns. "@return never" means that there is no such type
  because the method never returns.
* Incomplete/partial/broken doc tags

Change-Id: Ide86bd6d2b44387f37d234c2b059d6fbc42ec962
2023-03-25 00:30:15 +00:00

99 lines
2.9 KiB
PHP

<?php
namespace MediaWiki\Session;
use PHPUnit\Framework\Assert;
use Psr\Log\LoggerInterface;
use Wikimedia\TestingAccessWrapper;
/**
* Utility functions for Session unit tests
*/
class TestUtils {
/**
* Override the singleton for unit testing
* @param SessionManager|null $manager
* @return \Wikimedia\ScopedCallback|null
*/
public static function setSessionManagerSingleton( SessionManager $manager = null ) {
session_write_close();
$rInstance = new \ReflectionProperty(
SessionManager::class, 'instance'
);
$rInstance->setAccessible( true );
$rGlobalSession = new \ReflectionProperty(
SessionManager::class, 'globalSession'
);
$rGlobalSession->setAccessible( true );
$rGlobalSessionRequest = new \ReflectionProperty(
SessionManager::class, 'globalSessionRequest'
);
$rGlobalSessionRequest->setAccessible( true );
$oldInstance = $rInstance->getValue();
$reset = [
[ $rInstance, $oldInstance ],
[ $rGlobalSession, $rGlobalSession->getValue() ],
[ $rGlobalSessionRequest, $rGlobalSessionRequest->getValue() ],
];
$rInstance->setValue( $manager );
$rGlobalSession->setValue( null );
$rGlobalSessionRequest->setValue( null );
if ( $manager && PHPSessionHandler::isInstalled() ) {
PHPSessionHandler::install( $manager );
}
return new \Wikimedia\ScopedCallback( static function () use ( &$reset, $oldInstance ) {
foreach ( $reset as &$arr ) {
$arr[0]->setValue( $arr[1] );
}
if ( $oldInstance && PHPSessionHandler::isInstalled() ) {
PHPSessionHandler::install( $oldInstance );
}
} );
}
/**
* If you need a SessionBackend for testing but don't want to create a real
* one, use this.
* @return SessionBackend Unconfigured! Use reflection to set any private
* fields necessary.
*/
public static function getDummySessionBackend() {
$rc = new \ReflectionClass( SessionBackend::class );
if ( !method_exists( $rc, 'newInstanceWithoutConstructor' ) ) {
Assert::markTestSkipped(
'ReflectionClass::newInstanceWithoutConstructor isn\'t available'
);
}
$ret = $rc->newInstanceWithoutConstructor();
TestingAccessWrapper::newFromObject( $ret )->logger = new \TestLogger;
return $ret;
}
/**
* If you need a Session for testing but don't want to create a backend to
* construct one, use this.
* @phpcs:ignore MediaWiki.Commenting.FunctionComment.ObjectTypeHintParam
* @param object|null $backend Object to serve as the SessionBackend
* @param int $index
* @param LoggerInterface|null $logger
* @return Session
*/
public static function getDummySession( $backend = null, $index = -1, $logger = null ) {
$rc = new \ReflectionClass( Session::class );
$session = $rc->newInstanceWithoutConstructor();
$priv = TestingAccessWrapper::newFromObject( $session );
$priv->backend = $backend ?? new DummySessionBackend();
$priv->index = $index;
$priv->logger = $logger ?? new \TestLogger();
return $session;
}
}