Skip to content

Commit c2eb405

Browse files
committed
tests: Update Registrar tests
1 parent 89f6c1e commit c2eb405

File tree

3 files changed

+129
-2
lines changed

3 files changed

+129
-2
lines changed

Diff for: tests/_support/Config/TestRegistrar.php

+41-1
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,53 @@
2020
*/
2121
class TestRegistrar
2222
{
23-
public static function RegistrarConfig()
23+
/**
24+
* @param array<int|string, mixed> $previous
25+
*/
26+
public static function RegistrarConfig(array $previous = [])
2427
{
28+
if ($previous === []) {
29+
return [
30+
'bar' => [
31+
'first',
32+
'second',
33+
],
34+
'cars' => [
35+
'Trucks' => [
36+
'Volvo' => [
37+
'year' => 2019,
38+
'color' => 'dark blue',
39+
],
40+
],
41+
'Sedans Lux' => [
42+
'Toyota' => [
43+
'year' => 2025,
44+
'color' => 'silver',
45+
],
46+
],
47+
],
48+
];
49+
}
50+
2551
return [
2652
'bar' => [
2753
'first',
2854
'second',
2955
],
56+
'cars' => array_replace_recursive($previous['cars'], [
57+
'Trucks' => [
58+
'Volvo' => [
59+
'year' => 2019,
60+
'color' => 'dark blue',
61+
],
62+
],
63+
'Sedans Lux' => [
64+
'Toyota' => [
65+
'year' => 2025,
66+
'color' => 'silver',
67+
],
68+
],
69+
]),
3070
];
3171
}
3272
}

Diff for: tests/system/Config/BaseConfigTest.php

+70-1
Original file line numberDiff line numberDiff line change
@@ -252,18 +252,87 @@ public function testRecognizesLooseValues(): void
252252
$this->assertFalse($config->QFALSE);
253253
}
254254

255-
public function testRegistrars(): void
255+
public function testRegistrarsWithDisabledRegistrarHasData(): void
256256
{
257+
$modules = new Modules();
258+
259+
$modules->registrarHasData = false;
260+
BaseConfig::setModules($modules);
261+
257262
$config = new RegistrarConfig();
258263
$config::$registrars = [TestRegistrar::class];
264+
259265
$this->setPrivateProperty($config, 'didDiscovery', true);
260266
$method = $this->getPrivateMethodInvoker($config, 'registerProperties');
261267
$method();
262268

269+
$cars = [
270+
'Sedans' => [
271+
'Toyota' => [
272+
'year' => 2018,
273+
'color' => 'silver',
274+
],
275+
],
276+
'Trucks' => [
277+
'Volvo' => [
278+
'year' => 2019,
279+
'color' => 'dark blue',
280+
],
281+
],
282+
'Sedans Lux' => [
283+
'Toyota' => [
284+
'year' => 2025,
285+
'color' => 'silver',
286+
],
287+
],
288+
];
289+
263290
// no change to unmodified property
264291
$this->assertSame('bar', $config->foo);
265292
// add to an existing array property
266293
$this->assertSame(['baz', 'first', 'second'], $config->bar);
294+
// replace some of the keys with another value
295+
$this->assertSame($cars, $config->cars);
296+
}
297+
298+
public function testRegistrarsWithEnabledRegistrarHasData(): void
299+
{
300+
$modules = new Modules();
301+
302+
$modules->registrarHasData = true;
303+
BaseConfig::setModules($modules);
304+
305+
$config = new RegistrarConfig();
306+
$config::$registrars = [TestRegistrar::class];
307+
308+
$this->setPrivateProperty($config, 'didDiscovery', true);
309+
$method = $this->getPrivateMethodInvoker($config, 'registerProperties');
310+
$method();
311+
312+
$cars = [
313+
'Sedans' => [
314+
'Toyota' => [
315+
'year' => 2018,
316+
'color' => 'silver',
317+
],
318+
],
319+
'Trucks' => [
320+
'Volvo' => [
321+
'year' => 2019,
322+
'color' => 'dark blue',
323+
],
324+
],
325+
'Sedans Lux' => [
326+
'Toyota' => [
327+
'year' => 2025,
328+
'color' => 'silver',
329+
],
330+
],
331+
];
332+
333+
$this->assertSame('bar', $config->foo);
334+
$this->assertSame(['first', 'second'], $config->bar);
335+
$this->assertSame($cars, $config->cars);
267336
}
268337

269338
public function testBadRegistrar(): void

Diff for: tests/system/Config/fixtures/RegistrarConfig.php

+18
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,22 @@ class RegistrarConfig extends CodeIgniter\Config\BaseConfig
1717
public $bar = [
1818
'baz',
1919
];
20+
21+
/**
22+
* @var array<string, array<string, mixed>>
23+
*/
24+
public $cars = [
25+
'Sedans' => [
26+
'Toyota' => [
27+
'year' => 2018,
28+
'color' => 'silver',
29+
],
30+
],
31+
'Trucks' => [
32+
'Volvo' => [
33+
'year' => 2019,
34+
'color' => 'blue',
35+
],
36+
],
37+
];
2038
}

0 commit comments

Comments
 (0)