| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Unit\Settings\Config; |
| 4 |
|
| 5 |
use MediaWiki\Settings\Config\ConfigBuilder; |
| 6 |
use MediaWiki\Settings\Config\MergeStrategy; |
| 7 |
|
| 8 |
trait ConfigSinkTestTrait { |
| 9 |
|
| 10 |
abstract protected function getConfigSink(): ConfigBuilder; |
| 11 |
|
| 12 |
abstract protected function assertKeyHasValue( string $key, $value ); |
| 13 |
|
| 14 |
public function testSet() { |
| 15 |
$this->getConfigSink() |
| 16 |
->set( 'TestKey1', 'foo' ) |
| 17 |
->set( 'TestKey2', 'bar' ); |
| 18 |
$this->assertKeyHasValue( 'TestKey1', 'foo' ); |
| 19 |
$this->assertKeyHasValue( 'TestKey2', 'bar' ); |
| 20 |
} |
| 21 |
|
| 22 |
public function testSetMulti() { |
| 23 |
$this->getConfigSink() |
| 24 |
->setMulti( |
| 25 |
[ |
| 26 |
'TestSetMulti_string' => 'a', |
| 27 |
'TestSetMulti_array1' => [ 1, 2 ], |
| 28 |
'TestSetMulti_array2' => [ 'a', 'b' ] |
| 29 |
], |
| 30 |
[ |
| 31 |
'TestSetMulti_array1' => MergeStrategy::newFromName( MergeStrategy::ARRAY_MERGE ), |
| 32 |
'TestSetMulti_array2' => MergeStrategy::newFromName( MergeStrategy::ARRAY_MERGE ), |
| 33 |
] |
| 34 |
) |
| 35 |
->setMulti( |
| 36 |
[ |
| 37 |
'TestSetMulti_string' => 'b', |
| 38 |
'TestSetMulti_array1' => [ 3, 4 ], |
| 39 |
'TestSetMulti_array2' => [ 'c', 'd' ] |
| 40 |
], |
| 41 |
[ |
| 42 |
'TestSetMulti_array1' => MergeStrategy::newFromName( MergeStrategy::ARRAY_MERGE ), |
| 43 |
'TestSetMulti_array2' => MergeStrategy::newFromName( MergeStrategy::ARRAY_MERGE ), |
| 44 |
] |
| 45 |
) |
| 46 |
->setMulti( [ 'TestSetMulti_array1' => [ 'x' ] ] ); |
| 47 |
|
| 48 |
$this->assertKeyHasValue( 'TestSetMulti_string', 'b' ); |
| 49 |
$this->assertKeyHasValue( 'TestSetMulti_array1', [ 'x' ] ); |
| 50 |
$this->assertKeyHasValue( 'TestSetMulti_array2', [ 'a', 'b', 'c', 'd' ] ); |
| 51 |
} |
| 52 |
|
| 53 |
public function testSetDefault() { |
| 54 |
$this->getConfigSink() |
| 55 |
->setDefault( 'TestDefaultKey1', 'foo' ) |
| 56 |
->setDefault( 'TestDefaultKey2', 'bar' ); |
| 57 |
$this->assertKeyHasValue( 'TestDefaultKey1', 'foo' ); |
| 58 |
$this->assertKeyHasValue( 'TestDefaultKey2', 'bar' ); |
| 59 |
} |
| 60 |
|
| 61 |
public function provideSetNewValue() { |
| 62 |
yield 'replace 1 with 2' => [ 1, 2, null, 2 ]; |
| 63 |
yield 'replace 1 with 0' => [ 1, 0, null, 0 ]; |
| 64 |
yield 'replace 1 with null' => [ 1, null, null, null ]; |
| 65 |
|
| 66 |
yield 'merge two arrays' => [ |
| 67 |
[ 'a' ], [ 'b' ], MergeStrategy::ARRAY_MERGE, [ 'a', 'b' ] |
| 68 |
]; |
| 69 |
yield 'merge two maps' => [ |
| 70 |
[ 'a' => 1 ], [ 'a' => 2 ], MergeStrategy::ARRAY_MERGE, [ 'a' => 2 ] |
| 71 |
]; |
| 72 |
|
| 73 |
yield 'empty array replaces 1' => [ 1, [], MergeStrategy::ARRAY_MERGE, [] ]; |
| 74 |
yield '1 replaces non-empty array' => [ [ 'x' ], 1, MergeStrategy::ARRAY_MERGE, 1 ]; |
| 75 |
yield 'null replaces non-empty array' => [ [ 'x' ], null, MergeStrategy::ARRAY_MERGE, null ]; |
| 76 |
|
| 77 |
yield 'empty array replaces non-empty array' => [ [ 'x' ], [], MergeStrategy::REPLACE, [] ]; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function testSetNewValue( $first, $second, $strategy, $expected ) { |
| 89 |
$this->getConfigSink() |
| 90 |
->set( 'TestNewValueKey', $first ) |
| 91 |
->set( |
| 92 |
'TestNewValueKey', |
| 93 |
$second, $strategy ? MergeStrategy::newFromName( $strategy ) : null |
| 94 |
); |
| 95 |
$this->assertKeyHasValue( 'TestNewValueKey', $expected ); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function testSetNewValue_multi( $first, $second, $strategy, $expected ) { |
| 110 |
$this->getConfigSink() |
| 111 |
->set( 'TestNewValueKeyMulti', $first ) |
| 112 |
->setMulti( |
| 113 |
[ 'TestNewValueKeyMulti' => $second ], |
| 114 |
[ 'TestNewValueKeyMulti' => $strategy ? MergeStrategy::newFromName( $strategy ) : null ] |
| 115 |
); |
| 116 |
$this->assertKeyHasValue( 'TestNewValueKeyMulti', $expected ); |
| 117 |
} |
| 118 |
|
| 119 |
public function provideSetDefaultValue() { |
| 120 |
yield 'do not replace 1 with 2' => [ 1, 2, null, 1 ]; |
| 121 |
yield 'do not replace 0 with 2' => [ 0, 2, null, 0 ]; |
| 122 |
yield 'do not replace null with 2' => [ null, 2, null, null ]; |
| 123 |
yield 'do not replace false with 2' => [ false, 2, null, false ]; |
| 124 |
yield 'do not replace an empty array with 2' => [ [], 2, null, [] ]; |
| 125 |
yield 'do not replace an empty array with a non-empty one' => [ [], [ 2 ], null, [] ]; |
| 126 |
|
| 127 |
yield 'merge two arrays' => [ |
| 128 |
[ 'a' ], [ 'b' ], MergeStrategy::ARRAY_MERGE, [ 'b', 'a' ] |
| 129 |
]; |
| 130 |
yield 'merge two maps' => [ |
| 131 |
[ 'a' => 1 ], [ 'a' => 2 ], MergeStrategy::ARRAY_MERGE, [ 'a' => 1 ] |
| 132 |
]; |
| 133 |
|
| 134 |
yield 'non-empty array does not replace 1' => [ 1, [ 'x' ], MergeStrategy::ARRAY_MERGE, 1 ]; |
| 135 |
yield '1 does not replace empty array' => [ [], 1, MergeStrategy::ARRAY_MERGE, [] ]; |
| 136 |
yield '1 does not replace non-empty array' => [ [ 'x' ], 1, MergeStrategy::ARRAY_MERGE, [ 'x' ] ]; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function testSetDefaultValue( $first, $second, $strategy, $expected ) { |
| 148 |
$this->getConfigSink() |
| 149 |
->set( 'TestDefaultValueKey', $first ) |
| 150 |
->setDefault( |
| 151 |
'TestDefaultValueKey', |
| 152 |
$second, |
| 153 |
$strategy ? MergeStrategy::newFromName( $strategy ) : null |
| 154 |
); |
| 155 |
$this->assertKeyHasValue( 'TestDefaultValueKey', $expected ); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public function testSetDefaultValue_multi( $first, $second, $strategy, $expected ) { |
| 170 |
$this->getConfigSink() |
| 171 |
->set( 'TestDefaultValueKey', $first ) |
| 172 |
->setMultiDefault( |
| 173 |
[ 'TestDefaultValueKey' => $second ], |
| 174 |
[ 'TestDefaultValueKey' => $strategy ? MergeStrategy::newFromName( $strategy ) : null ] |
| 175 |
); |
| 176 |
$this->assertKeyHasValue( 'TestDefaultValueKey', $expected ); |
| 177 |
} |
| 178 |
|
| 179 |
public function testSetMultiDefault() { |
| 180 |
$this->getConfigSink() |
| 181 |
->setMultiDefault( |
| 182 |
[ |
| 183 |
'TestSetMultiDefault_string' => 'a', |
| 184 |
'TestSetMultiDefault_array' => [ 1, 2 ] |
| 185 |
], |
| 186 |
[ |
| 187 |
'TestSetMultiDefault_array' => MergeStrategy::newFromName( MergeStrategy::ARRAY_MERGE ) |
| 188 |
] |
| 189 |
) |
| 190 |
->setMultiDefault( |
| 191 |
[ |
| 192 |
'TestSetMultiDefault_string' => 'b', |
| 193 |
'TestSetMultiDefault_array' => [ 3, 4 ] |
| 194 |
], |
| 195 |
[ |
| 196 |
'TestSetMultiDefault_array' => MergeStrategy::newFromName( MergeStrategy::ARRAY_MERGE ) |
| 197 |
] |
| 198 |
); |
| 199 |
$this->assertKeyHasValue( 'TestSetMultiDefault_string', 'a' ); |
| 200 |
$this->assertKeyHasValue( 'TestSetMultiDefault_array', [ 3, 4, 1, 2 ] ); |
| 201 |
} |
| 202 |
|
| 203 |
} |