1<?php
2
3namespace Tests\Icinga\Module\Director\Objects;
4
5use Icinga\Module\Director\Objects\IcingaHost;
6use Icinga\Module\Director\Test\BaseTestCase;
7
8class IcingaTemplateResolverTest extends BaseTestCase
9{
10    /** @var IcingaHost[] */
11    private $scenario;
12
13    private $prefix = '__TEST_i1_';
14
15    public function testParentNamesCanBeRetrieved()
16    {
17        $this->assertEquals(
18            array(
19                $this->prefixed('t6'),
20                $this->prefixed('t5'),
21                $this->prefixed('t2')
22            ),
23            $this->getHost('t1')->templateResolver()->listParentNames()
24        );
25    }
26
27    public function testFullInhertancePathIdsAreCorrect()
28    {
29        $this->assertEquals(
30            $this->getIds(array('t5', 't6', 't5', 't5', 't4', 't3', 't5', 't6', 't2', 't1')),
31            $this->getHost('t1')->templateResolver()->listFullInheritancePathIds()
32        );
33    }
34
35    public function testInhertancePathIdsAreCorrect()
36    {
37        $this->assertEquals(
38            $this->getIds(array('t4', 't3', 't5', 't6', 't2', 't1')),
39            $this->getHost('t1')->templateResolver()->listInheritancePathIds()
40        );
41    }
42
43    protected function getHost($name)
44    {
45        $hosts = $this->getScenario();
46        return $hosts[$name];
47    }
48
49    protected function getId($name)
50    {
51        $hosts = $this->getScenario();
52        return $hosts[$name]->id;
53    }
54
55    protected function getIds($names)
56    {
57        $ids = array();
58        foreach ($names as $name) {
59            $ids[] = $this->getId($name);
60        }
61
62        return $ids;
63    }
64
65    protected function prefixed($name)
66    {
67        return $this->prefix . $name;
68    }
69
70    /**
71     * @return IcingaHost[]
72     */
73    protected function getScenario()
74    {
75        if ($this->scenario === null) {
76            $this->scenario = $this->createScenario();
77        }
78
79        return $this->scenario;
80    }
81
82    /**
83     * @return IcingaHost[]
84     */
85    protected function createScenario()
86    {
87        // Defionition imports (object -> imported)
88        // t1 -> t6, t5, t2
89        // t6 -> t5
90        // t3 -> t4
91        // t2 -> t3, t6
92        // t4 -> t5
93
94        // 5, 6, 5, 5, 4, 3, 5, 6, 2, 1
95
96        $t1 = $this->create('t1');
97        $t4 = $this->create('t4');
98        $t3 = $this->create('t3');
99        $t2 = $this->create('t2');
100        $t5 = $this->create('t5');
101        $t6 = $this->create('t6');
102
103        // TODO: Must work without this:
104        $t1->templateResolver()->clearCache();
105        $t1->set('imports', array($t6, $t5, $t2));
106        $t6->set('imports', array($t5));
107        $t3->set('imports', array($t4));
108        $t2->set('imports', array($t3, $t6));
109        $t4->set('imports', array($t5));
110
111        $t5->store();
112        $t4->store();
113        $t3->store();
114        $t6->store();
115        $t2->store();
116        $t1->store();
117
118        // TODO: Must work without this:
119        $t1->templateResolver()->clearCache();
120        return array(
121            't1' => $t1,
122            't2' => $t2,
123            't3' => $t3,
124            't4' => $t4,
125            't5' => $t5,
126            't6' => $t6,
127        );
128    }
129
130    /**
131     * @param $name
132     * @return IcingaHost
133     */
134    protected function create($name)
135    {
136        $host = IcingaHost::create(
137            array(
138                'object_name' => $this->prefixed($name),
139                'object_type' => 'template'
140            )
141        );
142
143        $host->store($this->getDb());
144        return $host;
145    }
146
147    public function tearDown()
148    {
149        $db = $this->getDb();
150        $kill = array('t1', 't2', 't6', 't3', 't4', 't5');
151        foreach ($kill as $short) {
152            $name = $this->prefixed($short);
153            if (IcingaHost::exists($name, $db)) {
154                IcingaHost::load($name, $db)->delete();
155            }
156        }
157    }
158}
159