MediaWiki  1.28.0
TestUtils.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Session;
4 
6 
10 class TestUtils {
11 
17  public static function setSessionManagerSingleton( SessionManager $manager = null ) {
18  session_write_close();
19 
20  $rInstance = new \ReflectionProperty(
21  SessionManager::class, 'instance'
22  );
23  $rInstance->setAccessible( true );
24  $rGlobalSession = new \ReflectionProperty(
25  SessionManager::class, 'globalSession'
26  );
27  $rGlobalSession->setAccessible( true );
28  $rGlobalSessionRequest = new \ReflectionProperty(
29  SessionManager::class, 'globalSessionRequest'
30  );
31  $rGlobalSessionRequest->setAccessible( true );
32 
33  $oldInstance = $rInstance->getValue();
34 
35  $reset = [
36  [ $rInstance, $oldInstance ],
37  [ $rGlobalSession, $rGlobalSession->getValue() ],
38  [ $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 \Wikimedia\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 
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'
69  );
70  }
71 
72  $ret = $rc->newInstanceWithoutConstructor();
73  \TestingAccessWrapper::newFromObject( $ret )->logger = new \TestLogger;
74  return $ret;
75  }
76 
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'
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 }
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:85
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:1936
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static setSessionManagerSingleton(SessionManager $manager=null)
Override the singleton for unit testing.
Definition: TestUtils.php:17
static install(SessionManager $manager)
Install a session handler for the current web request.
static getDummySessionBackend()
If you need a SessionBackend for testing but don't want to create a real one, use this...
Definition: TestUtils.php:64
Utility functions for Session unit tests.
Definition: TestUtils.php:10
static isInstalled()
Test whether the handler is installed.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
This serves as the entry point to the MediaWiki session handling system.
static newFromObject($object)
Return the same object, without access restrictions.