BatchCacheTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Cache;
  3. use Composer\InstalledVersions;
  4. use Composer\Semver\VersionParser;
  5. use DateInterval;
  6. use Illuminate\Cache\ArrayStore;
  7. use Illuminate\Cache\Events\KeyWritten;
  8. use Illuminate\Cache\Repository;
  9. use Illuminate\Support\Facades\Event;
  10. use Maatwebsite\Excel\Cache\BatchCache;
  11. use Maatwebsite\Excel\Cache\BatchCacheDeprecated;
  12. use Maatwebsite\Excel\Cache\CacheManager;
  13. use Maatwebsite\Excel\Cache\MemoryCache;
  14. use Maatwebsite\Excel\Tests\TestCase;
  15. use Psr\SimpleCache\CacheInterface;
  16. class BatchCacheTest extends TestCase
  17. {
  18. /**
  19. * @var Repository
  20. */
  21. private $cache;
  22. /**
  23. * @var MemoryCache
  24. */
  25. private $memory;
  26. /**
  27. * @test
  28. */
  29. public function will_get_multiple_from_memory_if_cells_hold_in_memory()
  30. {
  31. $inMemory = [
  32. 'A1' => 'A1-value',
  33. 'A2' => 'A2-value',
  34. 'A3' => 'A3-value',
  35. ];
  36. $cache = $this->givenCache($inMemory);
  37. $this->assertEquals(
  38. $inMemory,
  39. $cache->getMultiple(['A1', 'A2', 'A3'])
  40. );
  41. $this->assertEquals('A3-value', $cache->get('A3'));
  42. }
  43. /**
  44. * @test
  45. */
  46. public function will_get_multiple_from_cache_if_cells_are_persisted()
  47. {
  48. $inMemory = [];
  49. $persisted = [
  50. 'A1' => 'A1-value',
  51. 'A2' => 'A2-value',
  52. 'A3' => 'A3-value',
  53. ];
  54. $cache = $this->givenCache($inMemory, $persisted);
  55. $this->assertEquals(
  56. $persisted,
  57. $cache->getMultiple(['A1', 'A2', 'A3'])
  58. );
  59. $this->assertEquals('A3-value', $cache->get('A3'));
  60. }
  61. /**
  62. * @test
  63. */
  64. public function will_get_multiple_from_cache_and_persisted()
  65. {
  66. $inMemory = [
  67. 'A1' => 'A1-value',
  68. 'A2' => 'A2-value',
  69. 'A3' => 'A3-value',
  70. ];
  71. $persisted = [
  72. 'A4' => 'A4-value',
  73. 'A5' => 'A5-value',
  74. 'A6' => 'A6-value',
  75. ];
  76. $cache = $this->givenCache($inMemory, $persisted);
  77. $this->assertEquals(
  78. array_merge($inMemory, $persisted),
  79. $cache->getMultiple(['A1', 'A2', 'A3', 'A4', 'A5', 'A6'])
  80. );
  81. $this->assertEquals('A3-value', $cache->get('A3'));
  82. $this->assertEquals('A6-value', $cache->get('A6'));
  83. }
  84. /**
  85. * @test
  86. */
  87. public function it_persists_to_cache_when_memory_limit_reached_on_setting_a_value()
  88. {
  89. $memoryLimit = 3;
  90. $persisted = [];
  91. $inMemory = [
  92. 'A1' => 'A1-value',
  93. 'A2' => 'A2-value',
  94. 'A3' => 'A3-value',
  95. ];
  96. $cache = $this->givenCache($inMemory, $persisted, $memoryLimit);
  97. // Setting a 4th value will reach the memory limit
  98. $cache->set('A4', 'A4-value', 10000);
  99. // Nothing in memory anymore
  100. $this->assertEquals([], array_filter($this->memory->getMultiple(['A1', 'A2', 'A3', 'A4'])));
  101. // All 4 cells show be persisted
  102. $this->assertEquals([
  103. 'A1' => 'A1-value',
  104. 'A2' => 'A2-value',
  105. 'A3' => 'A3-value',
  106. 'A4' => 'A4-value',
  107. ], $this->cache->getMultiple(['A1', 'A2', 'A3', 'A4']));
  108. // Batch cache should return all 4 cells
  109. $this->assertEquals([
  110. 'A1' => 'A1-value',
  111. 'A2' => 'A2-value',
  112. 'A3' => 'A3-value',
  113. 'A4' => 'A4-value',
  114. ], $cache->getMultiple(['A1', 'A2', 'A3', 'A4']));
  115. }
  116. /**
  117. * @test
  118. */
  119. public function it_persists_to_cache_when_memory_limit_reached_on_setting_multiple_values()
  120. {
  121. $memoryLimit = 3;
  122. $persisted = [];
  123. $inMemory = [
  124. 'A1' => 'A1-value',
  125. 'A2' => 'A2-value',
  126. 'A3' => 'A3-value',
  127. ];
  128. $cache = $this->givenCache($inMemory, $persisted, $memoryLimit);
  129. // Setting a 4th value will reach the memory limit
  130. $cache->setMultiple([
  131. 'A4' => 'A4-value',
  132. 'A5' => 'A5-value',
  133. ], 10000);
  134. // Nothing in memory anymore
  135. $this->assertEquals([], array_filter($this->memory->getMultiple(['A1', 'A2', 'A3', 'A4', 'A5'])));
  136. // All 4 cells show be persisted
  137. $this->assertEquals([
  138. 'A1' => 'A1-value',
  139. 'A2' => 'A2-value',
  140. 'A3' => 'A3-value',
  141. 'A4' => 'A4-value',
  142. 'A5' => 'A5-value',
  143. ], $this->cache->getMultiple(['A1', 'A2', 'A3', 'A4', 'A5']));
  144. // Batch cache should return all 4 cells
  145. $this->assertEquals([
  146. 'A1' => 'A1-value',
  147. 'A2' => 'A2-value',
  148. 'A3' => 'A3-value',
  149. 'A4' => 'A4-value',
  150. 'A5' => 'A5-value',
  151. ], $cache->getMultiple(['A1', 'A2', 'A3', 'A4', 'A5']));
  152. }
  153. /**
  154. * @test
  155. *
  156. * @dataProvider defaultTTLDataProvider
  157. */
  158. public function it_writes_to_cache_with_default_ttl($defaultTTL, $receivedAs)
  159. {
  160. config()->set('excel.cache.default_ttl', $defaultTTL);
  161. $cache = $this->givenCache(['A1' => 'A1-value'], [], 1);
  162. $this->cache->setEventDispatcher(Event::fake());
  163. $cache->set('A2', 'A2-value');
  164. $expectedTTL = value($receivedAs);
  165. $dispatchedCollection = Event::dispatched(
  166. KeyWritten::class,
  167. function (KeyWritten $event) use ($expectedTTL) {
  168. return $event->seconds === $expectedTTL;
  169. });
  170. $this->assertCount(2, $dispatchedCollection);
  171. }
  172. /**
  173. * @test
  174. */
  175. public function it_writes_to_cache_with_a_dateinterval_ttl()
  176. {
  177. // DateInterval is 1 minute
  178. config()->set('excel.cache.default_ttl', new DateInterval('PT1M'));
  179. $cache = $this->givenCache(['A1' => 'A1-value'], [], 1);
  180. $this->cache->setEventDispatcher(Event::fake());
  181. $cache->set('A2', 'A2-value');
  182. $dispatchedCollection = Event::dispatched(
  183. KeyWritten::class,
  184. function (KeyWritten $event) {
  185. return $event->seconds === 60;
  186. });
  187. $this->assertCount(2, $dispatchedCollection);
  188. }
  189. /**
  190. * @test
  191. */
  192. public function it_can_override_default_ttl()
  193. {
  194. config()->set('excel.cache.default_ttl', 1);
  195. $cache = $this->givenCache(['A1' => 'A1-value'], [], 1);
  196. $this->cache->setEventDispatcher(Event::fake());
  197. $cache->set('A2', 'A2-value', null);
  198. $dispatchedCollection = Event::dispatched(
  199. KeyWritten::class,
  200. function (KeyWritten $event) {
  201. return $event->seconds === null;
  202. });
  203. $this->assertCount(2, $dispatchedCollection);
  204. }
  205. public static function defaultTTLDataProvider(): array
  206. {
  207. return [
  208. 'null (forever)' => [null, null],
  209. 'int value' => [$value = rand(1, 100), $value],
  210. 'callable' => [$closure = function () {
  211. return 199;
  212. }, $closure],
  213. ];
  214. }
  215. /**
  216. * Construct a BatchCache with a in memory store
  217. * and an array cache, pretending to be a persistence store.
  218. *
  219. * @param array $memory
  220. * @param array $persisted
  221. * @param int|null $memoryLimit
  222. * @return CacheInterface
  223. */
  224. private function givenCache(array $memory = [], array $persisted = [], int $memoryLimit = null): CacheInterface
  225. {
  226. config()->set('excel.cache.batch.memory_limit', $memoryLimit ?: 60000);
  227. $this->memory = $this->app->make(CacheManager::class)->createMemoryDriver();
  228. $this->memory->setMultiple($memory);
  229. $store = new ArrayStore();
  230. $store->putMany($persisted, 10000);
  231. $this->cache = new Repository($store);
  232. if (!InstalledVersions::satisfies(new VersionParser, 'psr/simple-cache', '^3.0')) {
  233. return new BatchCacheDeprecated(
  234. $this->cache,
  235. $this->memory,
  236. config('excel.cache.default_ttl')
  237. );
  238. }
  239. return new BatchCache(
  240. $this->cache,
  241. $this->memory,
  242. config('excel.cache.default_ttl')
  243. );
  244. }
  245. }