From fc67a7f1069e37e75dd49de105cbcc943b96a4bc Mon Sep 17 00:00:00 2001 From: AdrianDev374 Date: Mon, 20 Oct 2025 23:05:22 +0200 Subject: [PATCH 1/3] Add tests for window reload assertions and update API contracts and fakes --- src/Contracts/WindowManager.php | 2 ++ src/Fakes/WindowManagerFake.php | 49 +++++++++++++++++++++++++++ tests/Fakes/FakeWindowManagerTest.php | 35 +++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/src/Contracts/WindowManager.php b/src/Contracts/WindowManager.php index 3bba7226..e6b37145 100644 --- a/src/Contracts/WindowManager.php +++ b/src/Contracts/WindowManager.php @@ -20,4 +20,6 @@ public function current(): Window; public function all(): array; public function get(string $id): Window; + + public function reload($id = null): void; } diff --git a/src/Fakes/WindowManagerFake.php b/src/Fakes/WindowManagerFake.php index 0cb80edc..e063964a 100644 --- a/src/Fakes/WindowManagerFake.php +++ b/src/Fakes/WindowManagerFake.php @@ -20,6 +20,8 @@ class WindowManagerFake implements WindowManagerContract public array $shown = []; + public array $reloaded = []; + public array $forcedWindowReturnValues = []; public function __construct( @@ -71,6 +73,11 @@ public function show($id = null) $this->shown[] = $id; } + public function reload($id = null): void + { + $this->reloaded[] = $id; + } + public function current(): Window { $this->ensureForceReturnWindowsProvided(); @@ -184,6 +191,48 @@ public function assertShown(string|Closure $id): void PHPUnit::assertTrue($hit); } + /** + * @param string|Closure(string): bool $id + */ + public function assertReloaded(string|Closure $id): void + { + if (is_callable($id) === false) { + PHPUnit::assertContains($id, $this->reloaded); + + return; + } + + $hit = empty( + array_filter( + $this->reloaded, + fn (mixed $reloadedId) => $id($reloadedId) === true + ) + ) === false; + + PHPUnit::assertTrue($hit); + } + + /** + * @param string|Closure(string): bool $id + */ + public function assertNotReloaded(string|Closure $id): void + { + if (is_callable($id) === false) { + PHPUnit::assertNotContains($id, $this->reloaded); + return; + } + + $hit = empty( + array_filter( + $this->reloaded, + fn (mixed $reloadedId) => $id($reloadedId) === true + ) + ) === true; + + PHPUnit::assertTrue($hit); + } + + public function assertOpenedCount(int $expected): void { PHPUnit::assertCount($expected, $this->opened); diff --git a/tests/Fakes/FakeWindowManagerTest.php b/tests/Fakes/FakeWindowManagerTest.php index b510277d..fce0e98f 100644 --- a/tests/Fakes/FakeWindowManagerTest.php +++ b/tests/Fakes/FakeWindowManagerTest.php @@ -154,6 +154,41 @@ $this->fail('Expected assertion to fail'); }); +it('asserts that a window was reloaded', function () { + swap(WindowManagerContract::class, $fake = app(WindowManagerFake::class)); + + app(WindowManagerContract::class)->reload('main'); + app(WindowManagerContract::class)->reload('secondary'); + + $fake->assertReloaded('main'); + $fake->assertReloaded('secondary'); + + try { + $fake->assertReloaded('tertiary'); + } catch (AssertionFailedError) { + return; + } + + $this->fail('Expected assertion to fail'); +}); + +it('asserts that a window was\'t reloaded', function () { + swap(WindowManagerContract::class, $fake = app(WindowManagerFake::class)); + + app(WindowManagerContract::class)->reload('main'); + app(WindowManagerContract::class)->reload('secondary'); + + $fake->assertNotReloaded('tertiary'); + + try { + $fake->assertNotReloaded('main'); + } catch (AssertionFailedError) { + return; + } + + $this->fail('Expected assertion to fail'); +}); + it('asserts opened count', function () { Http::fake(['*' => Http::response(status: 200)]); From 6343af65dd74138a202cf71e28ded4a89499081a Mon Sep 17 00:00:00 2001 From: gwleuverink Date: Thu, 6 Nov 2025 13:14:19 +0100 Subject: [PATCH 2/3] make assertion argument nullable - so it works with the 'default' window --- src/Fakes/WindowManagerFake.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Fakes/WindowManagerFake.php b/src/Fakes/WindowManagerFake.php index bc5bda0a..6228691f 100644 --- a/src/Fakes/WindowManagerFake.php +++ b/src/Fakes/WindowManagerFake.php @@ -111,9 +111,9 @@ public function get(string $id): Window } /** - * @param string|Closure(string): bool $id + * @param null|string|Closure(string): bool $id */ - public function assertOpened(string|Closure $id): void + public function assertOpened(null|string|Closure $id = null): void { if (is_callable($id) === false) { PHPUnit::assertContains($id, $this->opened); @@ -132,9 +132,9 @@ public function assertOpened(string|Closure $id): void } /** - * @param string|Closure(string): bool $id + * @param null|string|Closure(string): bool $id */ - public function assertClosed(string|Closure $id): void + public function assertClosed(null|string|Closure $id = null): void { if (is_callable($id) === false) { PHPUnit::assertContains($id, $this->closed); @@ -153,9 +153,9 @@ public function assertClosed(string|Closure $id): void } /** - * @param string|Closure(string): bool $id + * @param null|string|Closure(string): bool $id */ - public function assertHidden(string|Closure $id): void + public function assertHidden(null|string|Closure $id = null): void { if (is_callable($id) === false) { PHPUnit::assertContains($id, $this->hidden); @@ -174,9 +174,9 @@ public function assertHidden(string|Closure $id): void } /** - * @param string|Closure(string): bool $id + * @param null;|string|Closure(string): bool $id */ - public function assertShown(string|Closure $id): void + public function assertShown(null|string|Closure $id = null): void { if (is_callable($id) === false) { PHPUnit::assertContains($id, $this->shown); @@ -195,9 +195,9 @@ public function assertShown(string|Closure $id): void } /** - * @param string|Closure(string): bool $id + * @param null|string|Closure(string): bool $id */ - public function assertReloaded(string|Closure $id): void + public function assertReloaded(null|string|Closure $id = null): void { if (is_callable($id) === false) { PHPUnit::assertContains($id, $this->reloaded); @@ -216,12 +216,13 @@ public function assertReloaded(string|Closure $id): void } /** - * @param string|Closure(string): bool $id + * @param null|string|Closure(string): bool $id */ - public function assertNotReloaded(string|Closure $id): void + public function assertNotReloaded(null|string|Closure $id = null): void { if (is_callable($id) === false) { PHPUnit::assertNotContains($id, $this->reloaded); + return; } @@ -230,12 +231,11 @@ public function assertNotReloaded(string|Closure $id): void $this->reloaded, fn (mixed $reloadedId) => $id($reloadedId) === true ) - ) === true; + ) === true; PHPUnit::assertTrue($hit); } - public function assertOpenedCount(int $expected): void { PHPUnit::assertCount($expected, $this->opened); From f89b3f98fe84987a167aefb664ed72bbd986a0ee Mon Sep 17 00:00:00 2001 From: gwleuverink Date: Thu, 6 Nov 2025 13:23:43 +0100 Subject: [PATCH 3/3] fix phpdoc typo --- src/Fakes/WindowManagerFake.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fakes/WindowManagerFake.php b/src/Fakes/WindowManagerFake.php index 6228691f..c1311bf3 100644 --- a/src/Fakes/WindowManagerFake.php +++ b/src/Fakes/WindowManagerFake.php @@ -174,7 +174,7 @@ public function assertHidden(null|string|Closure $id = null): void } /** - * @param null;|string|Closure(string): bool $id + * @param null|string|Closure(string): bool $id */ public function assertShown(null|string|Closure $id = null): void {