1414use Sammyjo20 \Saloon \Tests \Fixtures \Connectors \QueryParameterConnector ;
1515use Sammyjo20 \Saloon \Tests \Fixtures \Requests \DifferentServiceUserRequest ;
1616use Sammyjo20 \Saloon \Tests \Fixtures \Requests \QueryParameterConnectorRequest ;
17- use function PHPUnit \Framework \assertFileDoesNotExist ;
18- use function PHPUnit \Framework \assertFileExists ;
1917
2018$ filesystem = new Filesystem (new LocalFilesystemAdapter ('tests/Fixtures/Saloon ' ));
2119
@@ -235,7 +233,72 @@ function (SaloonRequest $request): MockResponse {
235233 expect ($ sequenceResponse ->json ())->toEqual (['request_class ' => UserRequest::class]);
236234});
237235
238- test ('you can use a fixture mock response and it will record the request for future use ' , function () use ($ filesystem ) {
236+ test ('a fixture can be used with a mock sequence ' , function () {
237+ $ mockClient = new MockClient ([
238+ MockResponse::fixture ('user ' ),
239+ MockResponse::fixture ('user ' ),
240+ ]);
241+
242+ $ responseA = UserRequest::make ()->send ($ mockClient );
243+
244+ expect ($ responseA ->isMocked ())->toBeFalse ();
245+ expect ($ responseA ->status ())->toEqual (200 );
246+ expect ($ responseA ->json ())->toEqual ([
247+ 'name ' => 'Sammyjo20 ' ,
248+ 'actual_name ' => 'Sam ' ,
249+ 'twitter ' => '@carre_sam ' ,
250+ ]);
251+
252+ $ responseB = UserRequest::make ()->send ($ mockClient );
253+
254+ expect ($ responseB ->isMocked ())->toBeTrue ();
255+ expect ($ responseB ->status ())->toEqual (200 );
256+ expect ($ responseB ->json ())->toEqual ([
257+ 'name ' => 'Sammyjo20 ' ,
258+ 'actual_name ' => 'Sam ' ,
259+ 'twitter ' => '@carre_sam ' ,
260+ ]);
261+ });
262+
263+ test ('a fixture can be used with a connector mock ' , function () {
264+ $ mockClient = new MockClient ([
265+ TestConnector::class => MockResponse::fixture ('connector ' ),
266+ ]);
267+
268+ $ responseA = UserRequest::make ()->send ($ mockClient );
269+
270+ expect ($ responseA ->isMocked ())->toBeFalse ();
271+ expect ($ responseA ->status ())->toEqual (200 );
272+ expect ($ responseA ->json ())->toEqual ([
273+ 'name ' => 'Sammyjo20 ' ,
274+ 'actual_name ' => 'Sam ' ,
275+ 'twitter ' => '@carre_sam ' ,
276+ ]);
277+
278+ $ responseB = UserRequest::make ()->send ($ mockClient );
279+
280+ expect ($ responseB ->isMocked ())->toBeTrue ();
281+ expect ($ responseB ->status ())->toEqual (200 );
282+ expect ($ responseB ->json ())->toEqual ([
283+ 'name ' => 'Sammyjo20 ' ,
284+ 'actual_name ' => 'Sam ' ,
285+ 'twitter ' => '@carre_sam ' ,
286+ ]);
287+
288+ // Even though it's a different request, it should use the same fixture
289+
290+ $ responseC = ErrorRequest::make ()->send ($ mockClient );
291+
292+ expect ($ responseC ->isMocked ())->toBeTrue ();
293+ expect ($ responseC ->status ())->toEqual (200 );
294+ expect ($ responseC ->json ())->toEqual ([
295+ 'name ' => 'Sammyjo20 ' ,
296+ 'actual_name ' => 'Sam ' ,
297+ 'twitter ' => '@carre_sam ' ,
298+ ]);
299+ });
300+
301+ test ('a fixture can be used with a request mock ' , function () use ($ filesystem ) {
239302 $ mockClient = new MockClient ([
240303 UserRequest::class => MockResponse::fixture ('user ' ),
241304 ]);
@@ -246,13 +309,158 @@ function (SaloonRequest $request): MockResponse {
246309
247310 expect ($ responseA ->isMocked ())->toBeFalse ();
248311 expect ($ responseA ->status ())->toEqual (200 );
312+ expect ($ responseA ->json ())->toEqual ([
313+ 'name ' => 'Sammyjo20 ' ,
314+ 'actual_name ' => 'Sam ' ,
315+ 'twitter ' => '@carre_sam ' ,
316+ ]);
249317
250318 expect ($ filesystem ->fileExists ('user.json ' ))->toBeTrue ();
251319
252320 $ responseB = UserRequest::make ()->send ($ mockClient );
253321
254322 expect ($ responseB ->isMocked ())->toBeTrue ();
255323 expect ($ responseB ->status ())->toEqual (200 );
324+ expect ($ responseB ->json ())->toEqual ([
325+ 'name ' => 'Sammyjo20 ' ,
326+ 'actual_name ' => 'Sam ' ,
327+ 'twitter ' => '@carre_sam ' ,
328+ ]);
256329});
257330
258- // Tests: Make sure the fixture works with all the mocking types including closures
331+ test ('a fixture can be used with a url mock ' , function () use ($ filesystem ) {
332+ $ mockClient = new MockClient ([
333+ 'tests.saloon.dev/api/user ' => MockResponse::fixture ('user ' ), // Test Exact Route
334+ 'tests.saloon.dev/* ' => MockResponse::fixture ('other ' ), // Test Wildcard Routes
335+ ]);
336+
337+ expect ($ filesystem ->fileExists ('user.json ' ))->toBeFalse ();
338+ expect ($ filesystem ->fileExists ('other.json ' ))->toBeFalse ();
339+
340+ $ responseA = UserRequest::make ()->send ($ mockClient );
341+
342+ expect ($ filesystem ->fileExists ('user.json ' ))->toBeTrue ();
343+ expect ($ filesystem ->fileExists ('other.json ' ))->toBeFalse ();
344+
345+ expect ($ responseA ->isMocked ())->toBeFalse ();
346+ expect ($ responseA ->status ())->toEqual (200 );
347+ expect ($ responseA ->json ())->toEqual ([
348+ 'name ' => 'Sammyjo20 ' ,
349+ 'actual_name ' => 'Sam ' ,
350+ 'twitter ' => '@carre_sam ' ,
351+ ]);
352+
353+ $ responseB = ErrorRequest::make ()->send ($ mockClient );
354+
355+ expect ($ filesystem ->fileExists ('user.json ' ))->toBeTrue ();
356+ expect ($ filesystem ->fileExists ('other.json ' ))->toBeTrue ();
357+
358+ expect ($ responseB ->isMocked ())->toBeFalse ();
359+ expect ($ responseB ->status ())->toEqual (500 );
360+ expect ($ responseB ->json ())->toEqual ([
361+ 'message ' => 'Fake Error ' ,
362+ ]);
363+
364+ // This should use the first mock
365+
366+ $ responseC = UserRequest::make ()->send ($ mockClient );
367+
368+ expect ($ responseC ->isMocked ())->toBeTrue ();
369+ expect ($ responseC ->status ())->toEqual (200 );
370+ expect ($ responseC ->json ())->toEqual ([
371+ 'name ' => 'Sammyjo20 ' ,
372+ 'actual_name ' => 'Sam ' ,
373+ 'twitter ' => '@carre_sam ' ,
374+ ]);
375+
376+ // Another error request should use the "other" mock
377+
378+ $ responseD = ErrorRequest::make ()->send ($ mockClient );
379+
380+ expect ($ responseD ->isMocked ())->toBeTrue ();
381+ expect ($ responseD ->status ())->toEqual (500 );
382+ expect ($ responseD ->json ())->toEqual ([
383+ 'message ' => 'Fake Error ' ,
384+ ]);
385+ });
386+
387+ test ('a fixture can be used with a wildcard url mock ' , function () {
388+ $ mockClient = new MockClient ([
389+ '* ' => MockResponse::fixture ('user ' ), // Test Exact Route
390+ ]);
391+
392+ $ responseA = UserRequest::make ()->send ($ mockClient );
393+
394+ expect ($ responseA ->isMocked ())->toBeFalse ();
395+ expect ($ responseA ->status ())->toEqual (200 );
396+ expect ($ responseA ->json ())->toEqual ([
397+ 'name ' => 'Sammyjo20 ' ,
398+ 'actual_name ' => 'Sam ' ,
399+ 'twitter ' => '@carre_sam ' ,
400+ ]);
401+
402+ $ responseB = ErrorRequest::make ()->send ($ mockClient );
403+
404+ expect ($ responseB ->isMocked ())->toBeTrue ();
405+ expect ($ responseB ->status ())->toEqual (200 );
406+ expect ($ responseB ->json ())->toEqual ([
407+ 'name ' => 'Sammyjo20 ' ,
408+ 'actual_name ' => 'Sam ' ,
409+ 'twitter ' => '@carre_sam ' ,
410+ ]);
411+ });
412+
413+ test ('a fixture can be used within a closure mock ' , function () use ($ filesystem ) {
414+ $ mockClient = new MockClient ([
415+ '* ' => function (SaloonRequest $ request ) {
416+ if ($ request instanceof UserRequest) {
417+ return MockResponse::fixture ('user ' );
418+ }
419+
420+ return MockResponse::fixture ('other ' );
421+ }
422+ ]);
423+
424+ expect ($ filesystem ->fileExists ('user.json ' ))->toBeFalse ();
425+ expect ($ filesystem ->fileExists ('other.json ' ))->toBeFalse ();
426+
427+ $ responseA = UserRequest::make ()->send ($ mockClient );
428+
429+ expect ($ responseA ->isMocked ())->toBeFalse ();
430+ expect ($ responseA ->status ())->toEqual (200 );
431+ expect ($ responseA ->json ())->toEqual ([
432+ 'name ' => 'Sammyjo20 ' ,
433+ 'actual_name ' => 'Sam ' ,
434+ 'twitter ' => '@carre_sam ' ,
435+ ]);
436+
437+ $ responseB = UserRequest::make ()->send ($ mockClient );
438+
439+ expect ($ responseB ->isMocked ())->toBeTrue ();
440+ expect ($ responseB ->status ())->toEqual (200 );
441+ expect ($ responseB ->json ())->toEqual ([
442+ 'name ' => 'Sammyjo20 ' ,
443+ 'actual_name ' => 'Sam ' ,
444+ 'twitter ' => '@carre_sam ' ,
445+ ]);
446+
447+ // Now we'll test a different route
448+
449+ $ responseC = ErrorRequest::make ()->send ($ mockClient );
450+
451+ expect ($ responseC ->isMocked ())->toBeFalse ();
452+ expect ($ responseC ->status ())->toEqual (500 );
453+ expect ($ responseC ->json ())->toEqual ([
454+ 'message ' => 'Fake Error ' ,
455+ ]);
456+
457+ // Another error request should use the "other" mock
458+
459+ $ responseD = ErrorRequest::make ()->send ($ mockClient );
460+
461+ expect ($ responseD ->isMocked ())->toBeTrue ();
462+ expect ($ responseD ->status ())->toEqual (500 );
463+ expect ($ responseD ->json ())->toEqual ([
464+ 'message ' => 'Fake Error ' ,
465+ ]);
466+ });
0 commit comments