Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 12 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| TestHandler | |
0.00% |
0 / 12 |
|
0.00% |
0 / 3 |
30 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onUnitTestsAfterDatabaseSetup | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| onUnitTestsBeforeDatabaseTeardown | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\OATHAuth\Hook; |
| 4 | |
| 5 | use MediaWiki\Hook\UnitTestsAfterDatabaseSetupHook; |
| 6 | use MediaWiki\Hook\UnitTestsBeforeDatabaseTeardownHook; |
| 7 | use Wikimedia\Rdbms\ILoadBalancer; |
| 8 | |
| 9 | class TestHandler implements |
| 10 | UnitTestsAfterDatabaseSetupHook, |
| 11 | UnitTestsBeforeDatabaseTeardownHook |
| 12 | { |
| 13 | |
| 14 | public function __construct( |
| 15 | private readonly ILoadBalancer $loadBalancer, |
| 16 | ) { |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * If OATHAuth uses a different DB from the wiki default, create the tables in that DB. |
| 21 | * Largely follows UnitTestsHookHandler in CentralAuth. |
| 22 | * @inheritDoc |
| 23 | */ |
| 24 | public function onUnitTestsAfterDatabaseSetup( $db, $prefix ) { |
| 25 | $originalPrefix = $db->tablePrefix(); |
| 26 | $db->tablePrefix( $prefix ); |
| 27 | if ( !$db->tableExists( 'oathauth_types', __METHOD__ ) ) { |
| 28 | $engine = $db->getType(); |
| 29 | $db->sourceFile( __DIR__ . "/../../sql/$engine/tables-generated.sql" ); |
| 30 | } |
| 31 | $db->tablePrefix( $originalPrefix ); |
| 32 | } |
| 33 | |
| 34 | public function onUnitTestsBeforeDatabaseTeardown() { |
| 35 | $schema = json_decode( file_get_contents( __DIR__ . '/../../sql/tables.json' ), true ); |
| 36 | $tables = array_map( static fn ( $tableSchema ) => $tableSchema['name'], $schema ); |
| 37 | $dbw = $this->loadBalancer->getMaintenanceConnectionRef( DB_PRIMARY ); |
| 38 | foreach ( $tables as $table ) { |
| 39 | $dbw->dropTable( $table ); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | } |