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