WithStrictNullComparisonTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\WithHeadings;
  7. use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
  8. use Maatwebsite\Excel\Tests\TestCase;
  9. class WithStrictNullComparisonTest extends TestCase
  10. {
  11. /**
  12. * @test
  13. */
  14. public function exported_zero_values_are_not_null_when_exporting_with_strict_null_comparison()
  15. {
  16. $export = new class implements FromCollection, WithHeadings, WithStrictNullComparison
  17. {
  18. use Exportable;
  19. /**
  20. * @return Collection
  21. */
  22. public function collection()
  23. {
  24. return collect([
  25. ['string', '0', 0, 0.0, 'string'],
  26. ]);
  27. }
  28. /**
  29. * @return array
  30. */
  31. public function headings(): array
  32. {
  33. return ['string', '0', 0, 0.0, 'string'];
  34. }
  35. };
  36. $response = $export->store('with-strict-null-comparison-store.xlsx');
  37. $this->assertTrue($response);
  38. $actual = $this->readAsArray(__DIR__ . '/../Data/Disks/Local/with-strict-null-comparison-store.xlsx', 'Xlsx');
  39. $expected = [
  40. ['string', 0.0, 0.0, 0.0, 'string'],
  41. ['string', 0.0, 0.0, 0.0, 'string'],
  42. ];
  43. $this->assertEquals($expected, $actual);
  44. }
  45. /**
  46. * @test
  47. */
  48. public function exported_zero_values_are_null_when_not_exporting_with_strict_null_comparison()
  49. {
  50. $export = new class implements FromCollection, WithHeadings
  51. {
  52. use Exportable;
  53. /**
  54. * @return Collection
  55. */
  56. public function collection()
  57. {
  58. return collect([
  59. ['string', 0, 0.0, 'string'],
  60. ]);
  61. }
  62. /**
  63. * @return array
  64. */
  65. public function headings(): array
  66. {
  67. return ['string', 0, 0.0, 'string'];
  68. }
  69. };
  70. $response = $export->store('without-strict-null-comparison-store.xlsx');
  71. $this->assertTrue($response);
  72. $actual = $this->readAsArray(__DIR__ . '/../Data/Disks/Local/without-strict-null-comparison-store.xlsx', 'Xlsx');
  73. $expected = [
  74. ['string', null, null, 'string'],
  75. ['string', null, null, 'string'],
  76. ];
  77. $this->assertEquals($expected, $actual);
  78. }
  79. /**
  80. * @test
  81. */
  82. public function exports_trailing_empty_cells()
  83. {
  84. $export = new class implements FromCollection, WithStrictNullComparison
  85. {
  86. use Exportable;
  87. /**
  88. * @return Collection
  89. */
  90. public function collection()
  91. {
  92. return collect([
  93. ['a1', '', '', 'd1', ''],
  94. ['a2', '', '', 'd2', ''],
  95. ]);
  96. }
  97. };
  98. $response = $export->store('empty-cells.csv');
  99. $this->assertTrue($response);
  100. $file = __DIR__ . '/../Data/Disks/Local/empty-cells.csv';
  101. $actual = $this->readAsArray($file, 'Csv');
  102. $expected = [
  103. ['a1', null, null, 'd1'],
  104. ['a2', null, null, 'd2'],
  105. ];
  106. $this->assertEquals($expected, $actual);
  107. $contents = file_get_contents($file);
  108. $this->assertStringContains('"a1","","","d1",""', $contents);
  109. $this->assertStringContains('"a2","","","d2",""', $contents);
  110. }
  111. /**
  112. * @test
  113. */
  114. public function exports_trailing_empty_cells_by_setting_config_strict_null_comparison()
  115. {
  116. config()->set('excel.exports.strict_null_comparison', false);
  117. $export = new class implements FromCollection
  118. {
  119. use Exportable;
  120. /**
  121. * @return Collection
  122. */
  123. public function collection()
  124. {
  125. return collect([
  126. ['a1', '', '', 'd1', ''],
  127. ['a2', '', '', 'd2', ''],
  128. ]);
  129. }
  130. };
  131. $file = __DIR__ . '/../Data/Disks/Local/empty-cells-config.csv';
  132. $export->store('empty-cells-config.csv');
  133. $contents = file_get_contents($file);
  134. $this->assertStringContains('"a1","","","d1"', $contents);
  135. config()->set('excel.exports.strict_null_comparison', true);
  136. $export->store('empty-cells-config.csv');
  137. $contents = file_get_contents($file);
  138. $this->assertStringContains('"a1","","","d1",""', $contents);
  139. }
  140. }