JsonFormatterTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 137
c 0
b 0
f 0
wmc 3
lcom 0
cbo 3
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testFormat() 0 9 1
B formatTestData() 0 100 1
A testFormatWithANonTraversableObjectWillThrowAnException() 0 10 1
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
use InvalidArgumentException;
22
23
class JsonFormatterTest extends TestCase
24
{
25
    /**
26
     * @dataProvider formatTestData
27
     *
28
     * @param JsonFormatInterface $format
29
     * @param array               $row
30
     * @param string              $expected
31
     * @param string              $start
32
     * @param string              $separator
33
     * @param string              $end
34
     */
35
    public function testFormat(JsonFormatInterface $format, array $row, $expected, $start, $separator, $end)
36
    {
37
        $formatter = new JsonFormatter($format);
38
39
        static::assertEquals($expected, $formatter->format($row));
40
        static::assertEquals($start, $formatter->getInitialBlock());
41
        static::assertEquals($separator, $formatter->getRowSeparator());
42
        static::assertEquals($end, $formatter->getClosingBlock());
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    public function formatTestData()
49
    {
50
        $date = new DateTime();
51
        return [
52
            [
53
                new JsonFormat(),
54
                [
55
                    'key'    => 'value',
56
                    'int'    => 1,
57
                    'float'  => 2.5,
58
                    'bool'   => true,
59
                    'null'   => null,
60
                    'array'  => ['a', 'b', 'c'],
61
                    'object' => (object) ['a' => 1],
62
                    'date'   => $date,
63
                ],
64
                '{"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') . '"}',
65
                '[',
66
                ",\n",
67
                "]\n",
68
            ],
69
            [
70
                new JsonFormat([
71
                    JsonFormat::OPTION_ENCODE_OPTIONS => JSON_PRETTY_PRINT,
72
                    JsonFormat::OPTION_FILE_TYPE      => JsonFormat::JSON_FILE_TYPE_SINGLE_BLOCK,
73
                ]),
74
                [
75
                    'key'    => 'value',
76
                    'int'    => 1,
77
                    'float'  => 2.5,
78
                    'bool'   => true,
79
                    'null'   => null,
80
                    'array'  => ['a', 'b', 'c'],
81
                    'object' => (object) ['a' => 1],
82
                    'date'   => $date,
83
                ],
84
                <<<JSON
85
{
86
    "key": "value",
87
    "int": 1,
88
    "float": 2.5,
89
    "bool": true,
90
    "null": null,
91
    "array": [
92
        "a",
93
        "b",
94
        "c"
95
    ],
96
    "object": {
97
        "a": 1
98
    },
99
    "date": "{$date->format('Y-m-d H:i:s')}"
100
}
101
JSON
102
                ,
103
                '[',
104
                ",\n",
105
                "]\n",
106
            ],
107
            [
108
                new JsonFormat([
109
                    JsonFormat::OPTION_FILE_TYPE => JsonFormat::JSON_FILE_TYPE_EACH_LINE,
110
                ]),
111
                [
112
                    'key'    => 'value',
113
                    'int'    => 1,
114
                    'float'  => 2.5,
115
                    'bool'   => true,
116
                    'null'   => null,
117
                    'array'  => ['a', 'b', 'c'],
118
                    'object' => (object) ['a' => 1],
119
                    'date'   => $date,
120
                ],
121
                '{"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') . '"}',
122
                '',
123
                "\n",
124
                '',
125
            ],
126
            [
127
                new JsonFormat([
128
                    JsonFormat::OPTION_FILE_TYPE      => JsonFormat::JSON_FILE_TYPE_EACH_LINE,
129
                    JsonFormat::OPTION_ENCODE_OPTIONS => JSON_PRETTY_PRINT,
130
                ]),
131
                [
132
                    'key'    => 'value',
133
                    'int'    => 1,
134
                    'float'  => 2.5,
135
                    'bool'   => true,
136
                    'null'   => null,
137
                    'array'  => ['a', 'b', 'c'],
138
                    'object' => (object) ['a' => 1],
139
                    'date'   => $date,
140
                ],
141
                '{"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') . '"}',
142
                '',
143
                "\n",
144
                '',
145
            ],
146
        ];
147
    }
148
149
    public function testFormatWithANonTraversableObjectWillThrowAnException()
150
    {
151
        $formatter = new JsonFormatter(new JsonFormat());
152
153
        static::expectException(InvalidArgumentException::class);
154
155
        $stuff = (object) ['cake'];
156
157
        $formatter->format($stuff);
158
    }
159
}
160