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
|
|
|
} |