summaryrefslogtreecommitdiffstats
path: root/test/php/library/Director/PropertyModifier/PropertyModifierArrayElementByPositionTest.php
blob: 2f0522ae85ac132c6be5a112d398f3a9f75577bc (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
<?php

namespace Tests\Icinga\Module\Director\Objects;

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

class PropertyModifierArrayElementByPositionTest extends BaseTestCase
{
    /*
     * Allowed settings:
     *
     * position_type: first, last, fixed
     * position
     * when_missing: fail, null
     */

    public function testGivesFirstElementOfArray()
    {
        $this->assertEquals(
            'one',
            $this->buildModifier('first')->transform(['one', 'two', 'three'])
        );
    }

    public function testGivesFirstElementOfObject()
    {
        $this->assertEquals(
            'one',
            $this->buildModifier('first')->transform((object) ['one', 'two', 'three'])
        );
    }

    public function testGettingFirstFailsForEmptyArray()
    {
        $this->expectException(\InvalidArgumentException::class);

        $this->buildModifier('first')->transform([]);
    }

    public function testGettingFirstGivesNullForEmptyArray()
    {
        $this->assertNull($this->buildModifier('first', null, 'null')->transform([]));
    }

    public function testGivesLastElementOfArray()
    {
        $this->assertEquals(
            'three',
            $this->buildModifier('last')->transform(['one', 'two', 'three'])
        );
    }

    public function testGivesLastElementOfObject()
    {
        $this->assertEquals(
            'three',
            $this->buildModifier('last')->transform((object) ['one', 'two', 'three'])
        );
    }

    public function testGettingLastFailsForEmptyArray()
    {
        $this->expectException(\InvalidArgumentException::class);

        $this->buildModifier('last')->transform([]);
    }

    public function testGettingLastGivesNullForEmptyArray()
    {
        $this->assertNull($this->buildModifier('last', null, 'null')->transform([]));
    }

    public function testGivesSpecificElementOfArray()
    {
        $this->assertEquals(
            'two',
            $this->buildModifier('fixed', '1')->transform(['one', 'two', 'three'])
        );
    }

    public function testGivesSpecificElementOfObject()
    {
        $this->assertEquals(
            'two',
            $this->buildModifier('fixed', 1)->transform((object) ['one', 'two', 'three'])
        );
    }

    public function testGettingSpecificFailsForEmptyArray()
    {
        $this->expectException(\InvalidArgumentException::class);

        $this->buildModifier('fixed', 1)->transform([]);
    }

    public function testGettingSpecificGivesNullForEmptyArray()
    {
        $this->assertNull($this->buildModifier('fixed', 1, 'null')->transform([]));
    }

    public function testGettingSpecificFailsForMissingValue()
    {
        $this->expectException(\InvalidArgumentException::class);

        $this->buildModifier('fixed', 3)->transform(['one', 'two', 'three']);
    }

    public function testGettingSpecificGivesNullForMissingValue()
    {
        $this->assertNull($this->buildModifier('fixed', 3, 'null')->transform(['one', 'two', 'three']));
    }

    public function testFailsForStrings()
    {
        $this->expectException(\InvalidArgumentException::class);

        $this->buildModifier('first')->transform('string');
    }

    public function testAnnouncesArraySupport()
    {
        $modifier = new PropertyModifierArrayElementByPosition();
        $this->assertTrue($modifier->hasArraySupport());
    }

    protected function buildModifier($type, $position = null, $whenMissing = 'fail')
    {
        $modifier = new PropertyModifierArrayElementByPosition();
        $modifier->setSettings([
            'position_type' => $type,
            'position'      => $position,
            'when_missing'  => $whenMissing,
        ]);

        return $modifier;
    }
}