MediaWiki  1.29.1
TestUtils.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Session;
4 
5 use Psr\Log\LoggerInterface;
6 use Wikimedia\TestingAccessWrapper;
7 
11 class 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() ) {
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 
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 }
MediaWiki\Session\PHPSessionHandler\install
static install(SessionManager $manager)
Install a session handler for the current web request.
Definition: PHPSessionHandler.php:108
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MediaWiki\Session\TestUtils
Utility functions for Session unit tests.
Definition: TestUtils.php:11
MediaWiki\Session\TestUtils\setSessionManagerSingleton
static setSessionManagerSingleton(SessionManager $manager=null)
Override the singleton for unit testing.
Definition: TestUtils.php:18
php
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
MediaWiki\Session
Definition: BotPasswordSessionProvider.php:24
MediaWiki\Session\TestUtils\getDummySessionBackend
static getDummySessionBackend()
If you need a SessionBackend for testing but don't want to create a real one, use this.
Definition: TestUtils.php:65
MediaWiki\Session\DummySessionBackend
Dummy session backend.
Definition: DummySessionBackend.php:11
MediaWiki\Session\TestUtils\getDummySession
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
MediaWiki\Session\SessionManager
This serves as the entry point to the MediaWiki session handling system.
Definition: SessionManager.php:49
$ret
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:1956
as
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
class
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
MediaWiki\Session\PHPSessionHandler\isInstalled
static isInstalled()
Test whether the handler is installed.
Definition: PHPSessionHandler.php:92