summaryrefslogtreecommitdiffstats
path: root/test/php/library/Director/PropertyModifier/PropertyModifierListToObjectTest.php
blob: 93d498c911b40c7db65535fd6842a84b4046e269 (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
<?php

namespace Tests\Icinga\Module\Director\Objects;

use Icinga\Module\Director\PropertyModifier\PropertyModifierListToObject;
use Icinga\Module\Director\Test\BaseTestCase;

class PropertyModifierListToObjectTest extends BaseTestCase
{
    public function testConvertsAListOfArrays()
    {
        $this->assertEquals(
            $this->getOutput(),
            $this->getNewModifier()->transform($this->getInputArrays())
        );
    }

    public function testConvertsAListOfObjects()
    {
        $this->assertEquals(
            $this->getOutput(),
            $this->getNewModifier()->transform($this->getInputObjects())
        );
    }

    /**
     * @expectedException \InvalidArgumentException
     */
    public function testFailsOnMissingKey()
    {
        $input = $this->getInputArrays();
        unset($input[0]['name']);
        $this->getNewModifier()->transform($input);
    }

    /**
     * @expectedException \InvalidArgumentException
     */
    public function testFailsWithDuplicateRows()
    {
        $input = $this->getInputArrays();
        $input[1]['name'] = 'row1';
        $this->getNewModifier()->transform($input);
    }

    public function testKeepsFirstRowOnDuplicate()
    {
        $input = $this->getInputArrays();
        $input[1]['name'] = 'row1';
        $modifier = $this->getNewModifier()->setSetting('on_duplicate', 'keep_first');
        $result = $modifier->transform($input);
        $this->assertEquals(
            (object) ['some' => 'property'],
            $result->row1->props
        );
    }

    public function testKeepsLastRowOnDuplicate()
    {
        $input = $this->getInputArrays();
        $input[1]['name'] = 'row1';
        $modifier = $this->getNewModifier()->setSetting('on_duplicate', 'keep_last');
        $result = $modifier->transform($input);
        $this->assertEquals(
            (object) ['other' => 'property'],
            $result->row1->props
        );
    }

    protected function getNewModifier()
    {
        $modifier = new PropertyModifierListToObject();
        $modifier->setSettings([
            'key_property' => 'name',
            'on_duplicate' => 'fail'
        ]);

        return $modifier;
    }

    protected function getInputArrays()
    {
        return [
            ['name' => 'row1', 'props' => (object) ['some' => 'property']],
            ['name' => 'row2', 'props' => (object) ['other' => 'property']],
        ];
    }

    protected function getInputObjects()
    {
        return [
            (object) ['name' => 'row1', 'props' => (object) ['some' => 'property']],
            (object) ['name' => 'row2', 'props' => (object) ['other' => 'property']],
        ];
    }

    protected function getOutput()
    {
        return (object) [
            'row1' => (object) ['name' => 'row1', 'props' => (object) ['some' => 'property']],
            'row2' => (object) ['name' => 'row2', 'props' => (object) ['other' => 'property']],
        ];
    }
}