summaryrefslogtreecommitdiffstats
path: root/test/php/library/Director/IcingaConfig/ExtensibleSetTest.php
blob: 34bd83a3d4f9404a91fb14fc86806a346022d9d5 (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php

namespace Tests\Icinga\Module\Director\IcingaConfig;

use Icinga\Module\Director\IcingaConfig\ExtensibleSet;
use Icinga\Module\Director\Objects\IcingaUser;
use Icinga\Module\Director\Test\BaseTestCase;

class ExtensibleSetTest extends BaseTestCase
{
    public function testNoValuesResultInEmptySet()
    {
        $set = new ExtensibleSet();

        $this->assertEquals(
            array(),
            $set->getResolvedValues()
        );
    }

    public function testValuesPassedToConstructorAreAccepted()
    {
        $values = array('Val1', 'Val2', 'Val4');
        $set = new ExtensibleSet($values);

        $this->assertEquals(
            $values,
            $set->getResolvedValues()
        );
    }

    public function testConstructorAcceptsSingleValues()
    {
        $set = new ExtensibleSet('Bla');

        $this->assertEquals(
            array('Bla'),
            $set->getResolvedValues()
        );
    }

    public function testSingleValuesCanBeBlacklisted()
    {
        $values = array('Val1', 'Val2', 'Val4');
        $set = new ExtensibleSet($values);
        $set->blacklist('Val2');

        $this->assertEquals(
            array('Val1', 'Val4'),
            $set->getResolvedValues()
        );
    }

    public function testMultipleValuesCanBeBlacklisted()
    {
        $values = array('Val1', 'Val2', 'Val4');
        $set = new ExtensibleSet($values);
        $set->blacklist(array('Val4', 'Val1'));

        $this->assertEquals(
            array('Val2'),
            $set->getResolvedValues()
        );
    }

    public function testSimpleInheritanceWorksFine()
    {
        $values = array('Val1', 'Val2', 'Val4');
        $parent = new ExtensibleSet($values);
        $child = new ExtensibleSet();
        $child->inheritFrom($parent);

        $this->assertEquals(
            $values,
            $child->getResolvedValues()
        );
    }

    public function testWeCanInheritFromMultipleParents()
    {
        $p1set = array('p1a', 'p1c');
        $p2set = array('p2a', 'p2d');
        $parent1 = new ExtensibleSet($p1set);
        $parent2 = new ExtensibleSet($p2set);
        $child = new ExtensibleSet();
        $child->inheritFrom($parent1)->inheritFrom($parent2);

        $this->assertEquals(
            $p2set,
            $child->getResolvedValues()
        );
    }

    public function testOwnValuesOverrideParents()
    {
        $cset = array('p1a', 'p1c');
        $pset = array('p2a', 'p2d');
        $child = new ExtensibleSet($cset);
        $parent = new ExtensibleSet($pset);
        $child->inheritFrom($parent);

        $this->assertEquals(
            $cset,
            $child->getResolvedValues()
        );
    }

    public function testInheritedValuesCanBeBlacklisted()
    {
        $child = new ExtensibleSet();
        $child->blacklist('p2');

        $pset = array('p1', 'p2', 'p3');
        $parent = new ExtensibleSet($pset);

        $child->inheritFrom($parent);
        $child->blacklist(array('not', 'yet', 'p1'));

        $this->assertEquals(
            array('p3'),
            $child->getResolvedValues()
        );

        $child->blacklist(array('p3'));
        $this->assertEquals(
            array(),
            $child->getResolvedValues()
        );
    }

    public function testInheritedValuesCanBeExtended()
    {
        $pset = array('p1', 'p2', 'p3');

        $child = new ExtensibleSet();
        $child->extend('p5');

        $parent = new ExtensibleSet($pset);
        $child->inheritFrom($parent);

        $this->assertEquals(
            array('p1', 'p2', 'p3', 'p5'),
            $child->getResolvedValues()
        );
    }

    public function testCombinedDefinitionRendersCorrectly()
    {
        $set = new ExtensibleSet(array('Pre', 'Def', 'Ined'));
        $set->blacklist(array('And', 'Not', 'Those'));
        $set->extend('PlusThis');

        $out = '    key_name = [ Pre, Def, Ined ]' . "\n"
             . '    key_name += [ PlusThis ]' . "\n"
             . '    key_name -= [ And, Not, Those ]' . "\n";

        $this->assertEquals(
            $out,
            $set->renderAs('key_name')
        );
    }
}