Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal: more tests #6

Merged
merged 6 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

9 changes: 6 additions & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
84 changes: 84 additions & 0 deletions tests/Feature/DebounceCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() {}
}
35 changes: 35 additions & 0 deletions tests/Feature/DebounceJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
59 changes: 59 additions & 0 deletions tests/Feature/DebounceNotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
}