ImportableTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Concerns;
  3. use Maatwebsite\Excel\Concerns\Importable;
  4. use Maatwebsite\Excel\Concerns\ToArray;
  5. use Maatwebsite\Excel\Excel;
  6. use Maatwebsite\Excel\Importer;
  7. use Maatwebsite\Excel\Tests\TestCase;
  8. use PHPUnit\Framework\Assert;
  9. class ImportableTest extends TestCase
  10. {
  11. /**
  12. * @test
  13. */
  14. public function can_import_a_simple_xlsx_file()
  15. {
  16. $import = new class implements ToArray
  17. {
  18. use Importable;
  19. /**
  20. * @param array $array
  21. */
  22. public function array(array $array)
  23. {
  24. Assert::assertEquals([
  25. ['test', 'test'],
  26. ['test', 'test'],
  27. ], $array);
  28. }
  29. };
  30. $imported = $import->import('import.xlsx');
  31. $this->assertInstanceOf(Importer::class, $imported);
  32. }
  33. /**
  34. * @test
  35. */
  36. public function can_import_a_simple_xlsx_file_from_uploaded_file()
  37. {
  38. $import = new class implements ToArray
  39. {
  40. use Importable;
  41. /**
  42. * @param array $array
  43. */
  44. public function array(array $array)
  45. {
  46. Assert::assertEquals([
  47. ['test', 'test'],
  48. ['test', 'test'],
  49. ], $array);
  50. }
  51. };
  52. $import->import($this->givenUploadedFile(__DIR__ . '/../Data/Disks/Local/import.xlsx'));
  53. }
  54. /**
  55. * @test
  56. */
  57. public function can_import_a_simple_csv_file_with_html_tags_inside()
  58. {
  59. $import = new class implements ToArray
  60. {
  61. use Importable;
  62. /**
  63. * @param array $array
  64. */
  65. public function array(array $array)
  66. {
  67. Assert::assertEquals([
  68. ['key1', 'A', 'row1'],
  69. ['key2', 'B', '<p>row2</p>'],
  70. ['key3', 'C', 'row3'],
  71. ['key4', 'D', 'row4'],
  72. ['key5', 'E', 'row5'],
  73. ['key6', 'F', '<a href=/url-example">link</a>"'],
  74. ], $array);
  75. }
  76. };
  77. $import->import('csv-with-html-tags.csv', 'local', Excel::CSV);
  78. }
  79. /**
  80. * @test
  81. */
  82. public function can_import_a_simple_xlsx_file_with_ignore_empty_set_to_true()
  83. {
  84. config()->set('excel.imports.ignore_empty', true);
  85. $import = new class implements ToArray
  86. {
  87. use Importable;
  88. /**
  89. * @param array $array
  90. */
  91. public function array(array $array)
  92. {
  93. Assert::assertEquals([
  94. ['test', 'test'],
  95. ['test', 'test'],
  96. ], $array);
  97. }
  98. };
  99. $imported = $import->import('import-with-some-empty-rows.xlsx');
  100. $this->assertInstanceOf(Importer::class, $imported);
  101. }
  102. /**
  103. * @test
  104. */
  105. public function can_import_a_simple_xlsx_file_with_ignore_empty_set_to_false()
  106. {
  107. config()->set('excel.imports.ignore_empty', false);
  108. $import = new class implements ToArray
  109. {
  110. use Importable;
  111. /**
  112. * @param array $array
  113. */
  114. public function array(array $array)
  115. {
  116. Assert::assertEquals([
  117. ['test', 'test'],
  118. ['test', 'test'],
  119. ['', ''],
  120. ['', ''],
  121. ], $array);
  122. }
  123. };
  124. $imported = $import->import('import-with-some-empty-rows.xlsx');
  125. $this->assertInstanceOf(Importer::class, $imported);
  126. }
  127. }