-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4076 from oleibman/pr1415
CSV Writer Allow Varying Number of Columns
- Loading branch information
Showing
4 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
tests/PhpSpreadsheetTests/Writer/Csv/VariableColumnsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests\Writer\Csv; | ||
|
||
use PhpOffice\PhpSpreadsheet\Shared\File; | ||
use PhpOffice\PhpSpreadsheet\Spreadsheet; | ||
use PhpOffice\PhpSpreadsheet\Writer\Csv; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class VariableColumnsTest extends TestCase | ||
{ | ||
public function testVariableColumns(): void | ||
{ | ||
$spreadsheet = new Spreadsheet(); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
$sheet->fromArray( | ||
[ | ||
[1, 2, 3, 4], | ||
[1, 2], | ||
[1, 2, 3, 4, 5], | ||
[], | ||
[1], | ||
[1, 2, 3], | ||
] | ||
); | ||
|
||
$filename = File::temporaryFilename(); | ||
$writer = new Csv($spreadsheet); | ||
$writer->setVariableColumns(true); | ||
$writer->save($filename); | ||
|
||
$contents = (string) file_get_contents($filename); | ||
unlink($filename); | ||
$spreadsheet->disconnectWorksheets(); | ||
|
||
$rows = explode(PHP_EOL, $contents); | ||
|
||
self::assertSame('"1","2","3","4"', $rows[0]); | ||
self::assertSame('"1","2"', $rows[1]); | ||
self::assertSame('"1","2","3","4","5"', $rows[2]); | ||
self::assertSame('', $rows[3]); | ||
self::assertSame('"1"', $rows[4]); | ||
self::assertSame('"1","2","3"', $rows[5]); | ||
} | ||
|
||
public function testFixedColumns(): void | ||
{ | ||
$spreadsheet = new Spreadsheet(); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
$sheet->fromArray( | ||
[ | ||
[1, 2, 3, 4], | ||
[1, 2], | ||
[1, 2, 3, 4, 5], | ||
[], | ||
[1], | ||
[1, 2, 3], | ||
] | ||
); | ||
|
||
$filename = File::temporaryFilename(); | ||
$writer = new Csv($spreadsheet); | ||
self::assertFalse($writer->getVariableColumns()); | ||
$writer->save($filename); | ||
|
||
$contents = (string) file_get_contents($filename); | ||
unlink($filename); | ||
$spreadsheet->disconnectWorksheets(); | ||
|
||
$rows = explode(PHP_EOL, $contents); | ||
|
||
self::assertSame('"1","2","3","4",""', $rows[0]); | ||
self::assertSame('"1","2","","",""', $rows[1]); | ||
self::assertSame('"1","2","3","4","5"', $rows[2]); | ||
self::assertSame('"","","","",""', $rows[3]); | ||
self::assertSame('"1","","","",""', $rows[4]); | ||
self::assertSame('"1","2","3","",""', $rows[5]); | ||
} | ||
} |