Skip to content

Global shortcut test double #436

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

Merged
merged 1 commit into from
Dec 1, 2024
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
14 changes: 14 additions & 0 deletions src/Contracts/GlobalShortcut.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Native\Laravel\Contracts;

interface GlobalShortcut
{
public function key(string $key): self;

public function event(string $event): self;

public function register(): void;

public function unregister(): void;
}
11 changes: 10 additions & 1 deletion src/Facades/GlobalShortcut.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Native\Laravel\Facades;

use Illuminate\Support\Facades\Facade;
use Native\Laravel\Contracts\GlobalShortcut as GlobalShortcutContract;
use Native\Laravel\Fakes\GlobalShortcutFake;

/**
* @method static \Native\Laravel\GlobalShortcut key(string $key)
Expand All @@ -12,8 +14,15 @@
*/
class GlobalShortcut extends Facade
{
public static function fake()
{
return tap(static::getFacadeApplication()->make(GlobalShortcutFake::class), function ($fake) {
static::swap($fake);
});
}

protected static function getFacadeAccessor()
{
return \Native\Laravel\GlobalShortcut::class;
return GlobalShortcutContract::class;
}
}
100 changes: 100 additions & 0 deletions src/Fakes/GlobalShortcutFake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Native\Laravel\Fakes;

use Closure;
use Native\Laravel\Contracts\GlobalShortcut as GlobalShortcutContract;
use PHPUnit\Framework\Assert as PHPUnit;

class GlobalShortcutFake implements GlobalShortcutContract
{
/**
* @var array<int, string>
*/
public array $keys = [];

/**
* @var array<int, string>
*/
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);
}
}
3 changes: 2 additions & 1 deletion src/GlobalShortcut.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions src/NativeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
124 changes: 124 additions & 0 deletions tests/Fakes/FakeGlobalShortcutTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

use Native\Laravel\Facades\GlobalShortcut;
use Native\Laravel\Contracts\GlobalShortcut as GlobalShortcutContract;
use Native\Laravel\Fakes\GlobalShortcutFake;

use PHPUnit\Framework\AssertionFailedError;

use function Pest\Laravel\swap;

it('swaps implementations using facade', function () {
GlobalShortcut::fake();

expect(app(GlobalShortcutContract::class))->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');
});

Loading