From f70d2d8b8ebf8c18d699906bb77d588819011e21 Mon Sep 17 00:00:00 2001 From: neznaika0 Date: Sat, 8 Feb 2025 01:41:41 +0300 Subject: [PATCH] tests: Update Registrar tests --- tests/_support/Config/TestRegistrar.php | 42 ++++++++++- tests/system/Config/BaseConfigTest.php | 71 ++++++++++++++++++- .../Config/fixtures/RegistrarConfig.php | 17 +++++ 3 files changed, 128 insertions(+), 2 deletions(-) diff --git a/tests/_support/Config/TestRegistrar.php b/tests/_support/Config/TestRegistrar.php index 806fd1518402..07b8e0957e70 100644 --- a/tests/_support/Config/TestRegistrar.php +++ b/tests/_support/Config/TestRegistrar.php @@ -20,13 +20,53 @@ */ class TestRegistrar { - public static function RegistrarConfig() + /** + * @param array $previous + */ + public static function RegistrarConfig(array $previous = []) { + if ($previous === []) { + return [ + 'bar' => [ + 'first', + 'second', + ], + 'cars' => [ + 'Trucks' => [ + 'Volvo' => [ + 'year' => 2019, + 'color' => 'dark blue', + ], + ], + 'Sedans Lux' => [ + 'Toyota' => [ + 'year' => 2025, + 'color' => 'silver', + ], + ], + ], + ]; + } + return [ 'bar' => [ 'first', 'second', ], + 'cars' => array_replace_recursive($previous['cars'], [ + 'Trucks' => [ + 'Volvo' => [ + 'year' => 2019, + 'color' => 'dark blue', + ], + ], + 'Sedans Lux' => [ + 'Toyota' => [ + 'year' => 2025, + 'color' => 'silver', + ], + ], + ]), ]; } } diff --git a/tests/system/Config/BaseConfigTest.php b/tests/system/Config/BaseConfigTest.php index a46551ae8bb4..842dc04b2339 100644 --- a/tests/system/Config/BaseConfigTest.php +++ b/tests/system/Config/BaseConfigTest.php @@ -252,18 +252,87 @@ public function testRecognizesLooseValues(): void $this->assertFalse($config->QFALSE); } - public function testRegistrars(): void + public function testRegistrarsWithDisabledRegistrarHasData(): void { + $modules = new Modules(); + + $modules->registrarHasData = false; + BaseConfig::setModules($modules); + $config = new RegistrarConfig(); $config::$registrars = [TestRegistrar::class]; + $this->setPrivateProperty($config, 'didDiscovery', true); $method = $this->getPrivateMethodInvoker($config, 'registerProperties'); $method(); + $cars = [ + 'Sedans' => [ + 'Toyota' => [ + 'year' => 2018, + 'color' => 'silver', + ], + ], + 'Trucks' => [ + 'Volvo' => [ + 'year' => 2019, + 'color' => 'dark blue', + ], + ], + 'Sedans Lux' => [ + 'Toyota' => [ + 'year' => 2025, + 'color' => 'silver', + ], + ], + ]; + // no change to unmodified property $this->assertSame('bar', $config->foo); // add to an existing array property $this->assertSame(['baz', 'first', 'second'], $config->bar); + // replace some of the keys with another value + $this->assertSame($cars, $config->cars); + } + + public function testRegistrarsWithEnabledRegistrarHasData(): void + { + $modules = new Modules(); + + $modules->registrarHasData = true; + BaseConfig::setModules($modules); + + $config = new RegistrarConfig(); + $config::$registrars = [TestRegistrar::class]; + + $this->setPrivateProperty($config, 'didDiscovery', true); + $method = $this->getPrivateMethodInvoker($config, 'registerProperties'); + $method(); + + $cars = [ + 'Sedans' => [ + 'Toyota' => [ + 'year' => 2018, + 'color' => 'silver', + ], + ], + 'Trucks' => [ + 'Volvo' => [ + 'year' => 2019, + 'color' => 'dark blue', + ], + ], + 'Sedans Lux' => [ + 'Toyota' => [ + 'year' => 2025, + 'color' => 'silver', + ], + ], + ]; + + $this->assertSame('bar', $config->foo); + $this->assertSame(['first', 'second'], $config->bar); + $this->assertSame($cars, $config->cars); } public function testBadRegistrar(): void diff --git a/tests/system/Config/fixtures/RegistrarConfig.php b/tests/system/Config/fixtures/RegistrarConfig.php index 4f6e1b493eed..346375ad12e4 100644 --- a/tests/system/Config/fixtures/RegistrarConfig.php +++ b/tests/system/Config/fixtures/RegistrarConfig.php @@ -17,4 +17,21 @@ class RegistrarConfig extends CodeIgniter\Config\BaseConfig public $bar = [ 'baz', ]; + /** + * @var array> + */ + public $cars = [ + 'Sedans' => [ + 'Toyota' => [ + 'year' => 2018, + 'color' => 'silver', + ], + ], + 'Trucks' => [ + 'Volvo' => [ + 'year' => 2019, + 'color' => 'blue', + ], + ], + ]; }