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" diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 24ba834..2789551 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -1,10 +1,13 @@ -name: PHP Composer +name: Run tests on: push: - branches: [ $default-branch ] + branches: + - main pull_request: - branches: [ $default-branch ] + branches: + - main + - dev permissions: contents: read diff --git a/tests/Feature/DebounceCommandTest.php b/tests/Feature/DebounceCommandTest.php index 6e01e23..c380efb 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,57 @@ 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); + } + } + + 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 @@ -87,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; + } +}