Debian Codegraph @

tests/phpunit/includes/session/TestUtils.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Session;
4
5 use MediaWiki\Session\PHPSessionHandler;
6 use MediaWiki\Session\Session;
7 use MediaWiki\Session\SessionBackend;
8 use MediaWiki\Session\SessionManager;
9 use PHPUnit\Framework\Assert;
10 use Psr\Log\LoggerInterface;
11 use ReflectionClass;
12 use TestLogger;
13 use Wikimedia\ScopedCallback;
14 use Wikimedia\TestingAccessWrapper;
15
16 /**
17 * Utility functions for Session unit tests
18 */
19 class TestUtils {
20
21 /**
22 * Override the singleton for unit testing
23 * @param SessionManager|null $manager
24 * @return ScopedCallback|null
25 */
26 public static function setSessionManagerSingleton( ?SessionManager $manager = null ) {
27 session_write_close();
28
29 $staticAccess = TestingAccessWrapper::newFromClass( SessionManager::class );
30
31 $oldInstance = $staticAccess->instance;
32
33 $reset = [
34 [ 'instance', $oldInstance ],
35 [ 'globalSession', $staticAccess->globalSession ],
36 [ 'globalSessionRequest', $staticAccess->globalSessionRequest ],
37 ];
38
39 $staticAccess->instance = $manager;
40 $staticAccess->globalSession = null;
41 $staticAccess->globalSessionRequest = null;
42 if ( $manager && PHPSessionHandler::isInstalled() ) {
43 PHPSessionHandler::install( $manager );
44 }
45
46 return new ScopedCallback( static function () use ( $reset, $staticAccess, $oldInstance ) {
47 foreach ( $reset as [ $property, $oldValue ] ) {
48 $staticAccess->$property = $oldValue;
49 }
50 if ( $oldInstance && PHPSessionHandler::isInstalled() ) {
51 PHPSessionHandler::install( $oldInstance );
52 }
53 } );
54 }
55
56 /**
57 * If you need a SessionBackend for testing but don't want to create a real
58 * one, use this.
59 * @return SessionBackend Unconfigured! Use reflection to set any private
60 * fields necessary.
61 */
62 public static function getDummySessionBackend() {
63 $rc = new ReflectionClass( SessionBackend::class );
64 if ( !method_exists( $rc, 'newInstanceWithoutConstructor' ) ) {
65 Assert::markTestSkipped(
66 'ReflectionClass::newInstanceWithoutConstructor isn\'t available'
67 );
68 }
69
70 $ret = $rc->newInstanceWithoutConstructor();
71 TestingAccessWrapper::newFromObject( $ret )->logger = new TestLogger;
72 return $ret;
73 }
74
75 /**
76 * If you need a Session for testing but don't want to create a backend to
77 * construct one, use this.
78 * @phpcs:ignore MediaWiki.Commenting.FunctionComment.ObjectTypeHintParam
79 * @param object|null $backend Object to serve as the SessionBackend
80 * @param int $index
81 * @param LoggerInterface|null $logger
82 * @return Session
83 */
84 public static function getDummySession( $backend = null, $index = -1, $logger = null ) {
85 $rc = new ReflectionClass( Session::class );
86
87 $session = $rc->newInstanceWithoutConstructor();
88 $priv = TestingAccessWrapper::newFromObject( $session );
89 $priv->backend = $backend ?? new DummySessionBackend();
90 $priv->index = $index;
91 $priv->logger = $logger ?? new TestLogger();
92 return $session;
93 }
94
95 }