22
33namespace Native \Laravel \Fakes ;
44
5+ use Closure ;
56use Illuminate \Support \Arr ;
7+ use Native \Laravel \Client \Client ;
68use Native \Laravel \Contracts \WindowManager as WindowManagerContract ;
79use Native \Laravel \Windows \Window ;
810use PHPUnit \Framework \Assert as PHPUnit ;
11+ use Webmozart \Assert \Assert ;
912
1013class 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