Skip to content

Commit

Permalink
Improvements to window test doubles (#426)
Browse files Browse the repository at this point in the history
* Improvements to fake assertions:
- Add support for optionally passing closures to methods that perform value assertions
- Add count assertions
- Enhancements to FakeWindowManagerTest::class

* Improvements to window faking:
- Resolve test environment error caused FakeWindowManager::open() not returning a Window/PendingOpenWindow object. This now mimics the real WindowManager::open() behavior
- Remove usage of PHPUnit assertions in FakeWindowManager::class for internal assertions. Now only used for final assertions. This change fixes issue where PHPUnit was reporting 2 assertions per ::assertX() call on the test double
  • Loading branch information
XbNz authored Nov 22, 2024
1 parent 3133d14 commit a6dea1f
Show file tree
Hide file tree
Showing 3 changed files with 287 additions and 27 deletions.
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

0 comments on commit a6dea1f

Please sign in to comment.