Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / session / TestUtils.php
blobf00de55a3089f0cf388af7e7037895c131f82494
1 <?php
3 namespace MediaWiki\Session;
5 use Psr\Log\LoggerInterface;
7 /**
8 * Utility functions for Session unit tests
9 */
10 class TestUtils {
12 /**
13 * Override the singleton for unit testing
14 * @param SessionManager|null $manager
15 * @return \\Wikimedia\ScopedCallback|null
17 public static function setSessionManagerSingleton( SessionManager $manager = null ) {
18 session_write_close();
20 $rInstance = new \ReflectionProperty(
21 SessionManager::class, 'instance'
23 $rInstance->setAccessible( true );
24 $rGlobalSession = new \ReflectionProperty(
25 SessionManager::class, 'globalSession'
27 $rGlobalSession->setAccessible( true );
28 $rGlobalSessionRequest = new \ReflectionProperty(
29 SessionManager::class, 'globalSessionRequest'
31 $rGlobalSessionRequest->setAccessible( true );
33 $oldInstance = $rInstance->getValue();
35 $reset = [
36 [ $rInstance, $oldInstance ],
37 [ $rGlobalSession, $rGlobalSession->getValue() ],
38 [ $rGlobalSessionRequest, $rGlobalSessionRequest->getValue() ],
41 $rInstance->setValue( $manager );
42 $rGlobalSession->setValue( null );
43 $rGlobalSessionRequest->setValue( null );
44 if ( $manager && PHPSessionHandler::isInstalled() ) {
45 PHPSessionHandler::install( $manager );
48 return new \Wikimedia\ScopedCallback( function () use ( &$reset, $oldInstance ) {
49 foreach ( $reset as &$arr ) {
50 $arr[0]->setValue( $arr[1] );
52 if ( $oldInstance && PHPSessionHandler::isInstalled() ) {
53 PHPSessionHandler::install( $oldInstance );
55 } );
58 /**
59 * If you need a SessionBackend for testing but don't want to create a real
60 * one, use this.
61 * @return SessionBackend Unconfigured! Use reflection to set any private
62 * fields necessary.
64 public static function getDummySessionBackend() {
65 $rc = new \ReflectionClass( SessionBackend::class );
66 if ( !method_exists( $rc, 'newInstanceWithoutConstructor' ) ) {
67 \PHPUnit_Framework_Assert::markTestSkipped(
68 'ReflectionClass::newInstanceWithoutConstructor isn\'t available'
72 $ret = $rc->newInstanceWithoutConstructor();
73 \TestingAccessWrapper::newFromObject( $ret )->logger = new \TestLogger;
74 return $ret;
77 /**
78 * If you need a Session for testing but don't want to create a backend to
79 * construct one, use this.
80 * @param object $backend Object to serve as the SessionBackend
81 * @param int $index Index
82 * @param LoggerInterface $logger
83 * @return Session
85 public static function getDummySession( $backend = null, $index = -1, $logger = null ) {
86 $rc = new \ReflectionClass( Session::class );
87 if ( !method_exists( $rc, 'newInstanceWithoutConstructor' ) ) {
88 \PHPUnit_Framework_Assert::markTestSkipped(
89 'ReflectionClass::newInstanceWithoutConstructor isn\'t available'
93 if ( $backend === null ) {
94 $backend = new DummySessionBackend;
97 $session = $rc->newInstanceWithoutConstructor();
98 $priv = \TestingAccessWrapper::newFromObject( $session );
99 $priv->backend = $backend;
100 $priv->index = $index;
101 $priv->logger = $logger ?: new \TestLogger;
102 return $session;