WithCallbackTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Tests\Exporters\CSV;
  3. use Dcat\EasyExcel\Excel;
  4. use Tests\Exporters\Exporter;
  5. use Tests\TestCase;
  6. class WithCallbackTest extends TestCase
  7. {
  8. use Exporter;
  9. /**
  10. * @group exporter
  11. */
  12. public function testStore()
  13. {
  14. $users = include __DIR__.'/../../resources/users.php';
  15. $storePath = $this->generateTempFilePath('csv');
  16. // 保存
  17. Excel::export(function () use ($users) {
  18. return $users;
  19. })->store($storePath);
  20. // 判断是否正确
  21. $this->assertSingleSheet($storePath, 0, $users);
  22. /*
  23. |---------------------------------------------------------------
  24. | 测试多个sheet
  25. |---------------------------------------------------------------
  26. */
  27. $users1 = array_slice($users, 0, 30);
  28. $users2 = array_values(array_slice($users, 30, 30));
  29. $storePath = $this->generateTempFilePath('csv');
  30. // 保存
  31. Excel::export(function () use ($users1, $users2) {
  32. return ['sheet1' => $users1, 'sheet2' => $users2];
  33. })->store($storePath);
  34. $this->assertSingleSheet($storePath, 0, $users);
  35. }
  36. /**
  37. * @group exporter
  38. */
  39. public function testRaw()
  40. {
  41. $users = include __DIR__.'/../../resources/users.php';
  42. $storePath = $this->generateTempFilePath('csv');
  43. // 获取内容
  44. $contents = Excel::csv(function () use ($users) {
  45. return $users;
  46. })->raw();
  47. $this->assertIsString($contents);
  48. // 保存文件内容
  49. file_put_contents($storePath, $contents);
  50. // 判断内容是否正确
  51. $this->assertSingleSheet($storePath, 0, $users);
  52. /*
  53. |---------------------------------------------------------------
  54. | 测试多个sheet
  55. |---------------------------------------------------------------
  56. */
  57. $users1 = array_slice($users, 0, 30);
  58. $users2 = array_values(array_slice($users, 30, 30));
  59. $storePath = $this->generateTempFilePath('csv');
  60. // 获取内容
  61. $contents = Excel::csv(function () use ($users1, $users2) {
  62. return ['sheet1' => $users1, 'sheet2' => $users2];
  63. })->raw();
  64. $this->assertIsString($contents);
  65. // 保存文件内容
  66. file_put_contents($storePath, $contents);
  67. // 判断内容是否正确
  68. $this->assertSingleSheet($storePath, 0, $users);
  69. }
  70. }