Session: Implement ArrayAccess
[lhc/web/wiklou.git] / tests / phpunit / includes / session / TestUtils.php
1 <?php
2
3 namespace MediaWiki\Session;
4
5 use Psr\Log\LoggerInterface;
6
7 /**
8 * Utility functions for Session unit tests
9 */
10 class TestUtils {
11
12 /**
13 * Override the singleton for unit testing
14 * @param SessionManager|null $manager
15 * @return \\ScopedCallback|null
16 */
17 public static function setSessionManagerSingleton( SessionManager $manager = null ) {
18 session_write_close();
19
20 $rInstance = new \ReflectionProperty(
21 'MediaWiki\\Session\\SessionManager', 'instance'
22 );
23 $rInstance->setAccessible( true );
24 $rGlobalSession = new \ReflectionProperty(
25 'MediaWiki\\Session\\SessionManager', 'globalSession'
26 );
27 $rGlobalSession->setAccessible( true );
28 $rGlobalSessionRequest = new \ReflectionProperty(
29 'MediaWiki\\Session\\SessionManager', 'globalSessionRequest'
30 );
31 $rGlobalSessionRequest->setAccessible( true );
32
33 $oldInstance = $rInstance->getValue();
34
35 $reset = array(
36 array( $rInstance, $oldInstance ),
37 array( $rGlobalSession, $rGlobalSession->getValue() ),
38 array( $rGlobalSessionRequest, $rGlobalSessionRequest->getValue() ),
39 );
40
41 $rInstance->setValue( $manager );
42 $rGlobalSession->setValue( null );
43 $rGlobalSessionRequest->setValue( null );
44 if ( $manager && PHPSessionHandler::isInstalled() ) {
45 PHPSessionHandler::install( $manager );
46 }
47
48 return new \ScopedCallback( function () use ( &$reset, $oldInstance ) {
49 foreach ( $reset as &$arr ) {
50 $arr[0]->setValue( $arr[1] );
51 }
52 if ( $oldInstance && PHPSessionHandler::isInstalled() ) {
53 PHPSessionHandler::install( $oldInstance );
54 }
55 } );
56 }
57
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.
63 */
64 public static function getDummySessionBackend() {
65 $rc = new \ReflectionClass( 'MediaWiki\\Session\\SessionBackend' );
66 if ( !method_exists( $rc, 'newInstanceWithoutConstructor' ) ) {
67 \PHPUnit_Framework_Assert::markTestSkipped(
68 'ReflectionClass::newInstanceWithoutConstructor isn\'t available'
69 );
70 }
71
72 $ret = $rc->newInstanceWithoutConstructor();
73 \TestingAccessWrapper::newFromObject( $ret )->logger = new \TestLogger;
74 return $ret;
75 }
76
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
84 */
85 public static function getDummySession( $backend = null, $index = -1, $logger = null ) {
86 $rc = new \ReflectionClass( 'MediaWiki\\Session\\Session' );
87 if ( !method_exists( $rc, 'newInstanceWithoutConstructor' ) ) {
88 \PHPUnit_Framework_Assert::markTestSkipped(
89 'ReflectionClass::newInstanceWithoutConstructor isn\'t available'
90 );
91 }
92
93 if ( $backend === null ) {
94 $backend = new DummySessionBackend;
95 }
96
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;
103 }
104
105 }