From 9b497e00873afb3f4536c1715832401d32b88e88 Mon Sep 17 00:00:00 2001 From: Leandro Ross Date: Wed, 31 Jan 2018 16:19:34 -0200 Subject: [PATCH] Added delimiter to CSV --- readme.md | 1 + src/Formatter.php | 9 +- src/Parsers/CsvParser.php | 7 +- tests/unit/FormatterTest.php | 76 +++++++------ tests/unit/Parsers/ArrayParserTest.php | 101 ++++++++++-------- tests/unit/Parsers/CsvParserDelemiterTest.php | 41 +++++++ tests/unit/Parsers/CsvParserTest.php | 51 +++++---- tests/unit/Parsers/JsonParserTest.php | 49 +++++---- tests/unit/Parsers/XmlParserTest.php | 36 ++++--- tests/unit/Parsers/YamlParserTest.php | 29 ++--- 10 files changed, 240 insertions(+), 160 deletions(-) create mode 100644 tests/unit/Parsers/CsvParserDelemiterTest.php diff --git a/readme.md b/readme.md index 01741fc..3d13aeb 100644 --- a/readme.md +++ b/readme.md @@ -9,6 +9,7 @@ Changelog - JSON parse fix (Instead of only converting the first level to array, use the associative array parameter with true, so all levels will be decoded to array structure) - Add support for laravel 5 - add package discovery for laravel 5 +- add support delimiter to a csv Formatter Bundle ================ diff --git a/src/Formatter.php b/src/Formatter.php index 5e47b79..d28210c 100644 --- a/src/Formatter.php +++ b/src/Formatter.php @@ -29,17 +29,18 @@ class Formatter /** * Make: Returns an instance of formatter initialized with data and type * - * @param mixed $data The data that formatter should parse - * @param string $type The type of data formatter is expected to parse + * @param mixed $data The data that formatter should parse + * @param string $type The type of data formatter is expected to parse + * @param string $delimiter The delimitation of data formatter to csv * @return Formatter */ - public static function make($data, $type) + public static function make($data, $type, $delimiter = null) { if (in_array($type, self::$supportedTypes)) { $parser = null; switch ($type) { case self::CSV: - $parser = new CsvParser($data); + $parser = new CsvParser($data, $delimiter); break; case self::JSON: $parser = new JsonParser($data); diff --git a/src/Parsers/CsvParser.php b/src/Parsers/CsvParser.php index 40248c1..3dbd236 100644 --- a/src/Parsers/CsvParser.php +++ b/src/Parsers/CsvParser.php @@ -6,13 +6,16 @@ class CsvParser extends Parser { - private $csv; - public function __construct($data) + public function __construct($data, $delimiter = null) { if (is_string($data)) { $this->csv = Reader::createFromString($data); + if ($delimiter) { + $this->csv->setDelimiter($delimiter); + } + $this->csv->setEnclosure('|'); } else { throw new InvalidArgumentException( 'CsvParser only accepts (string) [csv] for $data.' diff --git a/tests/unit/FormatterTest.php b/tests/unit/FormatterTest.php index d1e96a5..88e5948 100644 --- a/tests/unit/FormatterTest.php +++ b/tests/unit/FormatterTest.php @@ -2,53 +2,61 @@ use SoapBox\Formatter\Formatter; -class FormatterTest extends TestCase { +class FormatterTest extends TestCase +{ - public function testFormatterProvidesCsvConstant() { - $expected = 'csv'; - $actual = Formatter::CSV; + public function testFormatterProvidesCsvConstant() + { + $expected = 'csv'; + $actual = Formatter::CSV; - $this->assertEquals($expected, $actual); - } + $this->assertEquals($expected, $actual); + } - public function testFormatterProvidesJsonConstant() { - $expected = 'json'; - $actual = Formatter::JSON; + public function testFormatterProvidesJsonConstant() + { + $expected = 'json'; + $actual = Formatter::JSON; - $this->assertEquals($expected, $actual); - } + $this->assertEquals($expected, $actual); + } - public function testFormatterProvidesXmlConstant() { - $expected = 'xml'; - $actual = Formatter::XML; + public function testFormatterProvidesXmlConstant() + { + $expected = 'xml'; + $actual = Formatter::XML; - $this->assertEquals($expected, $actual); - } + $this->assertEquals($expected, $actual); + } - public function testFormatterProvidesArrayConstant() { - $expected = 'array'; - $actual = Formatter::ARR; + public function testFormatterProvidesArrayConstant() + { + $expected = 'array'; + $actual = Formatter::ARR; - $this->assertEquals($expected, $actual); - } + $this->assertEquals($expected, $actual); + } - public function testFormatterProvidesYamlConstant() { - $expected = 'yaml'; - $actual = Formatter::YAML; + public function testFormatterProvidesYamlConstant() + { + $expected = 'yaml'; + $actual = Formatter::YAML; - $this->assertEquals($expected, $actual); - } + $this->assertEquals($expected, $actual); + } /** * @expectedException InvalidArgumentException */ - public function testFormatterMakeThrowsInvalidTypeException() { - $formatter = Formatter::make('', 'blue'); - } - - public function testFormatterMakeReturnsInstanceOfFormatter() { - $formatter = Formatter::make('', Formatter::CSV); - $this->assertTrue($formatter instanceof Formatter); - } + public function testFormatterMakeThrowsInvalidTypeException() + { + $formatter = Formatter::make('', 'blue'); + } + + public function testFormatterMakeReturnsInstanceOfFormatter() + { + $formatter = Formatter::make('', Formatter::CSV); + $this->assertTrue($formatter instanceof Formatter); + } } diff --git a/tests/unit/Parsers/ArrayParserTest.php b/tests/unit/Parsers/ArrayParserTest.php index d18c0b3..ebe609e 100644 --- a/tests/unit/Parsers/ArrayParserTest.php +++ b/tests/unit/Parsers/ArrayParserTest.php @@ -1,58 +1,67 @@ assertTrue($parser instanceof Parser); - } + public function testArrayParserIsInstanceOfParserInterface() + { + $parser = new ArrayParser(new \stdClass); + $this->assertTrue($parser instanceof Parser); + } - public function testConstructorAcceptsSerializedArray() { - $expected = [0, 1, 2]; - $parser = new ArrayParser(serialize($expected)); - $this->assertEquals($expected, $parser->toArray()); - } + public function testConstructorAcceptsSerializedArray() + { + $expected = [0, 1, 2]; + $parser = new ArrayParser(serialize($expected)); + $this->assertEquals($expected, $parser->toArray()); + } - public function testConstructorAcceptsObject() { - $expected = ['foo' => 'bar']; - $input = new stdClass; - $input->foo = 'bar'; - $parser = new ArrayParser($input); - $this->assertEquals($expected, $parser->toArray()); - } + public function testConstructorAcceptsObject() + { + $expected = ['foo' => 'bar']; + $input = new stdClass; + $input->foo = 'bar'; + $parser = new ArrayParser($input); + $this->assertEquals($expected, $parser->toArray()); + } /** * @expectedException InvalidArgumentException */ - public function testArrayParserThrowsExceptionWithInvalidInputOfEmptyString() { - $parser = new ArrayParser(''); - } - - public function testtoArrayReturnsArray() { - $parser = new ArrayParser(serialize([0, 1, 2])); - $this->assertTrue(is_array($parser->toArray())); - } - - public function testtoJsonReturnsJsonRepresentationOfArray() { - $expected = '[0,1,2]'; - $parser = new ArrayParser([0, 1, 2]); - $this->assertEquals($expected, $parser->toJson()); - } - - public function testtoJsonReturnsJsonRepresentationOfNamedArray() { - $expected = '{"foo":"bar"}'; - $parser = new ArrayParser(['foo' => 'bar']); - $this->assertEquals($expected, $parser->toJson()); - } - - public function testtoCSVFromArrayContainingContentWithCommasWorks() { - $expected = "\"0\",\"1\",\"2\",\"3\"\n\"a\",\"b\",\"c,e\",\"d\""; - $parser = new ArrayParser(['a','b','c,e','d']); - $this->assertEquals($expected, $parser->toCsv()); - } + public function testArrayParserThrowsExceptionWithInvalidInputOfEmptyString() + { + $parser = new ArrayParser(''); + } + + public function testtoArrayReturnsArray() + { + $parser = new ArrayParser(serialize([0, 1, 2])); + $this->assertTrue(is_array($parser->toArray())); + } + + public function testtoJsonReturnsJsonRepresentationOfArray() + { + $expected = '[0,1,2]'; + $parser = new ArrayParser([0, 1, 2]); + $this->assertEquals($expected, $parser->toJson()); + } + + public function testtoJsonReturnsJsonRepresentationOfNamedArray() + { + $expected = '{"foo":"bar"}'; + $parser = new ArrayParser(['foo' => 'bar']); + $this->assertEquals($expected, $parser->toJson()); + } + + public function testtoCSVFromArrayContainingContentWithCommasWorks() + { + $expected = "\"0\",\"1\",\"2\",\"3\"\n\"a\",\"b\",\"c,e\",\"d\""; + $parser = new ArrayParser(['a', 'b', 'c,e', 'd']); + $this->assertEquals($expected, $parser->toCsv()); + } } diff --git a/tests/unit/Parsers/CsvParserDelemiterTest.php b/tests/unit/Parsers/CsvParserDelemiterTest.php new file mode 100644 index 0000000..87d11bd --- /dev/null +++ b/tests/unit/Parsers/CsvParserDelemiterTest.php @@ -0,0 +1,41 @@ +assertTrue($parser instanceof Parser); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testConstructorThrowsInvalidExecptionWhenArrayDataIsProvided() + { + $parser = new CsvParser([0, 1, 3], ';'); + } + + public function testtoArrayReturnsCsvArrayRepresentation() + { + $expected = [['foo' => 'bar', 'boo' => 'far']]; + $parser = new CsvParser($this->simpleCsv, ';'); + $this->assertEquals($expected, $parser->toArray()); + } + + public function testtoJsonReturnsJsonRepresentationOfNamedArray() + { + $expected = '[{"foo":"bar","boo":"far"}]'; + $parser = new CsvParser($this->simpleCsv, ';'); + $this->assertEquals($expected, $parser->toJson()); + } + +} diff --git a/tests/unit/Parsers/CsvParserTest.php b/tests/unit/Parsers/CsvParserTest.php index 39547c0..246416e 100644 --- a/tests/unit/Parsers/CsvParserTest.php +++ b/tests/unit/Parsers/CsvParserTest.php @@ -1,36 +1,41 @@ assertTrue($parser instanceof Parser); - } + public function testCsvParserIsInstanceOfParserInterface() + { + $parser = new CsvParser(''); + $this->assertTrue($parser instanceof Parser); + } /** * @expectedException InvalidArgumentException */ - public function testConstructorThrowsInvalidExecptionWhenArrayDataIsProvided() { - $parser = new CsvParser([0, 1, 3]); - } - - public function testtoArrayReturnsCsvArrayRepresentation() { - $expected = [['foo' => 'bar', 'boo' => 'far']]; - $parser = new CsvParser($this->simpleCsv); - $this->assertEquals($expected, $parser->toArray()); - } - - public function testtoJsonReturnsJsonRepresentationOfNamedArray() { - $expected = '[{"foo":"bar","boo":"far"}]'; - $parser = new CsvParser($this->simpleCsv); - $this->assertEquals($expected, $parser->toJson()); - } + public function testConstructorThrowsInvalidExecptionWhenArrayDataIsProvided() + { + $parser = new CsvParser([0, 1, 3]); + } + + public function testtoArrayReturnsCsvArrayRepresentation() + { + $expected = [['foo' => 'bar', 'boo' => 'far']]; + $parser = new CsvParser($this->simpleCsv); + $this->assertEquals($expected, $parser->toArray()); + } + + public function testtoJsonReturnsJsonRepresentationOfNamedArray() + { + $expected = '[{"foo":"bar","boo":"far"}]'; + $parser = new CsvParser($this->simpleCsv); + $this->assertEquals($expected, $parser->toJson()); + } } diff --git a/tests/unit/Parsers/JsonParserTest.php b/tests/unit/Parsers/JsonParserTest.php index 9bca418..17ebba7 100644 --- a/tests/unit/Parsers/JsonParserTest.php +++ b/tests/unit/Parsers/JsonParserTest.php @@ -1,31 +1,36 @@ assertTrue($parser instanceof Parser); - } + public function testJsonParserIsInstanceOfParserInterface() + { + $parser = new JsonParser(''); + $this->assertTrue($parser instanceof Parser); + } - public function testtoArrayReturnsArrayRepresentationOfJsonObject() { - $expected = ['foo' => 'bar']; - $parser = new JsonParser('{"foo": "bar"}'); - $this->assertEquals($expected, $parser->toArray()); - } + public function testtoArrayReturnsArrayRepresentationOfJsonObject() + { + $expected = ['foo' => 'bar']; + $parser = new JsonParser('{"foo": "bar"}'); + $this->assertEquals($expected, $parser->toArray()); + } - public function testtoJsonReturnsArrayRepresentationOfArray() { - $expected = '[0,1,2]'; - $parser = new JsonParser($expected); - $this->assertEquals($expected, $parser->toJson()); - } + public function testtoJsonReturnsArrayRepresentationOfArray() + { + $expected = '[0,1,2]'; + $parser = new JsonParser($expected); + $this->assertEquals($expected, $parser->toJson()); + } - public function testtoJsonReturnsJsonRepresentationOfNamedArray() { - $expected = '{"foo":"bar"}'; - $parser = new JsonParser($expected); - $this->assertEquals($expected, $parser->toJson()); - } + public function testtoJsonReturnsJsonRepresentationOfNamedArray() + { + $expected = '{"foo":"bar"}'; + $parser = new JsonParser($expected); + $this->assertEquals($expected, $parser->toJson()); + } } diff --git a/tests/unit/Parsers/XmlParserTest.php b/tests/unit/Parsers/XmlParserTest.php index 614be06..7f1b93b 100644 --- a/tests/unit/Parsers/XmlParserTest.php +++ b/tests/unit/Parsers/XmlParserTest.php @@ -1,25 +1,29 @@ assertTrue($parser instanceof Parser); - } + public function testXmlParserIsInstanceOfParserInterface() + { + $parser = new XmlParser(''); + $this->assertTrue($parser instanceof Parser); + } - public function testtoArrayReturnsArrayRepresenationOfXmlObject() { - $expected = ['foo' => 'bar']; - $parser = new XmlParser('bar'); - $this->assertEquals($expected, $parser->toArray()); - } + public function testtoArrayReturnsArrayRepresenationOfXmlObject() + { + $expected = ['foo' => 'bar']; + $parser = new XmlParser('bar'); + $this->assertEquals($expected, $parser->toArray()); + } - public function testtoJsonReturnsJsonRepresentationOfXmlObject() { - $expected = '{"foo":"bar"}'; - $parser = new XmlParser('bar'); - $this->assertEquals($expected, $parser->toJson()); - } + public function testtoJsonReturnsJsonRepresentationOfXmlObject() + { + $expected = '{"foo":"bar"}'; + $parser = new XmlParser('bar'); + $this->assertEquals($expected, $parser->toJson()); + } } diff --git a/tests/unit/Parsers/YamlParserTest.php b/tests/unit/Parsers/YamlParserTest.php index bf0edac..e25c53c 100644 --- a/tests/unit/Parsers/YamlParserTest.php +++ b/tests/unit/Parsers/YamlParserTest.php @@ -1,22 +1,25 @@ assertTrue($parser instanceof Parser); - } + public function testYamlParserIsInstanceOfParserInterface() + { + $parser = new YamlParser(''); + $this->assertTrue($parser instanceof Parser); + } - public function testtoArrayReturnsArrayRepresenationOfYamlObject() { - $expected = ['foo' => 'bar']; - $parser = new XmlParser('bar'); - $x = new YamlParser($parser->toYaml()); - $this->assertEquals($expected, $x->toArray()); - } + public function testtoArrayReturnsArrayRepresenationOfYamlObject() + { + $expected = ['foo' => 'bar']; + $parser = new XmlParser('bar'); + $x = new YamlParser($parser->toYaml()); + $this->assertEquals($expected, $x->toArray()); + } }