Skip to content

Commit 1c62340

Browse files
Add tests for window reload assertions and update API contracts & fakes (#25)
* Add tests for window reload assertions and update API contracts and fakes * make assertion argument nullable - so it works with the 'default' window * fix phpdoc typo --------- Co-authored-by: gwleuverink <willem@leuver.ink>
1 parent 585fd3c commit 1c62340

File tree

3 files changed

+94
-8
lines changed

3 files changed

+94
-8
lines changed

src/Contracts/WindowManager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ public function current(): Window;
2020
public function all(): array;
2121

2222
public function get(string $id): Window;
23+
24+
public function reload($id = null): void;
2325
}

src/Fakes/WindowManagerFake.php

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class WindowManagerFake implements WindowManagerContract
2323

2424
public array $shown = [];
2525

26+
public array $reloaded = [];
27+
2628
public array $forcedWindowReturnValues = [];
2729

2830
public function __construct(
@@ -74,6 +76,11 @@ public function show($id = null)
7476
$this->shown[] = $id;
7577
}
7678

79+
public function reload($id = null): void
80+
{
81+
$this->reloaded[] = $id;
82+
}
83+
7784
public function current(): Window
7885
{
7986
$this->ensureForceReturnWindowsProvided();
@@ -104,9 +111,9 @@ public function get(string $id): Window
104111
}
105112

106113
/**
107-
* @param string|Closure(string): bool $id
114+
* @param null|string|Closure(string): bool $id
108115
*/
109-
public function assertOpened(string|Closure $id): void
116+
public function assertOpened(null|string|Closure $id = null): void
110117
{
111118
if (is_callable($id) === false) {
112119
PHPUnit::assertContains($id, $this->opened);
@@ -125,9 +132,9 @@ public function assertOpened(string|Closure $id): void
125132
}
126133

127134
/**
128-
* @param string|Closure(string): bool $id
135+
* @param null|string|Closure(string): bool $id
129136
*/
130-
public function assertClosed(string|Closure $id): void
137+
public function assertClosed(null|string|Closure $id = null): void
131138
{
132139
if (is_callable($id) === false) {
133140
PHPUnit::assertContains($id, $this->closed);
@@ -146,9 +153,9 @@ public function assertClosed(string|Closure $id): void
146153
}
147154

148155
/**
149-
* @param string|Closure(string): bool $id
156+
* @param null|string|Closure(string): bool $id
150157
*/
151-
public function assertHidden(string|Closure $id): void
158+
public function assertHidden(null|string|Closure $id = null): void
152159
{
153160
if (is_callable($id) === false) {
154161
PHPUnit::assertContains($id, $this->hidden);
@@ -167,9 +174,9 @@ public function assertHidden(string|Closure $id): void
167174
}
168175

169176
/**
170-
* @param string|Closure(string): bool $id
177+
* @param null|string|Closure(string): bool $id
171178
*/
172-
public function assertShown(string|Closure $id): void
179+
public function assertShown(null|string|Closure $id = null): void
173180
{
174181
if (is_callable($id) === false) {
175182
PHPUnit::assertContains($id, $this->shown);
@@ -187,6 +194,48 @@ public function assertShown(string|Closure $id): void
187194
PHPUnit::assertTrue($hit);
188195
}
189196

197+
/**
198+
* @param null|string|Closure(string): bool $id
199+
*/
200+
public function assertReloaded(null|string|Closure $id = null): void
201+
{
202+
if (is_callable($id) === false) {
203+
PHPUnit::assertContains($id, $this->reloaded);
204+
205+
return;
206+
}
207+
208+
$hit = empty(
209+
array_filter(
210+
$this->reloaded,
211+
fn (mixed $reloadedId) => $id($reloadedId) === true
212+
)
213+
) === false;
214+
215+
PHPUnit::assertTrue($hit);
216+
}
217+
218+
/**
219+
* @param null|string|Closure(string): bool $id
220+
*/
221+
public function assertNotReloaded(null|string|Closure $id = null): void
222+
{
223+
if (is_callable($id) === false) {
224+
PHPUnit::assertNotContains($id, $this->reloaded);
225+
226+
return;
227+
}
228+
229+
$hit = empty(
230+
array_filter(
231+
$this->reloaded,
232+
fn (mixed $reloadedId) => $id($reloadedId) === true
233+
)
234+
) === true;
235+
236+
PHPUnit::assertTrue($hit);
237+
}
238+
190239
public function assertOpenedCount(int $expected): void
191240
{
192241
PHPUnit::assertCount($expected, $this->opened);

tests/Fakes/FakeWindowManagerTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,41 @@
154154
$this->fail('Expected assertion to fail');
155155
});
156156

157+
it('asserts that a window was reloaded', function () {
158+
swap(WindowManagerContract::class, $fake = app(WindowManagerFake::class));
159+
160+
app(WindowManagerContract::class)->reload('main');
161+
app(WindowManagerContract::class)->reload('secondary');
162+
163+
$fake->assertReloaded('main');
164+
$fake->assertReloaded('secondary');
165+
166+
try {
167+
$fake->assertReloaded('tertiary');
168+
} catch (AssertionFailedError) {
169+
return;
170+
}
171+
172+
$this->fail('Expected assertion to fail');
173+
});
174+
175+
it('asserts that a window was\'t reloaded', function () {
176+
swap(WindowManagerContract::class, $fake = app(WindowManagerFake::class));
177+
178+
app(WindowManagerContract::class)->reload('main');
179+
app(WindowManagerContract::class)->reload('secondary');
180+
181+
$fake->assertNotReloaded('tertiary');
182+
183+
try {
184+
$fake->assertNotReloaded('main');
185+
} catch (AssertionFailedError) {
186+
return;
187+
}
188+
189+
$this->fail('Expected assertion to fail');
190+
});
191+
157192
it('asserts opened count', function () {
158193
Http::fake(['*' => Http::response(status: 200)]);
159194

0 commit comments

Comments
 (0)