wiki.techinc.nl/tests/phpunit/unit/includes/Settings/Source/FileSourceTest.php
Dan Duvall 9a4af25664 Introduced settings sources and formats
A `SettingsSource` is meant to represent any kind of local or remote
store from which settings can be read, be this a local file, remote URL,
database, etc. It is concerned with reading in (and possibly decoding)
settings data, and computing a consistent hash key that may be used in
caching.

A `SettingsFormat` is meant to detect supported file types and/or decode
source contents into settings arrays. As of now, JSON is the only
supported format but others may be implemented.

`FileSource` is the first source implementation, with its default format
being JSON, meant to read settings from local JSON files.

`ArraySource` is mostly useful for testing using array literals.

Refactored `SettingsBuilder` methods to use the new source abstractions.

Bug: T295499
Change-Id: If7869609c4ad1ccd0894d5ba358f885007168972
2021-11-15 14:07:59 -08:00

57 lines
1.4 KiB
PHP

<?php
namespace MediaWiki\Tests\Unit\Settings\Source;
use MediaWiki\Settings\SettingsBuilderException;
use MediaWiki\Settings\Source\FileSource;
use MediaWiki\Settings\Source\Format\JsonFormat;
use PHPUnit\Framework\TestCase;
/**
* @covers \MediaWiki\Settings\Source\FileSource
*/
class FileSourceTest extends TestCase {
public function testLoad() {
$source = new FileSource( __DIR__ . '/fixtures/settings.json' );
$settings = $source->load();
$this->assertSame(
[ 'config' => [ 'MySetting' => 'BlaBla' ] ],
$source->load()
);
}
public function testLoadFormat() {
$source = new FileSource( __DIR__ . '/fixtures/settings.json', new JsonFormat() );
$settings = $source->load();
$this->assertSame(
[ 'config' => [ 'MySetting' => 'BlaBla' ] ],
$source->load()
);
}
public function testLoadBadFormat() {
$source = new FileSource( __DIR__ . '/fixtures/bad.json', new JsonFormat() );
$this->expectException( SettingsBuilderException::class );
$settings = $source->load();
}
public function testLoadDirectory() {
$source = new FileSource( __DIR__ . '/fixtures/dir.json' );
$this->expectException( SettingsBuilderException::class );
$settings = $source->load();
}
public function testLoadNoSuitableFormats() {
$source = new FileSource( __DIR__ . '/fixtures/settings.toml', new JsonFormat() );
$this->expectException( SettingsBuilderException::class );
$settings = $source->load();
}
}