MediaWiki REL1_30
TestUtils.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Session;
4
5use Psr\Log\LoggerInterface;
6use Wikimedia\TestingAccessWrapper;
7
11class TestUtils {
12
18 public static function setSessionManagerSingleton( SessionManager $manager = null ) {
19 session_write_close();
20
21 $rInstance = new \ReflectionProperty(
22 SessionManager::class, 'instance'
23 );
24 $rInstance->setAccessible( true );
25 $rGlobalSession = new \ReflectionProperty(
26 SessionManager::class, 'globalSession'
27 );
28 $rGlobalSession->setAccessible( true );
29 $rGlobalSessionRequest = new \ReflectionProperty(
30 SessionManager::class, 'globalSessionRequest'
31 );
32 $rGlobalSessionRequest->setAccessible( true );
33
34 $oldInstance = $rInstance->getValue();
35
36 $reset = [
37 [ $rInstance, $oldInstance ],
38 [ $rGlobalSession, $rGlobalSession->getValue() ],
39 [ $rGlobalSessionRequest, $rGlobalSessionRequest->getValue() ],
40 ];
41
42 $rInstance->setValue( $manager );
43 $rGlobalSession->setValue( null );
44 $rGlobalSessionRequest->setValue( null );
45 if ( $manager && PHPSessionHandler::isInstalled() ) {
47 }
48
49 return new \Wikimedia\ScopedCallback( function () use ( &$reset, $oldInstance ) {
50 foreach ( $reset as &$arr ) {
51 $arr[0]->setValue( $arr[1] );
52 }
53 if ( $oldInstance && PHPSessionHandler::isInstalled() ) {
54 PHPSessionHandler::install( $oldInstance );
55 }
56 } );
57 }
58
65 public static function getDummySessionBackend() {
66 $rc = new \ReflectionClass( SessionBackend::class );
67 if ( !method_exists( $rc, 'newInstanceWithoutConstructor' ) ) {
68 \PHPUnit_Framework_Assert::markTestSkipped(
69 'ReflectionClass::newInstanceWithoutConstructor isn\'t available'
70 );
71 }
72
73 $ret = $rc->newInstanceWithoutConstructor();
74 TestingAccessWrapper::newFromObject( $ret )->logger = new \TestLogger;
75 return $ret;
76 }
77
86 public static function getDummySession( $backend = null, $index = -1, $logger = null ) {
87 $rc = new \ReflectionClass( Session::class );
88 if ( !method_exists( $rc, 'newInstanceWithoutConstructor' ) ) {
89 \PHPUnit_Framework_Assert::markTestSkipped(
90 'ReflectionClass::newInstanceWithoutConstructor isn\'t available'
91 );
92 }
93
94 if ( $backend === null ) {
95 $backend = new DummySessionBackend;
96 }
97
98 $session = $rc->newInstanceWithoutConstructor();
99 $priv = TestingAccessWrapper::newFromObject( $session );
100 $priv->backend = $backend;
101 $priv->index = $index;
102 $priv->logger = $logger ?: new \TestLogger;
103 return $session;
104 }
105
106}
static isInstalled()
Test whether the handler is installed.
static install(SessionManager $manager)
Install a session handler for the current web request.
This serves as the entry point to the MediaWiki session handling system.
Utility functions for Session unit tests.
Definition TestUtils.php:11
static setSessionManagerSingleton(SessionManager $manager=null)
Override the singleton for unit testing.
Definition TestUtils.php:18
static getDummySessionBackend()
If you need a SessionBackend for testing but don't want to create a real one, use this.
Definition TestUtils.php:65
static getDummySession( $backend=null, $index=-1, $logger=null)
If you need a Session for testing but don't want to create a backend to construct one,...
Definition TestUtils.php:86
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:1975