Skip to content

Commit a6bc412

Browse files
committed
organize tests, fix error about splat operator where unpacking does not work with assoc. arrays
1 parent 571d35b commit a6bc412

16 files changed

+291
-196
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{
99
"name": "Burak Özdemir",
1010
"email": "[email protected]",
11-
"homepage": "https://burakozdemir.co.uk"
11+
"homepage": "https://ozdemirburak.com"
1212
}
1313
],
1414
"require": {

src/File/Csv.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,29 @@ class Csv extends AbstractFile
2323
*/
2424
public function convert(): string
2525
{
26-
$data = explode("\n", $this->data);
27-
$keys = str_getcsv(
28-
array_shift($data),
26+
$data = $this->parseData();
27+
$keys = $this->parseCsv(array_shift($data));
28+
return json_encode(array_map(function ($line) use ($keys) {
29+
return array_combine($keys, $this->parseCsv($line));
30+
}, $data), $this->conversion['options']);
31+
}
32+
33+
private function parseCsv($line)
34+
{
35+
return str_getcsv(
36+
$line,
2937
$this->conversion['delimiter'],
3038
$this->conversion['enclosure'],
3139
$this->conversion['escape']
3240
);
33-
return json_encode(array_map(function ($line) use ($keys) {
34-
return array_combine(
35-
$keys,
36-
str_getcsv(
37-
$line,
38-
$this->conversion['delimiter'],
39-
$this->conversion['enclosure'],
40-
$this->conversion['escape']
41-
)
42-
);
43-
}, $data), $this->conversion['options']);
41+
}
42+
43+
private function parseData()
44+
{
45+
$data = explode("\n", $this->data);
46+
if (end($data) === '') {
47+
array_pop($data);
48+
}
49+
return $data;
4450
}
4551
}

src/File/Json.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,10 @@ public function convert(): string
4242
protected function toCsvString(array $data): string
4343
{
4444
$f = fopen('php://temp', 'wb');
45-
fputcsv(
46-
$f,
47-
array_keys(current($data)),
48-
$this->conversion['delimiter'],
49-
$this->conversion['enclosure'],
50-
$this->conversion['escape']
51-
);
52-
foreach ($data as $row) {
53-
fputcsv(
54-
$f,
55-
$row,
56-
$this->conversion['delimiter'],
57-
$this->conversion['enclosure'],
58-
$this->conversion['escape']
59-
);
60-
}
45+
$this->putCsv($f, array_keys(current($data)));
46+
array_walk($data, function ($row) use (&$f) {
47+
$this->putCsv($f, $row);
48+
});
6149
rewind($f);
6250
$csv = stream_get_contents($f);
6351
fclose($f);
@@ -90,7 +78,25 @@ protected function flatten(array $array = [], $prefix = '', array $result = []):
9078
*/
9179
protected function getArrayOfNulls($flattened): array
9280
{
81+
$flattened = array_values($flattened);
9382
$keys = array_keys(array_merge(...$flattened));
9483
return array_fill_keys($keys, $this->conversion['null']);
9584
}
85+
86+
/**
87+
* @param $handle
88+
* @param $fields
89+
*
90+
* @return bool|int
91+
*/
92+
private function putCsv($handle, $fields)
93+
{
94+
return fputcsv(
95+
$handle,
96+
$fields,
97+
$this->conversion['delimiter'],
98+
$this->conversion['enclosure'],
99+
$this->conversion['escape']
100+
);
101+
}
96102
}

tests/CsvTest.php

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22

33
namespace OzdemirBurak\JsonCsv\Tests;
44

5-
use OzdemirBurak\JsonCsv\File\Csv;
5+
use OzdemirBurak\JsonCsv\Tests\Traits\TestTrait;
66
use PHPUnit\Framework\TestCase;
77

88
class CsvTest extends TestCase
99
{
10+
use TestTrait;
11+
12+
/**
13+
* @var string
14+
*/
15+
protected $ext = 'json';
16+
1017
/**
1118
* @group csv-basic-test
1219
*/
1320
public function testFileReading()
1421
{
15-
$this->assertEquals('iris', ($csv = $this->init())->getFilename());
22+
$this->assertEquals('iris', ($csv = $this->initCsv())->getFilename());
1623
$this->assertContains('6.3,3.3,6.0,2.5,Iris-virginica', $csv->getData());
1724
}
1825

@@ -21,44 +28,31 @@ public function testFileReading()
2128
*/
2229
public function testSetter()
2330
{
24-
$conversion = $this->init()->setConversionKey('options', $options = JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
31+
$options = JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES;
32+
$conversion = $this->initCsv()->setConversionKey('options', $options);
2533
$this->assertEquals($options, $conversion['options']);
2634
}
2735

2836
/**
29-
* @group csv-conversion-test
37+
* @group csv-conversion-download-save-test
3038
*/
31-
public function testConversion()
39+
public function testConversionAndDownload()
3240
{
33-
$this->assertContains('{"SepalLength":"6.3","SepalWidth":"3.3","PetalLength":"6.0","PetalWidth":"2.5","Name":"Iris-virginica"}', $this->init()->convert());
41+
$this->initCsv()->convertAndDownload(null, false);
42+
$this->expectOutputRegex('/{"SL":"6.3","SW":"3.3","PL":"6.0","PW":"2.5","Name":"Iris-virginica"}/');
3443
}
3544

3645
/**
37-
* @group csv-conversion-test
46+
* @group csv-conversion-download-save-test
3847
*/
3948
public function testConversionAndSave()
4049
{
41-
$this->init()->convertAndSave($path = __DIR__ . '/data/iris.json');
50+
$path = $this->path('iris', 'json');
51+
$this->initCsv()->convertAndSave($path);
4252
$this->assertFileExists($path);
43-
$this->assertContains('{"SepalLength":"6.3","SepalWidth":"3.3","PetalLength":"6.0","PetalWidth":"2.5","Name":"Iris-virginica"}', file_get_contents($path));
53+
$json = '{"SL":"6.3","SW":"3.3","PL":"6.0","PW":"2.5","Name":"Iris-virginica"}';
54+
$this->assertContains($json, file_get_contents($path));
4455
unlink($path);
4556
$this->assertFileNotExists($path);
4657
}
47-
48-
/**
49-
* @group csv-conversion-test
50-
*/
51-
public function testConversionAndDownload()
52-
{
53-
$this->init()->convertAndDownload(null, false);
54-
$this->expectOutputRegex('/{"SepalLength":"6.3","SepalWidth":"3.3","PetalLength":"6.0","PetalWidth":"2.5","Name":"Iris-virginica"}/');
55-
}
56-
57-
/**
58-
* @return \OzdemirBurak\JsonCsv\File\Csv
59-
*/
60-
private function init() : Csv
61-
{
62-
return new Csv(__DIR__ . '/data/iris.csv');
63-
}
6458
}

tests/JsonComplexTest.php

Lines changed: 0 additions & 59 deletions
This file was deleted.

tests/JsonTest.php

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,70 @@
22

33
namespace OzdemirBurak\JsonCsv\Tests;
44

5-
use OzdemirBurak\JsonCsv\File\Json;
5+
use OzdemirBurak\JsonCsv\Tests\Traits\TestTrait;
66
use PHPUnit\Framework\TestCase;
77

88
class JsonTest extends TestCase
99
{
10+
use TestTrait;
11+
12+
/**
13+
* @var string
14+
*/
15+
protected $ext = 'csv';
16+
1017
/**
1118
* @group json-basic-test
1219
*/
1320
public function testFileReading()
1421
{
15-
$this->assertEquals('countries', ($json = $this->init())->getFilename());
22+
$this->assertEquals('countries', ($json = $this->initJson())->getFilename());
1623
$this->assertContains('"common": "Turkey"', $json->getData());
1724
}
1825

1926
/**
2027
* @group json-conversion-test
2128
*/
22-
public function testConversion()
29+
public function testPeople()
2330
{
24-
$this->assertContains("name_common,name_official,name_native,area,latlng_0,latlng_1\n", $this->init()->convert());
31+
$this->checkConversion('people');
2532
}
2633

2734
/**
2835
* @group json-conversion-test
2936
*/
30-
public function testConversionAndSave()
37+
public function testProperties()
3138
{
32-
$this->init()->convertAndSave($path = __DIR__ . '/data/countries.csv');
33-
$this->assertFileExists($path);
34-
$this->assertContains("Turkey,\"Republic of Turkey\",Türkiye,783562,39,35\n", file_get_contents($path));
35-
unlink($path);
36-
$this->assertFileNotExists($path);
39+
$this->checkConversion('properties');
3740
}
3841

3942
/**
4043
* @group json-conversion-test
4144
*/
42-
public function testConversionAndDownload()
45+
public function testStats()
4346
{
44-
$this->init()->convertAndDownload(null, false);
45-
$this->expectOutputRegex('/Turkey,"Republic of Turkey",Türkiye,783562,39,35\\n/');
47+
$this->checkConversion('stats');
4648
}
4749

4850
/**
49-
* @group json-conversion-test
51+
* @group json-conversion-download-save-test
5052
*/
51-
public function testMixedProperties()
53+
public function testConversionAndDownload()
5254
{
53-
$json = new Json(__DIR__ . '/data/mixed-properties.json');
54-
$result = $json->convert();
55-
$this->assertStringEqualsFile(__DIR__ . '/data/mixed-properties.csv', $result);
55+
$this->initJson()->convertAndDownload(null, false);
56+
$this->expectOutputRegex('/Turkey,"Republic of Turkey",Türkiye,783562,39,35\\n/');
5657
}
5758

5859
/**
59-
* @return \OzdemirBurak\JsonCsv\File\Json
60+
* @group json-conversion-download-save-test
6061
*/
61-
private function init() : Json
62+
public function testConversionAndSave()
6263
{
63-
return new Json(__DIR__ . '/data/countries.json');
64+
$path = $this->path('iris', 'countries');
65+
$this->initJson()->convertAndSave($path);
66+
$this->assertFileExists($path);
67+
$this->assertContains("Turkey,\"Republic of Turkey\",Türkiye,783562,39,35\n", file_get_contents($path));
68+
unlink($path);
69+
$this->assertFileNotExists($path);
6470
}
6571
}

tests/Traits/TestTrait.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace OzdemirBurak\JsonCsv\Tests\Traits;
4+
5+
use OzdemirBurak\JsonCsv\File\Csv;
6+
use OzdemirBurak\JsonCsv\File\Json;
7+
8+
trait TestTrait
9+
{
10+
/**
11+
* @param string $file
12+
*/
13+
private function checkConversion($file)
14+
{
15+
$method = 'init' . str_replace('Test', '', substr(strrchr(\get_class($this), '\\'), 1));
16+
$this->assertStringEqualsFile($this->path($file, $this->ext), $this->$method($file)->convert());
17+
}
18+
19+
/**
20+
* @param $file
21+
* @param string $extension
22+
*
23+
* @return string
24+
*/
25+
private function path($file, $extension = 'csv'): string
26+
{
27+
return __DIR__ . '/../data/' . $file . '.' . $extension;
28+
}
29+
30+
/**
31+
* @param string $file
32+
*
33+
* @return \OzdemirBurak\JsonCsv\File\Csv
34+
*/
35+
private function initCsv($file = 'iris'): Csv
36+
{
37+
return new Csv($this->path($file, 'csv'));
38+
}
39+
40+
/**
41+
* @param string $file
42+
*
43+
* @return \OzdemirBurak\JsonCsv\File\Json
44+
*/
45+
private function initJson($file = 'countries'): Json
46+
{
47+
return new Json($this->path($file, 'json'));
48+
}
49+
}

tests/data/complex.csv

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)