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 null |
22
|
|
|
*/ |
23
|
|
|
// TO DO: Fix after setting up travis successfully |
24
|
|
|
/*public function testReturnSameArray() |
25
|
|
|
{ |
26
|
|
|
$multidimensional = [ |
27
|
|
|
"foo" => [8, "bar"] |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
$unidimensional = ArrayDimension::toUnidimensional($multidimensional, ""); |
31
|
|
|
|
32
|
|
|
$this->assertEquals($multidimensional, $unidimensional); |
33
|
|
|
}*/ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Tests if it's possible transform a simple two-dimensional array into an unidimensional |
37
|
|
|
* |
38
|
|
|
* @return null |
39
|
|
|
*/ |
40
|
|
|
public function testCanBeCreatedFromATwoDimensionalArray() |
41
|
|
|
{ |
42
|
|
|
# two-dimensional array |
43
|
|
|
$multidimensional = [ |
44
|
|
|
"foo" => [ |
45
|
|
|
"bar" => 87 |
46
|
|
|
], |
47
|
|
|
"foo2" => [ |
48
|
|
|
"bar2" => "value" |
49
|
|
|
] |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
$expected = [ |
53
|
|
|
"foo_bar" => 87, |
54
|
|
|
"foo2_bar2" => "value" |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
$unidimensional = ArrayDimension::toUnidimensional($multidimensional, "_"); |
58
|
|
|
|
59
|
|
|
$this->assertEquals($expected, $unidimensional); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Tests if it's possible transform a simple three-dimensional array into an unidimensional |
64
|
|
|
* |
65
|
|
|
* @return null |
66
|
|
|
*/ |
67
|
|
|
public function testCanBeCreatedFromAThreeDimensionalArray() |
68
|
|
|
{ |
69
|
|
|
# three-dimensional array |
70
|
|
|
$multidimensional = [ |
71
|
|
|
"foo" => [ |
72
|
|
|
"bar" => [ |
73
|
|
|
"abc" => false |
74
|
|
|
] |
75
|
|
|
], |
76
|
|
|
"foo2" => [ |
77
|
|
|
"bar2" => [ |
78
|
|
|
"def" => 7854 |
79
|
|
|
] |
80
|
|
|
] |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
$expected = [ |
84
|
|
|
"foo_bar_abc" => false, |
85
|
|
|
"foo2_bar2_def" => 7854 |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
$unidimensional = ArrayDimension::toUnidimensional($multidimensional, "_"); |
89
|
|
|
|
90
|
|
|
$this->assertEquals($expected, $unidimensional); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Tests if it's possible transform a mixed n-dimensional array into an unidimensional |
95
|
|
|
* |
96
|
|
|
* @return null |
97
|
|
|
*/ |
98
|
|
|
public function testCanBeCreatedFromAMixedArray() |
99
|
|
|
{ |
100
|
|
|
# mixed n-dimensional array |
101
|
|
|
$multidimensional = [ |
102
|
|
|
"foo" => "value", |
103
|
|
|
"bar" => [ |
104
|
|
|
"abc" => false |
105
|
|
|
], |
106
|
|
|
"foo2" => [ |
107
|
|
|
"bar2" => [ |
108
|
|
|
"def" => 7854 |
109
|
|
|
] |
110
|
|
|
] |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
$expected = [ |
114
|
|
|
"foo" => "value", |
115
|
|
|
"bar_abc" => false, |
116
|
|
|
"foo2_bar2_def" => 7854 |
117
|
|
|
]; |
118
|
|
|
|
119
|
|
|
$unidimensional = ArrayDimension::toUnidimensional($multidimensional, "_"); |
120
|
|
|
|
121
|
|
|
$this->assertEquals($expected, $unidimensional); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Tests if it's possible transform an object to an array |
126
|
|
|
* |
127
|
|
|
* @return null |
128
|
|
|
*/ |
129
|
|
|
public function testConvertAnObjectToArray() |
130
|
|
|
{ |
131
|
|
|
$object = new \StdClass(); |
132
|
|
|
$object->id = 34389; |
133
|
|
|
$object->prices = new \StdClass(); |
134
|
|
|
$object->prices->house = 1200000; |
135
|
|
|
$object->prices->car = 650000; |
136
|
|
|
|
137
|
|
|
$expected = [ |
138
|
|
|
"id" => 34389, |
139
|
|
|
"prices" => [ |
140
|
|
|
"house" => 1200000, |
141
|
|
|
"car" => 650000 |
142
|
|
|
] |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
$array = ArrayDimension::objectToArray($object); |
146
|
|
|
|
147
|
|
|
$this->assertSame($expected, $array); |
148
|
|
|
} |
149
|
|
|
} |