This patch ensures that we know which arrays are lists (JsonSchema type "array") and which are maps (JsonSchema type "object"). We can then default to array_merge for lists and to array_plus for maps. This seems clearer than requiring an explicit merge strategy to be declared for all arrays. This patch specified a mergeTrategy for some config variables that need behavior different from the default. This patch also changes the merging behavior to allow non-array values to replace arrays and vice versa. It also changes the behavior of defaults to allow falsy values to override non-falsy defaults. Bug: T300129 Change-Id: Ia7b0c0250af6a957eac1efb554fb47511f5e093f
106 lines
3.3 KiB
PHP
106 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Tests\Unit\Settings\Config;
|
|
|
|
use MediaWiki\Settings\Config\ConfigBuilder;
|
|
use MediaWiki\Settings\Config\MergeStrategy;
|
|
|
|
trait ConfigSinkTestTrait {
|
|
|
|
abstract protected function getConfigSink(): ConfigBuilder;
|
|
|
|
abstract protected function assertKeyHasValue( string $key, $value );
|
|
|
|
public function testSet() {
|
|
$this->getConfigSink()
|
|
->set( 'TestKey1', 'foo' )
|
|
->set( 'TestKey2', 'bar' );
|
|
$this->assertKeyHasValue( 'TestKey1', 'foo' );
|
|
$this->assertKeyHasValue( 'TestKey2', 'bar' );
|
|
}
|
|
|
|
public function testSetDefault() {
|
|
$this->getConfigSink()
|
|
->setDefault( 'TestKey1', 'foo' )
|
|
->setDefault( 'TestKey2', 'bar' );
|
|
$this->assertKeyHasValue( 'TestKey1', 'foo' );
|
|
$this->assertKeyHasValue( 'TestKey2', 'bar' );
|
|
}
|
|
|
|
public function provideSetNewValue() {
|
|
yield 'replace 1 with 2' => [ 1, 2, null, 2 ];
|
|
yield 'replace 1 with 0' => [ 1, 0, null, 0 ];
|
|
yield 'replace 1 with null' => [ 1, null, null, null ];
|
|
|
|
yield 'merge two arrays' => [
|
|
[ 'a' ], [ 'b' ], MergeStrategy::ARRAY_MERGE, [ 'a', 'b' ]
|
|
];
|
|
yield 'merge two maps' => [
|
|
[ 'a' => 1 ], [ 'a' => 2 ], MergeStrategy::ARRAY_MERGE, [ 'a' => 2 ]
|
|
];
|
|
|
|
yield 'empty array replaces 1' => [ 1, [], MergeStrategy::ARRAY_MERGE, [] ];
|
|
yield '1 replaces non-empty array' => [ [ 'x' ], 1, MergeStrategy::ARRAY_MERGE, 1 ];
|
|
yield 'null replaces non-empty array' => [ [ 'x' ], null, MergeStrategy::ARRAY_MERGE, null ];
|
|
|
|
yield 'empty array replaces non-empty array' => [ [ 'x' ], [], MergeStrategy::REPLACE, [] ];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideSetNewValue
|
|
*
|
|
* @param mixed $first
|
|
* @param mixed $second
|
|
* @param string $strategy
|
|
* @param mixed $expected
|
|
*/
|
|
public function testSetNewValue( $first, $second, $strategy, $expected ) {
|
|
$this->getConfigSink()
|
|
->set( 'TestKey', $first )
|
|
->set(
|
|
'TestKey',
|
|
$second, $strategy ? MergeStrategy::newFromName( $strategy ) : null
|
|
);
|
|
$this->assertKeyHasValue( 'TestKey', $expected );
|
|
}
|
|
|
|
public function provideSetDefaultValue() {
|
|
yield 'do not replace 1 with 2' => [ 1, 2, null, 1 ];
|
|
yield 'do not replace 0 with 2' => [ 0, 2, null, 0 ];
|
|
yield 'do not replace null with 2' => [ false, 2, null, null ];
|
|
yield 'do not replace false with 2' => [ null, 2, null, false ];
|
|
yield 'do not replace an empty array with 2' => [ [], 2, null, [] ];
|
|
yield 'do not replace an empty array with a non-empty one' => [ [], [ 2 ], null, [] ];
|
|
|
|
yield 'merge two arrays' => [
|
|
[ 'a' ], [ 'b' ], MergeStrategy::ARRAY_MERGE, [ 'b', 'a' ]
|
|
];
|
|
yield 'merge two maps' => [
|
|
[ 'a' => 1 ], [ 'a' => 2 ], MergeStrategy::ARRAY_MERGE, [ 'a' => 1 ]
|
|
];
|
|
|
|
yield 'non-empty array does not replace 1' => [ 1, [ 'x' ], MergeStrategy::ARRAY_MERGE, 1 ];
|
|
yield '1 does not replace empty array' => [ [], 1, MergeStrategy::ARRAY_MERGE, [] ];
|
|
yield '1 does not replace non-empty array' => [ [ 'x' ], 1, MergeStrategy::ARRAY_MERGE, [ 'x' ] ];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideSetDefaultValue
|
|
*
|
|
* @param mixed $first
|
|
* @param mixed $second
|
|
* @param string $strategy
|
|
* @param mixed $expected
|
|
*/
|
|
public function testSetDefaultValue( $first, $second, $strategy, $expected ) {
|
|
$this->getConfigSink()
|
|
->set( 'TestKey', $first )
|
|
->setDefault(
|
|
'TestKey',
|
|
$second,
|
|
$strategy ? MergeStrategy::newFromName( $strategy ) : null
|
|
);
|
|
$this->assertKeyHasValue( 'TestKey', $expected );
|
|
}
|
|
|
|
}
|