WithHeadingsTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Concerns;
  3. use Illuminate\Support\Collection;
  4. use Maatwebsite\Excel\Concerns\Exportable;
  5. use Maatwebsite\Excel\Concerns\FromCollection;
  6. use Maatwebsite\Excel\Concerns\WithCustomStartCell;
  7. use Maatwebsite\Excel\Concerns\WithHeadings;
  8. use Maatwebsite\Excel\Tests\TestCase;
  9. class WithHeadingsTest extends TestCase
  10. {
  11. /**
  12. * @test
  13. */
  14. public function can_export_from_collection_with_heading_row()
  15. {
  16. $export = new class implements FromCollection, WithHeadings
  17. {
  18. use Exportable;
  19. /**
  20. * @return Collection
  21. */
  22. public function collection()
  23. {
  24. return collect([
  25. ['A1', 'B1', 'C1'],
  26. ['A2', 'B2', 'C2'],
  27. ]);
  28. }
  29. /**
  30. * @return array
  31. */
  32. public function headings(): array
  33. {
  34. return ['A', 'B', 'C'];
  35. }
  36. };
  37. $response = $export->store('with-heading-store.xlsx');
  38. $this->assertTrue($response);
  39. $actual = $this->readAsArray(__DIR__ . '/../Data/Disks/Local/with-heading-store.xlsx', 'Xlsx');
  40. $expected = [
  41. ['A', 'B', 'C'],
  42. ['A1', 'B1', 'C1'],
  43. ['A2', 'B2', 'C2'],
  44. ];
  45. $this->assertEquals($expected, $actual);
  46. }
  47. /**
  48. * @test
  49. */
  50. public function can_export_from_collection_with_multiple_heading_rows()
  51. {
  52. $export = new class implements FromCollection, WithHeadings
  53. {
  54. use Exportable;
  55. /**
  56. * @return Collection
  57. */
  58. public function collection()
  59. {
  60. return collect([
  61. ['A1', 'B1', 'C1'],
  62. ['A2', 'B2', 'C2'],
  63. ]);
  64. }
  65. /**
  66. * @return array
  67. */
  68. public function headings(): array
  69. {
  70. return [
  71. ['A', 'B', 'C'],
  72. ['Aa', 'Bb', 'Cc'],
  73. ];
  74. }
  75. };
  76. $response = $export->store('with-heading-store.xlsx');
  77. $this->assertTrue($response);
  78. $actual = $this->readAsArray(__DIR__ . '/../Data/Disks/Local/with-heading-store.xlsx', 'Xlsx');
  79. $expected = [
  80. ['A', 'B', 'C'],
  81. ['Aa', 'Bb', 'Cc'],
  82. ['A1', 'B1', 'C1'],
  83. ['A2', 'B2', 'C2'],
  84. ];
  85. $this->assertEquals($expected, $actual);
  86. }
  87. /**
  88. * @test
  89. */
  90. public function can_export_from_collection_with_heading_row_with_custom_start_cell()
  91. {
  92. $export = new class implements FromCollection, WithHeadings, WithCustomStartCell
  93. {
  94. use Exportable;
  95. /**
  96. * @return Collection
  97. */
  98. public function collection()
  99. {
  100. return collect([
  101. ['A1', 'B1', 'C1'],
  102. ['A2', 'B2', 'C2'],
  103. ]);
  104. }
  105. /**
  106. * @return array
  107. */
  108. public function headings(): array
  109. {
  110. return ['A', 'B', 'C'];
  111. }
  112. /**
  113. * @return string
  114. */
  115. public function startCell(): string
  116. {
  117. return 'B2';
  118. }
  119. };
  120. $response = $export->store('with-heading-store.xlsx');
  121. $this->assertTrue($response);
  122. $actual = $this->readAsArray(__DIR__ . '/../Data/Disks/Local/with-heading-store.xlsx', 'Xlsx');
  123. $expected = [
  124. [null, null, null, null],
  125. [null, 'A', 'B', 'C'],
  126. [null, 'A1', 'B1', 'C1'],
  127. [null, 'A2', 'B2', 'C2'],
  128. ];
  129. $this->assertEquals($expected, $actual);
  130. }
  131. }