SheetTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Tests\Importers;
  3. use Box\Spout\Reader\SheetInterface;
  4. use Dcat\EasyExcel\Contracts\Sheet;
  5. use Dcat\EasyExcel\Excel;
  6. use Dcat\EasyExcel\Support\SheetCollection;
  7. use Tests\TestCase;
  8. class SheetTest extends TestCase
  9. {
  10. /**
  11. * @group importer
  12. */
  13. public function testToArray()
  14. {
  15. $sheet = $this->makeSheet();
  16. $this->validateSheetArray($sheet->toArray());
  17. }
  18. /**
  19. * @group importer
  20. */
  21. public function testToCollection()
  22. {
  23. $sheet = $this->makeSheet();
  24. $collection = $sheet->collect();
  25. $this->assertInstanceOf(SheetCollection::class, $collection);
  26. $this->validateSheetArray($collection->toArray());
  27. }
  28. /**
  29. * @group importer
  30. */
  31. public function testEach()
  32. {
  33. $rows = [];
  34. $headers = [];
  35. $lastLine = 0;
  36. $this->makeSheet()->each(function (array $row, int $lineNumber, array $originalHeaders) use (&$rows, &$headers, &$lastLine) {
  37. $rows[] = $row;
  38. $headers = $originalHeaders;
  39. $lastLine = $lineNumber;
  40. });
  41. $users = include __DIR__.'/../resources/users.php';
  42. $this->assertEquals(array_keys(current($users)), $headers);
  43. $this->assertEquals(
  44. array_values($rows),
  45. array_slice($users, 0, 30)
  46. );
  47. $this->assertEquals($lastLine, 31);
  48. }
  49. /**
  50. * @group importer
  51. */
  52. public function testChunk()
  53. {
  54. $chunkSize = 20;
  55. $chunks = [];
  56. $this->makeSheet()->chunk($chunkSize, function (SheetCollection $collection) use (&$chunks) {
  57. $chunks[] = array_values($collection->toArray());
  58. });
  59. $users = include __DIR__.'/../resources/users.php';
  60. $users = (new SheetCollection(array_slice($users, 0, 30)))
  61. ->chunk($chunkSize)
  62. ->map(function ($collection) {
  63. return array_values($collection->toArray());
  64. })
  65. ->toArray();
  66. $this->assertEquals($chunks, $users);
  67. }
  68. /**
  69. * @group importer
  70. */
  71. public function testSheetMethods()
  72. {
  73. $sheet = $this->makeSheet();
  74. // getName
  75. $this->assertEquals($sheet->getName(), 'name1');
  76. // getIndex
  77. $this->assertEquals($sheet->getIndex(), 0);
  78. // valid
  79. $this->assertEquals($sheet->valid(), true);
  80. // isWorking
  81. $this->assertEquals($sheet->isActive(), false);
  82. // isWorking
  83. $this->assertEquals($this->factory()->active()->isActive(), true);
  84. // getSheet
  85. $this->assertInstanceOf(SheetInterface::class, $sheet->getSheet());
  86. }
  87. protected function validateSheetArray(array $sheet)
  88. {
  89. $this->assertIsArray($sheet);
  90. $this->assertEquals(count($sheet), 30);
  91. $sheet = $this->convertDatetimeObjectToString($sheet);
  92. $users = include __DIR__.'/../resources/users.php';
  93. $this->assertEquals(
  94. array_values($sheet),
  95. array_slice($users, 0, 30)
  96. );
  97. }
  98. protected function makeSheet()
  99. {
  100. $sheet = $this->factory()->first();
  101. $this->assertInstanceOf(Sheet::class, $sheet);
  102. return $sheet;
  103. }
  104. protected function factory()
  105. {
  106. $file = __DIR__.'/../resources/test-sheets.xlsx';
  107. return Excel::xlsx($file);
  108. }
  109. }