Merge remote-tracking branch 'origin/1.6' into 1.6

This commit is contained in:
Matias Griese
2019-03-13 15:21:18 +02:00
2 changed files with 50 additions and 0 deletions

View File

@@ -43,6 +43,9 @@ class CsvFormatter extends AbstractFormatter
*/
public function encode($data, $delimiter = null): string
{
if (count($data) === 0) {
return '';
}
$delimiter = $delimiter ?? $this->getDelimiter();
$header = array_keys(reset($data));

View File

@@ -0,0 +1,47 @@
<?php
use Grav\Framework\File\Formatter\CsvFormatter;
class CsvFormatterTest extends \Codeception\TestCase\Test
{
public function testEncodeWithAssocColumns()
{
$data = [
['col1' => 1, 'col2' => 2, 'col3' => 3],
['col1' => 'aaa', 'col2' => 'bbb', 'col3' => 'ccc'],
];
$encoded = (new CsvFormatter())->encode($data);
$lines = array_filter(explode(PHP_EOL, $encoded));
self::assertCount(3, $lines);
self::assertEquals('col1,col2,col3', $lines[0]);
}
/**
* TBD - If indexes are all numeric, what's the purpose
* of displaying header
*/
public function testEncodeWithIndexColumns()
{
$data = [
[0 => 1, 1 => 2, 2 => 3],
];
$encoded = (new CsvFormatter())->encode($data);
$lines = array_filter(explode(PHP_EOL, $encoded));
self::assertCount(2, $lines);
self::assertEquals('0,1,2', $lines[0]);
}
public function testEncodeEmptyData()
{
$encoded = (new CsvFormatter())->encode([]);
self::assertEquals('', $encoded);
}
}