2
2
3
3
namespace Native \Laravel \Fakes ;
4
4
5
+ use Closure ;
5
6
use Illuminate \Support \Arr ;
7
+ use Native \Laravel \Client \Client ;
6
8
use Native \Laravel \Contracts \WindowManager as WindowManagerContract ;
7
9
use Native \Laravel \Windows \Window ;
8
10
use PHPUnit \Framework \Assert as PHPUnit ;
11
+ use Webmozart \Assert \Assert ;
9
12
10
13
class WindowManagerFake implements WindowManagerContract
11
14
{
@@ -17,6 +20,10 @@ class WindowManagerFake implements WindowManagerContract
17
20
18
21
public array $ forcedWindowReturnValues = [];
19
22
23
+ public function __construct (
24
+ protected Client $ client
25
+ ) {}
26
+
20
27
/**
21
28
* @param array<int, Window> $windows
22
29
*/
@@ -30,6 +37,21 @@ public function alwaysReturnWindows(array $windows): self
30
37
public function open (string $ id = 'main ' )
31
38
{
32
39
$ 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 );
33
55
}
34
56
35
57
public function close ($ id = null )
@@ -65,29 +87,92 @@ public function get(string $id): Window
65
87
66
88
$ matchingWindows = array_filter ($ this ->forcedWindowReturnValues , fn (Window $ window ) => $ window ->getId () === $ id );
67
89
68
- PHPUnit:: assertNotEmpty ($ matchingWindows );
69
- PHPUnit:: assertCount ( 1 , $ matchingWindows );
90
+ Assert:: notEmpty ($ matchingWindows );
91
+ Assert:: count ( $ matchingWindows, 1 );
70
92
71
93
return Arr::first ($ matchingWindows );
72
94
}
73
95
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
75
160
{
76
- PHPUnit::assertContains ( $ id , $ this ->opened );
161
+ PHPUnit::assertCount ( $ expected , $ this ->opened );
77
162
}
78
163
79
- public function assertClosed (? string $ id ): void
164
+ public function assertClosedCount ( int $ expected ): void
80
165
{
81
- PHPUnit::assertContains ( $ id , $ this ->closed );
166
+ PHPUnit::assertCount ( $ expected , $ this ->closed );
82
167
}
83
168
84
- public function assertHidden (? string $ id ): void
169
+ public function assertHiddenCount ( int $ expected ): void
85
170
{
86
- PHPUnit::assertContains ( $ id , $ this ->hidden );
171
+ PHPUnit::assertCount ( $ expected , $ this ->hidden );
87
172
}
88
173
89
174
private function ensureForceReturnWindowsProvided (): void
90
175
{
91
- PHPUnit:: assertNotEmpty ($ this ->forcedWindowReturnValues );
176
+ Assert:: notEmpty ($ this ->forcedWindowReturnValues , ' No windows were provided to return ' );
92
177
}
93
178
}
0 commit comments