Skip to content

Commit fe81fa9

Browse files
Add support for Window::show() (#454)
1 parent e700c62 commit fe81fa9

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

src/Fakes/WindowManagerFake.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class WindowManagerFake implements WindowManagerContract
1818

1919
public array $hidden = [];
2020

21+
public array $shown = [];
22+
2123
public array $forcedWindowReturnValues = [];
2224

2325
public function __construct(
@@ -64,6 +66,11 @@ public function hide($id = null)
6466
$this->hidden[] = $id;
6567
}
6668

69+
public function show($id = null)
70+
{
71+
$this->shown[] = $id;
72+
}
73+
6774
public function current(): Window
6875
{
6976
$this->ensureForceReturnWindowsProvided();
@@ -156,6 +163,27 @@ public function assertHidden(string|Closure $id): void
156163
PHPUnit::assertTrue($hit);
157164
}
158165

166+
/**
167+
* @param string|Closure(string): bool $id
168+
*/
169+
public function assertShown(string|Closure $id): void
170+
{
171+
if (is_callable($id) === false) {
172+
PHPUnit::assertContains($id, $this->shown);
173+
174+
return;
175+
}
176+
177+
$hit = empty(
178+
array_filter(
179+
$this->shown,
180+
fn (mixed $shownId) => $id($shownId) === true
181+
)
182+
) === false;
183+
184+
PHPUnit::assertTrue($hit);
185+
}
186+
159187
public function assertOpenedCount(int $expected): void
160188
{
161189
PHPUnit::assertCount($expected, $this->opened);
@@ -171,6 +199,11 @@ public function assertHiddenCount(int $expected): void
171199
PHPUnit::assertCount($expected, $this->hidden);
172200
}
173201

202+
public function assertShownCount(int $expected): void
203+
{
204+
PHPUnit::assertCount($expected, $this->shown);
205+
}
206+
174207
private function ensureForceReturnWindowsProvided(): void
175208
{
176209
Assert::notEmpty($this->forcedWindowReturnValues, 'No windows were provided to return');

src/Windows/WindowManager.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ public function hide($id = null)
3131
]);
3232
}
3333

34+
public function show($id = null)
35+
{
36+
$this->client->post('window/show', [
37+
'id' => $id ?? $this->detectId(),
38+
]);
39+
}
40+
3441
public function current(): Window
3542
{
3643
$window = (object) $this->client->get('window/current')->json();

tests/Fakes/FakeWindowManagerTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@
136136
$this->fail('Expected assertion to fail');
137137
});
138138

139+
it('asserts that a window was shown', function () {
140+
swap(WindowManagerContract::class, $fake = app(WindowManagerFake::class));
141+
142+
app(WindowManagerContract::class)->show('main');
143+
app(WindowManagerContract::class)->show('secondary');
144+
145+
$fake->assertShown('main');
146+
$fake->assertShown('secondary');
147+
148+
try {
149+
$fake->assertShown('tertiary');
150+
} catch (AssertionFailedError) {
151+
return;
152+
}
153+
154+
$this->fail('Expected assertion to fail');
155+
});
156+
139157
it('asserts opened count', function () {
140158
Http::fake(['*' => Http::response(status: 200)]);
141159

@@ -196,6 +214,24 @@
196214
$this->fail('Expected assertion to fail');
197215
});
198216

217+
it('asserts shown count', function () {
218+
swap(WindowManagerContract::class, $fake = app(WindowManagerFake::class));
219+
220+
app(WindowManagerContract::class)->show('main');
221+
app(WindowManagerContract::class)->show();
222+
app(WindowManagerContract::class)->show();
223+
224+
$fake->assertShownCount(3);
225+
226+
try {
227+
$fake->assertShownCount(4);
228+
} catch (AssertionFailedError) {
229+
return;
230+
}
231+
232+
$this->fail('Expected assertion to fail');
233+
});
234+
199235
it('forces the return value of current window', function () {
200236
swap(WindowManagerContract::class, $fake = app(WindowManagerFake::class));
201237

0 commit comments

Comments
 (0)