Skip to content

Commit

Permalink
organize tests, fix error about splat operator where unpacking does n…
Browse files Browse the repository at this point in the history
…ot work with assoc. arrays
  • Loading branch information
ozdemirburak committed Oct 17, 2018
1 parent 571d35b commit a6bc412
Show file tree
Hide file tree
Showing 16 changed files with 291 additions and 196 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{
"name": "Burak Özdemir",
"email": "[email protected]",
"homepage": "https://burakozdemir.co.uk"
"homepage": "https://ozdemirburak.com"
}
],
"require": {
Expand Down
34 changes: 20 additions & 14 deletions src/File/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,29 @@ class Csv extends AbstractFile
*/
public function convert(): string
{
$data = explode("\n", $this->data);
$keys = str_getcsv(
array_shift($data),
$data = $this->parseData();
$keys = $this->parseCsv(array_shift($data));
return json_encode(array_map(function ($line) use ($keys) {
return array_combine($keys, $this->parseCsv($line));
}, $data), $this->conversion['options']);
}

private function parseCsv($line)
{
return str_getcsv(
$line,
$this->conversion['delimiter'],
$this->conversion['enclosure'],
$this->conversion['escape']
);
return json_encode(array_map(function ($line) use ($keys) {
return array_combine(
$keys,
str_getcsv(
$line,
$this->conversion['delimiter'],
$this->conversion['enclosure'],
$this->conversion['escape']
)
);
}, $data), $this->conversion['options']);
}

private function parseData()
{
$data = explode("\n", $this->data);
if (end($data) === '') {
array_pop($data);
}
return $data;
}
}
38 changes: 22 additions & 16 deletions src/File/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,10 @@ public function convert(): string
protected function toCsvString(array $data): string
{
$f = fopen('php://temp', 'wb');
fputcsv(
$f,
array_keys(current($data)),
$this->conversion['delimiter'],
$this->conversion['enclosure'],
$this->conversion['escape']
);
foreach ($data as $row) {
fputcsv(
$f,
$row,
$this->conversion['delimiter'],
$this->conversion['enclosure'],
$this->conversion['escape']
);
}
$this->putCsv($f, array_keys(current($data)));
array_walk($data, function ($row) use (&$f) {
$this->putCsv($f, $row);
});
rewind($f);
$csv = stream_get_contents($f);
fclose($f);
Expand Down Expand Up @@ -90,7 +78,25 @@ protected function flatten(array $array = [], $prefix = '', array $result = []):
*/
protected function getArrayOfNulls($flattened): array
{
$flattened = array_values($flattened);
$keys = array_keys(array_merge(...$flattened));
return array_fill_keys($keys, $this->conversion['null']);
}

/**
* @param $handle
* @param $fields
*
* @return bool|int
*/
private function putCsv($handle, $fields)
{
return fputcsv(
$handle,
$fields,
$this->conversion['delimiter'],
$this->conversion['enclosure'],
$this->conversion['escape']
);
}
}
46 changes: 20 additions & 26 deletions tests/CsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@

namespace OzdemirBurak\JsonCsv\Tests;

use OzdemirBurak\JsonCsv\File\Csv;
use OzdemirBurak\JsonCsv\Tests\Traits\TestTrait;
use PHPUnit\Framework\TestCase;

class CsvTest extends TestCase
{
use TestTrait;

/**
* @var string
*/
protected $ext = 'json';

/**
* @group csv-basic-test
*/
public function testFileReading()
{
$this->assertEquals('iris', ($csv = $this->init())->getFilename());
$this->assertEquals('iris', ($csv = $this->initCsv())->getFilename());
$this->assertContains('6.3,3.3,6.0,2.5,Iris-virginica', $csv->getData());
}

Expand All @@ -21,44 +28,31 @@ public function testFileReading()
*/
public function testSetter()
{
$conversion = $this->init()->setConversionKey('options', $options = JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
$options = JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES;
$conversion = $this->initCsv()->setConversionKey('options', $options);
$this->assertEquals($options, $conversion['options']);
}

/**
* @group csv-conversion-test
* @group csv-conversion-download-save-test
*/
public function testConversion()
public function testConversionAndDownload()
{
$this->assertContains('{"SepalLength":"6.3","SepalWidth":"3.3","PetalLength":"6.0","PetalWidth":"2.5","Name":"Iris-virginica"}', $this->init()->convert());
$this->initCsv()->convertAndDownload(null, false);
$this->expectOutputRegex('/{"SL":"6.3","SW":"3.3","PL":"6.0","PW":"2.5","Name":"Iris-virginica"}/');
}

/**
* @group csv-conversion-test
* @group csv-conversion-download-save-test
*/
public function testConversionAndSave()
{
$this->init()->convertAndSave($path = __DIR__ . '/data/iris.json');
$path = $this->path('iris', 'json');
$this->initCsv()->convertAndSave($path);
$this->assertFileExists($path);
$this->assertContains('{"SepalLength":"6.3","SepalWidth":"3.3","PetalLength":"6.0","PetalWidth":"2.5","Name":"Iris-virginica"}', file_get_contents($path));
$json = '{"SL":"6.3","SW":"3.3","PL":"6.0","PW":"2.5","Name":"Iris-virginica"}';
$this->assertContains($json, file_get_contents($path));
unlink($path);
$this->assertFileNotExists($path);
}

/**
* @group csv-conversion-test
*/
public function testConversionAndDownload()
{
$this->init()->convertAndDownload(null, false);
$this->expectOutputRegex('/{"SepalLength":"6.3","SepalWidth":"3.3","PetalLength":"6.0","PetalWidth":"2.5","Name":"Iris-virginica"}/');
}

/**
* @return \OzdemirBurak\JsonCsv\File\Csv
*/
private function init() : Csv
{
return new Csv(__DIR__ . '/data/iris.csv');
}
}
59 changes: 0 additions & 59 deletions tests/JsonComplexTest.php

This file was deleted.

48 changes: 27 additions & 21 deletions tests/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,70 @@

namespace OzdemirBurak\JsonCsv\Tests;

use OzdemirBurak\JsonCsv\File\Json;
use OzdemirBurak\JsonCsv\Tests\Traits\TestTrait;
use PHPUnit\Framework\TestCase;

class JsonTest extends TestCase
{
use TestTrait;

/**
* @var string
*/
protected $ext = 'csv';

/**
* @group json-basic-test
*/
public function testFileReading()
{
$this->assertEquals('countries', ($json = $this->init())->getFilename());
$this->assertEquals('countries', ($json = $this->initJson())->getFilename());
$this->assertContains('"common": "Turkey"', $json->getData());
}

/**
* @group json-conversion-test
*/
public function testConversion()
public function testPeople()
{
$this->assertContains("name_common,name_official,name_native,area,latlng_0,latlng_1\n", $this->init()->convert());
$this->checkConversion('people');
}

/**
* @group json-conversion-test
*/
public function testConversionAndSave()
public function testProperties()
{
$this->init()->convertAndSave($path = __DIR__ . '/data/countries.csv');
$this->assertFileExists($path);
$this->assertContains("Turkey,\"Republic of Turkey\",Türkiye,783562,39,35\n", file_get_contents($path));
unlink($path);
$this->assertFileNotExists($path);
$this->checkConversion('properties');
}

/**
* @group json-conversion-test
*/
public function testConversionAndDownload()
public function testStats()
{
$this->init()->convertAndDownload(null, false);
$this->expectOutputRegex('/Turkey,"Republic of Turkey",Türkiye,783562,39,35\\n/');
$this->checkConversion('stats');
}

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

/**
* @return \OzdemirBurak\JsonCsv\File\Json
* @group json-conversion-download-save-test
*/
private function init() : Json
public function testConversionAndSave()
{
return new Json(__DIR__ . '/data/countries.json');
$path = $this->path('iris', 'countries');
$this->initJson()->convertAndSave($path);
$this->assertFileExists($path);
$this->assertContains("Turkey,\"Republic of Turkey\",Türkiye,783562,39,35\n", file_get_contents($path));
unlink($path);
$this->assertFileNotExists($path);
}
}
49 changes: 49 additions & 0 deletions tests/Traits/TestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace OzdemirBurak\JsonCsv\Tests\Traits;

use OzdemirBurak\JsonCsv\File\Csv;
use OzdemirBurak\JsonCsv\File\Json;

trait TestTrait
{
/**
* @param string $file
*/
private function checkConversion($file)
{
$method = 'init' . str_replace('Test', '', substr(strrchr(\get_class($this), '\\'), 1));
$this->assertStringEqualsFile($this->path($file, $this->ext), $this->$method($file)->convert());
}

/**
* @param $file
* @param string $extension
*
* @return string
*/
private function path($file, $extension = 'csv'): string
{
return __DIR__ . '/../data/' . $file . '.' . $extension;
}

/**
* @param string $file
*
* @return \OzdemirBurak\JsonCsv\File\Csv
*/
private function initCsv($file = 'iris'): Csv
{
return new Csv($this->path($file, 'csv'));
}

/**
* @param string $file
*
* @return \OzdemirBurak\JsonCsv\File\Json
*/
private function initJson($file = 'countries'): Json
{
return new Json($this->path($file, 'json'));
}
}
7 changes: 0 additions & 7 deletions tests/data/complex.csv

This file was deleted.

Loading

0 comments on commit a6bc412

Please sign in to comment.