10000 Improvements to window test doubles (#426) · geekwolverine/laravel@a6dea1f · GitHub
[go: up one dir, main page]

Skip to content

Commit a6dea1f

Browse files
authored
Improvements to window test doubles (NativePHP#426)
* 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
1 parent 3133d14 commit a6dea1f

File tree

3 files changed

+287
-27
lines changed

3 files changed

+287
-27
lines changed

src/Facades/Window.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Window extends Facade
2222
{
2323
public static function fake()
2424
{
25-
return tap(new WindowManagerFake, function ($fake) {
25+
return tap(static::getFacadeApplication()->make(WindowManagerFake::class), function ($fake) {
2626
static::swap($fake);
2727
});
2828
}

src/Fakes/WindowManagerFake.php

Lines changed: 94 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Native\Laravel\Fakes;
44

5+
use Closure;
56
use Illuminate\Support\Arr;
7+
use Native\Laravel\Client\Client;
68
use Native\Laravel\Contracts\WindowManager as WindowManagerContract;
79
use Native\Laravel\Windows\Window;
810
use PHPUnit\Framework\Assert as PHPUnit;
11+
use Webmozart\Assert\Assert;
912

1013
class WindowManagerFake implements WindowManagerContract
1114
{
@@ -17,6 +20,10 @@ class WindowManagerFake implements WindowManagerContract
1720

1821
public array $forcedWindowReturnValues = [];
1922

23+
public function __construct(
24+
protected Client $client
25+
) {}
26+
2027
/**
2128
* @param array<int, Window> $windows
2229
*/
@@ -30,6 +37,21 @@ public function alwaysReturnWindows(array $windows): self
3037
public function open(string $id = 'main')
3138
{
3239
$this->opened[] = $id;
40+
41+
$this->ensureForceReturnWindowsProvided();
42+
43+
$matchingWindows = array_filter(
44+
$this->forcedWindowReturnValues,
45+
fn (Window $window) => $window->getId() === $id
46+
);
47+
48+
if (empty($matchingWindows)) {
49+
return $this->forcedWindowReturnValues[array_rand($this->forcedWindowReturnValues)]->setClient($this->client);
50+
}
51+
52+
Assert::count($matchingWindows, 1);
53+
54+
return Arr::first($matchingWindows)->setClient($this->client);
3355
}
3456

3557
public function close($id = null)
@@ -65,29 +87,92 @@ public function get(string $id): Window
6587

6688
$matchingWindows = array_filter($this->forcedWindowReturnValues, fn (Window $window) => $window->getId() === $id);
6789

68-
PHPUnit::assertNotEmpty($matchingWindows);
69-
PHPUnit::assertCount(1, $matchingWindows);
90+
Assert::notEmpty($matchingWindows);
91+
Assert::count($matchingWindows, 1);
7092

7193
return Arr::first($matchingWindows);
7294
}
7395

74-
public function assertOpened(string $id): void
96+
/**
97+
* @param string|Closure(string): bool $id
98+
*/
99+
public function assertOpened(string|Closure $id): void
100+
{
101+
if (is_callable($id) === false) {
102+
PHPUnit::assertContains($id, $this->opened);
103+
104+
return;
105+
}
106+
107+
$hit = empty(
108+
array_filter(
109+
$this->opened,
110+
fn (string $openedId) => $id($openedId) === true
111+
)
112+
) === false;
113+
114+
PHPUnit::assertTrue($hit);
115+
}
116+
117+
/**
118+
* @param string|Closure(string): bool $id
119+
*/
120+
public function assertClosed(string|Closure $id): void
121+
{
122+
if (is_callable($id) === false) {
123+
PHPUnit::assertContains($id, $this->closed);
124+
125+
return;
126+
}
127+
128+
$hit = empty(
129+
array_filter(
130+
$this->closed,
131+
fn (mixed $closedId) => $id($closedId) === true
132+
)
133+
) === false;
134+
135+
PHPUnit::assertTrue($hit);
136+
}
137+
138+
/**
139+
* @param string|Closure(string): bool $id
140+
*/
141+
public function assertHidden(string|Closure $id): void
142+
{
143+
if (is_callable($id) === false) {
144+
PHPUnit::assertContains($id, $this->hidden);
145+
146+
return;
147+
}
148+
149+
$hit = empty(
150+
array_filter(
151+
$this->hidden,
152+
fn (mixed $hiddenId) => $id($hiddenId) === true
153+
)
154+
) === false;
155+
156+
PHPUnit::assertTrue($hit);
157+
}
158+
159+
public function assertOpenedCount(int $expected): void
75160
{
76-
PHPUnit::assertContains($id, $this->opened);
161+
PHPUnit::assertCount($expected, $this->opened);
77162
}
78163

79-
public function assertClosed(?string $id): void
164+
public function assertClosedCount(int $expected): void
80165
{
81-
PHPUnit::assertContains($id, $this->closed);
166+
PHPUnit::assertCount($expected, $this->closed);
82167
}
83168

84-
public function assertHidden(?string $id): void
169+
public function assertHiddenCount(int $expected): void
85170
{
86-
PHPUnit::assertContains($id, $this->hidden);
171+
PHPUnit::assertCount($expected, $this->hidden);
87172
}
88173

89174
private function ensureForceReturnWindowsProvided(): void
90175
{
91-
PHPUnit::assertNotEmpty($this->forcedWindowReturnValues);
176+
Assert::notEmpty($this->forcedWindowReturnValues, 'No windows were provided to return');
92177
}
93178
}

0 commit comments

Comments
 (0)
0