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

Improvements to window test doubles #426

Merged
merged 2 commits into from
Nov 22, 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
2 changes: 1 addition & 1 deletion src/Facades/Window.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Window extends Facade
{
public static function fake()
{
return tap(new WindowManagerFake, function ($fake) {
return tap(static::getFacadeApplication()->make(WindowManagerFake::class), function ($fake) {
static::swap($fake);
});
}
Expand Down
103 changes: 94 additions & 9 deletions src/Fakes/WindowManagerFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Native\Laravel\Fakes;

use Closure;
use Illuminate\Support\Arr;
use Native\Laravel\Client\Client;
use Native\Laravel\Contracts\WindowManager as WindowManagerContract;
use Native\Laravel\Windows\Window;
use PHPUnit\Framework\Assert as PHPUnit;
use Webmozart\Assert\Assert;

class WindowManagerFake implements WindowManagerContract
{
Expand All @@ -17,6 +20,10 @@ class WindowManagerFake implements WindowManagerContract

public array $forcedWindowReturnValues = [];

public function __construct(
protected Client $client
) {}

/**
* @param array<int, Window> $windows
*/
Expand All @@ -30,6 +37,21 @@ public function alwaysReturnWindows(array $windows): self
public function open(string $id = 'main')
{
$this->opened[] = $id;

$this->ensureForceReturnWindowsProvided();

$matchingWindows = array_filter(
$this->forcedWindowReturnValues,
fn (Window $window) => $window->getId() === $id
);

if (empty($matchingWindows)) {
return $this->forcedWindowReturnValues[array_rand($this->forcedWindowReturnValues)]->setClient($this->client);
}

Assert::count($matchingWindows, 1);

return Arr::first($matchingWindows)->setClient($this->client);
}

public function close($id = null)
Expand Down Expand Up @@ -65,29 +87,92 @@ public function get(string $id): Window

$matchingWindows = array_filter($this->forcedWindowReturnValues, fn (Window $window) => $window->getId() === $id);

PHPUnit::assertNotEmpty($matchingWindows);
PHPUnit::assertCount(1, $matchingWindows);
Assert::notEmpty($matchingWindows);
Assert::count($matchingWindows, 1);

return Arr::first($matchingWindows);
}

public function assertOpened(string $id): void
/**
* @param string|Closure(string): bool $id
*/
public function assertOpened(string|Closure $id): void
{
if (is_callable($id) === false) {
PHPUnit::assertContains($id, $this->opened);

return;
}

$hit = empty(
array_filter(
$this->opened,
fn (string $openedId) => $id($openedId) === true
)
) === false;

PHPUnit::assertTrue($hit);
}

/**
* @param string|Closure(string): bool $id
*/
public function assertClosed(string|Closure $id): void
{
if (is_callable($id) === false) {
PHPUnit::assertContains($id, $this->closed);

return;
}

$hit = empty(
array_filter(
$this->closed,
fn (mixed $closedId) => $id($closedId) === true
)
) === false;

PHPUnit::assertTrue($hit);
}

/**
* @param string|Closure(string): bool $id
*/
public function assertHidden(string|Closure $id): void
{
if (is_callable($id) === false) {
PHPUnit::assertContains($id, $this->hidden);

return;
}

$hit = empty(
array_filter(
$this->hidden,
fn (mixed $hiddenId) => $id($hiddenId) === true
)
) === false;

PHPUnit::assertTrue($hit);
}

public function assertOpenedCount(int $expected): void
{
PHPUnit::assertContains($id, $this->opened);
PHPUnit::assertCount($expected, $this->opened);
}

public function assertClosed(?string $id): void
public function assertClosedCount(int $expected): void
{
PHPUnit::assertContains($id, $this->closed);
PHPUnit::assertCount($expected, $this->closed);
}

public function assertHidden(?string $id): void
public function assertHiddenCount(int $expected): void
{
PHPUnit::assertContains($id, $this->hidden);
PHPUnit::assertCount($expected, $this->hidden);
}

private function ensureForceReturnWindowsProvided(): void
{
PHPUnit::assertNotEmpty($this->forcedWindowReturnValues);
Assert::notEmpty($this->forcedWindowReturnValues, 'No windows were provided to return');
}
}
Loading
Loading