44namespace Mpbarlow \LaravelQueueDebouncer \Tests ;
55
66
7- use Illuminate \Foundation \Bus \Dispatchable ;
7+ use Illuminate \Bus \PendingBatch ;
8+ use Illuminate \Foundation \Bus \PendingChain ;
89use Illuminate \Queue \CallQueuedClosure ;
910use Illuminate \Support \Facades \Bus ;
1011use Illuminate \Support \Facades \Cache ;
1112use Mpbarlow \LaravelQueueDebouncer \Debouncer ;
1213use Mpbarlow \LaravelQueueDebouncer \DispatcherFactory ;
1314use Mpbarlow \LaravelQueueDebouncer \Support \CacheKeyProvider ;
15+ use Mpbarlow \LaravelQueueDebouncer \Support \ParameterAwareCacheKeyProvider ;
1416use Mpbarlow \LaravelQueueDebouncer \Support \UniqueIdentifierProvider ;
17+ use Mpbarlow \LaravelQueueDebouncer \Tests \Support \CustomCacheKeyProvider ;
18+ use Mpbarlow \LaravelQueueDebouncer \Tests \Support \CustomUniqueIdentifierProvider ;
19+ use Mpbarlow \LaravelQueueDebouncer \Tests \Support \DummyJob ;
1520use ReflectionFunction ;
1621
22+ use function class_exists ;
1723
1824class DebounceTest extends TestCase
1925{
@@ -44,8 +50,10 @@ public function it_injects_the_configured_cache_key_provider_and_unique_identifi
4450 * @test
4551 * @dataProvider jobProvider
4652 */
47- public function it_debounces_jobs ($ job , $ expectedDispatch )
53+ public function it_debounces_jobs ($ provider )
4854 {
55+ [$ job , $ expectedDispatch , $ keyProvider ] = $ provider ();
56+
4957 // So this is pretty tricky to test. It's a job that dispatches another job, so when we're faking the bus we
5058 // have to get a bit creative.
5159 Bus::fake ();
@@ -57,13 +65,14 @@ public function it_debounces_jobs($job, $expectedDispatch)
5765 ->andReturn (1 , 2 , 3 );
5866 });
5967
60- $ key = $ this ->app ->make (CacheKeyProvider::class )->getKey ($ job );
68+ $ key = $ this ->app ->make ($ keyProvider )->getKey ($ job );
6169 $ debouncer = $ this ->app ->make (Debouncer::class);
6270
6371 // Then we call the debouncer three times, asserting that each time, it's setting the cache value correctly,
6472 // and dispatching a runner closure with the values we expect.
6573 foreach ([1 , 2 , 3 ] as $ i ) {
6674 $ debouncer ($ job , PHP_INT_MAX );
75+
6776 $ this ->assertEquals ($ i , Cache::get ($ key ));
6877
6978 Bus::assertDispatched (CallQueuedClosure::class, function ($ dispatch ) use ($ job , $ key , $ i ) {
@@ -101,19 +110,63 @@ public function it_debounces_jobs($job, $expectedDispatch)
101110
102111 public function jobProvider (): array
103112 {
104- return [
105- [new Job (), Job::class],
106- [function () {}, CallQueuedClosure::class]
113+ // Annoyingly we have to wrap each data set in a function as data providers run before the container is
114+ // initialised. Each provides the job to debounce, the class we ultimately expect to be dispatched, and the
115+ // cache key provider to use for the test.
116+
117+ // Closures and standard classes should work with both included cache key providers.
118+ // Chains and batches only work with the serialisation-based key provider.
119+ $ jobTypes = [
120+ [
121+ function () {
122+ return [
123+ function () {
124+ },
125+ CallQueuedClosure::class,
126+ CacheKeyProvider::class
127+ ];
128+ }
129+ ],
130+ [
131+ function () {
132+ return [
133+ function () {
134+ },
135+ CallQueuedClosure::class,
136+ ParameterAwareCacheKeyProvider::class
137+ ];
138+ }
139+ ],
140+ [
141+ function () {
142+ return [new DummyJob (), DummyJob::class, CacheKeyProvider::class];
143+ }
144+ ],
145+ [
146+ function () {
147+ return [new DummyJob (), DummyJob::class, ParameterAwareCacheKeyProvider::class];
148+ }
149+ ],
150+ [
151+ function () {
152+ return [
153+ DummyJob::withChain ([new DummyJob ()]),
154+ PendingChain::class,
155+ ParameterAwareCacheKeyProvider::class
156+ ];
157+ }
158+ ]
107159 ];
108- }
109- }
110160
111- class Job
112- {
113- use Dispatchable;
161+ // PendingBatch is only available in Laravel >= 8.0.
162+ if (class_exists ('\Illuminate\Bus\PendingBatch ' )) {
163+ $ jobTypes [] = [
164+ function () {
165+ return [Bus::batch ([new DummyJob ()]), PendingBatch::class, ParameterAwareCacheKeyProvider::class];
166+ }
167+ ];
168+ }
114169
115- public function handle ()
116- {
117- //
170+ return $ jobTypes ;
118171 }
119172}
0 commit comments