Skip to content

Commit

Permalink
Merge pull request #5 from SoapBox/F-Adding-Test-Cases
Browse files Browse the repository at this point in the history
Updating travis/composer and adding basic test cases.
  • Loading branch information
grahammccarthy committed Mar 26, 2014
2 parents 086d423 + b995109 commit f01ec3a
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install --dev

script: phpunit
script: phpunit

16 changes: 13 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"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",
"version": "1.1",
"authors": [
{
"name": "Graham McCarthy",
Expand All @@ -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": {
Expand All @@ -23,4 +33,4 @@
}
},
"minimum-stability": "dev"
}
}
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -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...

Expand Down
10 changes: 7 additions & 3 deletions src/SoapBox/Formatter/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
*
Expand Down Expand Up @@ -369,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)) {
Expand All @@ -379,6 +379,10 @@ protected function _from_csv($string, $attributes = array()) {
}
}

if(empty($rows) && !empty($headings) && count($headings) > 0) {
$data = $headings;
}

return $data;
}

Expand Down
1 change: 0 additions & 1 deletion src/config/config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

return array(
'csv' => array(
'delimiter' => ',',
Expand Down
5 changes: 2 additions & 3 deletions src/lang/en/formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
);
63 changes: 63 additions & 0 deletions tests/FormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

use SoapBox\Formatter\Formatter;
//use Mockery as m;

class FormatterTest extends Orchestra\Testbench\TestCase {

public function setUp() {
parent::setUp();

}
/**
* 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 for Array to JSON conversion
*
* @return void
*/
public function testArrayToJson() {
$data = array('foo'=>'bar', 'bar'=>'foo');
$result = Formatter::make($data)->to_json();
$expected = '{"foo":"bar","bar":"foo"}';
$this->assertEquals($expected, $result);
}

/**
* 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);
}

/**
* 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','bing','bam','boom');
$this->assertEquals($expected, $result);
}

}
3 changes: 3 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

require dirname(__DIR__) . '/src/SoapBox/Formatter/Formatter.php';

0 comments on commit f01ec3a

Please sign in to comment.