Skip to content

Commit 8dc51a8

Browse files
committed
Add tests
1 parent 85a7765 commit 8dc51a8

File tree

8 files changed

+126
-3
lines changed

8 files changed

+126
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ composer.phar
66
# composer.lock
77
.idea
88
.vscode
9+
.phpunit.cache

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "",
44
"type": "library",
55
"require": {
6-
"php": "^8.1"
6+
"php": "^8.0"
77
},
88
"keywords": [
99
"dsv",

src/DSV/DSV.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
class DSV
66
{
7-
87
/**
98
* @var non-empty-string
109
*/

src/DSV/DSVWriter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function write(array $data, string $mode = 'wb'): void
1717
{
1818
$file = fopen($this->outputFile, $mode);
1919
$size = count($data);
20+
$lastRowNumber = $size - 1;
2021

2122
/**
2223
* @var int $rowNumber
@@ -26,7 +27,7 @@ public function write(array $data, string $mode = 'wb'): void
2627
$newRow = implode($this->delimiter, $row);
2728

2829
// the record separator shouldn't be added to the end of the file:
29-
if ($rowNumber !== $size - 1) {
30+
if ($rowNumber !== $lastRowNumber) {
3031
$newRow .= $this->recordSeparator;
3132
}
3233

tests/DSV/.gitkeep

Whitespace-only changes.

tests/DSV/DSVReaderTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace DSV;
4+
5+
use Exception;
6+
use PHPUnit\Framework\Attributes\CoversClass;
7+
use PHPUnit\Framework\TestCase;
8+
9+
#[CoversClass(DSVReader::class)]
10+
class DSVReaderTest extends TestCase
11+
{
12+
/**
13+
* @throws Exception
14+
*/
15+
public function testRead(): void
16+
{
17+
$mockStr = "NameCommentAliceShe said, \"Hello\" and waved.BobThis is a multi-line" .
18+
"comment\r\nspanning two lines.CharlieMore fun with\ntwo lines.DianaHow about some" .
19+
"UTF-8: café, naïve, résumé. 📝Edwardアップル";
20+
21+
$tempFile = tempnam(sys_get_temp_dir(), 'dsv-test-data');
22+
$handle = fopen($tempFile, "w");
23+
fwrite($handle, $mockStr);
24+
fclose($handle);
25+
26+
$totalFileLines = 6;
27+
28+
$reader = new DSVReader($tempFile);
29+
$result = $reader->read();
30+
$this->assertEquals(count($result), $totalFileLines);
31+
unlink($tempFile);
32+
}
33+
34+
/**
35+
* @throws Exception
36+
*/
37+
public function testException(): void
38+
{
39+
$this->expectException(Exception::class);
40+
$reader = new DSVReader(random_int(0, 999999) . '.dsv');
41+
$reader->read();
42+
}
43+
}

tests/DSV/DSVTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace DSV;
4+
5+
use PHPUnit\Framework\Attributes\CoversClass;
6+
use PHPUnit\Framework\TestCase;
7+
8+
#[CoversClass(DSV::class)]
9+
class DSVTest extends TestCase
10+
{
11+
public function testDelimiter(): void
12+
{
13+
$dsv = new DSV();
14+
$this->assertEquals("\x1F", $dsv->delimiter);
15+
}
16+
17+
public function testSeparator(): void
18+
{
19+
$dsv = new DSV();
20+
$this->assertEquals("\x1E", $dsv->recordSeparator);
21+
}
22+
}

tests/DSV/DSVWriterTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace DSV;
4+
5+
use PHPUnit\Framework\Attributes\CoversClass;
6+
use PHPUnit\Framework\Attributes\DataProvider;
7+
use PHPUnit\Framework\TestCase;
8+
9+
#[CoversClass(DSVWriter::class)]
10+
class DSVWriterTest extends TestCase
11+
{
12+
/**
13+
* @return array[]
14+
*/
15+
public static function dataProvider(): array
16+
{
17+
return [
18+
[
19+
[
20+
['alpha', "fun with\ntwo lines"],
21+
['beta', 'café, résumé 📝'],
22+
['gamma', 'アップル']
23+
], "alphafun with\ntwo linesbetacafé, résumé 📝gammaアップル"
24+
],
25+
[
26+
[
27+
['alpha']
28+
], "alpha"
29+
],
30+
[
31+
[
32+
['alpha', 'beta']
33+
], "alphabeta"
34+
],
35+
[
36+
[
37+
['']
38+
], ''
39+
],
40+
[
41+
[
42+
[]
43+
], ''
44+
]
45+
];
46+
}
47+
48+
#[DataProvider('dataProvider')]
49+
public function testWrite(array $data, string $result): void
50+
{
51+
$file = tempnam(sys_get_temp_dir(), 'dsv-test-data');
52+
$writer = new DSVWriter($file);
53+
$writer->write($data);
54+
$this->assertEquals($result, file_get_contents($file));
55+
unlink($file);
56+
}
57+
}

0 commit comments

Comments
 (0)