Skip to content

Commit

Permalink
bug fix issue #287 (#288)
Browse files Browse the repository at this point in the history
* bug fix issue #287
  • Loading branch information
nyamsprod authored Mar 26, 2018
1 parent 0d0b12f commit fa94758
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 14 deletions.
25 changes: 20 additions & 5 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
build:
nodes:
analysis:
project_setup:
override: true
tests:
override:
- php-scrutinizer-run --enable-security-analysis
filter:
paths: [src/*]
excluded_paths: [tests/*]
paths:
- src/
excluded_paths:
- tests/
checks:
php:
code_rating: true
duplication: true
tools:
external_code_coverage:
timeout: 600
runs: 2
php_code_coverage: false
php_loc:
enabled: true
excluded_dirs: [tests, vendor]
php_cpd:
excluded_dirs:
- tests/
- vendor/
php_cpd: false
php_sim:
enabled: true
excluded_dirs: [tests, vendor]
filter:
paths: ['src/']
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ install:

script:
- composer phpunit
- if [ "$VALIDATE_CODING_STYLE" == "true" ]; then composer phpcs; fi
- if [ "$RUN_PHPSTAN" == "true" ]; then composer phpstan; fi

after_script:
- if [ "$COLLECT_COVERAGE" == "true" ]; then wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover build/clover.xml; fi
- if [ "$VALIDATE_CODING_STYLE" == "true" ]; then composer phpcs; fi
- if [ "$RUN_PHPSTAN" == "true" ]; then composer phpstan; fi
12 changes: 6 additions & 6 deletions src/CharsetConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,17 @@ public function __invoke(array $record): array
/**
* Walker method to convert the offset and the value of a CSV record field
*
* @param string|null $value
* @param string|int $offset
* @param mixed $value
* @param mixed $offset
*/
protected function encodeField(&$value, &$offset)
{
if (null !== $value) {
$value = mb_convert_encoding($value, $this->output_encoding, $this->input_encoding);
if (null !== $value && !is_numeric($value)) {
$value = mb_convert_encoding((string) $value, $this->output_encoding, $this->input_encoding);
}

if (!is_int($offset)) {
$offset = mb_convert_encoding($offset, $this->output_encoding, $this->input_encoding);
if (!is_numeric($offset)) {
$offset = mb_convert_encoding((string) $offset, $this->output_encoding, $this->input_encoding);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function insertOne(array $record): int
$record = array_reduce($this->formatters, [$this, 'formatRecord'], $record);
$this->validateRecord($record);
$bytes = $this->document->fputcsv($record, $this->delimiter, $this->enclosure, $this->escape);
if (false !== $bytes && null !== $bytes) {
if ('' !== (string) $bytes) {
return $bytes + $this->consolidate();
}

Expand Down
39 changes: 39 additions & 0 deletions tests/CharsetConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,43 @@ public function testOnCreateFailedWithWrongParams()
$converter->filtername = CharsetConverter::FILTERNAME.'.foo/bar';
$this->assertFalse($converter->onCreate());
}

/**
* @covers ::convert
* @covers ::encodeField
*
* @dataProvider converterProvider
* @param array $record
* @param array $expected
*/
public function testConvertOnlyStringField(array $record, array $expected)
{
$converter = (new CharsetConverter())
->inputEncoding('iso-8859-15')
->outputEncoding('utf-8');
$res = $converter->convert([$record]);
$this->assertSame($expected, $res[0]);
}

public function converterProvider()
{
return [
'only numeric values' => [
'record' => [1, 2, 3],
'expected' => [1, 2, 3],
],
'only string values' => [
'record' => ['1', '2', '3'],
'expected' => ['1', '2', '3'],
],
'mixed values' => [
'record' => [1, '2', 3],
'expected' => [1, '2', 3],
],
'mixed offset' => [
'record' => [1 => 1, '2' => '2', 3 => 3],
'expected' => [1 => 1, '2' => '2', 3 => 3],
],
];
}
}

0 comments on commit fa94758

Please sign in to comment.