22
33namespace DirectoryTree \ImapEngine \Connection ;
44
5+ use DirectoryTree \ImapEngine \Collections \ResponseCollection ;
6+ use DirectoryTree \ImapEngine \Connection \Responses \TaggedResponse ;
57use DirectoryTree \ImapEngine \Connection \Responses \UntaggedResponse ;
68use RuntimeException ;
79
8- /**
9- * Example usage:
10- *
11- * $fake = new AssertableFakeConnection;
12- * $fake->expect('connect', ['imap.test.dev', 993], Response::make());
13- * $fake->connect('imap.test.dev', 993);
14- * $fake->assertCalled('connect'); // passes
15- * $fake->assertCalledTimes('connect', 1); // passes
16- */
1710class FakeConnection extends Connection
1811{
1912 /**
@@ -30,40 +23,6 @@ class FakeConnection extends Connection
3023 */
3124 protected array $ expectations = [];
3225
33- /**
34- * Attempt to get the pre-configured response for the given method and arguments.
35- */
36- protected function getExpectationResponse (string $ method , mixed $ args ): mixed
37- {
38- if (
39- ! isset ($ this ->expectations [$ method ])) {
40- throw new RuntimeException ("No expectations set for method [ $ method]. " );
41- }
42-
43- foreach ($ this ->expectations [$ method ] as $ index => $ expectation ) {
44- if ($ this ->matches ($ expectation ['matcher ' ], $ args )) {
45- unset($ this ->expectations [$ method ][$ index ]);
46-
47- return $ expectation ['response ' ];
48- }
49- }
50-
51- return null ;
52- }
53-
54- /**
55- * Determine if the given matcher matches the given arguments.
56- */
57- protected function matches (callable |array $ matcher , array $ args ): bool
58- {
59- if (is_array ($ matcher )) {
60- return $ matcher == $ args ;
61- }
62-
63- // Otherwise, assume it's a callable:
64- return (bool ) call_user_func ($ matcher , $ args );
65- }
66-
6726 /**
6827 * Register an expectation for a given method.
6928 */
@@ -88,201 +47,193 @@ public function connect(string $host, ?int $port = null): void
8847 /**
8948 * {@inheritDoc}
9049 */
91- public function login (string $ user , string $ password ): ResponseCollection
50+ public function login (string $ user , string $ password ): TaggedResponse
9251 {
93- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
52+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
9453 }
9554
9655 /**
9756 * {@inheritDoc}
9857 */
99- public function authenticate (string $ user , string $ token ): ResponseCollection
58+ public function authenticate (string $ user , string $ token ): TaggedResponse
10059 {
101- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
60+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
10261 }
10362
10463 /**
10564 * {@inheritDoc}
10665 */
107- public function logout (): ResponseCollection
66+ public function logout (): TaggedResponse
10867 {
109- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
68+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
11069 }
11170
11271 /**
11372 * {@inheritDoc}
11473 */
11574 public function capability (): UntaggedResponse
11675 {
117- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
76+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
11877 }
11978
12079 /**
12180 * {@inheritDoc}
12281 */
12382 public function select (string $ folder = 'INBOX ' ): ResponseCollection
12483 {
125- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
84+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
12685 }
12786
12887 /**
12988 * {@inheritDoc}
13089 */
13190 public function examine (string $ folder = 'INBOX ' ): ResponseCollection
13291 {
133- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
92+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
13493 }
13594
13695 /**
13796 * {@inheritDoc}
13897 */
139- public function status (string $ folder = 'INBOX ' , array $ arguments = ['MESSAGES ' , 'UNSEEN ' , 'RECENT ' , 'UIDNEXT ' , 'UIDVALIDITY ' ]): ResponseCollection
98+ public function status (string $ folder = 'INBOX ' , array $ arguments = ['MESSAGES ' , 'UNSEEN ' , 'RECENT ' , 'UIDNEXT ' , 'UIDVALIDITY ' ]): UntaggedResponse
14099 {
141- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
100+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
142101 }
143102
144103 /**
145104 * {@inheritDoc}
146105 */
147106 public function uid (int |array $ msgns ): ResponseCollection
148107 {
149- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
108+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
150109 }
151110
152111 /**
153112 * {@inheritDoc}
154113 */
155- public function text (array |int $ ids ): ResponseCollection
114+ public function text (array |int $ ids, bool $ peek = true ): ResponseCollection
156115 {
157- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
116+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
158117 }
159118
160119 /**
161120 * {@inheritDoc}
162121 */
163- public function header (array |int $ ids ): ResponseCollection
122+ public function header (array |int $ ids, bool $ peek = true ): ResponseCollection
164123 {
165- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
124+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
166125 }
167126
168127 /**
169128 * {@inheritDoc}
170129 */
171130 public function flags (array |int $ ids ): ResponseCollection
172131 {
173- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
132+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
174133 }
175134
176135 /**
177136 * {@inheritDoc}
178137 */
179138 public function sizes (array |int $ ids ): ResponseCollection
180139 {
181- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
140+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
182141 }
183142
184143 /**
185144 * {@inheritDoc}
186145 */
187146 public function list (string $ reference = '' , string $ folder = '* ' ): ResponseCollection
188147 {
189- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
148+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
190149 }
191150
192151 /**
193152 * {@inheritDoc}
194153 */
195154 public function store (array |string $ flags , int $ from , ?int $ to = null , ?string $ mode = null , bool $ silent = true , ?string $ item = null ): ResponseCollection
196155 {
197- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
156+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
198157 }
199158
200159 /**
201160 * {@inheritDoc}
202161 */
203162 public function append (string $ folder , string $ message , ?array $ flags = null , ?string $ date = null ): ResponseCollection
204163 {
205- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection::make ();
206- }
207-
208- /**
209- * {@inheritDoc}
210- */
211- public function copy (string $ folder , $ from , ?int $ to = null ): ResponseCollection
212- {
213- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection::make ();
164+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
214165 }
215166
216167 /**
217168 * {@inheritDoc}
218169 */
219- public function copyManyMessages ( array $ messages , string $ folder ): ResponseCollection
170+ public function copy ( string $ folder , $ from , ? int $ to = null ): void
220171 {
221- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make ( );
172+ $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
222173 }
223174
224175 /**
225176 * {@inheritDoc}
226177 */
227- public function move (string $ folder , $ from , ?int $ to = null ): ResponseCollection
178+ public function move (string $ folder , $ from , ?int $ to = null ): void
228179 {
229- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make ( );
180+ $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
230181 }
231182
232183 /**
233184 * {@inheritDoc}
234185 */
235186 public function moveManyMessages (array $ messages , string $ folder ): ResponseCollection
236187 {
237- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
188+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
238189 }
239190
240191 /**
241192 * {@inheritDoc}
242193 */
243- public function id (?array $ ids = null ): ResponseCollection
194+ public function id (?array $ ids = null ): UntaggedResponse
244195 {
245- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
196+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
246197 }
247198
248199 /**
249200 * {@inheritDoc}
250201 */
251202 public function create (string $ folder ): ResponseCollection
252203 {
253- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
204+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
254205 }
255206
256207 /**
257208 * {@inheritDoc}
258209 */
259- public function rename (string $ oldPath , string $ newPath ): ResponseCollection
210+ public function rename (string $ oldPath , string $ newPath ): TaggedResponse
260211 {
261- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
212+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
262213 }
263214
264215 /**
265216 * {@inheritDoc}
266217 */
267- public function delete (string $ folder ): ResponseCollection
218+ public function delete (string $ folder ): TaggedResponse
268219 {
269- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
220+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
270221 }
271222
272223 /**
273224 * {@inheritDoc}
274225 */
275- public function subscribe (string $ folder ): ResponseCollection
226+ public function subscribe (string $ folder ): TaggedResponse
276227 {
277- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
228+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
278229 }
279230
280231 /**
281232 * {@inheritDoc}
282233 */
283- public function unsubscribe (string $ folder ): ResponseCollection
234+ public function unsubscribe (string $ folder ): TaggedResponse
284235 {
285- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
236+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
286237 }
287238
288239 /**
@@ -306,22 +257,56 @@ public function done(): void
306257 */
307258 public function expunge (): ResponseCollection
308259 {
309- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
260+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
310261 }
311262
312263 /**
313264 * {@inheritDoc}
314265 */
315- public function noop (): ResponseCollection
266+ public function noop (): TaggedResponse
316267 {
317- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection:: make () ;
268+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
318269 }
319270
320271 /**
321272 * {@inheritDoc}
322273 */
323- public function search (array $ params ): ResponseCollection
274+ public function search (array $ params ): UntaggedResponse
275+ {
276+ return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ());
277+ }
278+
279+ /**
280+ * Attempt to get the pre-configured response for the given method and arguments.
281+ */
282+ protected function getExpectationResponse (string $ method , mixed $ args ): mixed
283+ {
284+ if (
285+ ! isset ($ this ->expectations [$ method ])) {
286+ throw new RuntimeException ("No expectations set for method [ $ method]. " );
287+ }
288+
289+ foreach ($ this ->expectations [$ method ] as $ index => $ expectation ) {
290+ if ($ this ->matches ($ expectation ['matcher ' ], $ args )) {
291+ unset($ this ->expectations [$ method ][$ index ]);
292+
293+ return $ expectation ['response ' ];
294+ }
295+ }
296+
297+ return null ;
298+ }
299+
300+ /**
301+ * Determine if the given matcher matches the given arguments.
302+ */
303+ protected function matches (callable |array $ matcher , array $ args ): bool
324304 {
325- return $ this ->getExpectationResponse (__FUNCTION__ , func_get_args ()) ?? ResponseCollection::make ();
305+ if (is_array ($ matcher )) {
306+ return $ matcher == $ args ;
307+ }
308+
309+ // Otherwise, assume it's a callable:
310+ return (bool ) call_user_func ($ matcher , $ args );
326311 }
327312}
0 commit comments