Skip to content

Commit

Permalink
Test async requests
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Feb 13, 2024
1 parent de83c3b commit 06daceb
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion tests/Feature/RequestsOptimizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
->middleware('log', Middleware::tap(fn() => $log->push('before'), fn() => $log->push('after')))
->onRequest(fn() => $log->push('onRequest'))
->onResponse(fn() => $log->push('onResponse'))
->sync()
->totalPages('meta.total_pages')
->collect('data.*');

Expand Down Expand Up @@ -49,3 +48,22 @@

expect($log)->sequence('before', 'after', 'onError');
});

it('sends HTTP requests asynchronously', function () {
$log = collect();

$lazyCollection = LazyJsonPages::from('https://example.com/api/v1/users')
->onRequest(fn() => $log->push('sending'))
->onResponse(fn() => $log->push('sent'))
->async(3)
->totalPages('meta.total_pages')
->collect('data.*');

expect($lazyCollection)->toLoadItemsViaRequests([
'https://example.com/api/v1/users' => 'pagination/page1.json',
'https://example.com/api/v1/users?page=2' => 'pagination/page2.json',
'https://example.com/api/v1/users?page=3' => 'pagination/page3.json',
]);

expect($log)->sequence('sending', 'sent', 'sending', 'sending', 'sent', 'sent');
});

0 comments on commit 06daceb

Please sign in to comment.