|
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; |
|
15
|
|
|
|
|
16
|
|
|
use Graze\DataFile\Format\JsonFormat; |
|
17
|
|
|
use Graze\DataFile\Test\TestCase; |
|
18
|
|
|
|
|
19
|
|
|
class JsonFormatTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
public function testType() |
|
22
|
|
|
{ |
|
23
|
|
|
$format = new JsonFormat(); |
|
24
|
|
|
static::assertEquals('json', $format->getType()); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function testDefaultsAreAssignedWhenNoOptionsSupplied() |
|
28
|
|
|
{ |
|
29
|
|
|
$definition = new JsonFormat(); |
|
30
|
|
|
|
|
31
|
|
|
static::assertEquals( |
|
32
|
|
|
JsonFormat::JSON_FILE_TYPE_SINGLE_BLOCK, |
|
33
|
|
|
$definition->getJsonFileType(), |
|
34
|
|
|
"File Type should be single block" |
|
35
|
|
|
); |
|
36
|
|
|
static::assertEquals(0, $definition->getJsonEncodeOptions(), "Default encode options should be 0"); |
|
37
|
|
|
static::assertEquals(0, $definition->getJsonDecodeOptions(), "Default decode options should be 0"); |
|
38
|
|
|
static::assertFalse($definition->isJsonDecodeAssoc(), "Default decode association should be false"); |
|
39
|
|
|
static::assertTrue($definition->isIgnoreBlankLines(), "Default ignore blank lines should be true"); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testAssigningOptionsModifiesTheDefinition() |
|
43
|
|
|
{ |
|
44
|
|
|
$definition = new JsonFormat([ |
|
45
|
|
|
'fileType' => JsonFormat::JSON_FILE_TYPE_EACH_LINE, |
|
46
|
|
|
'encodeOptions' => JSON_PRETTY_PRINT | JSON_HEX_AMP, |
|
47
|
|
|
'decodeOptions' => JSON_BIGINT_AS_STRING, |
|
48
|
|
|
JsonFormat::OPTION_DECODE_ASSOC => true, |
|
49
|
|
|
JsonFormat::OPTION_IGNORE_BLANK_LINES => false, |
|
50
|
|
|
]); |
|
51
|
|
|
|
|
52
|
|
|
static::assertEquals( |
|
53
|
|
|
JsonFormat::JSON_FILE_TYPE_EACH_LINE, |
|
54
|
|
|
$definition->getJsonFileType(), |
|
55
|
|
|
"File Type should be set to: Each Line" |
|
56
|
|
|
); |
|
57
|
|
|
static::assertEquals( |
|
58
|
|
|
JSON_PRETTY_PRINT | JSON_HEX_AMP, |
|
59
|
|
|
$definition->getJsonEncodeOptions(), |
|
60
|
|
|
"Encode options should be pretty print and hex amp" |
|
61
|
|
|
); |
|
62
|
|
|
static::assertEquals( |
|
63
|
|
|
JSON_BIGINT_AS_STRING, |
|
64
|
|
|
$definition->getJsonDecodeOptions(), |
|
65
|
|
|
"Decode options should be BIGINT_AS_STRING" |
|
66
|
|
|
); |
|
67
|
|
|
static::assertTrue($definition->isJsonDecodeAssoc(), "Json Decode Assoc should be true"); |
|
68
|
|
|
static::assertFalse($definition->isIgnoreBlankLines(), "Ignore Blank Lines should be false"); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testSettingProperties() |
|
72
|
|
|
{ |
|
73
|
|
|
$definition = new JsonFormat(); |
|
74
|
|
|
|
|
75
|
|
|
static::assertEquals( |
|
76
|
|
|
JsonFormat::JSON_FILE_TYPE_SINGLE_BLOCK, |
|
77
|
|
|
$definition->getJsonFileType(), |
|
78
|
|
|
"File Type should be single block" |
|
79
|
|
|
); |
|
80
|
|
|
static::assertEquals(0, $definition->getJsonEncodeOptions(), "Default encode options should be 0"); |
|
81
|
|
|
static::assertEquals(0, $definition->getJsonDecodeOptions(), "Default decode options should be 0"); |
|
82
|
|
|
static::assertFalse($definition->isJsonDecodeAssoc(), "Default decode association should be false"); |
|
83
|
|
|
static::assertTrue($definition->isIgnoreBlankLines(), "Default ignore blank lines should be true"); |
|
84
|
|
|
|
|
85
|
|
|
static::assertSame( |
|
86
|
|
|
$definition, |
|
87
|
|
|
$definition->setJsonFileType(JsonFormat::JSON_FILE_TYPE_EACH_LINE), |
|
88
|
|
|
"setJsonFileType should be fluent" |
|
89
|
|
|
); |
|
90
|
|
|
static::assertEquals( |
|
91
|
|
|
JsonFormat::JSON_FILE_TYPE_EACH_LINE, |
|
92
|
|
|
$definition->getJsonFileType(), |
|
93
|
|
|
"File Type should be set to: Each Line" |
|
94
|
|
|
); |
|
95
|
|
|
static::assertSame( |
|
96
|
|
|
$definition, |
|
97
|
|
|
$definition->setJsonEncodeOptions(JSON_HEX_APOS | JSON_FORCE_OBJECT), |
|
98
|
|
|
"setJsonEncodeOptions should be fluent" |
|
99
|
|
|
); |
|
100
|
|
|
static::assertEquals( |
|
101
|
|
|
JSON_HEX_APOS | JSON_FORCE_OBJECT, |
|
102
|
|
|
$definition->getJsonEncodeOptions(), |
|
103
|
|
|
"Encode options should be: HEX_APOS and FORCE_OBJECT" |
|
104
|
|
|
); |
|
105
|
|
|
static::assertSame( |
|
106
|
|
|
$definition, |
|
107
|
|
|
$definition->setJsonDecodeOptions(JSON_BIGINT_AS_STRING), |
|
108
|
|
|
"setJsonDencodeOptions should be fluent" |
|
109
|
|
|
); |
|
110
|
|
|
static::assertEquals( |
|
111
|
|
|
JSON_BIGINT_AS_STRING, |
|
112
|
|
|
$definition->getJsonDecodeOptions(), |
|
113
|
|
|
"Decode options should be BIGINT_AS_STRING" |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
static::assertSame($definition, $definition->setJsonDecodeAssoc(true), "setJsonDecodeAssoc should be fluent"); |
|
117
|
|
|
static::assertTrue($definition->isJsonDecodeAssoc(), "JsonDecodeAssoc should be true"); |
|
118
|
|
|
static::assertSame( |
|
119
|
|
|
$definition, |
|
120
|
|
|
$definition->setIgnoreBlankLines(false), |
|
121
|
|
|
"setIgnoreBlankLines should be fluent" |
|
122
|
|
|
); |
|
123
|
|
|
static::assertFalse($definition->isIgnoreBlankLines(), "Ignore Blank Lines should be false"); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|