3 namespace MediaWiki\Session
;
6 * Utility functions for Session unit tests
11 * Override the singleton for unit testing
12 * @param SessionManager|null $manager
13 * @return \\ScopedCallback|null
15 public static function setSessionManagerSingleton( SessionManager
$manager = null ) {
16 session_write_close();
18 $rInstance = new \
ReflectionProperty(
19 'MediaWiki\\Session\\SessionManager', 'instance'
21 $rInstance->setAccessible( true );
22 $rGlobalSession = new \
ReflectionProperty(
23 'MediaWiki\\Session\\SessionManager', 'globalSession'
25 $rGlobalSession->setAccessible( true );
26 $rGlobalSessionRequest = new \
ReflectionProperty(
27 'MediaWiki\\Session\\SessionManager', 'globalSessionRequest'
29 $rGlobalSessionRequest->setAccessible( true );
31 $oldInstance = $rInstance->getValue();
34 array( $rInstance, $oldInstance ),
35 array( $rGlobalSession, $rGlobalSession->getValue() ),
36 array( $rGlobalSessionRequest, $rGlobalSessionRequest->getValue() ),
39 $rInstance->setValue( $manager );
40 $rGlobalSession->setValue( null );
41 $rGlobalSessionRequest->setValue( null );
42 if ( $manager && PHPSessionHandler
::isInstalled() ) {
43 PHPSessionHandler
::install( $manager );
46 return new \
ScopedCallback( function () use ( &$reset, $oldInstance ) {
47 foreach ( $reset as &$arr ) {
48 $arr[0]->setValue( $arr[1] );
50 if ( $oldInstance && PHPSessionHandler
::isInstalled() ) {
51 PHPSessionHandler
::install( $oldInstance );
57 * If you need a SessionBackend for testing but don't want to create a real
59 * @return SessionBackend Unconfigured! Use reflection to set any private
62 public static function getDummySessionBackend() {
63 $rc = new \
ReflectionClass( 'MediaWiki\\Session\\SessionBackend' );
64 if ( !method_exists( $rc, 'newInstanceWithoutConstructor' ) ) {
65 \PHPUnit_Framework_Assert
::markTestSkipped(
66 'ReflectionClass::newInstanceWithoutConstructor isn\'t available'
70 return $rc->newInstanceWithoutConstructor();
74 * If you need a Session for testing but don't want to create a backend to
75 * construct one, use this.
76 * @param object $backend Object to serve as the SessionBackend
77 * @param int $index Index
80 public static function getDummySession( $backend = null, $index = -1 ) {
81 $rc = new \
ReflectionClass( 'MediaWiki\\Session\\Session' );
82 if ( !method_exists( $rc, 'newInstanceWithoutConstructor' ) ) {
83 \PHPUnit_Framework_Assert
::markTestSkipped(
84 'ReflectionClass::newInstanceWithoutConstructor isn\'t available'
88 if ( $backend === null ) {
89 $backend = new DummySessionBackend
;
92 $session = $rc->newInstanceWithoutConstructor();
93 $priv = \TestingAccessWrapper
::newFromObject( $session );
94 $priv->backend
= $backend;
95 $priv->index
= $index;