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
|
<?php
namespace MediaWiki\Tests\Unit\Settings\Source\Format;
use MediaWiki\Settings\Source\Format\YamlFormat;
use PHPUnit\Framework\TestCase;
use UnexpectedValueException;
/** @covers \MediaWiki\Settings\Source\Format\YamlFormat */
class YamlFormatTest extends TestCase {
private const VALID_YAML = <<<'VALID_YAML'
config-schema:
MySetting:
type: boolean
VALID_YAML;
private const INVALID_YAML = <<<'INVALID_YAML'
config-schema: []
MySetting: []
INVALID_YAML;
public static function provideParser() {
yield 'php-yaml' => [ 'parser' => YamlFormat::PARSER_PHP_YAML ];
yield 'symfony' => [ 'parser' => YamlFormat::PARSER_SYMFONY ];
}
/**
* @dataProvider provideParser
*/
public function testDecode( string $parser ) {
if ( !YamlFormat::isParserAvailable( $parser ) ) {
$this->markTestSkipped( "Parser '$parser' is not available" );
}
$format = new YamlFormat( [ $parser ] );
$this->assertEquals(
[ 'config-schema' => [ 'MySetting' => [ 'type' => 'boolean' ] ] ],
$format->decode( self::VALID_YAML )
);
}
/**
* @dataProvider provideParser
*/
public function testDecodeInvalid( string $parser ) {
if ( !YamlFormat::isParserAvailable( $parser ) ) {
$this->markTestSkipped( "Parser '$parser' is not available" );
}
$format = new YamlFormat( [ $parser ] );
$this->expectException( UnexpectedValueException::class );
$format->decode( self::INVALID_YAML );
}
/**
* @dataProvider provideParser
*/
public function testDecodeDoesNotUnserializeObjects( string $parser ) {
if ( !YamlFormat::isParserAvailable( $parser ) ) {
$this->markTestSkipped( "Parser '$parser' is not available" );
}
$format = new YamlFormat( [ $parser ] );
$this->expectException( UnexpectedValueException::class );
$format->decode( 'object: !php/object "' . serialize( $format ) . '"' );
}
/**
* @dataProvider provideParser
*/
public function testDecodeDoesNotRespectPHPConst( string $parser ) {
if ( !YamlFormat::isParserAvailable( $parser ) ) {
$this->markTestSkipped( "Parser '$parser' is not available" );
}
$format = new YamlFormat( [ $parser ] );
$this->expectException( UnexpectedValueException::class );
$format->decode( '{ bar: !php/const PHP_INT_SIZE }' );
}
public static function provideSupportsFileExtension() {
yield 'Supported' => [ 'yaml', true ];
yield 'Supported, uppercase' => [ 'YAML', true ];
yield 'Supported, short' => [ 'yml', true ];
yield 'Unsupported' => [ 'txt', false ];
}
/**
* @dataProvider provideSupportsFileExtension
*/
public function testSupportsFileExtension( $extension, $expected ) {
$this->assertSame( $expected, YamlFormat::supportsFileExtension( $extension ) );
}
}
|