From a0e35bac89b55efe58c8872c80e41dca7337b0ed Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 17 May 2024 08:44:27 +0900 Subject: [PATCH 1/8] fix: change to give priority to first command discoverd Now you can replace CI4's commands with your commands in app/Commands/. --- system/CLI/Commands.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/CLI/Commands.php b/system/CLI/Commands.php index 30bd2c2652cd..2115714106bc 100644 --- a/system/CLI/Commands.php +++ b/system/CLI/Commands.php @@ -125,7 +125,7 @@ public function discoverCommands() /** @var BaseCommand $class */ $class = new $className($this->logger, $this); - if (isset($class->group)) { + if (isset($class->group) && ! isset($this->commands[$class->name])) { $this->commands[$class->name] = [ 'class' => $className, 'file' => $file, From e6b59311e229a471ba9b785aac390128f34b0a19 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 18 Jun 2024 11:21:47 +0900 Subject: [PATCH 2/8] test: add test --- tests/_support/_command/ListCommands.php | 59 +++++++++++++++++ tests/system/Commands/CommandOverrideTest.php | 64 +++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 tests/_support/_command/ListCommands.php create mode 100644 tests/system/Commands/CommandOverrideTest.php diff --git a/tests/_support/_command/ListCommands.php b/tests/_support/_command/ListCommands.php new file mode 100644 index 000000000000..fe1c9611a222 --- /dev/null +++ b/tests/_support/_command/ListCommands.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace App\Commands; + +use CodeIgniter\CLI\CLI; +use CodeIgniter\Commands\ListCommands as BaseListCommands; + +class ListCommands extends BaseListCommands +{ + /** + * The group the command is lumped under + * when listing commands. + * + * @var string + */ + protected $group = 'App'; + + /** + * The Command's name + * + * @var string + */ + protected $name = 'list'; + + /** + * the Command's short description + * + * @var string + */ + protected $description = 'This is testing to override `list` command.'; + + /** + * the Command's usage + * + * @var string + */ + protected $usage = 'list'; + + /** + * Displays the help for the spark cli script itself. + */ + public function run(array $params) + { + CLI::write('This is ' . self::class); + + return EXIT_SUCCESS; + } +} diff --git a/tests/system/Commands/CommandOverrideTest.php b/tests/system/Commands/CommandOverrideTest.php new file mode 100644 index 000000000000..b21b7e0c6faa --- /dev/null +++ b/tests/system/Commands/CommandOverrideTest.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Commands; + +use CodeIgniter\Test\CIUnitTestCase; +use CodeIgniter\Test\StreamFilterTrait; +use PHPUnit\Framework\Attributes\Group; + +/** + * @internal + */ +#[Group('Others')] +final class CommandOverrideTest extends CIUnitTestCase +{ + use StreamFilterTrait; + + protected function setUp(): void + { + $this->resetServices(); + + parent::setUp(); + } + + protected function getBuffer(): string + { + return $this->getStreamFilterBuffer(); + } + + public function testOverrideListCommands(): void + { + $this->copyListCommands(); + + command('list'); + + $this->assertStringContainsString('This is App\Commands\ListCommands', $this->getBuffer()); + $this->assertStringNotContainsString('Displays basic usage information.', $this->getBuffer()); + + $this->deleteListCommands(); + } + + private function copyListCommands(): void + { + if (! is_dir(APPPATH . 'Commands')) { + mkdir(APPPATH . 'Commands'); + } + copy(SUPPORTPATH . '_command/ListCommands.php', APPPATH . 'Commands/ListCommands.php'); + } + + private function deleteListCommands(): void + { + unlink(APPPATH . 'Commands/ListCommands.php'); + } +} From 54b0441f5fb8263f9766c1bdcb88295b039446b3 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 22 Jun 2024 14:37:09 +0900 Subject: [PATCH 3/8] fix: if conditions to build DSN --- system/Database/OCI8/Connection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Database/OCI8/Connection.php b/system/Database/OCI8/Connection.php index be7eb37cb9a9..7acb3fd72dbc 100644 --- a/system/Database/OCI8/Connection.php +++ b/system/Database/OCI8/Connection.php @@ -120,7 +120,7 @@ private function isValidDSN(): bool */ public function connect(bool $persistent = false) { - if (empty($this->DSN) && ! $this->isValidDSN()) { + if ($this->DSN === null || $this->DSN === '' || ! $this->isValidDSN()) { $this->buildDSN(); } From 9a9da142629240e84d969f100c95952847ba089c Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 22 Jun 2024 14:45:44 +0900 Subject: [PATCH 4/8] refactor: replace empty() --- phpstan-baseline.php | 2 +- system/Database/OCI8/Connection.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpstan-baseline.php b/phpstan-baseline.php index 91cb95cc0a44..2351c19b7c3e 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -3106,7 +3106,7 @@ $ignoreErrors[] = [ // identifier: empty.notAllowed 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 5, + 'count' => 3, 'path' => __DIR__ . '/system/Database/OCI8/Connection.php', ]; $ignoreErrors[] = [ diff --git a/system/Database/OCI8/Connection.php b/system/Database/OCI8/Connection.php index 7acb3fd72dbc..86d2b349000e 100644 --- a/system/Database/OCI8/Connection.php +++ b/system/Database/OCI8/Connection.php @@ -126,7 +126,7 @@ public function connect(bool $persistent = false) $func = $persistent ? 'oci_pconnect' : 'oci_connect'; - return empty($this->charset) + return ($this->charset === '') ? $func($this->username, $this->password, $this->DSN) : $func($this->username, $this->password, $this->DSN, $this->charset); } From fa5f5c6051f21760b6853eca2af25b260ed3f687 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 22 Jun 2024 14:53:29 +0900 Subject: [PATCH 5/8] fix: Deprecated ctype_digit(): Argument of type int will be interpreted as string in the future --- system/Database/OCI8/Connection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Database/OCI8/Connection.php b/system/Database/OCI8/Connection.php index 86d2b349000e..da91f2cc20b0 100644 --- a/system/Database/OCI8/Connection.php +++ b/system/Database/OCI8/Connection.php @@ -632,7 +632,7 @@ protected function buildDSN() } $isEasyConnectableHostName = $this->hostname !== '' && ! str_contains($this->hostname, '/') && ! str_contains($this->hostname, ':'); - $easyConnectablePort = ! empty($this->port) && ctype_digit($this->port) ? ':' . $this->port : ''; + $easyConnectablePort = ! empty($this->port) && ctype_digit((string) $this->port) ? ':' . $this->port : ''; $easyConnectableDatabase = $this->database !== '' ? '/' . ltrim($this->database, '/') : ''; if ($isEasyConnectableHostName && ($easyConnectablePort !== '' || $easyConnectableDatabase !== '')) { From 5ee0df20c4f9f9e0194d52da6103cb10b73db84c Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 22 Jun 2024 14:57:53 +0900 Subject: [PATCH 6/8] refactor: replace empty() --- phpstan-baseline.php | 2 +- system/Database/OCI8/Connection.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpstan-baseline.php b/phpstan-baseline.php index 2351c19b7c3e..1bda1f35594b 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -3106,7 +3106,7 @@ $ignoreErrors[] = [ // identifier: empty.notAllowed 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 3, + 'count' => 2, 'path' => __DIR__ . '/system/Database/OCI8/Connection.php', ]; $ignoreErrors[] = [ diff --git a/system/Database/OCI8/Connection.php b/system/Database/OCI8/Connection.php index da91f2cc20b0..c3dab9ecc2fc 100644 --- a/system/Database/OCI8/Connection.php +++ b/system/Database/OCI8/Connection.php @@ -632,7 +632,7 @@ protected function buildDSN() } $isEasyConnectableHostName = $this->hostname !== '' && ! str_contains($this->hostname, '/') && ! str_contains($this->hostname, ':'); - $easyConnectablePort = ! empty($this->port) && ctype_digit((string) $this->port) ? ':' . $this->port : ''; + $easyConnectablePort = ($this->port !== '') && ctype_digit((string) $this->port) ? ':' . $this->port : ''; $easyConnectableDatabase = $this->database !== '' ? '/' . ltrim($this->database, '/') : ''; if ($isEasyConnectableHostName && ($easyConnectablePort !== '' || $easyConnectableDatabase !== '')) { From 99346b48aa0b8bdb8b491f640d27640091de95a8 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 22 Jun 2024 17:46:59 +0900 Subject: [PATCH 7/8] refactor: move if conditions to isValidDSN() --- system/Database/OCI8/Connection.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/system/Database/OCI8/Connection.php b/system/Database/OCI8/Connection.php index c3dab9ecc2fc..f7983a695c0c 100644 --- a/system/Database/OCI8/Connection.php +++ b/system/Database/OCI8/Connection.php @@ -104,6 +104,10 @@ class Connection extends BaseConnection */ private function isValidDSN(): bool { + if ($this->DSN === null || $this->DSN === '') { + return false; + } + foreach ($this->validDSNs as $regexp) { if (preg_match($regexp, $this->DSN)) { return true; @@ -120,7 +124,7 @@ private function isValidDSN(): bool */ public function connect(bool $persistent = false) { - if ($this->DSN === null || $this->DSN === '' || ! $this->isValidDSN()) { + if (! $this->isValidDSN()) { $this->buildDSN(); } From 3ab06445b45653415c372ecb300dcf73c77b5d16 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 23 Jun 2024 02:13:54 +0700 Subject: [PATCH 8/8] refactor: using phpunit 10 assertObjectHasNotProperty() and assertObjectHasProperty() --- tests/system/Config/BaseConfigTest.php | 2 +- tests/system/Test/FabricatorTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/system/Config/BaseConfigTest.php b/tests/system/Config/BaseConfigTest.php index c8e27dce75ef..2217120f1ea4 100644 --- a/tests/system/Config/BaseConfigTest.php +++ b/tests/system/Config/BaseConfigTest.php @@ -131,7 +131,7 @@ public function testEnvironmentOverrides(): void // override config with shortPrefix ENV var $this->assertSame('hubbahubba', $config->delta); // incorrect env name should not inject property - $this->assertFalse(property_exists($config, 'notthere')); + $this->assertObjectNotHasProperty('notthere', $config); // empty ENV var should not affect config setting $this->assertSame('pineapple', $config->fruit); // non-empty ENV var should overrideconfig setting diff --git a/tests/system/Test/FabricatorTest.php b/tests/system/Test/FabricatorTest.php index 46e7f7d95cfe..c8ae1a7a1db6 100644 --- a/tests/system/Test/FabricatorTest.php +++ b/tests/system/Test/FabricatorTest.php @@ -413,7 +413,7 @@ public function testCreateMockSetsDatabaseFields(): void $this->assertIsInt($result->created_at); $this->assertIsInt($result->updated_at); - $this->assertTrue(property_exists($result, 'deleted_at')); + $this->assertObjectHasProperty('deleted_at', $result); $this->assertNull($result->deleted_at); }