Passed
Push — master ( 669ff7...829ee4 )
by Darío
02:32
created

testCanBeCreatedFromATwoDimensionalArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 20
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace DroneTest\Util;
12
13
use Drone\Util\ArrayDimension;
14
use PHPUnit\Framework\TestCase;
15
16
class ArrayDimensionTest extends TestCase
17
{
18
    /**
19
     * Tests if returns the same array if it's unidimensional
20
     *
21
     * @return void
22
     */
23
    public function testReturnSameArray() : void
24
    {
25
        $multidimensional = [
26
            "foo" => [8, "bar"]
27
        ];
28
29
        $unidimensional = ArrayDimension::toUnidimensional($multidimensional, "");
30
31
        $this->assertEquals($multidimensional, $unidimensional);
32
    }
33
34
    /**
35
     * Tests if it's possible transform a simple two-dimensional array into an unidimensional
36
     *
37
     * @return void
38
     */
39
    public function testCanBeCreatedFromATwoDimensionalArray() : void
40
    {
41
        # two-dimensional array
42
        $multidimensional = [
43
            "foo" => [
44
                "bar" => 87
45
            ],
46
            "foo2" => [
47
                "bar2" => "value"
48
            ]
49
        ];
50
51
        $expected = [
52
            "foo_bar"   => 87,
53
            "foo2_bar2" => "value"
54
        ];
55
56
        $unidimensional = ArrayDimension::toUnidimensional($multidimensional, "_");
57
58
        $this->assertEquals($expected, $unidimensional);
59
    }
60
61
    /**
62
     * Tests if it's possible transform a simple three-dimensional array into an unidimensional
63
     *
64
     * @return void
65
     */
66
    public function testCanBeCreatedFromAThreeDimensionalArray() : void
67
    {
68
        # three-dimensional array
69
        $multidimensional = [
70
            "foo" => [
71
                "bar" => [
72
                    "abc" => false
73
                ]
74
            ],
75
            "foo2" => [
76
                "bar2" => [
77
                    "def" => 7854
78
                ]
79
            ]
80
        ];
81
82
        $expected = [
83
            "foo_bar_abc"   => false,
84
            "foo2_bar2_def" => 7854
85
        ];
86
87
        $unidimensional = ArrayDimension::toUnidimensional($multidimensional, "_");
88
89
        $this->assertEquals($expected, $unidimensional);
90
    }
91
92
    /**
93
     * Tests if it's possible transform a mixed n-dimensional array into an unidimensional
94
     *
95
     * @return void
96
     */
97
    public function testCanBeCreatedFromAMixedArray() : void
98
    {
99
        # mixed n-dimensional array
100
        $multidimensional = [
101
            "foo" => "value",
102
            "bar" => [
103
                "abc" => false
104
            ],
105
            "foo2" => [
106
                "bar2" => [
107
                    "def" => 7854
108
                ]
109
            ]
110
        ];
111
112
        $expected = [
113
            "foo"           => "value",
114
            "bar_abc"       => false,
115
            "foo2_bar2_def" => 7854
116
        ];
117
118
        $unidimensional = ArrayDimension::toUnidimensional($multidimensional, "_");
119
120
        $this->assertEquals($expected, $unidimensional);
121
    }
122
}