ExcelFakeTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. namespace Maatwebsite\Excel\Tests;
  3. use Illuminate\Contracts\Queue\ShouldQueue;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Foundation\Bus\PendingDispatch;
  6. use Illuminate\Support\Collection;
  7. use Maatwebsite\Excel\Concerns\FromCollection;
  8. use Maatwebsite\Excel\Concerns\ToModel;
  9. use Maatwebsite\Excel\Facades\Excel as ExcelFacade;
  10. use Maatwebsite\Excel\Fakes\ExcelFake;
  11. use Maatwebsite\Excel\Tests\Data\Stubs\ChainedJobStub;
  12. use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;
  13. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  14. class ExcelFakeTest extends TestCase
  15. {
  16. /**
  17. * @test
  18. */
  19. public function can_fake_an_export()
  20. {
  21. ExcelFacade::fake();
  22. // Excel instance should be swapped to the fake now.
  23. $this->assertInstanceOf(ExcelFake::class, $this->app->make('excel'));
  24. }
  25. /**
  26. * @test
  27. */
  28. public function can_assert_against_a_fake_downloaded_export()
  29. {
  30. ExcelFacade::fake();
  31. $response = ExcelFacade::download($this->givenExport(), 'downloaded-filename.csv');
  32. $this->assertInstanceOf(BinaryFileResponse::class, $response);
  33. ExcelFacade::assertDownloaded('downloaded-filename.csv');
  34. ExcelFacade::assertDownloaded('downloaded-filename.csv', function (FromCollection $export) {
  35. return $export->collection()->contains('foo');
  36. });
  37. ExcelFacade::matchByRegex();
  38. ExcelFacade::assertDownloaded('/\w{10}-\w{8}\.csv/');
  39. }
  40. /**
  41. * @test
  42. */
  43. public function can_assert_against_a_fake_stored_export()
  44. {
  45. ExcelFacade::fake();
  46. $response = ExcelFacade::store($this->givenExport(), 'stored-filename.csv', 's3');
  47. $this->assertTrue($response);
  48. ExcelFacade::assertStored('stored-filename.csv', 's3');
  49. ExcelFacade::assertStored('stored-filename.csv', 's3', function (FromCollection $export) {
  50. return $export->collection()->contains('foo');
  51. });
  52. ExcelFacade::matchByRegex();
  53. ExcelFacade::assertStored('/\w{6}-\w{8}\.csv/', 's3');
  54. }
  55. /**
  56. * @test
  57. */
  58. public function can_assert_regex_against_a_fake_stored_export_with_multiple_files()
  59. {
  60. ExcelFacade::fake();
  61. $response = ExcelFacade::store($this->givenExport(), 'stored-filename-one.csv', 's3');
  62. $this->assertTrue($response);
  63. $response = ExcelFacade::store($this->givenExport(), 'stored-filename-two.csv', 's3');
  64. $this->assertTrue($response);
  65. ExcelFacade::matchByRegex();
  66. ExcelFacade::assertStored('/\w{6}-\w{8}-one\.csv/', 's3');
  67. ExcelFacade::assertStored('/\w{6}-\w{8}-two\.csv/', 's3');
  68. }
  69. /**
  70. * @test
  71. */
  72. public function a_callback_can_be_passed_as_the_second_argument_when_asserting_against_a_faked_stored_export()
  73. {
  74. ExcelFacade::fake();
  75. $response = ExcelFacade::store($this->givenExport(), 'stored-filename.csv');
  76. $this->assertTrue($response);
  77. ExcelFacade::assertStored('stored-filename.csv');
  78. ExcelFacade::assertStored('stored-filename.csv', function (FromCollection $export) {
  79. return $export->collection()->contains('foo');
  80. });
  81. ExcelFacade::matchByRegex();
  82. ExcelFacade::assertStored('/\w{6}-\w{8}\.csv/');
  83. }
  84. /**
  85. * @test
  86. */
  87. public function can_assert_against_a_fake_queued_export()
  88. {
  89. ExcelFacade::fake();
  90. $response = ExcelFacade::queue($this->givenExport(), 'queued-filename.csv', 's3');
  91. $this->assertInstanceOf(PendingDispatch::class, $response);
  92. ExcelFacade::assertQueued('queued-filename.csv', 's3');
  93. ExcelFacade::assertQueued('queued-filename.csv', 's3', function (FromCollection $export) {
  94. return $export->collection()->contains('foo');
  95. });
  96. ExcelFacade::matchByRegex();
  97. ExcelFacade::assertQueued('/\w{6}-\w{8}\.csv/', 's3');
  98. }
  99. /**
  100. * @test
  101. */
  102. public function can_assert_against_a_fake_implicitly_queued_export()
  103. {
  104. ExcelFacade::fake();
  105. $response = ExcelFacade::store($this->givenQueuedExport(), 'queued-filename.csv', 's3');
  106. $this->assertInstanceOf(PendingDispatch::class, $response);
  107. ExcelFacade::assertStored('queued-filename.csv', 's3');
  108. ExcelFacade::assertQueued('queued-filename.csv', 's3');
  109. ExcelFacade::assertQueued('queued-filename.csv', 's3', function (FromCollection $export) {
  110. return $export->collection()->contains('foo');
  111. });
  112. ExcelFacade::matchByRegex();
  113. ExcelFacade::assertQueued('/\w{6}-\w{8}\.csv/', 's3');
  114. }
  115. /**
  116. * @test
  117. */
  118. public function can_assert_against_a_fake_queued_export_with_chain()
  119. {
  120. ExcelFacade::fake();
  121. ExcelFacade::queue(
  122. $this->givenQueuedExport(), 'queued-filename.csv', 's3'
  123. )->chain([
  124. new ChainedJobStub(),
  125. ]);
  126. ExcelFacade::assertQueuedWithChain([
  127. new ChainedJobStub(),
  128. ]);
  129. }
  130. /**
  131. * @test
  132. */
  133. public function can_assert_against_a_fake_raw_export()
  134. {
  135. ExcelFacade::fake();
  136. $response = ExcelFacade::raw($this->givenExport(), \Maatwebsite\Excel\Excel::XLSX);
  137. $this->assertIsString($response);
  138. ExcelFacade::assertExportedInRaw(get_class($this->givenExport()));
  139. ExcelFacade::assertExportedInRaw(get_class($this->givenExport()), function (FromCollection $export) {
  140. return $export->collection()->contains('foo');
  141. });
  142. }
  143. /**
  144. * @test
  145. */
  146. public function can_assert_against_a_fake_import()
  147. {
  148. ExcelFacade::fake();
  149. ExcelFacade::import($this->givenImport(), 'stored-filename.csv', 's3');
  150. ExcelFacade::assertImported('stored-filename.csv', 's3');
  151. ExcelFacade::assertImported('stored-filename.csv', 's3', function (ToModel $import) {
  152. return $import->model([]) instanceof User;
  153. });
  154. ExcelFacade::matchByRegex();
  155. ExcelFacade::assertImported('/\w{6}-\w{8}\.csv/', 's3');
  156. }
  157. /**
  158. * @test
  159. */
  160. public function can_assert_against_a_fake_import_with_uploaded_file()
  161. {
  162. ExcelFacade::fake();
  163. ExcelFacade::import($this->givenImport(), $this->givenUploadedFile(__DIR__ . '/Data/Disks/Local/import.xlsx'));
  164. ExcelFacade::assertImported('import.xlsx');
  165. ExcelFacade::assertImported('import.xlsx', function (ToModel $import) {
  166. return $import->model([]) instanceof User;
  167. });
  168. ExcelFacade::matchByRegex();
  169. ExcelFacade::assertImported('/\w{6}\.xlsx/');
  170. }
  171. /**
  172. * @test
  173. */
  174. public function can_assert_against_a_fake_queued_import()
  175. {
  176. ExcelFacade::fake();
  177. $response = ExcelFacade::queueImport($this->givenQueuedImport(), 'queued-filename.csv', 's3');
  178. $this->assertInstanceOf(PendingDispatch::class, $response);
  179. ExcelFacade::assertImported('queued-filename.csv', 's3');
  180. ExcelFacade::assertQueued('queued-filename.csv', 's3');
  181. ExcelFacade::assertQueued('queued-filename.csv', 's3', function (ToModel $import) {
  182. return $import->model([]) instanceof User;
  183. });
  184. ExcelFacade::matchByRegex();
  185. ExcelFacade::assertQueued('/\w{6}-\w{8}\.csv/', 's3');
  186. }
  187. /**
  188. * @test
  189. */
  190. public function can_assert_against_a_fake_implicitly_queued_import()
  191. {
  192. ExcelFacade::fake();
  193. $response = ExcelFacade::import($this->givenQueuedImport(), 'queued-filename.csv', 's3');
  194. $this->assertInstanceOf(PendingDispatch::class, $response);
  195. ExcelFacade::assertImported('queued-filename.csv', 's3');
  196. ExcelFacade::assertQueued('queued-filename.csv', 's3');
  197. ExcelFacade::assertQueued('queued-filename.csv', 's3', function (ToModel $import) {
  198. return $import->model([]) instanceof User;
  199. });
  200. ExcelFacade::matchByRegex();
  201. ExcelFacade::assertQueued('/\w{6}-\w{8}\.csv/', 's3');
  202. }
  203. /**
  204. * @test
  205. */
  206. public function can_assert_against_a_fake_queued_import_with_chain()
  207. {
  208. ExcelFacade::fake();
  209. ExcelFacade::queueImport(
  210. $this->givenQueuedImport(), 'queued-filename.csv', 's3'
  211. )->chain([
  212. new ChainedJobStub(),
  213. ]);
  214. ExcelFacade::assertQueuedWithChain([
  215. new ChainedJobStub(),
  216. ]);
  217. }
  218. /**
  219. * @test
  220. */
  221. public function a_callback_can_be_passed_as_the_second_argument_when_asserting_against_a_faked_queued_export()
  222. {
  223. ExcelFacade::fake();
  224. $response = ExcelFacade::queue($this->givenExport(), 'queued-filename.csv');
  225. $this->assertInstanceOf(PendingDispatch::class, $response);
  226. ExcelFacade::assertQueued('queued-filename.csv');
  227. ExcelFacade::assertQueued('queued-filename.csv', function (FromCollection $export) {
  228. return $export->collection()->contains('foo');
  229. });
  230. ExcelFacade::matchByRegex();
  231. ExcelFacade::assertQueued('/\w{6}-\w{8}\.csv/');
  232. }
  233. /**
  234. * @return FromCollection
  235. */
  236. private function givenExport()
  237. {
  238. return new class implements FromCollection
  239. {
  240. /**
  241. * @return Collection
  242. */
  243. public function collection()
  244. {
  245. return collect(['foo', 'bar']);
  246. }
  247. };
  248. }
  249. /**
  250. * @return FromCollection
  251. */
  252. private function givenQueuedExport()
  253. {
  254. return new class implements FromCollection, ShouldQueue
  255. {
  256. /**
  257. * @return Collection
  258. */
  259. public function collection()
  260. {
  261. return collect(['foo', 'bar']);
  262. }
  263. };
  264. }
  265. /**
  266. * @return object
  267. */
  268. private function givenImport()
  269. {
  270. return new class implements ToModel
  271. {
  272. /**
  273. * @param array $row
  274. * @return Model|null
  275. */
  276. public function model(array $row)
  277. {
  278. return new User([]);
  279. }
  280. };
  281. }
  282. /**
  283. * @return object
  284. */
  285. private function givenQueuedImport()
  286. {
  287. return new class implements ToModel, ShouldQueue
  288. {
  289. /**
  290. * @param array $row
  291. * @return Model|null
  292. */
  293. public function model(array $row)
  294. {
  295. return new User([]);
  296. }
  297. };
  298. }
  299. }