123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- namespace Maatwebsite\Excel\Tests\Concerns;
- use Illuminate\Support\Collection;
- use Maatwebsite\Excel\Concerns\Exportable;
- use Maatwebsite\Excel\Concerns\FromCollection;
- use Maatwebsite\Excel\Concerns\WithHeadings;
- use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
- use Maatwebsite\Excel\Tests\TestCase;
- class WithStrictNullComparisonTest extends TestCase
- {
- /**
- * @test
- */
- public function exported_zero_values_are_not_null_when_exporting_with_strict_null_comparison()
- {
- $export = new class implements FromCollection, WithHeadings, WithStrictNullComparison
- {
- use Exportable;
- /**
- * @return Collection
- */
- public function collection()
- {
- return collect([
- ['string', '0', 0, 0.0, 'string'],
- ]);
- }
- /**
- * @return array
- */
- public function headings(): array
- {
- return ['string', '0', 0, 0.0, 'string'];
- }
- };
- $response = $export->store('with-strict-null-comparison-store.xlsx');
- $this->assertTrue($response);
- $actual = $this->readAsArray(__DIR__ . '/../Data/Disks/Local/with-strict-null-comparison-store.xlsx', 'Xlsx');
- $expected = [
- ['string', 0.0, 0.0, 0.0, 'string'],
- ['string', 0.0, 0.0, 0.0, 'string'],
- ];
- $this->assertEquals($expected, $actual);
- }
- /**
- * @test
- */
- public function exported_zero_values_are_null_when_not_exporting_with_strict_null_comparison()
- {
- $export = new class implements FromCollection, WithHeadings
- {
- use Exportable;
- /**
- * @return Collection
- */
- public function collection()
- {
- return collect([
- ['string', 0, 0.0, 'string'],
- ]);
- }
- /**
- * @return array
- */
- public function headings(): array
- {
- return ['string', 0, 0.0, 'string'];
- }
- };
- $response = $export->store('without-strict-null-comparison-store.xlsx');
- $this->assertTrue($response);
- $actual = $this->readAsArray(__DIR__ . '/../Data/Disks/Local/without-strict-null-comparison-store.xlsx', 'Xlsx');
- $expected = [
- ['string', null, null, 'string'],
- ['string', null, null, 'string'],
- ];
- $this->assertEquals($expected, $actual);
- }
- /**
- * @test
- */
- public function exports_trailing_empty_cells()
- {
- $export = new class implements FromCollection, WithStrictNullComparison
- {
- use Exportable;
- /**
- * @return Collection
- */
- public function collection()
- {
- return collect([
- ['a1', '', '', 'd1', ''],
- ['a2', '', '', 'd2', ''],
- ]);
- }
- };
- $response = $export->store('empty-cells.csv');
- $this->assertTrue($response);
- $file = __DIR__ . '/../Data/Disks/Local/empty-cells.csv';
- $actual = $this->readAsArray($file, 'Csv');
- $expected = [
- ['a1', null, null, 'd1'],
- ['a2', null, null, 'd2'],
- ];
- $this->assertEquals($expected, $actual);
- $contents = file_get_contents($file);
- $this->assertStringContains('"a1","","","d1",""', $contents);
- $this->assertStringContains('"a2","","","d2",""', $contents);
- }
- /**
- * @test
- */
- public function exports_trailing_empty_cells_by_setting_config_strict_null_comparison()
- {
- config()->set('excel.exports.strict_null_comparison', false);
- $export = new class implements FromCollection
- {
- use Exportable;
- /**
- * @return Collection
- */
- public function collection()
- {
- return collect([
- ['a1', '', '', 'd1', ''],
- ['a2', '', '', 'd2', ''],
- ]);
- }
- };
- $file = __DIR__ . '/../Data/Disks/Local/empty-cells-config.csv';
- $export->store('empty-cells-config.csv');
- $contents = file_get_contents($file);
- $this->assertStringContains('"a1","","","d1"', $contents);
- config()->set('excel.exports.strict_null_comparison', true);
- $export->store('empty-cells-config.csv');
- $contents = file_get_contents($file);
- $this->assertStringContains('"a1","","","d1",""', $contents);
- }
- }
|