From 9221819ab3e92e6083ec0ec2f537c0c612b9f00b Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 12 Oct 2023 14:21:51 +0900 Subject: [PATCH 1/5] refactor: make sure $component is all lower case --- system/Config/Factories.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/system/Config/Factories.php b/system/Config/Factories.php index a32df02725ac..077c25ca2761 100644 --- a/system/Config/Factories.php +++ b/system/Config/Factories.php @@ -100,6 +100,8 @@ class Factories */ public static function define(string $component, string $alias, string $classname): void { + $component = strtolower($component); + if (isset(self::$aliases[$component][$alias])) { if (self::$aliases[$component][$alias] === $classname) { return; @@ -130,12 +132,14 @@ public static function define(string $component, string $alias, string $classnam */ public static function __callStatic(string $component, array $arguments) { + $component = strtolower($component); + // First argument is the class alias, second is options $alias = trim(array_shift($arguments), '\\ '); $options = array_shift($arguments) ?? []; // Determine the component-specific options - $options = array_merge(self::getOptions(strtolower($component)), $options); + $options = array_merge(self::getOptions($component), $options); if (! $options['getShared']) { if (isset(self::$aliases[$component][$alias])) { @@ -394,6 +398,8 @@ public static function getOptions(string $component): array */ public static function setOptions(string $component, array $values): array { + $component = strtolower($component); + // Allow the config to replace the component name, to support "aliases" $values['component'] = strtolower($values['component'] ?? $component); @@ -452,8 +458,9 @@ public static function reset(?string $component = null) */ public static function injectMock(string $component, string $alias, object $instance) { - // Force a configuration to exist for this component $component = strtolower($component); + + // Force a configuration to exist for this component self::getOptions($component); $class = get_class($instance); From e78a438be738d8245f1071a5febba2d927f3911c Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 12 Oct 2023 14:35:23 +0900 Subject: [PATCH 2/5] fix: $options are not saved in cache --- system/Config/Factories.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/system/Config/Factories.php b/system/Config/Factories.php index 077c25ca2761..120278c37e64 100644 --- a/system/Config/Factories.php +++ b/system/Config/Factories.php @@ -502,12 +502,14 @@ public static function getComponentInstances(string $component): array { if (! isset(static::$aliases[$component])) { return [ + 'options' => [], 'aliases' => [], 'instances' => [], ]; } return [ + 'options' => static::$options[$component], 'aliases' => static::$aliases[$component], 'instances' => self::$instances[$component], ]; @@ -520,9 +522,11 @@ public static function getComponentInstances(string $component): array */ public static function setComponentInstances(string $component, array $data): void { - static::$aliases[$component] = $data['aliases']; - self::$instances[$component] = $data['instances']; - unset(self::$updated[$component]); + static::$options[$component] = $data['options']; + static::$aliases[$component] = $data['aliases']; + static::$instances[$component] = $data['instances']; + + unset(static::$updated[$component]); } /** From 34283e46a7b1df32b4a640afdf7f0413dfe85929 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 12 Oct 2023 14:48:40 +0900 Subject: [PATCH 3/5] refactor: replace ::static with ::self For consistency. --- system/Config/Factories.php | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/system/Config/Factories.php b/system/Config/Factories.php index 120278c37e64..d5ba12efabe1 100644 --- a/system/Config/Factories.php +++ b/system/Config/Factories.php @@ -430,19 +430,19 @@ public static function reset(?string $component = null) { if ($component) { unset( - static::$options[$component], - static::$aliases[$component], - static::$instances[$component], - static::$updated[$component] + self::$options[$component], + self::$aliases[$component], + self::$instances[$component], + self::$updated[$component] ); return; } - static::$options = []; - static::$aliases = []; - static::$instances = []; - static::$updated = []; + self::$options = []; + self::$aliases = []; + self::$instances = []; + self::$updated = []; } /** @@ -500,7 +500,7 @@ public static function getBasename(string $alias): string */ public static function getComponentInstances(string $component): array { - if (! isset(static::$aliases[$component])) { + if (! isset(self::$aliases[$component])) { return [ 'options' => [], 'aliases' => [], @@ -509,8 +509,8 @@ public static function getComponentInstances(string $component): array } return [ - 'options' => static::$options[$component], - 'aliases' => static::$aliases[$component], + 'options' => self::$options[$component], + 'aliases' => self::$aliases[$component], 'instances' => self::$instances[$component], ]; } @@ -522,11 +522,11 @@ public static function getComponentInstances(string $component): array */ public static function setComponentInstances(string $component, array $data): void { - static::$options[$component] = $data['options']; - static::$aliases[$component] = $data['aliases']; - static::$instances[$component] = $data['instances']; + self::$options[$component] = $data['options']; + self::$aliases[$component] = $data['aliases']; + self::$instances[$component] = $data['instances']; - unset(static::$updated[$component]); + unset(self::$updated[$component]); } /** From 7664bb689fcf0d52f50555ef2780fa8cddb70a93 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 12 Oct 2023 15:17:19 +0900 Subject: [PATCH 4/5] test: update assertion --- tests/system/Config/FactoriesTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/system/Config/FactoriesTest.php b/tests/system/Config/FactoriesTest.php index b53c1e9133e5..6f74414def4e 100644 --- a/tests/system/Config/FactoriesTest.php +++ b/tests/system/Config/FactoriesTest.php @@ -429,7 +429,7 @@ public function testGetComponentInstances() public function testSetComponentInstances(array $data) { $before = Factories::getComponentInstances('config'); - $this->assertSame(['aliases' => [], 'instances' => []], $before); + $this->assertSame(['options' => [], 'aliases' => [], 'instances' => []], $before); Factories::setComponentInstances('config', $data); From 054061aae60768f2a8254db782fcb079805b4794 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 17 Oct 2023 10:52:12 +0900 Subject: [PATCH 5/5] test: fix assertion There was 1 failure: 1) CodeIgniter\Database\BaseConnectionTest::testStoresConnectionTimings Failed asserting that '0.000000' is greater than 0.0. /home/runner/work/CodeIgniter4/CodeIgniter4/tests/system/Database/BaseConnectionTest.php:125 --- tests/system/Database/BaseConnectionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/system/Database/BaseConnectionTest.php b/tests/system/Database/BaseConnectionTest.php index 4fb68358066b..51f92a747884 100644 --- a/tests/system/Database/BaseConnectionTest.php +++ b/tests/system/Database/BaseConnectionTest.php @@ -122,7 +122,7 @@ public function testStoresConnectionTimings(): void $db->initialize(); $this->assertGreaterThan($start, $db->getConnectStart()); - $this->assertGreaterThan(0.0, $db->getConnectDuration()); + $this->assertGreaterThanOrEqual(0.0, $db->getConnectDuration()); } /**