ImporterTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace Tests\Importers;
  3. use Dcat\EasyExcel\Contracts;
  4. use Dcat\EasyExcel\Excel;
  5. use Tests\TestCase;
  6. class ImporterTest extends TestCase
  7. {
  8. /**
  9. * @group importer
  10. */
  11. public function testCsv()
  12. {
  13. $file = __DIR__.'/../resources/test.csv';
  14. $this->assertSheet($file, 0);
  15. }
  16. /**
  17. * @group importer
  18. */
  19. public function testXlsx()
  20. {
  21. $file = __DIR__.'/../resources/test.xlsx';
  22. $this->assertSheet($file, 'Sheet1');
  23. }
  24. /**
  25. * @group importer
  26. */
  27. public function testWithHeadings()
  28. {
  29. $xlsx = __DIR__.'/../resources/test.xlsx';
  30. $csv = __DIR__.'/../resources/test.csv';
  31. $headers = [
  32. 'ID', 'NAME', 'EMAIL',
  33. ];
  34. // xlsx
  35. $xlsxSheetArray = Excel::import($xlsx)
  36. ->headings($headers)
  37. ->first()
  38. ->toArray();
  39. $this->assertEquals($headers, array_keys(current($xlsxSheetArray)));
  40. // csv
  41. $csvSheetArray = Excel::import($csv)
  42. ->headings($headers)
  43. ->first()
  44. ->toArray();
  45. $this->assertEquals($headers, array_keys(current($csvSheetArray)));
  46. }
  47. /**
  48. * @group importer
  49. */
  50. public function testWithoutHeadings()
  51. {
  52. $xlsx = __DIR__.'/../resources/test.xlsx';
  53. $csv = __DIR__.'/../resources/test.csv';
  54. // xlsx
  55. $sheetArray = Excel::import($xlsx)
  56. ->headings(false)
  57. ->first()
  58. ->toArray();
  59. $this->assertEquals(range(0, 7), array_keys(current($sheetArray)));
  60. // csv
  61. $sheetArray = Excel::import($csv)
  62. ->headings(false)
  63. ->first()
  64. ->toArray();
  65. $this->assertEquals(range(0, 7), array_keys(current($sheetArray)));
  66. }
  67. /**
  68. * @group importer
  69. */
  70. public function testWorking()
  71. {
  72. // xlsx
  73. $file = __DIR__.'/../resources/test.xlsx';
  74. $sheetArray = Excel::import($file)->active()->toArray();
  75. $this->validateSheetArray($sheetArray);
  76. // csv
  77. $file = __DIR__.'/../resources/test.csv';
  78. $sheetArray = Excel::import($file)->active()->toArray();
  79. $this->validateSheetArray($sheetArray);
  80. }
  81. /**
  82. * @group importer
  83. */
  84. public function testFirst()
  85. {
  86. // xlsx
  87. $file = __DIR__.'/../resources/test.xlsx';
  88. $sheetArray = Excel::import($file)->first()->toArray();
  89. $this->validateSheetArray($sheetArray);
  90. // csv
  91. $file = __DIR__.'/../resources/test.csv';
  92. $sheetArray = Excel::import($file)->first()->toArray();
  93. $this->validateSheetArray($sheetArray);
  94. }
  95. /**
  96. * @group importer
  97. */
  98. public function testGetSheet()
  99. {
  100. // xlsx
  101. $xlsx = __DIR__.'/../resources/test.xlsx';
  102. $sheetArray = Excel::import($xlsx)->sheet('Sheet1')->toArray();
  103. $this->validateSheetArray($sheetArray);
  104. $sheetArray = Excel::import($xlsx)->sheet(0)->toArray();
  105. $this->validateSheetArray($sheetArray);
  106. // csv
  107. $csv = __DIR__.'/../resources/test.csv';
  108. $sheetArray = Excel::import($csv)->sheet(0)->toArray();
  109. $this->validateSheetArray($sheetArray);
  110. }
  111. /**
  112. * @group importer
  113. */
  114. public function testEach()
  115. {
  116. $xlsx = __DIR__.'/../resources/test.xlsx';
  117. $csv = __DIR__.'/../resources/test.csv';
  118. Excel::import($xlsx)->each(function (Contracts\Sheet $sheet) {
  119. $this->validateSheetArray($sheet->toArray());
  120. });
  121. Excel::import($csv)->each(function (Contracts\Sheet $sheet) {
  122. $this->validateSheetArray($sheet->toArray());
  123. });
  124. }
  125. /**
  126. * @group importer
  127. */
  128. public function testToArray()
  129. {
  130. $this->assertTrue(true);
  131. }
  132. /**
  133. * @group importer
  134. */
  135. public function testHeadingRow()
  136. {
  137. $xlsx = __DIR__.'/../resources/heading.xlsx';
  138. $sheetArray = Excel::import($xlsx)
  139. ->headingRow(2)
  140. ->sheet('Sheet1')
  141. ->toArray();
  142. $this->validateSheetArray($sheetArray);
  143. // 闭包测试
  144. $sheetArray = Excel::import($xlsx)
  145. ->headingRow(function (int $line, array $row) {
  146. $first = $row[0];
  147. return $line == 2;
  148. })
  149. ->sheet('Sheet1')
  150. ->toArray();
  151. $this->validateSheetArray($sheetArray);
  152. }
  153. /**
  154. * @group importer
  155. */
  156. public function testFilter()
  157. {
  158. $xlsx = __DIR__.'/../resources/test.xlsx';
  159. $csv = __DIR__.'/../resources/test.csv';
  160. $sheetArray = Excel::import($xlsx)
  161. ->sheet('Sheet1')
  162. ->filter(function ($row) {
  163. return $row['id'] > 10;
  164. })
  165. ->toArray();
  166. $this->assertEquals(count($sheetArray), 40);
  167. $users = include __DIR__.'/../resources/users.php';
  168. $this->assertEquals(array_values($sheetArray), array_values(array_slice($users, 10, 40)));
  169. // csv
  170. $sheetArray = Excel::import($csv)
  171. ->sheet(0)
  172. ->filter(function ($row) {
  173. return $row['id'] > 10;
  174. })
  175. ->toArray();
  176. $this->assertEquals(count($sheetArray), 40);
  177. $this->assertEquals(array_values($sheetArray), array_values(array_slice($users, 10, 40)));
  178. }
  179. protected function assertSheet($file, $key)
  180. {
  181. $sheetsArray = Excel::import($file)->toArray();
  182. $this->assertIsArray($sheetsArray);
  183. $this->assertEquals(count($sheetsArray), 1);
  184. $this->assertTrue(isset($sheetsArray[$key]));
  185. $this->assertIsArray($sheetsArray[$key]);
  186. $this->validateSheetArray($sheetsArray[$key]);
  187. }
  188. protected function validateSheetArray(array $sheetArray)
  189. {
  190. $this->assertEquals(count($sheetArray), 50);
  191. $users = include __DIR__.'/../resources/users.php';
  192. $this->assertEquals(array_values($sheetArray), array_slice($users, 0, 50));
  193. }
  194. }