WithCustomCsvSettingsTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace Maatwebsite\Excel\Tests\Concerns;
  3. use Illuminate\Support\Collection;
  4. use Maatwebsite\Excel\Concerns\FromCollection;
  5. use Maatwebsite\Excel\Concerns\ToArray;
  6. use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;
  7. use Maatwebsite\Excel\Excel;
  8. use Maatwebsite\Excel\HeadingRowImport;
  9. use Maatwebsite\Excel\Tests\TestCase;
  10. use PHPUnit\Framework\Assert;
  11. class WithCustomCsvSettingsTest extends TestCase
  12. {
  13. /**
  14. * @var Excel
  15. */
  16. protected $SUT;
  17. protected function setUp(): void
  18. {
  19. parent::setUp();
  20. $this->SUT = $this->app->make(Excel::class);
  21. }
  22. /**
  23. * @test
  24. */
  25. public function can_store_csv_export_with_custom_settings()
  26. {
  27. $export = new class implements FromCollection, WithCustomCsvSettings
  28. {
  29. /**
  30. * @return Collection
  31. */
  32. public function collection()
  33. {
  34. return collect([
  35. ['A1', 'B1'],
  36. ['A2', 'B2'],
  37. ]);
  38. }
  39. /**
  40. * @return array
  41. */
  42. public function getCsvSettings(): array
  43. {
  44. return [
  45. 'delimiter' => ';',
  46. 'enclosure' => '',
  47. 'line_ending' => PHP_EOL,
  48. 'use_bom' => true,
  49. 'include_separator_line' => true,
  50. 'excel_compatibility' => false,
  51. 'output_encoding' => '',
  52. 'test_auto_detect' => false,
  53. ];
  54. }
  55. };
  56. $this->SUT->store($export, 'custom-csv.csv');
  57. $contents = file_get_contents(__DIR__ . '/../Data/Disks/Local/custom-csv.csv');
  58. $this->assertStringContains('sep=;', $contents);
  59. $this->assertStringContains('A1;B1', $contents);
  60. $this->assertStringContains('A2;B2', $contents);
  61. }
  62. /**
  63. * @test
  64. */
  65. public function can_store_csv_export_with_custom_encoding()
  66. {
  67. $export = new class implements FromCollection, WithCustomCsvSettings
  68. {
  69. /**
  70. * @return Collection
  71. */
  72. public function collection()
  73. {
  74. return collect([
  75. ['A1', '€ŠšŽžŒœŸ'],
  76. ['A2', 'åßàèòìù'],
  77. ]);
  78. }
  79. /**
  80. * @return array
  81. */
  82. public function getCsvSettings(): array
  83. {
  84. return [
  85. 'delimiter' => ';',
  86. 'enclosure' => '',
  87. 'line_ending' => PHP_EOL,
  88. 'use_bom' => false,
  89. 'include_separator_line' => true,
  90. 'excel_compatibility' => false,
  91. 'output_encoding' => 'ISO-8859-15',
  92. ];
  93. }
  94. };
  95. $this->SUT->store($export, 'custom-csv-iso.csv');
  96. $contents = file_get_contents(__DIR__ . '/../Data/Disks/Local/custom-csv-iso.csv');
  97. Assert::assertEquals('ISO-8859-15', mb_detect_encoding($contents, 'ISO-8859-15', true));
  98. Assert::assertFalse(mb_detect_encoding($contents, 'UTF-8', true));
  99. $contents = mb_convert_encoding($contents, 'UTF-8', 'ISO-8859-15');
  100. $this->assertStringContains('sep=;', $contents);
  101. $this->assertStringContains('A1;€ŠšŽžŒœŸ', $contents);
  102. $this->assertStringContains('A2;åßàèòìù', $contents);
  103. }
  104. /**
  105. * @test
  106. */
  107. public function can_read_csv_with_auto_detecting_delimiter_semicolon()
  108. {
  109. $this->assertEquals([
  110. [
  111. ['a1', 'b1'],
  112. ],
  113. ], (new HeadingRowImport())->toArray('csv-with-other-delimiter.csv'));
  114. }
  115. /**
  116. * @test
  117. */
  118. public function can_read_csv_with_auto_detecting_delimiter_comma()
  119. {
  120. $this->assertEquals([
  121. [
  122. ['a1', 'b1'],
  123. ],
  124. ], (new HeadingRowImport())->toArray('csv-with-comma.csv'));
  125. }
  126. /**
  127. * @test
  128. */
  129. public function can_read_csv_import_with_custom_settings()
  130. {
  131. $import = new class implements WithCustomCsvSettings, ToArray
  132. {
  133. /**
  134. * @return array
  135. */
  136. public function getCsvSettings(): array
  137. {
  138. return [
  139. 'delimiter' => ';',
  140. 'enclosure' => '',
  141. 'escape_character' => '\\',
  142. 'contiguous' => true,
  143. 'input_encoding' => 'UTF-8',
  144. ];
  145. }
  146. /**
  147. * @param array $array
  148. */
  149. public function array(array $array)
  150. {
  151. Assert::assertEquals([
  152. ['A1', 'B1'],
  153. ['A2', 'B2'],
  154. ], $array);
  155. }
  156. };
  157. $this->SUT->import($import, 'csv-with-other-delimiter.csv');
  158. }
  159. /**
  160. * @test
  161. */
  162. public function cannot_read_with_wrong_delimiter()
  163. {
  164. $import = new class implements WithCustomCsvSettings, ToArray
  165. {
  166. /**
  167. * @return array
  168. */
  169. public function getCsvSettings(): array
  170. {
  171. return [
  172. 'delimiter' => ',',
  173. ];
  174. }
  175. /**
  176. * @param array $array
  177. */
  178. public function array(array $array)
  179. {
  180. Assert::assertEquals([
  181. ['A1;B1'],
  182. ['A2;B2'],
  183. ], $array);
  184. }
  185. };
  186. $this->SUT->import($import, 'csv-with-other-delimiter.csv');
  187. }
  188. }