Skip to content

Commit

Permalink
tests: Update Registrar tests
Browse files Browse the repository at this point in the history
  • Loading branch information
neznaika0 committed Feb 7, 2025
1 parent b8ca97c commit f70d2d8
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 2 deletions.
42 changes: 41 additions & 1 deletion tests/_support/Config/TestRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,53 @@
*/
class TestRegistrar
{
public static function RegistrarConfig()
/**
* @param array<int|string, mixed> $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',
],
],
]),
];
}
}
71 changes: 70 additions & 1 deletion tests/system/Config/BaseConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions tests/system/Config/fixtures/RegistrarConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,21 @@ class RegistrarConfig extends CodeIgniter\Config\BaseConfig
public $bar = [
'baz',
];
/**
* @var array<string, array<string, mixed>>
*/
public $cars = [
'Sedans' => [
'Toyota' => [
'year' => 2018,
'color' => 'silver',
],
],
'Trucks' => [
'Volvo' => [
'year' => 2019,
'color' => 'blue',
],
],
];
}

0 comments on commit f70d2d8

Please sign in to comment.