| 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 |
|
| 18 |
|
| 19 |
class TestUtils { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 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 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 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 |
} |