File: CustomvarFlatTest.php

package info (click to toggle)
icingadb-web 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,260 kB
  • sloc: php: 33,601; javascript: 640; sh: 17; xml: 16; makefile: 4
file content (121 lines) | stat: -rw-r--r-- 3,767 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php

/* Icinga DB Web | (c) 2023 Icinga GmbH | GPLv2 */

namespace Tests\Icinga\Modules\Icingadb\Model;

use Icinga\Module\Icingadb\Model\CustomvarFlat;
use PHPUnit\Framework\TestCase;

class CustomvarFlatTest extends TestCase
{
    const EMPTY_TEST_SOURCE = [
        ["dict.not_empty.foo","bar","dict","{\"empty\":{},\"not_empty\":{\"foo\":\"bar\"}}"],
        ["dict.empty",null,"dict","{\"empty\":{},\"not_empty\":{\"foo\":\"bar\"}}"],
        ["list[1]",null,"list","[[\"foo\",\"bar\"],[]]"],
        ["list[0][0]","foo","list","[[\"foo\",\"bar\"],[]]"],
        ["list[0][1]","bar","list","[[\"foo\",\"bar\"],[]]"],
        ["empty_list",null,"empty_list","[]"],
        ["empty_dict",null,"empty_dict","{}"],
        ["null","null","null","null"]
    ];

    const EMPTY_TEST_RESULT = [
        "dict" => [
            "not_empty" => [
                "foo" => "bar"
            ],
            "empty" => []
        ],
        "list" => [
            ["foo", "bar"],
            []
        ],
        "empty_list" => [],
        "empty_dict" => [],
        "null" => "null"
    ];

    const SPECIAL_CHAR_TEST_SOURCE = [
        [
            "vhosts.xxxxxxxxxxxxx.mgmt.xxxxxx.com.http_port",
            "443",
            "vhosts",
            "{\"xxxxxxxxxxxxx.mgmt.xxxxxx.com\":{\"http_port\":\"443\"}}"
        ],
        ["ex.ample.com.bla","blub","ex","{\"ample.com\":{\"bla\":\"blub\"}}"],
        ["example[1]","zyx","example[1]","\"zyx\""],
        ["example.0.org","xyz","example.0.org","\"xyz\""],
        ["ob.je.ct","***","ob","{\"je\":{\"ct\":\"tcejbo\"}}"],
        ["real_list[2]","three","real_list","[\"one\",\"two\",\"three\"]"],
        ["real_list[1]","two","real_list","[\"one\",\"two\",\"three\"]"],
        ["real_list[0]","one","real_list","[\"one\",\"two\",\"three\"]"],
        ["[1].2.[3].4.[5].6","123456","[1].2","{\"[3].4\":{\"[5].6\":123456}}"],
        ["ex.ample.com","cba","ex.ample.com","\"cba\""],
        ["[4]","four","[4]","\"four\""]
    ];

    const SPECIAL_CHAR_TEST_RESULT = [
        "vhosts" => [
            "xxxxxxxxxxxxx.mgmt.xxxxxx.com" => [
                "http_port" => 443
            ]
        ],
        "ex" => [
            "ample.com" => [
                "bla" => "blub"
            ]
        ],
        "example[1]" => "zyx",
        "example.0.org" => "xyz",
        "ob" => [
            "je" => [
                "ct" => "***"
            ]
        ],
        "real_list" => [
            "one",
            "two",
            "three"
        ],
        "[1].2" => [
            "[3].4" => [
                "[5].6" => "123456"
            ]
        ],
        "ex.ample.com" => "cba",
        "[4]" => "four"
    ];

    public function testUnflatteningOfEmptyCustomVariables()
    {
        $this->assertEquals(
            self::EMPTY_TEST_RESULT,
            (new CustomvarFlat())->unFlattenVars($this->transformSource(self::EMPTY_TEST_SOURCE)),
            "Empty custom variables are not correctly unflattened"
        );
    }

    public function testUnflatteningOfCustomVariablesWithSpecialCharacters()
    {
        $this->assertEquals(
            self::SPECIAL_CHAR_TEST_RESULT,
            (new CustomvarFlat())->unFlattenVars($this->transformSource(self::SPECIAL_CHAR_TEST_SOURCE)),
            "Custom variables with special characters are not correctly unflattened"
        );
    }

    protected function transformSource(array $source): \Generator
    {
        foreach ($source as $data) {
            yield (object) [
                'flatname' => $data[0],
                'flatvalue' => $data[1],
                'customvar' => (object) [
                    'name' => $data[2],
                    'value' => $data[3]
                ]
            ];
        }
    }
}