Completed
Push — master ( 9eda17...c0c747 )
by Adrian Florin
04:54
created

ArrayHelperTest::testHasKeyException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace NeedleProject\Common\Helper;
3
4
use PHPUnit_Framework_TestCase as TestCase;
5
6
class ArrayHelperTest extends TestCase
7
{
8
    /**
9
     * @dataProvider provideTrueScenarios
10
     * @param $array
11
     * @param $keys
12
     */
13
    public function testHasKey($array, $keys)
14
    {
15
        $arrayHelper = new ArrayHelper();
16
        $this->assertTrue(
17
            $arrayHelper->hasKeysInDepth($array, $keys)
18
        );
19
    }
20
21
    /**
22
     * @dataProvider provideFalseScenarios
23
     * @param $array
24
     * @param $keys
25
     */
26
    public function testFailHasKey($array, $keys)
27
    {
28
        $arrayHelper = new ArrayHelper();
29
        $this->assertFalse(
30
            $arrayHelper->hasKeysInDepth($array, $keys)
31
        );
32
    }
33
34
    /**
35
     * @expectedException \InvalidArgumentException
36
     * @dataProvider provideExceptionScenarios
37
     * @param $notArray
38
     */
39
    public function testHasKeyException($notArray)
40
    {
41
        $arrayHelper = new ArrayHelper();
42
        $arrayHelper->hasKeysInDepth($notArray, []);
43
    }
44
45
    /**
46
     * @expectedException \InvalidArgumentException
47
     * @dataProvider provideExceptionScenarios
48
     * @param $notArray
49
     */
50
    public function testGetValueException($notArray)
51
    {
52
        $arrayHelper = new ArrayHelper();
53
        $arrayHelper->getValueFromDepth($notArray, []);
54
    }
55
56
    /**
57
     * @dataProvider provideValueScenarios
58
     * @param array $array
59
     * @param array $keys
60
     * @param mixed $expectedValue
61
     */
62
    public function testGetValue($array, $keys, $expectedValue)
63
    {
64
        $arrayHelper = new ArrayHelper();
65
        $this->assertEquals(
66
            $expectedValue,
67
            $arrayHelper->getValueFromDepth($array, $keys)
68
        );
69
    }
70
71
    /**
72
     * @dataProvider provideValueScenarios
73
     * @param array $array
74
     * @param array $keys
75
     */
76
    public function testFailGetValue($array, $keys)
77
    {
78
        $arrayHelper = new ArrayHelper();
79
        $arrayHelper->getValueFromDepth($array, $keys);
80
    }
81
82
    /**
83
     * @expectedException \NeedleProject\Common\Exception\NotFoundException
84
     */
85
    public function testNotFoundGetValue()
86
    {
87
        $arrayHelper = new ArrayHelper();
88
        $arrayHelper->getValueFromDepth(['a' => 'b'], ['a', 'c']);
89
    }
90
91
    /**
92
     * Tied to ::testHasKey
93
     * @return array
94
     */
95
    public function provideTrueScenarios()
96
    {
97
        return [
98
            [
99
                [
100
                    'foo' => [
101
                        'bar' => [
102
                            'baz' => [
103
                                'qux' => 'Lorem ipsum'
104
                            ]
105
                        ]
106
                    ]
107
                ],
108
                ['foo','bar','baz','qux']
109
            ],
110
            [
111
                [[[['a' => ['bar']]]]],
112
                [0, 0, 0, 'a', 0]
113
            ]
114
        ];
115
    }
116
117
    /**
118
     * Tied to ::testFailHasKey
119
     * @return array
120
     */
121 View Code Duplication
    public function provideFalseScenarios()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
    {
123
        return [
124
            [
125
                [
126
                    'foo' => [
127
                        'foo' => [
128
                            'foo' => [
129
                                'foo' => [
130
                                    'bar' => 'Lorem ipsum'
131
                                ]
132
                            ]
133
                        ]
134
                    ]
135
                ],
136
                ['foo', 'foo', 'foo', 'foo', 'foo', 'bar']
137
            ]
138
        ];
139
    }
140
141
    /**
142
     * Tied to ::testException
143
     * @return array
144
     */
145
    public function provideExceptionScenarios()
146
    {
147
        return [
148
            [1],
149
            ['a'],
150
            [new \stdClass()],
151
            [0xFF],
152
            [1.2],
153
            [[]],
154
            [['a' => 'b']]
155
        ];
156
    }
157
158
    /**
159
     * Tied to ::testGetValue
160
     * @return array
161
     */
162 View Code Duplication
    public function provideValueScenarios()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
    {
164
        return [
165
            // first scenario
166
            [
167
                [
168
                    'foo' => [
169
                        'foo' => [
170
                            'foo' => [
171
                                'foo' => [
172
                                    'bar' => 'Lorem ipsum'
173
                                ]
174
                            ]
175
                        ]
176
                    ]
177
                ],
178
                ['foo', 'foo', 'foo', 'foo', 'bar'],
179
                'Lorem ipsum'
180
            ]
181
        ];
182
    }
183
184
185
    /**
186
     * Tied to ::testFailGetValue
187
     * @return array
188
     */
189 View Code Duplication
    public function provideFailValueScenarios()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
190
    {
191
        return [
192
            // first scenario
193
            [
194
                [
195
                    'foo' => [
196
                        'foo' => [
197
                            'foo' => [
198
                                'foo' => [
199
                                    'bar' => 'Lorem ipsum'
200
                                ]
201
                            ]
202
                        ]
203
                    ]
204
                ],
205
                ['foo', 'foo', 'foo', 'foo', 'foo', 'bar']
206
            ]
207
        ];
208
    }
209
}
210