KeyValueStringParserTest.php 5.62 KB
Newer Older
1
2
3
4
5
<?php
/**
 * @author Rafael Ostertag <rafael.ostertag@math.uzh.ch>
 */

Carsten  Rose's avatar
Carsten Rose committed
6
7
namespace qfq;

8
use qfq\KeyValueStringParser;
9
10
11

require_once(__DIR__ . '/../../qfq/Constants.php');

12
13
14
15
16
17

require_once(__DIR__ . '/../../qfq/helper/KeyValueStringParser.php');

class KeyValueStringParserTest extends \PHPUnit_Framework_TestCase {

    public function testSingleKeyValuePair() {
18
        $actual = keyValueStringParser::parse("key:value");
19
20
21
22
23
24
25

        $this->assertCount(1, $actual);
        $this->assertArrayHasKey('key', $actual);
        $this->assertEquals('value', $actual['key']);
    }

    public function testKeyWithoutValue() {
26
        $actual = keyValueStringParser::parse("keywithoutvalue");
27
28
29
30
31
32
        $this->assertCount(1, $actual);
        $this->assertArrayHasKey('keywithoutvalue', $actual);
        $this->assertSame("", $actual['keywithoutvalue']);
    }

    public function testGOODNAMEHERE() {
33
        $actual = keyValueStringParser::parse(",,");
34
35
36
37
38

        $this->assertCount(0, $actual);
    }

    /**
39
     * @expectedException \qfq\UserException
40
41
     */
    public function testNoKey() {
42
        keyValueStringParser::parse(":value,key:value");
43
44
45
    }

    public function testNoValue() {
46
        $actual = keyValueStringParser::parse("key1:,key2:value2");
47
48
49
50
51
52
53
54
        $this->assertCount(2, $actual);
        $this->assertArrayHasKey('key1', $actual);
        $this->assertArrayHasKey('key2', $actual);
        $this->assertEquals('', $actual['key1']);
        $this->assertEquals('value2', $actual['key2']);
    }

    public function testEmptyKeyValuePairString() {
55
        $actual = keyValueStringParser::parse("");
56
57
58
59
60

        $this->assertCount(0, $actual);
    }

    public function testMultipleKeyValuePairs() {
61
        $actual = keyValueStringParser::parse("key1:value1,key2:value2");
62
63
64
65
66
67
68
69
70

        $this->assertCount(2, $actual);
        $this->assertArrayHasKey('key1', $actual);
        $this->assertArrayHasKey('key2', $actual);
        $this->assertEquals('value1', $actual['key1']);
        $this->assertEquals('value2', $actual['key2']);
    }

    public function testKeyValueSeparatorInValue() {
71
        $actual = keyValueStringParser::parse("key1:val:ue1,key2:value2");
72
73
74
75
76
77
78
79
80

        $this->assertCount(2, $actual);
        $this->assertArrayHasKey('key1', $actual);
        $this->assertArrayHasKey('key2', $actual);
        $this->assertEquals('val:ue1', $actual['key1']);
        $this->assertEquals('value2', $actual['key2']);
    }

    public function testWhiteSpaceHandling() {
81
        $actual = keyValueStringParser::parse(" key1 : val:ue1 , key2 : value2 ");
82
83
84
85
86
87
88
89
90

        $this->assertCount(2, $actual);
        $this->assertArrayHasKey('key1', $actual);
        $this->assertArrayHasKey('key2', $actual);
        $this->assertEquals('val:ue1', $actual['key1']);
        $this->assertEquals('value2', $actual['key2']);
    }

    public function testSourroundingQuotes() {
91
        $actual = keyValueStringParser::parse("key1:\" val:ue1 \", key2:' value2 ', key3:\"value3', key4:''");
92
93
94
95
96
97
98
99
100
101
102

        $expected = [
            'key1' => ' val:ue1 ',
            'key2' => ' value2 ',
            'key3' => "\"value3'",
            'key4' => ''
        ];
        $this->assertEquals($expected, $actual);
    }

    public function testComments() {
103
        $actual = keyValueStringParser::parse(" key1 : val:ue1 , # key2 : value2 , ; : broken key value in comment, key3 : valid ");
104
105
106
107
108
109
110
111
112

        $expected = [
            'key1' => 'val:ue1',
            'key3' => "valid",
        ];
        $this->assertEquals($expected, $actual);
    }

    public function testUnparse() {
113
114
        $array = keyValueStringParser::parse("key1:\" val:ue1 \", key2:' value2 ', key3:\"value3'");
        $actual = keyValueStringParser::unparse($array);
115
116
117
118
119
        $expected = "key1:\" val:ue1 \",key2:\" value2 \",key3:\"value3'";

        $this->assertSame($expected, $actual);

    }
Carsten  Rose's avatar
Carsten Rose committed
120
121
122

    public function testKeyValuePairCR() {

123
        $actual = keyValueStringParser::parse("key1=value1\nkey2=value2", "=", "\n");
Carsten  Rose's avatar
Carsten Rose committed
124
125
126
127
128
129
130
131

        $this->assertCount(2, $actual);
        $this->assertArrayHasKey('key1', $actual);
        $this->assertArrayHasKey('key2', $actual);
        $this->assertEquals('value1', $actual['key1']);
        $this->assertEquals('value2', $actual['key2']);
    }

132
    public function testParseKeyValueSingle() {
133
        $actual = keyValueStringParser::parse('value1,value2', ':', ',', KVP_IF_VALUE_EMPTY_COPY_KEY);
134
135
136
137
138
139
140
        $expected = [
            'value1' => 'value1',
            'value2' => 'value2',
        ];
        $this->assertEquals($expected, $actual);
        $this->assertCount(2, $actual);

141
        $actual = keyValueStringParser::parse('key1:value1,key2:value2', ':', ',', KVP_IF_VALUE_EMPTY_COPY_KEY);
142
143
144
145
146
147
148
        $expected = [
            'key1' => 'value1',
            'key2' => 'value2',
        ];
        $this->assertEquals($expected, $actual);
        $this->assertCount(2, $actual);

149
        $actual = keyValueStringParser::parse('key1:value1,key2:value2', ':', ',', KVP_VALUE_GIVEN);
150
151
152
153
154
155
156
        $expected = [
            'key1' => 'value1',
            'key2' => 'value2',
        ];
        $this->assertEquals($expected, $actual);
        $this->assertCount(2, $actual);

157
        $actual = keyValueStringParser::parse('value1,value2', ':', ',', KVP_VALUE_GIVEN);
158
159
160
161
162
163
164
165
166
167
168
169
170
171
        $expected = [
            'value1' => '',
            'value2' => '',
        ];
        $this->assertEquals($expected, $actual);
        $this->assertCount(2, $actual);
    }

    public function testParseDefaults() {
        $actual = keyValueStringParser::parse("key1=value1,key2=value2");
        $expected = keyValueStringParser::parse("key1=value1,key2=value2", ":", ",");
        $this->assertEquals($expected, $actual);
        $this->assertCount(2, $actual);
    }
172
}