Skip to content

Commit

Permalink
CSVArray::toArray() returns array<int, array<string, string>> (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia authored Jun 29, 2023
1 parent 7958dd8 commit 9a3576b
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 64 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ See [GitHub releases](https://github.com/mll-lab/php-utils/releases).

## Unreleased

## v1.6.1

### Fixed

- `CSVArray::toArray()` returns `array<int, array<string, string>>`

## v1.6.0

### Added
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ coverage: vendor ## Collects coverage from running unit tests with phpunit

.PHONY: fix
fix: vendor
vendor/bin/php-cs-fixer fix
vendor/bin/rector process
vendor/bin/php-cs-fixer fix

.PHONY: infection
infection: vendor ## Runs mutation tests with infection
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
</source>

<testsuites>
<testsuite name="Unit">
<directory>tests/Unit</directory>
<testsuite name="Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
18 changes: 8 additions & 10 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(SetList::CODE_QUALITY);
$rectorConfig->import(SetList::PHP_71);

$rectorConfig->sets([
SetList::PHP_71,
SetList::PHP_72,
SetList::PHP_73,
SetList::PHP_74,
SetList::CODE_QUALITY,
]);
$rectorConfig->skip([
// skip csv test file to keep `\r` and `\n` for readability
JoinStringConcatRector::class => [
// single file
__DIR__ . '/tests/Unit/CSVArrayTest.php',
__DIR__ . '/tests/CSVArrayTest.php', // keep `\r\n` for readability
],
]);

// paths to refactor; solid alternative to CLI arguments
$rectorConfig->paths([__DIR__ . '/src', __DIR__ . '/tests']);

// Path to PHPStan with extensions, that PHPStan in Rector uses to determine types
$rectorConfig->phpstanConfig(__DIR__ . '/phpstan.neon');
};
4 changes: 3 additions & 1 deletion src/CSVArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ final class CSVArray
/**
* TODO: fix parsing multiline-content in csv.
*
* @return array<int, array<string, CSVPrimitive>>
* @return array<int, array<string, string>>
*/
public static function toArray(string $csv, string $delimiter = ';', string $enclosure = '"', string $escape = '\\'): array
{
/** @var array<int, array<string, string>> $result */
$result = [];

$lines = StringUtil::splitLines($csv);
Expand All @@ -35,6 +36,7 @@ public static function toArray(string $csv, string $delimiter = ';', string $enc
continue;
}

/** @var array<int, string> $entries */
$entries = str_getcsv($line, $delimiter, $enclosure, $escape);
if (count($entries) !== count($columnHeaders)) {
throw new \Exception("The number of columns in row {$index} does not match the headers in CSV: {$firstLine}");
Expand Down
108 changes: 60 additions & 48 deletions tests/Unit/CSVArrayTest.php → tests/CSVArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,74 +5,86 @@
use MLL\Utils\CSVArray;
use PHPUnit\Framework\TestCase;

class CSVArrayTest extends TestCase
/**
* @phpstan-import-type CSVPrimitive from CSVArray
*/
final class CSVArrayTest extends TestCase
{
public const ARRAY_CONTENT = [
1 => [
'Spalte1' => 'Wert11',
'Spalte2' => 'Wert21',
],
2 => [
'Spalte1' => 'Wert12',
'Spalte2' => 'Wert22',
],
];

public const CSV_CONTENT
= "Spalte1;Spalte2\r\n"
. "Wert11;Wert21\r\n"
. "Wert12;Wert22\r\n";

protected function setUp(): void
{
parent::setUp();
}

public function testToArray(): void
/** @return iterable<array{string, array<int, array<string, string>>}> */
public static function csvAndArrayStringValues(): iterable
{
self::assertSame(
self::ARRAY_CONTENT,
CSVArray::toArray(self::CSV_CONTENT)
);
yield [
"Spalte1;Spalte2\r\n"
. "Wert11;Wert21\r\n"
. "Wert12;Wert22\r\n",
[
1 => [
'Spalte1' => 'Wert11',
'Spalte2' => 'Wert21',
],
2 => [
'Spalte1' => 'Wert12',
'Spalte2' => 'Wert22',
],
],
];
yield [
"empty;int;float;bool;null\r\n"
. ";1;2.3;true;null\r\n",
[
1 => [
'empty' => '',
'int' => '1',
'float' => '2.3',
'bool' => 'true',
'null' => 'null',
],
],
];
}

public function testToCSV(): void
/**
* @dataProvider csvAndArrayStringValues
*
* @param array<int, array<string, string>> $array
*/
public function testStringValues(string $csv, array $array): void
{
self::assertSame(
self::CSV_CONTENT,
CSVArray::toCSV(self::ARRAY_CONTENT)
);
self::assertSame($array, CSVArray::toArray($csv));
self::assertSame($csv, CSVArray::toCSV($array));
}

public function testToCSVAndToArrayAreInverse(): void
public function testEscapesDelimiter(): void
{
self::assertSame(
self::ARRAY_CONTENT,
CSVArray::toArray(
CSVArray::toCSV(self::ARRAY_CONTENT)
)
);

self::assertSame(
self::CSV_CONTENT,
"foo\r\n"
. "\"bar;baz\"\r\n",
CSVArray::toCSV(
CSVArray::toArray(self::CSV_CONTENT)
[
1 => [
'foo' => 'bar;baz',
],
]
)
);
}

public function testEscapesDelimiter(): void
public function testPrimitives(): void
{
self::assertSame(
"foo\r\n"
. "\"bar;baz\"\r\n",
"empty;int;float;bool;null\r\n"
. ";1;2.3;1;\r\n",
CSVArray::toCSV(
[
1 => [
'foo' => 'bar;baz',
'empty' => '',
'int' => 1,
'float' => 2.3,
'bool' => true,
'null' => null,
],
]
)
],
),
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/NumberTest.php → tests/NumberTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

namespace MLL\Utils\Tests\Unit;
namespace MLL\Utils\Tests;

use MLL\Utils\Number;
use PHPUnit\Framework\TestCase;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/StringUtilTest.php → tests/StringUtilTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

namespace MLL\Utils\Tests\Unit;
namespace MLL\Utils\Tests;

use MLL\Utils\StringUtil;
use PHPUnit\Framework\TestCase;
Expand Down

0 comments on commit 9a3576b

Please sign in to comment.