From a4981b4330aea1764f0968ea3dc06270ae3b5e48 Mon Sep 17 00:00:00 2001 From: Watheq Alshowaiter Date: Fri, 19 Sep 2025 04:25:38 +0300 Subject: [PATCH 1/4] feat: add thinks command for branner --- composer.json | 5 +- src/Console/ThanksCommand.php | 68 ++++++++++++++++++++++ tests/ThanksCommandTest.php | 105 ++++++++++++++++++++++++++++++++++ todos.md | 2 +- 4 files changed, 178 insertions(+), 2 deletions(-) create mode 100644 src/Console/ThanksCommand.php create mode 100644 tests/ThanksCommandTest.php diff --git a/composer.json b/composer.json index 3c71ff9..ec683f8 100644 --- a/composer.json +++ b/composer.json @@ -58,7 +58,10 @@ "@composer run build", "@php vendor/bin/testbench serve" ], - "test": "vendor/bin/phpunit" + "test": "vendor/bin/phpunit", + "post-autoload-dump": [ + "@php -r \"class_exists('WatheqAlshowaiter\\\\ModelFields\\\\Console\\\\ThanksCommand') && WatheqAlshowaiter\\\\ModelFields\\\\Console\\\\ThanksCommand::show();\"" + ] }, "config": { "sort-packages": true, diff --git a/src/Console/ThanksCommand.php b/src/Console/ThanksCommand.php new file mode 100644 index 0000000..5a5798b --- /dev/null +++ b/src/Console/ThanksCommand.php @@ -0,0 +1,68 @@ +environment() === 'testing'; + } + + protected static function openUrl(string $url): void + { + switch (PHP_OS_FAMILY) { + case 'Darwin': + $command = 'open'; + break; + case 'Windows': + $command = 'start'; + break; + default: + $command = 'xdg-open'; + break; + } + + exec(sprintf('%s %s', $command, escapeshellarg($url))); + } +} diff --git a/tests/ThanksCommandTest.php b/tests/ThanksCommandTest.php new file mode 100644 index 0000000..c4294cd --- /dev/null +++ b/tests/ThanksCommandTest.php @@ -0,0 +1,105 @@ +assertTrue(class_exists(ThanksCommand::class)); + $this->assertTrue(method_exists(ThanksCommand::class, 'show')); + } + + public function test_cache_logic() + { + // Ensure cache key doesn't exist initially + Cache::forget('model-fields.banner_shown'); + $this->assertFalse(Cache::get('model-fields.banner_shown', false)); + + // Set cache manually to test the "already shown" logic + Cache::forever('model-fields.banner_shown', true); + $this->assertTrue(Cache::get('model-fields.banner_shown')); + + // Clean up for next test + Cache::forget('model-fields.banner_shown'); + $this->assertFalse(Cache::get('model-fields.banner_shown', false)); + } + + public function test_show_outputs_message_and_sets_cache() + { + // Clear cache and simulate non-CI environment + Cache::forget('model-fields.banner_shown'); + putenv('CI'); + + // Mock posix_isatty to return true (interactive terminal) + if (! function_exists('posix_isatty')) { + function posix_isatty($fd) + { + return true; + } + } + + // Create a mock stream for stdin + $input = fopen('php://memory', 'r+'); + fwrite($input, "n\n"); + rewind($input); + + // Use reflection to temporarily replace the input handling + ob_start(); + + // Since we can't easily mock stdin in the show method, we'll test the cache behavior + // and verify that the method runs without CI detection + $reflection = new \ReflectionMethod(ThanksCommand::class, 'runningInCi'); + $reflection->setAccessible(true); + + // Test that in testing environment, runningInCi returns true + $this->assertTrue($reflection->invoke(null)); + + // Therefore show() should return early and not output anything + ThanksCommand::show(); + $output = ob_get_clean(); + + $this->assertEmpty($output); + $this->assertFalse(Cache::get('model-fields.banner_shown', false)); + + fclose($input); + } + + public function test_show_skips_in_ci_environment() + { + Cache::forget('model-fields.banner_shown'); + putenv('CI=1'); // simulate CI environment + + ob_start(); + ThanksCommand::show(); // should skip showing + $output = ob_get_clean(); + + $this->assertEmpty($output, 'Expected no output in CI environment'); + + putenv('CI'); // clean up + } + + public function test_running_in_ci_detects_testing_environment() + { + // app()->environment() returns 'testing' in PHPUnit + $reflection = new \ReflectionMethod(ThanksCommand::class, 'runningInCi'); + $reflection->setAccessible(true); + $this->assertTrue($reflection->invoke(null)); + } + + public function test_cache_key_constant() + { + $reflection = new \ReflectionClass(ThanksCommand::class); + $constant = $reflection->getConstant('CACHE_KEY'); + $this->assertEquals('model-fields.banner_shown', $constant); + } +} diff --git a/todos.md b/todos.md index cd8f412..c01cffe 100644 --- a/todos.md +++ b/todos.md @@ -48,6 +48,6 @@ - [x] change the banner - [x] in next version change the whole namespace, github about to model fields - [x] remove old code that is not in v3 -- [ ] abstract logic in Field Facade and Builder macro because a lot of duplications +- [x] star after install - [ ] (next version) make commands like backup:tables - [ ] (next version) exclude from required fields that are filled in "creating" observers/events and add test cases for that From 908cb60c6430d7aed11d76634b52542944cfb677 Mon Sep 17 00:00:00 2001 From: WatheqAlshowaiter <24838274+WatheqAlshowaiter@users.noreply.github.com> Date: Fri, 19 Sep 2025 01:29:18 +0000 Subject: [PATCH 2/4] Update CHANGELOG --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8599e72..3ba72d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All notable changes to `model-required-fields` will be documented in this file. +## 3.0.1 - 2025-09-19 + +### What's Changed + +Add Star command after installation + +**Full Changelog**: https://github.com/WatheqAlshowaiter/model-fields/compare/3.0.0...3.0.1 + ## 3.0.3 - 2025-09-18 ### What's Changed From f2f98d4b05529d55d18df9b77ef667b826062658 Mon Sep 17 00:00:00 2001 From: Watheq Alshowaiter Date: Fri, 19 Sep 2025 04:42:44 +0300 Subject: [PATCH 3/4] fix(thanks): ensure banner shows during composer install - Always display thank you message in non-interactive environments - Move interactive browser prompt inside terminal detection check - Fixes issue where banner wasn't appearing during package installation --- src/Console/ThanksCommand.php | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/Console/ThanksCommand.php b/src/Console/ThanksCommand.php index 5a5798b..3d9a9af 100644 --- a/src/Console/ThanksCommand.php +++ b/src/Console/ThanksCommand.php @@ -14,27 +14,24 @@ public static function show(): void return; } - // Skip if running in a non-interactive environment - if (function_exists('posix_isatty') && ! posix_isatty(STDIN)) { - echo "\nšŸŽ‰ Thanks for using Model Fields!\n"; - - return; - } - echo "\n"; echo "šŸŽ‰ Thanks for using Model Fields!\n"; echo "If you find this package useful, please consider starring it on GitHub.\n"; echo "Repository: https://github.com/WatheqAlshowaiter/model-fields\n"; echo "\n"; - echo 'Would you like to open the repository in your browser? [y/N]: '; - $handle = fopen('php://stdin', 'r'); - $response = strtolower(trim(fgets($handle))); - fclose($handle); + // Only show interactive prompt if terminal is interactive + if (function_exists('posix_isatty') && posix_isatty(STDIN)) { + echo 'Would you like to open the repository in your browser? [y/N]: '; + + $handle = fopen('php://stdin', 'r'); + $response = strtolower(trim(fgets($handle))); + fclose($handle); - if ($response === 'y' || $response === 'yes') { - self::openUrl('https://github.com/WatheqAlshowaiter/model-fields'); - echo "Opening repository in your browser...\n"; + if ($response === 'y' || $response === 'yes') { + self::openUrl('https://github.com/WatheqAlshowaiter/model-fields'); + echo "Opening repository in your browser...\n"; + } } echo "Thank you! šŸ™\n\n"; From 5a94b6f122a3e1ed0cab8d47dc116c3f1f54c5e7 Mon Sep 17 00:00:00 2001 From: WatheqAlshowaiter <24838274+WatheqAlshowaiter@users.noreply.github.com> Date: Fri, 19 Sep 2025 01:44:33 +0000 Subject: [PATCH 4/4] Update CHANGELOG --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ba72d1..f35e66b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to `model-required-fields` will be documented in this file. +## 3.0.2 - 2025-09-19 + +Fix banner when install + +**Full Changelog**: https://github.com/WatheqAlshowaiter/model-fields/compare/3.0.1...3.0.2 + ## 3.0.1 - 2025-09-19 ### What's Changed