Skip to content

Commit

Permalink
Inject Modules into constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Oct 22, 2023
1 parent 8b44a23 commit 3773743
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions system/Config/BaseConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 18 additions & 18 deletions tests/system/Config/BaseConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,6 +48,8 @@ protected function setUp(): void
if (! class_exists('Encryption', false)) {
require $this->fixturesFolder . '/Encryption.php';
}

BaseConfig::$registrars = [];
}

public function testBasicValues(): void
Expand Down Expand Up @@ -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'));
}
Expand Down

0 comments on commit 3773743

Please sign in to comment.