diff --git a/system/Config/BaseConfig.php b/system/Config/BaseConfig.php index b4b9a4a21349..0269194bdc4c 100644 --- a/system/Config/BaseConfig.php +++ b/system/Config/BaseConfig.php @@ -80,9 +80,9 @@ public static function __set_state(array $array) * * The "shortPrefix" is the lowercase-only config class name. */ - public function __construct() + public function __construct(?Modules $modules = null) { - static::$moduleConfig = new Modules(); + static::$moduleConfig = $modules ?? new Modules(); if (! static::$override) { return; diff --git a/tests/system/Config/BaseConfigTest.php b/tests/system/Config/BaseConfigTest.php index 2da390392ed6..ca615602a276 100644 --- a/tests/system/Config/BaseConfigTest.php +++ b/tests/system/Config/BaseConfigTest.php @@ -11,8 +11,11 @@ namespace CodeIgniter\Config; +use CodeIgniter\Autoloader\FileLocator; use CodeIgniter\Test\CIUnitTestCase; +use Config\Modules; use Encryption; +use PHPUnit\Framework\MockObject\MockObject; use RegistrarConfig; use RuntimeException; use SimpleConfig; @@ -45,6 +48,8 @@ protected function setUp(): void if (! class_exists('Encryption', false)) { require $this->fixturesFolder . '/Encryption.php'; } + + BaseConfig::$registrars = []; } public function testBasicValues(): void @@ -265,32 +270,27 @@ public function testBadRegistrar(): void $this->assertSame('bar', $config->foo); } - public function testNotEnabled(): void + public function testDiscoveryNotEnabledWillNotPopulateRegistrarsArray(): void { - $modulesConfig = config('Modules'); - $modulesConfig->enabled = false; - - $config = new RegistrarConfig(); - $config::$registrars = []; - $expected = $config::$registrars; + /** @var MockObject&Modules $modules */ + $modules = $this->createMock(Modules::class); + $modules->method('shouldDiscover')->with('registrars')->willReturn(false); - $method = $this->getPrivateMethodInvoker($config, 'registerProperties'); - $method(); + $config = new RegistrarConfig($modules); - $this->assertSame($expected, $config::$registrars); + $this->assertSame([], $config::$registrars); } - public function testDidDiscovery(): void + public function testRedoingDiscoveryWillStillSetDidDiscoveryPropertyToTrue(): void { - $modulesConfig = config('Modules'); - $modulesConfig->enabled = true; + /** @var FileLocator&MockObject $locator */ + $locator = $this->createMock(FileLocator::class); + $locator->method('search')->with('Config/Registrar.php')->willReturn([]); + Services::injectMock('locator', $locator); - $config = new RegistrarConfig(); - $config::$registrars = []; + $config = new RegistrarConfig(); $this->setPrivateProperty($config, 'didDiscovery', false); - - $method = $this->getPrivateMethodInvoker($config, 'registerProperties'); - $method(); + ($this->getPrivateMethodInvoker($config, 'registerProperties'))(); $this->assertTrue($this->getPrivateProperty($config, 'didDiscovery')); }