3 namespace MediaWiki\Tests\Maintenance\Includes
;
5 use MediaWiki\DB\AbstractSchemaValidationError
;
6 use MediaWiki\Maintenance\SchemaGenerator
;
7 use MediaWikiIntegrationTestCase
;
10 * @covers \MediaWiki\Maintenance\SchemaGenerator
12 class SchemaGeneratorTest
extends MediaWikiIntegrationTestCase
{
14 private const DATA_DIR
= __DIR__
. '/../../data/schema-maintenance';
16 private function getFileWithContent( string $content ): string {
17 $testFilename = $this->getNewTempFile();
18 $testFile = fopen( $testFilename, 'w' );
19 fwrite( $testFile, $content );
25 * @dataProvider provideValidateAndGetSchema
27 public function testValidateAndGetSchema( string $fileContent, string $expectedExceptionSubstring ) {
28 $this->expectException( AbstractSchemaValidationError
::class );
29 $this->expectExceptionMessage( $expectedExceptionSubstring );
30 ( new SchemaGenerator() )->validateAndGetSchema( $this->getFileWithContent( $fileContent ) );
33 public static function provideValidateAndGetSchema() {
35 'Empty JSON file' => [ '', 'does not exist' ],
36 'Not valid JSON' => [ '{{{{', 'Invalid JSON' ],
37 'Not a valid schema' => [ '{"abc": "test"}', 'did not pass validation' ],
41 public function testValidateAndGetSchema__valid() {
42 $generator = new SchemaGenerator();
43 $schema = $generator->validateAndGetSchema( self
::DATA_DIR
. '/tables.json' );
44 $this->assertIsArray( $schema );
47 public function testGenerateSchemaChange__NoSchemaChangesMade() {
48 $generator = new SchemaGenerator();
49 $this->expectException( AbstractSchemaValidationError
::class );
50 $this->expectExceptionMessage( 'No schema changes detected' );
51 $generator->generateSchemaChange( 'mysql', self
::DATA_DIR
. '/patch-no_change.json' );