Skip to content

Commit

Permalink
Merge pull request #5 from byjg/5.0
Browse files Browse the repository at this point in the history
Fix CSVFormatter.php
  • Loading branch information
byjg authored Dec 3, 2024
2 parents 348e980 + 86ffaa3 commit 5de1efa
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
"ByJG\\AnyDataset\\Text\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"prefer-stable": true,
"minimum-stability": "dev",
"require": {
Expand All @@ -14,7 +19,8 @@
},
"require-dev": {
"phpunit/phpunit": "^9.6",
"vimeo/psalm": "^6.0"
"vimeo/psalm": "^6.0",
"byjg/anydataset-array": "^5.0"
},
"provide": {
"byjg/anydataset-implementation": "1.0"
Expand Down
4 changes: 3 additions & 1 deletion src/Formatter/CSVFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,16 @@ protected function anydatasetRaw($iterator)
protected function rowRaw($row)
{
$line = "";
$first = true;
foreach ($row as $value) {
$value = str_replace($this->quote, $this->quote . $this->quote, $value);
$quoteStr =
($this->getApplyQuote() == self::APPLY_QUOTE_ALWAYS)
|| ($this->getApplyQuote() == self::APPLY_QUOTE_WHEN_REQUIRED && (!is_numeric($value) && (strpos($value, $this->quote) !== false || strpos($value, $this->delimiter) !== false)))
|| ($this->getApplyQuote() == self::APPLY_QUOTE_ALL_STRINGS && !is_numeric($value))
? $this->quote : "";
$line .= (!empty($line) ? $this->delimiter : "") . $quoteStr . $value . $quoteStr;
$line .= ($first ? "" : $this->delimiter) . $quoteStr . $value . $quoteStr;
$first = false;
}
return $line . "\n";
}
Expand Down
8 changes: 3 additions & 5 deletions tests/FixedTextFileDatasetTest.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
<?php

namespace Tests\AnyDataset\Dataset;
namespace Tests;

use ByJG\AnyDataset\Core\AnyDataset;
use ByJG\AnyDataset\Core\Exception\IteratorException;
use ByJG\AnyDataset\Core\Row;
use ByJG\AnyDataset\Text\Definition\TextTypeEnum;
use ByJG\AnyDataset\Text\FixedTextFileDataset;
use ByJG\AnyDataset\Text\Definition\FixedTextDefinition;
use ByJG\AnyDataset\Text\Definition\TextTypeEnum;
use ByJG\AnyDataset\Text\Exception\MalformedException;
use ByJG\AnyDataset\Text\FixedTextFileDataset;
use ByJG\AnyDataset\Text\Formatter\FixedSizeColumnFormatter;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use SebastianBergmann\CodeCoverage\Report\Text;

class FixedTextFileDatasetTest extends TestCase
{
Expand Down
35 changes: 27 additions & 8 deletions tests/TextFileDatasetTest.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<?php

namespace Tests\AnyDataset\Dataset;
namespace Tests;

use ByJG\AnyDataset\Core\AnyDataset;
use ByJG\AnyDataset\Core\GenericIterator;
use ByJG\AnyDataset\Core\IteratorInterface;
use ByJG\AnyDataset\Core\Row;
use ByJG\AnyDataset\Lists\ArrayDataset;
use ByJG\AnyDataset\Text\Formatter\CSVFormatter;
use ByJG\AnyDataset\Text\TextFileDataset;
use PHPUnit\Framework\TestCase;

class TextFileDatasetTest extends TestCase
{

protected static $fieldNames;
protected static $fileName_Unix = "";
protected static $fileName_Windows = "";
protected static $fileName_MacClassic = "";
protected static $fileName_BlankLine = "";
protected static $firstline_Header = "";
protected static array $fieldNames;
protected static string $fileName_Unix = "";
protected static string $fileName_Windows = "";
protected static string $fileName_MacClassic = "";
protected static string $fileName_BlankLine = "";
protected static string $firstline_Header = "";

const REMOTEURL = "https://opensource-test-resources.web.app/%s";

Expand Down Expand Up @@ -410,4 +410,23 @@ public function assertRowCount(GenericIterator $it, $qty)

$this->assertEquals($qty, count($result));
}

public function testCsvFromArrayDataSet()
{
$data = [
["field1" => 1, "field2" => "STRING1", "field3" => "VALUE1"],
["field1" => 2, "field2" => "STRING2", "field3" => "VALUE2"],
["field1" => 3, "field2" => "STRING3", "field3" => "VALUE3"],
];

$anydataset = new ArrayDataset($data);
$formatter = new CSVFormatter($anydataset->getIterator());

$text = "__id,__key,field1,field2,field3\n" .
"0,0,1,STRING1,VALUE1\n" .
"1,1,2,STRING2,VALUE2\n" .
"2,2,3,STRING3,VALUE3\n";

$this->assertEquals($text, $formatter->toText());
}
}

0 comments on commit 5de1efa

Please sign in to comment.