WithArrayTest.php 2.2 KB

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