diff --git a/src/Contracts/GlobalShortcut.php b/src/Contracts/GlobalShortcut.php new file mode 100644 index 0000000..2fff35e --- /dev/null +++ b/src/Contracts/GlobalShortcut.php @@ -0,0 +1,14 @@ +make(GlobalShortcutFake::class), function ($fake) { + static::swap($fake); + }); + } + protected static function getFacadeAccessor() { - return \Native\Laravel\GlobalShortcut::class; + return GlobalShortcutContract::class; } } diff --git a/src/Fakes/GlobalShortcutFake.php b/src/Fakes/GlobalShortcutFake.php new file mode 100644 index 0000000..13efa1e --- /dev/null +++ b/src/Fakes/GlobalShortcutFake.php @@ -0,0 +1,100 @@ + + */ + public array $keys = []; + + /** + * @var array + */ + public array $events = []; + + public int $registeredCount = 0; + + public int $unregisteredCount = 0; + + public function key(string $key): self + { + $this->keys[] = $key; + + return $this; + } + + public function event(string $event): self + { + $this->events[] = $event; + + return $this; + } + + public function register(): void + { + $this->registeredCount++; + } + + public function unregister(): void + { + $this->unregisteredCount++; + } + + /** + * @param string|Closure(string): bool $key + */ + public function assertKey(string|Closure $key): void + { + if (is_callable($key) === false) { + PHPUnit::assertContains($key, $this->keys); + + return; + } + + $hit = empty( + array_filter( + $this->keys, + fn (string $keyIteration) => $key($keyIteration) === true + ) + ) === false; + + PHPUnit::assertTrue($hit); + } + + /** + * @param string|Closure(string): bool $event + */ + public function assertEvent(string|Closure $event): void + { + if (is_callable($event) === false) { + PHPUnit::assertContains($event, $this->events); + + return; + } + + $hit = empty( + array_filter( + $this->events, + fn (string $eventIteration) => $event($eventIteration) === true + ) + ) === false; + + PHPUnit::assertTrue($hit); + } + + public function assertRegisteredCount(int $count): void + { + PHPUnit::assertSame($count, $this->registeredCount); + } + + public function assertUnregisteredCount(int $count): void + { + PHPUnit::assertSame($count, $this->unregisteredCount); + } +} diff --git a/src/GlobalShortcut.php b/src/GlobalShortcut.php index 991c341..3859e6a 100644 --- a/src/GlobalShortcut.php +++ b/src/GlobalShortcut.php @@ -3,8 +3,9 @@ namespace Native\Laravel; use Native\Laravel\Client\Client; +use Native\Laravel\Contracts\GlobalShortcut as GlobalShortcutContract; -class GlobalShortcut +class GlobalShortcut implements GlobalShortcutContract { protected string $key; diff --git a/src/NativeServiceProvider.php b/src/NativeServiceProvider.php index a17d953..9701766 100644 --- a/src/NativeServiceProvider.php +++ b/src/NativeServiceProvider.php @@ -15,9 +15,11 @@ use Native\Laravel\Commands\MinifyApplicationCommand; use Native\Laravel\Commands\SeedDatabaseCommand; use Native\Laravel\Contracts\ChildProcess as ChildProcessContract; +use Native\Laravel\Contracts\GlobalShortcut as GlobalShortcutContract; use Native\Laravel\Contracts\WindowManager as WindowManagerContract; use Native\Laravel\Events\EventWatcher; use Native\Laravel\Exceptions\Handler; +use Native\Laravel\GlobalShortcut as GlobalShortcutImplementation; use Native\Laravel\Logging\LogWatcher; use Native\Laravel\Windows\WindowManager as WindowManagerImplementation; use Spatie\LaravelPackageTools\Package; @@ -60,6 +62,10 @@ public function packageRegistered() return $app->make(ChildProcessImplementation::class); }); + $this->app->bind(GlobalShortcutContract::class, function (Foundation $app) { + return $app->make(GlobalShortcutImplementation::class); + }); + if (config('nativephp-internal.running')) { $this->app->singleton( \Illuminate\Contracts\Debug\ExceptionHandler::class, diff --git a/tests/Fakes/FakeGlobalShortcutTest.php b/tests/Fakes/FakeGlobalShortcutTest.php new file mode 100644 index 0000000..07bba84 --- /dev/null +++ b/tests/Fakes/FakeGlobalShortcutTest.php @@ -0,0 +1,124 @@ +toBeInstanceOf(GlobalShortcutFake::class); +}); + +it('asserts key using string', function () { + swap(GlobalShortcutContract::class, $fake = app(GlobalShortcutFake::class)); + + $fake->key('testA'); + $fake->key('testB'); + + $fake->assertKey('testA'); + $fake->assertKey('testB'); + + try { + $fake->assertKey('testC'); + } catch (AssertionFailedError) { + return; + } + + $this->fail('Expected assertion to fail'); +}); + +it('asserts key using callable', function () { + swap(GlobalShortcutContract::class, $fake = app(GlobalShortcutFake::class)); + + $fake->key('testA'); + $fake->key('testB'); + + $fake->assertKey(fn (string $key) => $key === 'testA'); + $fake->assertKey(fn (string $key) => $key === 'testB'); + + try { + $fake->assertKey(fn (string $key) => $key === 'testC'); + } catch (AssertionFailedError) { + return; + } + + $this->fail('Expected assertion to fail'); +}); + +it('asserts event using string', function () { + swap(GlobalShortcutContract::class, $fake = app(GlobalShortcutFake::class)); + + $fake->event('testA'); + $fake->event('testB'); + + $fake->assertEvent('testA'); + $fake->assertEvent('testB'); + + try { + $fake->assertEvent('testC'); + } catch (AssertionFailedError) { + return; + } + + $this->fail('Expected assertion to fail'); +}); + +it('asserts event using callable', function () { + swap(GlobalShortcutContract::class, $fake = app(GlobalShortcutFake::class)); + + $fake->event('testA'); + $fake->event('testB'); + + $fake->assertEvent(fn (string $event) => $event === 'testA'); + $fake->assertEvent(fn (string $event) => $event === 'testB'); + + try { + $fake->assertEvent(fn (string $event) => $event === 'testC'); + } catch (AssertionFailedError) { + return; + } + + $this->fail('Expected assertion to fail'); +}); + +it('asserts registered count', function () { + swap(GlobalShortcutContract::class, $fake = app(GlobalShortcutFake::class)); + + $fake->register(); + $fake->register(); + $fake->register(); + + $fake->assertRegisteredCount(3); + + try { + $fake->assertRegisteredCount(2); + } catch (AssertionFailedError) { + return; + } + + $this->fail('Expected assertion to fail'); +}); + +it('asserts unregistered count', function () { + swap(GlobalShortcutContract::class, $fake = app(GlobalShortcutFake::class)); + + $fake->unregister(); + $fake->unregister(); + $fake->unregister(); + + $fake->assertUnregisteredCount(3); + + try { + $fake->assertUnregisteredCount(2); + } catch (AssertionFailedError) { + return; + } + + $this->fail('Expected assertion to fail'); +}); +