Skip to content

Commit

Permalink
Merge pull request #3 from cfv1000/feature/version-bump
Browse files Browse the repository at this point in the history
feature/version-bump
  • Loading branch information
Flaviu Chelaru committed Jul 12, 2022
2 parents 3c51a17 + 65aeaae commit 26339af
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ composer.phar
/vendor/
.idea
composer.lock
.phpunit
.phpunit.result.cache

# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
Expand Down
11 changes: 10 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@
"pause or resume via tell and seek"
],
"require": {
"php": "~7.0"
"php": "~7.1 || ~8.0"
},
"autoload": {
"psr-4": {
"cfv1000\\CsvReader\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"cfv1000\\CsvReader\\Tests\\": "tests/"
}
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"mikey179/vfsstream": "^1.6"
}
}
23 changes: 23 additions & 0 deletions phpunit.dist.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">

<testsuites>
<testsuite name="CSV Reader Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory>src</directory>
</include>
<report>
<clover outputFile="./.phpunit/clover.xml"/>
<html outputDirectory="./.phpunit/coverage" lowUpperBound="35" highLowerBound="70"/>
</report>
</coverage>
</phpunit>
6 changes: 3 additions & 3 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function valid(): bool

public function next()
{
++$this->key;
$this->file->next();
}

/**
Expand All @@ -92,12 +92,12 @@ public function current(): array
*/
public function key(): int
{
return $this->key;
return $this->file->key();
}

public function rewind()
{
$this->key = 0;
$this->file->rewind();
}

/**
Expand Down
121 changes: 121 additions & 0 deletions tests/ReaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace cfv1000\CsvReader\Tests;

use cfv1000\CsvReader\Reader;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;

class ReaderTest extends TestCase
{
public function setUp(): void
{
$this->root = vfsStream::setup('data', 0, [
'csv' => [
'file01.csv' => implode(PHP_EOL, [
'A1,B1,C1',
'A2,B2,C2',
'A3,B3,C3',
'A4,B4,C4',
'A5,B5,C5',
]),
'file02.csv' => implode(PHP_EOL, [
'A1|B1|C1',
'A2|B2|C2',
'A3|B3|C3',
'A4|B4|C4',
'A5|B5|C5',
])
]
]);
}

/**
* Configurations on column separators
*/
public function columnLineSeparatorDataProvider(): array
{
return [
'initial file' => [
'file' => '/csv/file01.csv',
'column separator' => ',',
],
'pipe column separator' => [
'file' => '/csv/file02.csv',
'column separator' => '|'
]
];
}

/**
* @return void
* @dataProvider columnLineSeparatorDataProvider
*/
public function testReader(string $file, string $columnSeparator): void
{
$reader = new Reader($this->root->url() . $file, $columnSeparator);
$data = $reader->current();
$this->assertSame(['A1', 'B1', 'C1'], $data);
$this->assertSame(1, $reader->key());
}

/**
* @return void
* @dataProvider columnLineSeparatorDataProvider
*/
public function testKey(string $file, string $columnSeparator): void
{
$reader = new Reader($this->root->url() . $file, $columnSeparator);
$reader->next();
$reader->next();

$this->assertSame(3, $reader->key());
}

/**
* @return void
*/
public function testCount(): void
{
$reader = new Reader($this->root->url() . '/csv/file01.csv');
$this->assertSame(4, $reader->count());
}

/**
* @return void
* @dataProvider columnLineSeparatorDataProvider
*/
public function testRewind(string $file, string $columnSeparator): void
{
$reader = new Reader($this->root->url() . $file, $columnSeparator);
$this->assertSame(['A1', 'B1', 'C1'], $reader->current());
$this->assertSame(['A2', 'B2', 'C2'], $reader->current());
$reader->rewind();
$this->assertSame(['A1', 'B1', 'C1'], $reader->current());

}

/**
* @return void
*/
public function testTell(): void
{
$reader = new Reader($this->root->url() . '/csv/file01.csv');
$this->assertSame(0, $reader->tell());
$reader->current();
$this->assertSame(10, $reader->tell());
$reader->current();