1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
<?php
namespace MediaWiki\Tests\Unit\Settings\Source;
use MediaWiki\Settings\SettingsBuilderException;
use MediaWiki\Settings\Source\ReflectionSchemaSource;
use PHPUnit\Framework\TestCase;
/**
* @covers \MediaWiki\Settings\Source\ReflectionSchemaSource
*/
class ReflectionSchemaSourceTest extends TestCase {
private const NOT_PUBLIC = [
'type' => 'object'
];
public const NOT_A_SCHEMA = 'test';
public const TEST_INTEGER = [
'type' => 'integer',
'default' => 7
];
public const TEST_MAP_TYPE = [
'type' => '?map',
'additionalProperties' => [
'type' => 'string|list',
'items' => [
'type' => 'float',
]
]
];
public const TEST_DYNAMIC_DEFAULT_AUTO = [
'type' => 'string',
'dynamicDefault' => true
];
public const TEST_DYNAMIC_DEFAULT_STRING = [
'type' => 'string',
'dynamicDefault' => 'get_include_path'
];
public const TEST_DYNAMIC_DEFAULT_IMPLIED = [
'type' => 'string',
'dynamicDefault' => [
'use' => [ 'A' ],
]
];
public const TEST_DYNAMIC_DEFAULT = [
'type' => 'string',
'dynamicDefault' => [
'use' => [ 'A' ],
'callback' => [ self::class, 'getTestDefault' ]
]
];
public const TEST_OBSOLETE = [
'type' => 'string',
'obsolete' => 'should be excluded',
];
public static function getDefaultTEST_DYNAMIC_DEFAULT_AUTO() {
// noop
}
public static function getDefaultTEST_DYNAMIC_DEFAULT_IMPLIED() {
// noop
}
public static function getTestDefault() {
// noop
}
public function testLoadAsComponents() {
$source = new ReflectionSchemaSource( self::class );
$settings = $source->loadAsComponents();
$this->assertSame( $source->load(), $settings );
$this->assertArrayHasKey( 'config-schema', $settings );
$schemas = $settings['config-schema'];
$this->assertArrayNotHasKey( 'NOT_PUBLIC', $schemas );
$this->assertArrayNotHasKey( 'NOT_A_SCHEMA', $schemas );
$this->assertArrayNotHasKey( 'TEST_OBSOLETE', $schemas );
$this->assertArrayHasKey( 'TEST_OBSOLETE', $settings['obsolete-config'] );
$this->assertArrayHasKey( 'TEST_INTEGER', $schemas );
$this->assertArrayHasKey( 'default', $schemas['TEST_INTEGER'] );
$this->assertArrayHasKey( 'type', $schemas['TEST_INTEGER'] );
$this->assertSame( 'integer', $schemas['TEST_INTEGER']['type'] );
$this->assertArrayHasKey( 'TEST_MAP_TYPE', $schemas );
$this->assertArrayHasKey( 'default', $schemas['TEST_MAP_TYPE'] );
$this->assertArrayHasKey( 'additionalProperties', $schemas['TEST_MAP_TYPE'] );
$this->assertSame(
[ 'object', 'null' ],
$schemas['TEST_MAP_TYPE']['type']
);
$this->assertSame(
[ 'string', 'array' ],
$schemas['TEST_MAP_TYPE']['additionalProperties']['type']
);
$this->assertSame(
'number',
$schemas['TEST_MAP_TYPE']['additionalProperties']['items']['type']
);
}
public function testLoadAsSchema() {
$expectedProperties = [
'TEST_INTEGER' => [
'type' => 'integer',
'default' => 7,
],
'TEST_MAP_TYPE' => [
'type' => [ 'object', 'null' ],
'additionalProperties' => [
'type' => [ 'string', 'array' ],
'items' => [
'type' => 'number',
]
],
'default' => null,
],
];
$source = new ReflectionSchemaSource( self::class );
$schema = $source->loadAsSchema();
$this->assertEquals( [ 'type', 'properties' ], array_keys( $schema ) );
$this->assertSame( 'object', $schema['type'] );
foreach ( $expectedProperties as $expectedPropertyName => $expectedProperty ) {
$this->assertEquals( $expectedProperty, $schema['properties'][$expectedPropertyName] );
}
}
public function testDynamicDefault() {
$source = new ReflectionSchemaSource( self::class );
$settings = $source->load();
$this->assertArrayHasKey( 'config-schema', $settings );
$schemas = $settings['config-schema'];
$this->assertSame(
[ self::class, 'getDefaultTEST_DYNAMIC_DEFAULT_AUTO' ],
$schemas['TEST_DYNAMIC_DEFAULT_AUTO']['dynamicDefault']['callback']
);
$this->assertSame(
'get_include_path',
$schemas['TEST_DYNAMIC_DEFAULT_STRING']['dynamicDefault']['callback']
);
$this->assertSame(
[ self::class, 'getDefaultTEST_DYNAMIC_DEFAULT_IMPLIED' ],
$schemas['TEST_DYNAMIC_DEFAULT_IMPLIED']['dynamicDefault']['callback']
);
$this->assertSame(
[ 'A' ],
$schemas['TEST_DYNAMIC_DEFAULT_IMPLIED']['dynamicDefault']['use']
);
$this->assertSame(
[ self::class, 'getTestDefault' ],
$schemas['TEST_DYNAMIC_DEFAULT']['dynamicDefault']['callback']
);
$this->assertSame(
[ 'A' ],
$schemas['TEST_DYNAMIC_DEFAULT']['dynamicDefault']['use']
);
}
public function testLoadInvalidClass() {
$this->expectException( SettingsBuilderException::class );
$source = new ReflectionSchemaSource( 'ThisClassDoesNotExist' );
$source->load();
}
}
|