From 3e261b26448fe46f7e4c58c0d22e1f88cc58d87e Mon Sep 17 00:00:00 2001 From: zackaj Date: Thu, 6 Feb 2025 17:01:29 +0100 Subject: [PATCH 1/6] add testing for debouncing from cli --- tests/Feature/DebounceCommandTest.php | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/Feature/DebounceCommandTest.php b/tests/Feature/DebounceCommandTest.php index 6e01e23..f0950a9 100644 --- a/tests/Feature/DebounceCommandTest.php +++ b/tests/Feature/DebounceCommandTest.php @@ -6,6 +6,7 @@ use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Queue; +use Zackaj\LaravelDebounce\Commands\DebounceConsoleCommand; use Zackaj\LaravelDebounce\DebounceCommand; use Zackaj\LaravelDebounce\Facades\Debounce; use Zackaj\LaravelDebounce\Tests\BaseCase; @@ -57,6 +58,44 @@ public function test_debounce_command_is_fired() $this->assertTrue(DCommand::$fired); } + + public function test_debounce_from_cli_is_debounced_and_fired() + { + Queue::fake(); + Artisan::registerCommand(new NormalCommand); + Artisan::registerCommand(new DCommand); + Artisan::registerCommand(new DebounceConsoleCommand); + $commands = [ + ['signature' => 'test:test', 'class' => NormalCommand::class], + ['signature' => 'dtest:test', 'class' => DCommand::class], + ]; + + foreach ($commands as $key => $cmd) { + //for future tests + $args = [ + 'command' => $cmd['signature'], + 'delay' => 0, + 'uniqueKey' => 'key', + 'parameters' => [ + 'word' => 'hello', + ], + ]; + + $commandString = sprintf( + 'debounce:command %s %s %s %s', + $args['delay'], + $args['uniqueKey'], + $args['command'], + $args['parameters']['word'], + ); + + $this->artisan($commandString)->assertSuccessful(); + $this->artisan($commandString)->assertSuccessful(); + Queue::assertCount($key + 1); + + $this->assertTrue($cmd['class']::$fired); + } + } } class NormalCommand extends Command From 72ae83986f05df8063696bd26362bdbb101db5ea Mon Sep 17 00:00:00 2001 From: zackAJ <101515566+zackAJ@users.noreply.github.com> Date: Thu, 6 Feb 2025 16:01:55 +0000 Subject: [PATCH 2/6] Pint styling --- tests/Feature/DebounceCommandTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/DebounceCommandTest.php b/tests/Feature/DebounceCommandTest.php index f0950a9..be9b5c2 100644 --- a/tests/Feature/DebounceCommandTest.php +++ b/tests/Feature/DebounceCommandTest.php @@ -71,7 +71,7 @@ public function test_debounce_from_cli_is_debounced_and_fired() ]; foreach ($commands as $key => $cmd) { - //for future tests + // for future tests $args = [ 'command' => $cmd['signature'], 'delay' => 0, From 205c9f8e812499808484d719cf27164a8df5b645 Mon Sep 17 00:00:00 2001 From: zackaj Date: Thu, 6 Feb 2025 17:02:50 +0100 Subject: [PATCH 3/6] changed code styling commit message --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 262aa1a..bd0bc66 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -30,5 +30,5 @@ jobs: - name: Commit linted files uses: stefanzweifel/git-auto-commit-action@v5 with: - commit_message: Pint styling + commit_message: "chore: code styling" From 5277cbd6bece697bafed3f80e6bf95b989efc67d Mon Sep 17 00:00:00 2001 From: zackaj Date: Fri, 7 Feb 2025 15:07:04 +0100 Subject: [PATCH 4/6] add testing for before and after hooks --- tests/Feature/DebounceCommandTest.php | 45 +++++++++++++++++ tests/Feature/DebounceJobTest.php | 35 +++++++++++++ tests/Feature/DebounceNotificationTest.php | 59 ++++++++++++++++++++++ 3 files changed, 139 insertions(+) diff --git a/tests/Feature/DebounceCommandTest.php b/tests/Feature/DebounceCommandTest.php index be9b5c2..c380efb 100644 --- a/tests/Feature/DebounceCommandTest.php +++ b/tests/Feature/DebounceCommandTest.php @@ -96,6 +96,19 @@ public function test_debounce_from_cli_is_debounced_and_fired() $this->assertTrue($cmd['class']::$fired); } } + + public function test_before_and_after_hooks_are_fired() + { + $commands = [new DCommandAfter, new DCommandBefore]; + + foreach ($commands as $cmd) { + Artisan::registerCommand($cmd); + + Debounce::command('dtest:test', 0, 'key', ['word' => 'test arg'], false); + + $this->assertTrue($cmd::$fired); + } + } } class NormalCommand extends Command @@ -126,3 +139,35 @@ public function handle() static::$fired = true; } } + +class DCommandAfter extends DebounceCommand +{ + protected $signature = 'dtest:test {word}'; + + protected $description = 'test debounce command'; + + public static $fired = false; + + public static function after(): void + { + static::$fired = true; + } + + public function handle() {} +} + +class DCommandBefore extends DebounceCommand +{ + protected $signature = 'dtest:test {word}'; + + protected $description = 'test debounce command'; + + public static $fired = false; + + public static function before(): void + { + static::$fired = true; + } + + public function handle() {} +} diff --git a/tests/Feature/DebounceJobTest.php b/tests/Feature/DebounceJobTest.php index f11604f..25efc26 100644 --- a/tests/Feature/DebounceJobTest.php +++ b/tests/Feature/DebounceJobTest.php @@ -52,6 +52,17 @@ public function test_debounce_job_is_fired() $this->assertTrue(DJob::$fired); } + + public function test_before_and_after_hooks_are_fired() + { + $jobs = [new DJobAfter, new DJobBefore]; + + foreach ($jobs as $job) { + Debounce::job($job, 0, 'key', false); + + $this->assertTrue($job::$fired); + } + } } class NormalJob implements ShouldQueue @@ -73,3 +84,27 @@ public function handle(): void static::$fired = true; } } + +class DJobAfter extends DebounceJob +{ + public static bool $fired = false; + + public function handle(): void {} + + public function after(): void + { + static::$fired = true; + } +} + +class DJobBefore extends DebounceJob +{ + public static bool $fired = false; + + public function handle(): void {} + + public function before(): void + { + static::$fired = true; + } +} diff --git a/tests/Feature/DebounceNotificationTest.php b/tests/Feature/DebounceNotificationTest.php index a563cb9..a8610b4 100644 --- a/tests/Feature/DebounceNotificationTest.php +++ b/tests/Feature/DebounceNotificationTest.php @@ -2,6 +2,7 @@ namespace Zackaj\LaravelDebounce\Tests\Feature; +use Carbon\Carbon; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Notifications\Notification; @@ -62,6 +63,30 @@ public function test_debounce_notification_is_fired() FacadesNotification::assertCount(1); } + + public function test_before_and_after_hooks_are_fired() + { + FacadesNotification::fake(); + $notifications = [new DNotificationAfter, new DNotificationBefore]; + $user = UserFactory::new()->create(); + + foreach ($notifications as $notif) { + Debounce::notification($user, $notif, 0, 'key', false); + + $this->assertTrue(DNotificationAfter::$fired); + } + } + + public function test_debounce_notification_latest_activity_is_called() + { + FacadesNotification::fake(); + $notif = new DNotificationLatestActivity; + $user = UserFactory::new()->create(); + + Debounce::notification($user, $notif, 0, 'key', false); + + $this->assertTrue(DNotificationLatestActivity::$delay === 1); + } } class NormalNotification extends Notification implements ShouldQueue @@ -84,3 +109,37 @@ public function via(): array return ['database']; } } + +class DNotificationAfter extends DNotification +{ + public static bool $fired = false; + + public function after($notifiables): void + { + static::$fired = true; + } +} + +class DNotificationBefore extends DNotification +{ + public static bool $fired = false; + + public function before($notifiables): void + { + static::$fired = true; + } +} + +class DNotificationLatestActivity extends DNotification +{ + public static bool $fired = false; + + public static $delay = 0; + + public function getLastActivityTimestamp(mixed $notifiables): ?Carbon + { + static::$delay = 1; + + return null; + } +} From c026cc455d75d9422d20cb97f6bded1d25a0a5cb Mon Sep 17 00:00:00 2001 From: zackaj Date: Fri, 7 Feb 2025 17:56:55 +0100 Subject: [PATCH 5/6] fix test workflow --- .github/workflows/tests.yaml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 24ba834..43b1fc3 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -2,9 +2,12 @@ name: PHP Composer on: push: - branches: [ $default-branch ] + branches: + - main pull_request: - branches: [ $default-branch ] + branches: + - main + - dev permissions: contents: read From d037d7ed929237d93066140b4217e44708905d5e Mon Sep 17 00:00:00 2001 From: zackaj Date: Fri, 7 Feb 2025 17:57:58 +0100 Subject: [PATCH 6/6] rename tests workflow --- .github/workflows/tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 43b1fc3..2789551 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -1,4 +1,4 @@ -name: PHP Composer +name: Run tests on: push: