ExportableTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Concerns;
  3. use Illuminate\Contracts\Support\Responsable;
  4. use Illuminate\Http\Request;
  5. use Maatwebsite\Excel\Concerns\Exportable;
  6. use Maatwebsite\Excel\Excel;
  7. use Maatwebsite\Excel\Exporter;
  8. use Maatwebsite\Excel\Tests\Data\Stubs\EmptyExport;
  9. use Maatwebsite\Excel\Tests\TestCase;
  10. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  11. class ExportableTest extends TestCase
  12. {
  13. /**
  14. * @test
  15. */
  16. public function needs_to_have_a_file_name_when_downloading()
  17. {
  18. $this->expectException(\Maatwebsite\Excel\Exceptions\NoFilenameGivenException::class);
  19. $this->expectExceptionMessage('A filename needs to be passed in order to download the export');
  20. $export = new class
  21. {
  22. use Exportable;
  23. };
  24. $export->download();
  25. }
  26. /**
  27. * @test
  28. */
  29. public function needs_to_have_a_file_name_when_storing()
  30. {
  31. $this->expectException(\Maatwebsite\Excel\Exceptions\NoFilePathGivenException::class);
  32. $this->expectExceptionMessage('A filepath needs to be passed in order to store the export');
  33. $export = new class
  34. {
  35. use Exportable;
  36. };
  37. $export->store();
  38. }
  39. /**
  40. * @test
  41. */
  42. public function needs_to_have_a_file_name_when_queuing()
  43. {
  44. $this->expectException(\Maatwebsite\Excel\Exceptions\NoFilePathGivenException::class);
  45. $this->expectExceptionMessage('A filepath needs to be passed in order to store the export');
  46. $export = new class
  47. {
  48. use Exportable;
  49. };
  50. $export->queue();
  51. }
  52. /**
  53. * @test
  54. */
  55. public function responsable_needs_to_have_file_name_configured_inside_the_export()
  56. {
  57. $this->expectException(\Maatwebsite\Excel\Exceptions\NoFilenameGivenException::class);
  58. $this->expectExceptionMessage('A filename needs to be passed in order to download the export');
  59. $export = new class implements Responsable
  60. {
  61. use Exportable;
  62. };
  63. $export->toResponse(new Request());
  64. }
  65. /**
  66. * @test
  67. */
  68. public function is_responsable()
  69. {
  70. $export = new class implements Responsable
  71. {
  72. use Exportable;
  73. protected $fileName = 'export.xlsx';
  74. };
  75. $this->assertInstanceOf(Responsable::class, $export);
  76. $response = $export->toResponse(new Request());
  77. $this->assertInstanceOf(BinaryFileResponse::class, $response);
  78. }
  79. /**
  80. * @test
  81. */
  82. public function can_have_customized_header()
  83. {
  84. $export = new class
  85. {
  86. use Exportable;
  87. };
  88. $response = $export->download(
  89. 'name.csv',
  90. Excel::CSV,
  91. [
  92. 'Content-Type' => 'text/csv',
  93. ]
  94. );
  95. $this->assertEquals('text/csv', $response->headers->get('Content-Type'));
  96. }
  97. /**
  98. * @test
  99. */
  100. public function can_set_custom_headers_in_export_class()
  101. {
  102. $export = new class
  103. {
  104. use Exportable;
  105. protected $fileName = 'name.csv';
  106. protected $writerType = Excel::CSV;
  107. protected $headers = [
  108. 'Content-Type' => 'text/csv',
  109. ];
  110. };
  111. $response = $export->toResponse(request());
  112. $this->assertEquals('text/csv', $response->headers->get('Content-Type'));
  113. }
  114. /**
  115. * @test
  116. */
  117. public function can_get_raw_export_contents()
  118. {
  119. $export = new EmptyExport;
  120. $response = $export->raw(Excel::XLSX);
  121. $this->assertNotEmpty($response);
  122. }
  123. /**
  124. * @test
  125. */
  126. public function can_have_customized_disk_options_when_storing()
  127. {
  128. $export = new EmptyExport;
  129. $this->mock(Exporter::class)
  130. ->shouldReceive('store')->once()
  131. ->with($export, 'name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
  132. $export->store('name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
  133. }
  134. /**
  135. * @test
  136. */
  137. public function can_have_customized_disk_options_when_queueing()
  138. {
  139. $export = new EmptyExport;
  140. $this->mock(Exporter::class)
  141. ->shouldReceive('queue')->once()
  142. ->with($export, 'name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
  143. $export->queue('name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
  144. }
  145. /**
  146. * @test
  147. */
  148. public function can_set_disk_options_in_export_class_when_storing()
  149. {
  150. $export = new class
  151. {
  152. use Exportable;
  153. public $disk = 's3';
  154. public $writerType = Excel::CSV;
  155. public $diskOptions = ['visibility' => 'private'];
  156. };
  157. $this->mock(Exporter::class)
  158. ->shouldReceive('store')->once()
  159. ->with($export, 'name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
  160. $export->store('name.csv');
  161. }
  162. /**
  163. * @test
  164. */
  165. public function can_set_disk_options_in_export_class_when_queuing()
  166. {
  167. $export = new class
  168. {
  169. use Exportable;
  170. public $disk = 's3';
  171. public $writerType = Excel::CSV;
  172. public $diskOptions = ['visibility' => 'private'];
  173. };
  174. $this->mock(Exporter::class)
  175. ->shouldReceive('queue')->once()
  176. ->with($export, 'name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
  177. $export->queue('name.csv');
  178. }
  179. /**
  180. * @test
  181. */
  182. public function can_override_export_class_disk_options_when_calling_store()
  183. {
  184. $export = new class
  185. {
  186. use Exportable;
  187. public $diskOptions = ['visibility' => 'public'];
  188. };
  189. $this->mock(Exporter::class)
  190. ->shouldReceive('store')->once()
  191. ->with($export, 'name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
  192. $export->store('name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
  193. }
  194. /**
  195. * @test
  196. */
  197. public function can_override_export_class_disk_options_when_calling_queue()
  198. {
  199. $export = new class
  200. {
  201. use Exportable;
  202. public $diskOptions = ['visibility' => 'public'];
  203. };
  204. $this->mock(Exporter::class)
  205. ->shouldReceive('queue')->once()
  206. ->with($export, 'name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
  207. $export->queue('name.csv', 's3', Excel::CSV, ['visibility' => 'private']);
  208. }
  209. /**
  210. * @test
  211. */
  212. public function can_have_empty_disk_options_when_storing()
  213. {
  214. $export = new EmptyExport;
  215. $this->mock(Exporter::class)
  216. ->shouldReceive('store')->once()
  217. ->with($export, 'name.csv', null, null, []);
  218. $export->store('name.csv');
  219. }
  220. /**
  221. * @test
  222. */
  223. public function can_have_empty_disk_options_when_queueing()
  224. {
  225. $export = new EmptyExport;
  226. $this->mock(Exporter::class)
  227. ->shouldReceive('queue')->once()
  228. ->with($export, 'name.csv', null, null, []);
  229. $export->queue('name.csv');
  230. }
  231. }