Completed
Pull Request — master (#3)
by Harry
05:31
created

JsonFormatterTest::formatTestData()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 100
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 100
rs 8.2857
cc 1
eloc 63
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Test\Unit\Format\Formatter;
15
16
use DateTime;
17
use Graze\DataFile\Format\Formatter\JsonFormatter;
18
use Graze\DataFile\Format\JsonFormat;
19
use Graze\DataFile\Format\JsonFormatInterface;
20
use Graze\DataFile\Test\TestCase;
21
22
class JsonFormatterTest extends TestCase
23
{
24
    /**
25
     * @dataProvider formatTestData
26
     *
27
     * @param JsonFormatInterface $format
28
     * @param array               $row
29
     * @param string              $expected
30
     * @param string              $start
31
     * @param string              $separator
32
     * @param string              $end
33
     */
34
    public function testFormat(JsonFormatInterface $format, array $row, $expected, $start, $separator, $end)
35
    {
36
        $formatter = new JsonFormatter($format);
37
38
        static::assertEquals($expected, $formatter->format($row));
39
        static::assertEquals($start, $formatter->getInitialBlock());
40
        static::assertEquals($separator, $formatter->getRowSeparator());
41
        static::assertEquals($end, $formatter->getClosingBlock());
42
    }
43
44
    /**
45
     * @return array
46
     */
47
    public function formatTestData()
48
    {
49
        $date = new DateTime();
50
        return [
51
            [
52
                new JsonFormat(),
53
                [
54
                    'key'    => 'value',
55
                    'int'    => 1,
56
                    'float'  => 2.5,
57
                    'bool'   => true,
58
                    'null'   => null,
59
                    'array'  => ['a', 'b', 'c'],
60
                    'object' => (object) ['a' => 1],
61
                    'date'   => $date,
62
                ],
63
                '{"key":"value","int":1,"float":2.5,"bool":true,"null":null,"array":["a","b","c"],"object":{"a":1},"date":"' . $date->format('Y-m-d H:i:s') . '"}',
64
                '[',
65
                ",\n",
66
                "]\n",
67
            ],
68
            [
69
                new JsonFormat([
70
                    JsonFormat::OPTION_ENCODE_OPTIONS => JSON_PRETTY_PRINT,
71
                    JsonFormat::OPTION_FILE_TYPE      => JsonFormat::JSON_FILE_TYPE_SINGLE_BLOCK,
72
                ]),
73
                [
74
                    'key'    => 'value',
75
                    'int'    => 1,
76
                    'float'  => 2.5,
77
                    'bool'   => true,
78
                    'null'   => null,
79
                    'array'  => ['a', 'b', 'c'],
80
                    'object' => (object) ['a' => 1],
81
                    'date'   => $date,
82
                ],
83
                <<<JSON
84
{
85
    "key": "value",
86
    "int": 1,
87
    "float": 2.5,
88
    "bool": true,
89
    "null": null,
90
    "array": [
91
        "a",
92
        "b",
93
        "c"
94
    ],
95
    "object": {
96
        "a": 1
97
    },
98
    "date": "{$date->format('Y-m-d H:i:s')}"
99
}
100
JSON
101
                ,
102
                '[',
103
                ",\n",
104
                "]\n",
105
            ],
106
            [
107
                new JsonFormat([
108
                    JsonFormat::OPTION_FILE_TYPE => JsonFormat::JSON_FILE_TYPE_EACH_LINE,
109
                ]),
110
                [
111
                    'key'    => 'value',
112
                    'int'    => 1,
113
                    'float'  => 2.5,
114
                    'bool'   => true,
115
                    'null'   => null,
116
                    'array'  => ['a', 'b', 'c'],
117
                    'object' => (object) ['a' => 1],
118
                    'date'   => $date,
119
                ],
120
                '{"key":"value","int":1,"float":2.5,"bool":true,"null":null,"array":["a","b","c"],"object":{"a":1},"date":"' . $date->format('Y-m-d H:i:s') . '"}',
121
                '',
122
                "\n",
123
                '',
124
            ],
125
            [
126
                new JsonFormat([
127
                    JsonFormat::OPTION_FILE_TYPE      => JsonFormat::JSON_FILE_TYPE_EACH_LINE,
128
                    JsonFormat::OPTION_ENCODE_OPTIONS => JSON_PRETTY_PRINT,
129
                ]),
130
                [
131
                    'key'    => 'value',
132
                    'int'    => 1,
133
                    'float'  => 2.5,
134
                    'bool'   => true,
135
                    'null'   => null,
136
                    'array'  => ['a', 'b', 'c'],
137
                    'object' => (object) ['a' => 1],
138
                    'date'   => $date,
139
                ],
140
                '{"key":"value","int":1,"float":2.5,"bool":true,"null":null,"array":["a","b","c"],"object":{"a":1},"date":"' . $date->format('Y-m-d H:i:s') . '"}',
141
                '',
142
                "\n",
143
                '',
144
            ],
145
        ];
146
    }
147
}
148