HeadingTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Tests\Exporters\XLSX;
  3. use Dcat\EasyExcel\Excel;
  4. use Dcat\EasyExcel\Support\SheetCollection;
  5. use Tests\Exporters\Exporter;
  6. use Tests\TestCase;
  7. class HeadingTest extends TestCase
  8. {
  9. use Exporter;
  10. protected $headings = [
  11. 'id' => 'ID',
  12. 'name' => '名称',
  13. 'email' => '邮箱',
  14. 'email_verified_at' => '邮箱验证时间',
  15. 'password' => '密码',
  16. 'remember_token' => '记住登录Token',
  17. 'created_at' => '创建时间',
  18. 'updated_at' => '更新时间',
  19. ];
  20. protected $headings2 = [
  21. 'id' => 'ID',
  22. 'name' => '名称',
  23. 'email' => '邮箱',
  24. 'email_verified_at' => '邮箱验证时间',
  25. 'remember_token' => '记住登录Token',
  26. 'password' => '密码',
  27. ];
  28. /**
  29. * @group exporter
  30. */
  31. public function test()
  32. {
  33. $users = include __DIR__.'/../../resources/users.php';
  34. $storePath = $this->generateTempFilePath('xlsx');
  35. // 保存
  36. Excel::export($users)->headings($this->headings)->store($storePath);
  37. // 读取
  38. $sheet = Excel::import($storePath)->first()->toArray();
  39. $this->assertSheetHeadings($sheet, $this->headings);
  40. /*
  41. |---------------------------------------------------------------
  42. | 读取时禁用标题
  43. |---------------------------------------------------------------
  44. */
  45. $sheet = Excel::import($storePath)->headings(false)->first()->toArray();
  46. $firstRow = current($sheet);
  47. $this->assertIsArray($firstRow);
  48. $this->assertEquals(count($firstRow), count($this->headings));
  49. $this->assertEquals(array_keys($firstRow), range(0, count($this->headings) - 1));
  50. /*
  51. |---------------------------------------------------------------
  52. | 多个sheets用同样的标题
  53. |---------------------------------------------------------------
  54. */
  55. $users1 = new SheetCollection(array_slice($users, 0, 30));
  56. $users2 = new SheetCollection(array_values(array_slice($users, 30, 30)));
  57. Excel::export(['sheet1' => $users1->toArray(), 'sheet2' => $users2->toArray()])->headings($this->headings)->store($storePath);
  58. // 读取
  59. $sheets = Excel::import($storePath)->toArray();
  60. $this->assertTrue(isset($sheets['sheet1']));
  61. $this->assertTrue(isset($sheets['sheet2']));
  62. $this->assertSheetHeadings($sheets['sheet1'], $this->headings);
  63. $this->assertSheetHeadings($sheets['sheet2'], $this->headings);
  64. }
  65. /**
  66. * @group exporter
  67. */
  68. public function testMultipleSheets()
  69. {
  70. $users = include __DIR__.'/../../resources/users.php';
  71. $sheet1 = Excel::createSheet(array_slice($users, 0, 30))->headings($this->headings);
  72. $sheet2 = Excel::createSheet(array_slice($users, 30, 30))->headings($this->headings2);
  73. $storePath = $this->generateTempFilePath('xlsx');
  74. // 保存
  75. Excel::export([$sheet1, $sheet2])->store($storePath);
  76. // 读取
  77. $sheetsArray = Excel::import($storePath)->toArray();
  78. $this->assertTrue(isset($sheetsArray['Sheet1']));
  79. $this->assertTrue(isset($sheetsArray['Sheet2']));
  80. $this->assertSheetHeadings($sheetsArray['Sheet1'], $this->headings);
  81. $this->assertSheetHeadings($sheetsArray['Sheet2'], $this->headings2);
  82. /*
  83. |---------------------------------------------------------------
  84. | 测试禁用其中一个表格标题
  85. |---------------------------------------------------------------
  86. */
  87. $sheet1 = Excel::createSheet(array_slice($users, 0, 30))->headings($this->headings);
  88. $sheet2 = Excel::createSheet(array_slice($users, 30, 30))->headings(false);
  89. $storePath = $this->generateTempFilePath('xlsx');
  90. // 保存
  91. Excel::export([$sheet1, $sheet2])->store($storePath);
  92. // 读取
  93. $sheetsArray = Excel::import($storePath)->headings(false)->toArray();
  94. $this->assertTrue(isset($sheetsArray['Sheet1']));
  95. $this->assertTrue(isset($sheetsArray['Sheet2']));
  96. $this->assertEquals(count($sheetsArray['Sheet1']), 31);
  97. $this->assertEquals(count($sheetsArray['Sheet2']), 30);
  98. $this->assertEquals(current($sheetsArray['Sheet1']), array_values($this->headings));
  99. }
  100. /**
  101. * @param $sheetArray
  102. * @param array $headings
  103. */
  104. protected function assertSheetHeadings($sheetArray, array $headings)
  105. {
  106. $firstRowInSheet = current($sheetArray);
  107. $this->assertIsArray($firstRowInSheet);
  108. $this->assertEquals(count($firstRowInSheet), count($headings));
  109. $this->assertEquals(array_keys($firstRowInSheet), array_values($headings));
  110. }
  111. }