Skip to content

Commit f01ec3a

Browse files
Merge pull request #5 from SoapBox/F-Adding-Test-Cases
Updating travis/composer and adding basic test cases.
2 parents 086d423 + b995109 commit f01ec3a

File tree

8 files changed

+93
-12
lines changed

8 files changed

+93
-12
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ before_script:
88
- curl -s http://getcomposer.org/installer | php
99
- php composer.phar install --dev
1010

11-
script: phpunit
11+
script: phpunit
12+

composer.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "soapbox/laravel-formatter",
33
"type": "library",
4-
"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.",
4+
"description": "A Laravel 4 formatting library that converts data output between XML, CSV, JSON, TXT, YAML and a few others. ",
55
"keywords": ["laravel", "formatter", "data","convert","csv", "xml", "yaml"],
66
"homepage": "http://github.com/SoapBox/laravel-formatter",
77
"license": "MIT",
8+
"version": "1.1",
89
"authors": [
910
{
1011
"name": "Graham McCarthy",
@@ -14,7 +15,16 @@
1415
],
1516
"require": {
1617
"php": ">=5.3.0",
17-
"illuminate/support": "4.*"
18+
"illuminate/support": ">=4.0,<4.2",
19+
"illuminate/foundation": ">=4.0,<4.2",
20+
"illuminate/config": ">=4.0,<4.2",
21+
"illuminate/session": ">=4.0,<4.2",
22+
"illuminate/filesystem": ">=4.0,<4.2",
23+
"illuminate/view": ">=4.0,<4.2"
24+
},
25+
"require-dev": {
26+
"orchestra/testbench": "2.1.*",
27+
"mockery/mockery": "dev-master"
1828
},
1929
"autoload": {
2030
"psr-0": {
@@ -23,4 +33,4 @@
2333
}
2434
},
2535
"minimum-stability": "dev"
26-
}
36+
}

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
Formatter Bundle
22
================
33

4-
A Laravel 4 Formatter Package based on the work done by @dberry37388 with FuelPHP's Formatter class.
4+
[![Build Status](https://travis-ci.org/SoapBox/laravel-formatter.svg?branch=master)](https://travis-ci.org/SoapBox/laravel-formatter)
5+
6+
A Laravel 4 Formatter Package based on the work done by @dberry37388 with FuelPHP's Formatter class.
57

68
This package will help you to easily convert between various formats such as XML, JSON, CSV, etc...
79

src/SoapBox/Formatter/Formatter.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
//namespace Formatter;
2020
namespace SoapBox\Formatter;
2121

22-
use Config, Lang;
22+
//use Config, Lang;
23+
use Illuminate\Support\Facades\Config;
24+
use Illuminate\Support\Facades\Lang;
2325

2426
/**
2527
* The Formatter Class
@@ -55,7 +57,6 @@ public static function make($data = null, $from_type = null, $attributes = array
5557
return new self($data, $from_type, $attributes);
5658
}
5759

58-
5960
/**
6061
* Should not be called directly. You should be using Formatter::make()
6162
*
@@ -369,7 +370,6 @@ protected function _from_csv($string, $attributes = array()) {
369370

370371
foreach ($rows as $row) {
371372
$data_fields = str_replace($escape.$enclosure, $enclosure, str_getcsv($row, $delimiter, $enclosure, $escape));
372-
373373
if (count($data_fields) > count($headings)) {
374374
array_push(self::$errors, Lang::get('formatter::formatter.more_data', array('line_number' => $line_number ) ));
375375
} else if (count($data_fields) < count($headings)) {
@@ -379,6 +379,10 @@ protected function _from_csv($string, $attributes = array()) {
379379
}
380380
}
381381

382+
if(empty($rows) && !empty($headings) && count($headings) > 0) {
383+
$data = $headings;
384+
}
385+
382386
return $data;
383387
}
384388

src/config/config.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
return array(
43
'csv' => array(
54
'delimiter' => ',',

src/lang/en/formatter.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
*/
1515

1616
return array(
17-
1817
'no_data' => 'No data to convert',
1918
'from_type_not_supported' => ':from_type is not a supported type to convert from.',
20-
'more_data' => 'The line :line_number contains more data fields than the heading.',
21-
'less_data' => 'The line :line_number contains less data fields than the heading.'
19+
'more_data' => 'The line :line_number contains more data fields than the heading.',
20+
'less_data' => 'The line :line_number contains less data fields than the heading.'
2221
);

tests/FormatterTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
use SoapBox\Formatter\Formatter;
4+
//use Mockery as m;
5+
6+
class FormatterTest extends Orchestra\Testbench\TestCase {
7+
8+
public function setUp() {
9+
parent::setUp();
10+
11+
}
12+
/**
13+
* A basic functional test for JSON to Array conversion
14+
*
15+
* @return void
16+
*/
17+
public function testJsonToArray() {
18+
$data = '{"foo":"bar","bar":"foo"}';
19+
$result = Formatter::make($data, 'json')->to_array();
20+
$expected = array('foo'=>'bar', 'bar'=>'foo');
21+
$this->assertEquals($expected, $result);
22+
}
23+
24+
/**
25+
* A basic functional test for Array to JSON conversion
26+
*
27+
* @return void
28+
*/
29+
public function testArrayToJson() {
30+
$data = array('foo'=>'bar', 'bar'=>'foo');
31+
$result = Formatter::make($data)->to_json();
32+
$expected = '{"foo":"bar","bar":"foo"}';
33+
$this->assertEquals($expected, $result);
34+
}
35+
36+
/**
37+
* A basic functional test for testJSONToXMLToArrayToJsonToArray data to array
38+
*
39+
* @return void
40+
*/
41+
public function testJSONToXMLToArrayToJsonToArray() {
42+
$data = '{"foo":"bar","bar":"foo"}';
43+
$result = Formatter::make($data, 'json')->to_xml();
44+
$result = Formatter::make($result, 'xml')->to_array();
45+
$result = Formatter::make($result, 'array')->to_json();
46+
$result = Formatter::make($result, 'json')->to_array();
47+
$expected = array('foo'=>'bar', 'bar'=>'foo');
48+
$this->assertEquals($expected, $result);
49+
}
50+
51+
/**
52+
* A basic functional test for CSV data to array
53+
*
54+
* @return void
55+
*/
56+
public function testCSVToArray() {
57+
$data = 'foo,bar,bing,bam,boom';
58+
$result = Formatter::make($data, 'csv')->to_array();
59+
$expected = array('foo','bar','bing','bam','boom');
60+
$this->assertEquals($expected, $result);
61+
}
62+
63+
}

tests/bootstrap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
require dirname(__DIR__) . '/src/SoapBox/Formatter/Formatter.php';

0 commit comments

Comments
 (0)