diff --git a/src/Contracts/WindowManager.php b/src/Contracts/WindowManager.php index 3bba722..e6b3714 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 377d37f..c1311bf 100644 --- a/src/Fakes/WindowManagerFake.php +++ b/src/Fakes/WindowManagerFake.php @@ -23,6 +23,8 @@ class WindowManagerFake implements WindowManagerContract public array $shown = []; + public array $reloaded = []; + public array $forcedWindowReturnValues = []; public function __construct( @@ -74,6 +76,11 @@ public function show($id = null) $this->shown[] = $id; } + public function reload($id = null): void + { + $this->reloaded[] = $id; + } + public function current(): Window { $this->ensureForceReturnWindowsProvided(); @@ -104,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); @@ -125,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); @@ -146,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); @@ -167,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); @@ -187,6 +194,48 @@ public function assertShown(string|Closure $id): void PHPUnit::assertTrue($hit); } + /** + * @param null|string|Closure(string): bool $id + */ + public function assertReloaded(null|string|Closure $id = null): 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 null|string|Closure(string): bool $id + */ + public function assertNotReloaded(null|string|Closure $id = null): 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 b510277..fce0e98 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)]);