44
55use Composer \InstalledVersions ;
66use Composer \Semver \VersionParser ;
7+ use DateInterval ;
78use Illuminate \Cache \ArrayStore ;
9+ use Illuminate \Cache \Events \KeyWritten ;
810use Illuminate \Cache \Repository ;
11+ use Illuminate \Support \Facades \Event ;
912use Maatwebsite \Excel \Cache \BatchCache ;
1013use Maatwebsite \Excel \Cache \BatchCacheDeprecated ;
1114use Maatwebsite \Excel \Cache \CacheManager ;
@@ -176,6 +179,82 @@ public function it_persists_to_cache_when_memory_limit_reached_on_setting_multip
176179 ], $ cache ->getMultiple (['A1 ' , 'A2 ' , 'A3 ' , 'A4 ' , 'A5 ' ]));
177180 }
178181
182+ /**
183+ * @test
184+ *
185+ * @dataProvider defaultTTLDataProvider
186+ */
187+ public function it_writes_to_cache_with_default_ttl ($ defaultTTL , $ receivedAs )
188+ {
189+ config ()->set ('excel.cache.default_ttl ' , $ defaultTTL );
190+
191+ $ cache = $ this ->givenCache (['A1 ' => 'A1-value ' ], [], 1 );
192+ $ this ->cache ->setEventDispatcher (Event::fake ());
193+ $ cache ->set ('A2 ' , 'A2-value ' );
194+
195+ $ expectedTTL = value ($ receivedAs );
196+
197+ $ dispatchedCollection = Event::dispatched (
198+ KeyWritten::class,
199+ function (KeyWritten $ event ) use ($ expectedTTL ) {
200+ return $ event ->seconds === $ expectedTTL ;
201+ });
202+
203+ $ this ->assertCount (2 , $ dispatchedCollection );
204+ }
205+
206+ /**
207+ * @test
208+ */
209+ public function it_writes_to_cache_with_a_dateinterval_ttl ()
210+ {
211+ // DateInterval is 1 minute
212+ config ()->set ('excel.cache.default_ttl ' , new DateInterval ('PT1M ' ));
213+
214+ $ cache = $ this ->givenCache (['A1 ' => 'A1-value ' ], [], 1 );
215+ $ this ->cache ->setEventDispatcher (Event::fake ());
216+ $ cache ->set ('A2 ' , 'A2-value ' );
217+
218+ $ dispatchedCollection = Event::dispatched (
219+ KeyWritten::class,
220+ function (KeyWritten $ event ) {
221+ return $ event ->seconds === 60 ;
222+ });
223+
224+ $ this ->assertCount (2 , $ dispatchedCollection );
225+ }
226+
227+ /**
228+ * @test
229+ */
230+ public function it_can_override_default_ttl ()
231+ {
232+ config ()->set ('excel.cache.default_ttl ' , 1 );
233+
234+ $ cache = $ this ->givenCache (['A1 ' => 'A1-value ' ], [], 1 );
235+ $ this ->cache ->setEventDispatcher (Event::fake ());
236+ $ cache ->set ('A2 ' , 'A2-value ' , null );
237+
238+ $ dispatchedCollection = Event::dispatched (
239+ KeyWritten::class,
240+ function (KeyWritten $ event ) {
241+ return $ event ->seconds === null ;
242+ });
243+
244+ $ this ->assertCount (2 , $ dispatchedCollection );
245+ }
246+
247+ public static function defaultTTLDataProvider (): array
248+ {
249+ return [
250+ 'null (forever) ' => [null , null ],
251+ 'int value ' => [$ value = rand (1 , 100 ), $ value ],
252+ 'callable ' => [$ closure = function () {
253+ return 199 ;
254+ }, $ closure ],
255+ ];
256+ }
257+
179258 /**
180259 * Construct a BatchCache with a in memory store
181260 * and an array cache, pretending to be a persistence store.
@@ -200,13 +279,15 @@ private function givenCache(array $memory = [], array $persisted = [], int $memo
200279 if (!InstalledVersions::satisfies (new VersionParser , 'psr/simple-cache ' , '^3.0 ' )) {
201280 return new BatchCacheDeprecated (
202281 $ this ->cache ,
203- $ this ->memory
282+ $ this ->memory ,
283+ config ('excel.cache.default_ttl ' )
204284 );
205285 }
206286
207287 return new BatchCache (
208288 $ this ->cache ,
209- $ this ->memory
289+ $ this ->memory ,
290+ config ('excel.cache.default_ttl ' )
210291 );
211292 }
212293}
0 commit comments