Merge "Add tests for ExtensionJsonValidator"
[lhc/web/wiklou.git] / tests / phpunit / includes / session / TestUtils.php
1 <?php
2
3 namespace MediaWiki\Session;
4
5 use Psr\Log\LoggerInterface;
6 use Wikimedia\TestingAccessWrapper;
7
8 /**
9 * Utility functions for Session unit tests
10 */
11 class TestUtils {
12
13 /**
14 * Override the singleton for unit testing
15 * @param SessionManager|null $manager
16 * @return \\Wikimedia\ScopedCallback|null
17 */
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() ) {
46 PHPSessionHandler::install( $manager );
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
59 /**
60 * If you need a SessionBackend for testing but don't want to create a real
61 * one, use this.
62 * @return SessionBackend Unconfigured! Use reflection to set any private
63 * fields necessary.
64 */
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
78 /**
79 * If you need a Session for testing but don't want to create a backend to
80 * construct one, use this.
81 * @param object $backend Object to serve as the SessionBackend
82 * @param int $index
83 * @param LoggerInterface $logger
84 * @return Session
85 */
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 }