From 9c488bc35ab2f2ad71fef40d3c83986570f2aa37 Mon Sep 17 00:00:00 2001 From: Graham McCarthy Date: Wed, 26 Mar 2014 12:15:01 -0400 Subject: [PATCH 1/4] updating travis and composer information. --- .travis.yml | 3 ++- composer.json | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index a198238..b1eb5d4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,4 +8,5 @@ before_script: - curl -s http://getcomposer.org/installer | php - php composer.phar install --dev -script: phpunit \ No newline at end of file +script: phpunit + diff --git a/composer.json b/composer.json index 02135ab..a417118 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "soapbox/laravel-formatter", "type": "library", - "description": "A Laravel 4 formatting library. Convert data output between XML/CSV/JSON/TXT/YAML/etc. The project builds on the work of Daniel Berry's Laravel 3 Formatter Bundle.", + "description": "A Laravel 4 formatting library that converts data output between XML, CSV, JSON, TXT, YAML and a few others. ", "keywords": ["laravel", "formatter", "data","convert","csv", "xml", "yaml"], "homepage": "http://github.com/SoapBox/laravel-formatter", "license": "MIT", From 080cc9fc8b62dbbaf118458e222f9e74dcd2ee11 Mon Sep 17 00:00:00 2001 From: Graham McCarthy Date: Wed, 26 Mar 2014 12:40:33 -0400 Subject: [PATCH 2/4] added a build status and am currently working through doing test cases for #3 --- readme.md | 4 +++- tests/FormatterTest.php | 27 +++++++++++++++++++++++++++ tests/bootstrap.php | 3 +++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/FormatterTest.php create mode 100644 tests/bootstrap.php diff --git a/readme.md b/readme.md index 425708c..c3991a7 100644 --- a/readme.md +++ b/readme.md @@ -1,7 +1,9 @@ Formatter Bundle ================ -A Laravel 4 Formatter Package based on the work done by @dberry37388 with FuelPHP's Formatter class. +[![Build Status](https://travis-ci.org/SoapBox/laravel-formatter.svg?branch=master)](https://travis-ci.org/SoapBox/laravel-formatter) + +A Laravel 4 Formatter Package based on the work done by @dberry37388 with FuelPHP's Formatter class. This package will help you to easily convert between various formats such as XML, JSON, CSV, etc... diff --git a/tests/FormatterTest.php b/tests/FormatterTest.php new file mode 100644 index 0000000..1828b81 --- /dev/null +++ b/tests/FormatterTest.php @@ -0,0 +1,27 @@ +assertTrue(true); + } + +/* + public function testFormatter() { + //$formatted = SoapBox\Formatter::make('hello')=>array(); + //var_dump($formatted); + + //$expected = array('hello'); + //$this->assertEquals($expected, $formatted); + } +*/ + +} \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..5e74e53 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,3 @@ + Date: Wed, 26 Mar 2014 16:31:14 -0400 Subject: [PATCH 3/4] this fixes #3 added all the necessary test cases. --- composer.json | 14 ++++- src/SoapBox/Formatter/Formatter.php | 5 +- src/lang/en/formatter.php | 5 +- tests/FormatterTest.php | 89 +++++++++++++++++++++++++---- 4 files changed, 94 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index a417118..149e924 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,7 @@ "keywords": ["laravel", "formatter", "data","convert","csv", "xml", "yaml"], "homepage": "http://github.com/SoapBox/laravel-formatter", "license": "MIT", + "version": "1.1", "authors": [ { "name": "Graham McCarthy", @@ -14,7 +15,16 @@ ], "require": { "php": ">=5.3.0", - "illuminate/support": "4.*" + "illuminate/support": ">=4.0,<4.2", + "illuminate/foundation": ">=4.0,<4.2", + "illuminate/config": ">=4.0,<4.2", + "illuminate/session": ">=4.0,<4.2", + "illuminate/filesystem": ">=4.0,<4.2", + "illuminate/view": ">=4.0,<4.2" + }, + "require-dev": { + "orchestra/testbench": "2.1.*", + "mockery/mockery": "dev-master" }, "autoload": { "psr-0": { @@ -23,4 +33,4 @@ } }, "minimum-stability": "dev" -} +} \ No newline at end of file diff --git a/src/SoapBox/Formatter/Formatter.php b/src/SoapBox/Formatter/Formatter.php index 7a8e13b..f1795d7 100644 --- a/src/SoapBox/Formatter/Formatter.php +++ b/src/SoapBox/Formatter/Formatter.php @@ -19,7 +19,9 @@ //namespace Formatter; namespace SoapBox\Formatter; -use Config, Lang; +//use Config, Lang; +use Illuminate\Support\Facades\Config; +use Illuminate\Support\Facades\Lang; /** * The Formatter Class @@ -55,7 +57,6 @@ public static function make($data = null, $from_type = null, $attributes = array return new self($data, $from_type, $attributes); } - /** * Should not be called directly. You should be using Formatter::make() * diff --git a/src/lang/en/formatter.php b/src/lang/en/formatter.php index aacd26c..8749c10 100644 --- a/src/lang/en/formatter.php +++ b/src/lang/en/formatter.php @@ -14,9 +14,8 @@ */ return array( - 'no_data' => 'No data to convert', 'from_type_not_supported' => ':from_type is not a supported type to convert from.', - 'more_data' => 'The line :line_number contains more data fields than the heading.', - 'less_data' => 'The line :line_number contains less data fields than the heading.' + 'more_data' => 'The line :line_number contains more data fields than the heading.', + 'less_data' => 'The line :line_number contains less data fields than the heading.' ); \ No newline at end of file diff --git a/tests/FormatterTest.php b/tests/FormatterTest.php index 1828b81..b4489f2 100644 --- a/tests/FormatterTest.php +++ b/tests/FormatterTest.php @@ -1,27 +1,92 @@ shouldReceive('instance')->once()->andReturn($app); + + //Illuminate\Support\Facades\Facade::setFacadeApplication($app); + //Illuminate\Support\Facades\Config::swap($config = m::mock('ConfigMock')); + //Illuminate\Support\Facades\Lang::swap($lang = m::mock('ConfigLang')); + + //$config->shouldReceive('get')->once()->with('logviewer::log_dirs')->andReturn(array('app' => 'app/storage/logs')); + //$this->logviewer = new Logviewer('app', 'cgi-fcgi', '2013-06-01'); + } + /** + * A basic functional test for JSON to Array conversion + * + * @return void + */ + public function testJsonToArray() { + $data = '{"foo":"bar","bar":"foo"}'; + $result = Formatter::make($data, 'json')->to_array(); + $expected = array('foo'=>'bar', 'bar'=>'foo'); + $this->assertEquals($expected, $result); + } /** - * A basic functional test example. + * A basic functional test for Array to JSON conversion * * @return void */ - public function testBasicExample() { - $this->assertTrue(true); + public function testArrayToJson() { + $data = array('foo'=>'bar', 'bar'=>'foo'); + + $result = Formatter::make($data)->to_json(); + $expected = '{"foo":"bar","bar":"foo"}'; + $this->assertEquals($expected, $result); } -/* - public function testFormatter() { - //$formatted = SoapBox\Formatter::make('hello')=>array(); - //var_dump($formatted); + /** + * A basic functional test for testJSONToXMLToArrayToJsonToArray data to array + * + * @return void + */ + public function testJSONToXMLToArrayToJsonToArray() { + $data = '{"foo":"bar","bar":"foo"}'; + + $result = Formatter::make($data, 'json')->to_xml(); + $result = Formatter::make($result, 'xml')->to_array(); + $result = Formatter::make($result, 'array')->to_json(); + $result = Formatter::make($result, 'json')->to_array(); + + $expected = array('foo'=>'bar', 'bar'=>'foo'); + + $this->assertEquals($expected, $result); + } - //$expected = array('hello'); - //$this->assertEquals($expected, $formatted); + /** + * A basic functional test for CSV data to array + * + * @return void + */ + public function testCSVToArray() { + $data = "foo,bar,bing,bam,boom"; + $result = Formatter::make($data, 'csv')->to_array(); + $expected = array('foo'=>'bar', 'bar'=>'foo'); + var_dump($result); + var_dump($expected); + die('dead'); + $this->assertEquals($expected, $result); } -*/ + /** + * A basic functional test for CSV data to array + * + * @return void + */ + public function testArrayToCSV() { + $expected = array('foo'=>'bar', 'bar'=>'foo'); + $result = Formatter::make($data, 'array')->to_csv(); + var_dump($result); + + $expected = "foo,bar,bing,bam,boom"; + $this->assertEquals($expected, $result); + } } \ No newline at end of file From b99510966f73b14f6edbf9e33221abfe0c0162a1 Mon Sep 17 00:00:00 2001 From: Graham McCarthy Date: Wed, 26 Mar 2014 18:52:09 -0400 Subject: [PATCH 4/4] this fixes #4 one line CSV conversions will now work. --- src/SoapBox/Formatter/Formatter.php | 5 ++++- src/config/config.php | 1 - tests/FormatterTest.php | 33 ++--------------------------- 3 files changed, 6 insertions(+), 33 deletions(-) diff --git a/src/SoapBox/Formatter/Formatter.php b/src/SoapBox/Formatter/Formatter.php index f1795d7..bfb0d0f 100644 --- a/src/SoapBox/Formatter/Formatter.php +++ b/src/SoapBox/Formatter/Formatter.php @@ -370,7 +370,6 @@ protected function _from_csv($string, $attributes = array()) { foreach ($rows as $row) { $data_fields = str_replace($escape.$enclosure, $enclosure, str_getcsv($row, $delimiter, $enclosure, $escape)); - if (count($data_fields) > count($headings)) { array_push(self::$errors, Lang::get('formatter::formatter.more_data', array('line_number' => $line_number ) )); } else if (count($data_fields) < count($headings)) { @@ -380,6 +379,10 @@ protected function _from_csv($string, $attributes = array()) { } } + if(empty($rows) && !empty($headings) && count($headings) > 0) { + $data = $headings; + } + return $data; } diff --git a/src/config/config.php b/src/config/config.php index 10ae79a..2d4f3af 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -1,5 +1,4 @@ array( 'delimiter' => ',', diff --git a/tests/FormatterTest.php b/tests/FormatterTest.php index b4489f2..4a68d0b 100644 --- a/tests/FormatterTest.php +++ b/tests/FormatterTest.php @@ -8,15 +8,6 @@ class FormatterTest extends Orchestra\Testbench\TestCase { public function setUp() { parent::setUp(); - //$app = m::mock('AppMock'); - //$app->shouldReceive('instance')->once()->andReturn($app); - - //Illuminate\Support\Facades\Facade::setFacadeApplication($app); - //Illuminate\Support\Facades\Config::swap($config = m::mock('ConfigMock')); - //Illuminate\Support\Facades\Lang::swap($lang = m::mock('ConfigLang')); - - //$config->shouldReceive('get')->once()->with('logviewer::log_dirs')->andReturn(array('app' => 'app/storage/logs')); - //$this->logviewer = new Logviewer('app', 'cgi-fcgi', '2013-06-01'); } /** * A basic functional test for JSON to Array conversion @@ -37,7 +28,6 @@ public function testJsonToArray() { */ public function testArrayToJson() { $data = array('foo'=>'bar', 'bar'=>'foo'); - $result = Formatter::make($data)->to_json(); $expected = '{"foo":"bar","bar":"foo"}'; $this->assertEquals($expected, $result); @@ -50,14 +40,11 @@ public function testArrayToJson() { */ public function testJSONToXMLToArrayToJsonToArray() { $data = '{"foo":"bar","bar":"foo"}'; - $result = Formatter::make($data, 'json')->to_xml(); $result = Formatter::make($result, 'xml')->to_array(); $result = Formatter::make($result, 'array')->to_json(); $result = Formatter::make($result, 'json')->to_array(); - $expected = array('foo'=>'bar', 'bar'=>'foo'); - $this->assertEquals($expected, $result); } @@ -67,26 +54,10 @@ public function testJSONToXMLToArrayToJsonToArray() { * @return void */ public function testCSVToArray() { - $data = "foo,bar,bing,bam,boom"; + $data = 'foo,bar,bing,bam,boom'; $result = Formatter::make($data, 'csv')->to_array(); - $expected = array('foo'=>'bar', 'bar'=>'foo'); - var_dump($result); - var_dump($expected); - die('dead'); + $expected = array('foo','bar','bing','bam','boom'); $this->assertEquals($expected, $result); } - /** - * A basic functional test for CSV data to array - * - * @return void - */ - public function testArrayToCSV() { - $expected = array('foo'=>'bar', 'bar'=>'foo'); - $result = Formatter::make($data, 'array')->to_csv(); - var_dump($result); - - $expected = "foo,bar,bing,bam,boom"; - $this->assertEquals($expected, $result); - } } \ No newline at end of file