Passed
Branch master (e8fd46)
by Alexey
03:15
created

ConfigSpec   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 123
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 1
1
<?php
2
3
namespace spec\Venta\Config;
4
5
use PhpSpec\ObjectBehavior;
6
use Venta\Config\Config;
7
use Venta\Contracts\Config\Config as ConfigContract;
8
9
class ConfigSpec extends ObjectBehavior
10
{
11
    function can_be_constructed_with_data()
12
    {
13
        $this->beConstructedWith(['key' => 'value']);
14
        $this->shouldHaveKeyWithValue('key', 'value');
15
    }
16
17
    public function getMatchers()
18
    {
19
        return [
20
            'matchJson' => function ($subject, $json) {
21
                return json_encode($subject) == $json;
22
            },
23
            'havePropertyWithValue' => function ($subject, $key, $value) {
24
                return $subject->{$key} === $value;
25
            },
26
            'haveProperty' => function ($subject, $key) {
27
                return property_exists($subject, $key);
28
            },
29
        ];
30
    }
31
32
    function it_can_be_locked_for_modifications()
33
    {
34
        $this->beConstructedWith([
35
            'key' => 'value',
36
            'sub' => [
37
                'key' => 'value',
38
            ],
39
        ]);
40
41
        $this->isLocked()->shouldBe(false);
42
        $this->lock();
43
        $this->isLocked()->shouldBe(true);
44
        $this->get('sub')->isLocked()->shouldBe(true);
45
    }
46
47
    function it_can_be_presented_as_array()
48
    {
49
        $array = ['key' => 'value'];
50
        $this->beConstructedWith($array);
51
        $this->toArray()->shouldBe($array);
52
    }
53
54
    function it_can_be_presented_as_json()
55
    {
56
        $array = ['key' => 'value'];
57
        $this->beConstructedWith($array);
58
        $this->shouldMatchJson(json_encode($array));
59
    }
60
61
    function it_can_push_values_as_array()
62
    {
63
        $this[] = 'value1';
64
        $this[] = 'value2';
65
        $this->toArray()->shouldBe(['value1', 'value2']);
66
    }
67
68
    function it_can_push_values_as_object()
69
    {
70
        $this->push('value1');
71
        $this->push('value2');
72
        $this->toArray()->shouldBe(['value1', 'value2']);
73
    }
74
75
    function it_checks_keys_for_existence()
76
    {
77
        $this->beConstructedWith([
78
            'key' => 'value',
79
            'nullable' => null,
80
        ]);
81
82
        $this->has('key')->shouldReturn(true);
83
        $this->has('nullable')->shouldReturn(true);
84
        $this->has('unknown')->shouldReturn(false);
85
    }
86
87
    function it_counts_number_of_keys()
88
    {
89
        $array = range(1, rand(1, 20));
90
        $this->beConstructedWith($array);
91
        $this->count()->shouldBe(count($array));
92
    }
93
94
    function it_is_initializable()
95
    {
96
        $this->shouldHaveType(Config::class);
97
        $this->shouldImplement(ConfigContract::class);
98
    }
99
100
    function it_returns_subtree_as_a_config_object()
101
    {
102
        $this->beConstructedWith([
103
            'key' => [],
104
        ]);
105
106
        $this->getName()->shouldBe('root');
107
        $this->get('key')->shouldBeAnInstanceOf(Config::class);
108
    }
109
110
    function it_works_as_array()
111
    {
112
        $this->shouldNotHaveKey('key');
113
        $this['key'] = 'value';
114
        $this->shouldHaveKeyWithValue('key', 'value');
115
        $this['key'] = 'new_value';
116
        $this->shouldHaveKeyWithValue('key', 'new_value');
117
        unset($this['key']);
118
        $this->shouldNotHaveKey('key');
119
    }
120
121
    function it_works_as_object()
122
    {
123
        $this->shouldNotHaveProperty('key');
124
        $this->key = 'value';
125
        $this->shouldHavePropertyWithValue('key', 'value');
126
        $this->key = 'new_value';
127
        $this->shouldHavePropertyWithValue('key', 'new_value');
128
        unset($this->key);
129
        $this->shouldNotHaveProperty('key');
130
    }
131
}
132