Debian Codegraph @

tests/phpunit/unit/includes/Settings/Source/Format/YamlFormatTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Unit\Settings\Source\Format;
4
5 use MediaWiki\Settings\Source\Format\YamlFormat;
6 use PHPUnit\Framework\TestCase;
7 use UnexpectedValueException;
8
9 /** @covers \MediaWiki\Settings\Source\Format\YamlFormat */
10 class YamlFormatTest extends TestCase {
11
12 private const VALID_YAML = <<<'VALID_YAML'
13 config-schema:
14 MySetting:
15 type: boolean
16 VALID_YAML;
17
18 private const INVALID_YAML = <<<'INVALID_YAML'
19 config-schema: []
20 MySetting: []
21 INVALID_YAML;
22
23 public static function provideParser() {
24 yield 'php-yaml' => [ 'parser' => YamlFormat::PARSER_PHP_YAML ];
25 yield 'symfony' => [ 'parser' => YamlFormat::PARSER_SYMFONY ];
26 }
27
28 /**
29 * @dataProvider provideParser
30 */
31 public function testDecode( string $parser ) {
32 if ( !YamlFormat::isParserAvailable( $parser ) ) {
33 $this->markTestSkipped( "Parser '$parser' is not available" );
34 }
35
36 $format = new YamlFormat( [ $parser ] );
37 $this->assertEquals(
38 [ 'config-schema' => [ 'MySetting' => [ 'type' => 'boolean' ] ] ],
39 $format->decode( self::VALID_YAML )
40 );
41 }
42
43 /**
44 * @dataProvider provideParser
45 */
46 public function testDecodeInvalid( string $parser ) {
47 if ( !YamlFormat::isParserAvailable( $parser ) ) {
48 $this->markTestSkipped( "Parser '$parser' is not available" );
49 }
50
51 $format = new YamlFormat( [ $parser ] );
52 $this->expectException( UnexpectedValueException::class );
53 $format->decode( self::INVALID_YAML );
54 }
55
56 /**
57 * @dataProvider provideParser
58 */
59 public function testDecodeDoesNotUnserializeObjects( string $parser ) {
60 if ( !YamlFormat::isParserAvailable( $parser ) ) {
61 $this->markTestSkipped( "Parser '$parser' is not available" );
62 }
63
64 $format = new YamlFormat( [ $parser ] );
65 $this->expectException( UnexpectedValueException::class );
66 $format->decode( 'object: !php/object "' . serialize( $format ) . '"' );
67 }
68
69 /**
70 * @dataProvider provideParser
71 */
72 public function testDecodeDoesNotRespectPHPConst( string $parser ) {
73 if ( !YamlFormat::isParserAvailable( $parser ) ) {
74 $this->markTestSkipped( "Parser '$parser' is not available" );
75 }
76
77 $format = new YamlFormat( [ $parser ] );
78 $this->expectException( UnexpectedValueException::class );
79 $format->decode( '{ bar: !php/const PHP_INT_SIZE }' );
80 }
81
82 public static function provideSupportsFileExtension() {
83 yield 'Supported' => [ 'yaml', true ];
84 yield 'Supported, uppercase' => [ 'YAML', true ];
85 yield 'Supported, short' => [ 'yml', true ];
86 yield 'Unsupported' => [ 'txt', false ];
87 }
88
89 /**
90 * @dataProvider provideSupportsFileExtension
91 */
92 public function testSupportsFileExtension( $extension, $expected ) {
93 $this->assertSame( $expected, YamlFormat::supportsFileExtension( $extension ) );
94 }
95 }