HeadingRowImportTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace Maatwebsite\Excel\Tests;
  3. use Maatwebsite\Excel\HeadingRowImport;
  4. use Maatwebsite\Excel\Imports\HeadingRowFormatter;
  5. class HeadingRowImportTest extends TestCase
  6. {
  7. protected function tearDown(): void
  8. {
  9. HeadingRowFormatter::reset();
  10. }
  11. /**
  12. * @test
  13. */
  14. public function can_import_only_heading_row()
  15. {
  16. $import = new HeadingRowImport();
  17. $headings = $import->toArray('import-users-with-headings.xlsx');
  18. $this->assertEquals([
  19. [
  20. ['name', 'email'],
  21. ],
  22. ], $headings);
  23. }
  24. /**
  25. * @test
  26. */
  27. public function can_import_only_heading_row_with_custom_heading_row_formatter()
  28. {
  29. HeadingRowFormatter::extend('custom', function ($value) {
  30. return 'custom-' . $value;
  31. });
  32. HeadingRowFormatter::default('custom');
  33. $import = new HeadingRowImport();
  34. $headings = $import->toArray('import-users-with-headings.xlsx');
  35. $this->assertEquals([
  36. [
  37. ['custom-name', 'custom-email'],
  38. ],
  39. ], $headings);
  40. }
  41. /**
  42. * @test
  43. */
  44. public function can_import_only_heading_row_with_custom_heading_row_formatter_with_key()
  45. {
  46. HeadingRowFormatter::extend('custom', function ($value, $key) {
  47. return $key;
  48. });
  49. HeadingRowFormatter::default('custom');
  50. $import = new HeadingRowImport();
  51. $headings = $import->toArray('import-users-with-headings.xlsx');
  52. $this->assertEquals([
  53. [
  54. [0, 1],
  55. ],
  56. ], $headings);
  57. }
  58. /**
  59. * @test
  60. */
  61. public function can_import_only_heading_row_with_custom_row_number()
  62. {
  63. $import = new HeadingRowImport(2);
  64. $headings = $import->toArray('import-users-with-headings.xlsx');
  65. $this->assertEquals([
  66. [
  67. ['patrick_brouwers', 'patrick_at_maatwebsitenl'],
  68. ],
  69. ], $headings);
  70. }
  71. /**
  72. * @test
  73. */
  74. public function can_import_only_heading_row_for_multiple_sheets()
  75. {
  76. $import = new HeadingRowImport();
  77. $headings = $import->toArray('import-multiple-sheets.xlsx');
  78. $this->assertEquals([
  79. [
  80. ['1a1', '1b1'], // slugged first row of sheet 1
  81. ],
  82. [
  83. ['2a1', '2b1'], // slugged first row of sheet 2
  84. ],
  85. ], $headings);
  86. }
  87. /**
  88. * @test
  89. */
  90. public function can_import_only_heading_row_for_multiple_sheets_with_key()
  91. {
  92. HeadingRowFormatter::extend('custom', function ($value, $key) {
  93. return $key;
  94. });
  95. HeadingRowFormatter::default('custom');
  96. $import = new HeadingRowImport();
  97. $headings = $import->toArray('import-multiple-sheets.xlsx');
  98. $this->assertEquals([
  99. [
  100. [0, 1], // slugged first row of sheet 1
  101. ],
  102. [
  103. [0, 1], // slugged first row of sheet 2
  104. ],
  105. ], $headings);
  106. }
  107. /**
  108. * @test
  109. */
  110. public function can_import_only_heading_row_for_multiple_sheets_with_custom_row_number()
  111. {
  112. $import = new HeadingRowImport(2);
  113. $headings = $import->toArray('import-multiple-sheets.xlsx');
  114. $this->assertEquals([
  115. [
  116. ['1a2', '1b2'], // slugged 2nd row of sheet 1
  117. ],
  118. [
  119. ['2a2', '2b2'], // slugged 2nd row of sheet 2
  120. ],
  121. ], $headings);
  122. }
  123. /**
  124. * @test
  125. */
  126. public function can_import_heading_row_with_custom_formatter_defined_in_config()
  127. {
  128. HeadingRowFormatter::extend('custom2', function ($value) {
  129. return 'custom2-' . $value;
  130. });
  131. config()->set('excel.imports.heading_row.formatter', 'custom2');
  132. $import = new HeadingRowImport();
  133. $headings = $import->toArray('import-users-with-headings.xlsx');
  134. $this->assertEquals([
  135. [
  136. ['custom2-name', 'custom2-email'],
  137. ],
  138. ], $headings);
  139. }
  140. }