1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the rafrsr/lib-array2object package. |
5
|
|
|
* |
6
|
|
|
* (c) Rafael SR <https://github.com/rafrsr> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Rafrsr\LibArray2Object\Tests; |
12
|
|
|
|
13
|
|
|
use Rafrsr\LibArray2Object\Array2ObjectBuilder; |
14
|
|
|
use Rafrsr\LibArray2Object\Matcher\MapMatcher; |
15
|
|
|
use Rafrsr\LibArray2Object\Parser\CallableParser; |
16
|
|
|
use Rafrsr\LibArray2Object\Tests\Fixtures\Team; |
17
|
|
|
|
18
|
|
|
class Array2ObjectTest extends \PHPUnit_Framework_TestCase |
19
|
|
|
{ |
20
|
|
|
public function testArray2Object() |
21
|
|
|
{ |
22
|
|
|
$teamArray = [ |
23
|
|
|
'id' => 1, //read-only |
24
|
|
|
'name' => 'Dream Team', |
25
|
|
|
'Manager' => [ |
26
|
|
|
'name' => 'Big Manager', |
27
|
|
|
'salary' => '$10000', |
28
|
|
|
], |
29
|
|
|
'createdAt' => '2016-01-01', |
30
|
|
|
'points' => '25', |
31
|
|
|
'players' => [ |
32
|
|
|
[ |
33
|
|
|
'name' => 'Player 1', |
34
|
|
|
'number' => '1', |
35
|
|
|
'height' => '1.80', |
36
|
|
|
'regular' => 'true', |
37
|
|
|
], |
38
|
|
|
[ |
39
|
|
|
'name' => 'Player 2', |
40
|
|
|
'number' => '2', |
41
|
|
|
'height' => '1.85', |
42
|
|
|
'regular' => 0, |
43
|
|
|
], |
44
|
|
|
[ |
45
|
|
|
'name' => 'Player 3', |
46
|
|
|
'number' => '3', |
47
|
|
|
'height' => '1.78', |
48
|
|
|
'regular' => 'yes', |
49
|
|
|
], |
50
|
|
|
], |
51
|
|
|
'scores' => [ |
52
|
|
|
'2016' => '29', |
53
|
|
|
'2015' => '28', |
54
|
|
|
'2014' => '30', |
55
|
|
|
], |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
//register custom parser |
59
|
|
|
$array2Object = Array2ObjectBuilder::create()->addParser( |
60
|
|
|
new CallableParser( |
61
|
|
|
function ($value, $type, \ReflectionProperty $property, $object) { |
|
|
|
|
62
|
|
|
if ($property->getName() === 'salary') { |
63
|
|
|
$value = str_replace('$', null, $value); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $value; |
67
|
|
|
} |
68
|
|
|
) |
69
|
|
|
)->build(); |
70
|
|
|
|
71
|
|
|
/** @var Team $team */ |
72
|
|
|
$team = $array2Object->createObject(Team::class, $teamArray); |
73
|
|
|
static::assertNull($team->getId()); |
74
|
|
|
static::assertEquals('Dream Team', $team->getName()); |
75
|
|
|
static::assertEquals(25, $team->getPoints()); |
76
|
|
|
static::assertEquals(29, $team->getScores()[2016]); |
77
|
|
|
static::assertEquals('2016-01-01', $team->getCreatedAt()->format('Y-m-d')); |
78
|
|
|
|
79
|
|
|
static::assertEquals('Big Manager', $team->getManager()->getName()); |
80
|
|
|
static::assertEquals(10000, $team->getManager()->getSalary()); |
81
|
|
|
|
82
|
|
|
static::assertEquals('Player 1', $team->getPlayers()[0]->getName()); |
83
|
|
|
static::assertEquals(1, $team->getPlayers()[0]->getNumber()); |
84
|
|
|
static::assertEquals(1.80, $team->getPlayers()[0]->getHeight()); |
85
|
|
|
static::assertTrue($team->getPlayers()[0]->isRegular()); |
86
|
|
|
|
87
|
|
|
static::assertEquals('Player 2', $team->getPlayers()[1]->getName()); |
88
|
|
|
static::assertEquals(2, $team->getPlayers()[1]->getNumber()); |
89
|
|
|
static::assertEquals(1.85, $team->getPlayers()[1]->getHeight()); |
90
|
|
|
static::assertFalse($team->getPlayers()[1]->isRegular()); |
91
|
|
|
|
92
|
|
|
static::assertTrue($team->getPlayers()[2]->isRegular()); |
93
|
|
|
|
94
|
|
|
static::assertInternalType('string', $team->getName()); |
95
|
|
|
static::assertInternalType('integer', $team->getPoints()); |
96
|
|
|
static::assertInternalType('float', $team->getManager()->getSalary()); |
97
|
|
|
static::assertInternalType('boolean', $team->getPlayers()[0]->isRegular()); |
98
|
|
|
static::assertInternalType('float', $team->getPlayers()[0]->getHeight()); |
99
|
|
|
static::assertInternalType('boolean', $team->getPlayers()[2]->isRegular()); |
100
|
|
|
static::assertInternalType('integer', $team->getScores()[2016]); |
101
|
|
|
|
102
|
|
|
$teamArray = [ |
103
|
|
|
'name' => 'New Name', |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
$array2Object->populate($team, $teamArray); |
107
|
|
|
static::assertEquals('New Name', $team->getName()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @see https://github.com/rafrsr/lib-array2object/issues/1 |
112
|
|
|
*/ |
113
|
|
|
public function testArray2ObjectWithNestingChildren() |
114
|
|
|
{ |
115
|
|
|
$teamArray = [ |
116
|
|
|
'name' => 'Dream Team', |
117
|
|
|
'players' => [ |
118
|
|
|
'player' => [ |
119
|
|
|
[ |
120
|
|
|
'name' => 'Player 1', |
121
|
|
|
'number' => '1', |
122
|
|
|
'height' => '1.80', |
123
|
|
|
'regular' => 'true', |
124
|
|
|
], |
125
|
|
|
[ |
126
|
|
|
'name' => 'Player 2', |
127
|
|
|
'number' => '2', |
128
|
|
|
'height' => '1.85', |
129
|
|
|
'regular' => 0, |
130
|
|
|
], |
131
|
|
|
], |
132
|
|
|
], |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
$array2Object = Array2ObjectBuilder::create()->build(); |
136
|
|
|
|
137
|
|
|
/** @var Team $team */ |
138
|
|
|
$team = $array2Object->createObject(Team::class, $teamArray); |
139
|
|
|
static::assertEquals('Dream Team', $team->getName()); |
140
|
|
|
|
141
|
|
|
static::assertEquals('Player 1', $team->getPlayers()[0]->getName()); |
142
|
|
|
static::assertEquals(1, $team->getPlayers()[0]->getNumber()); |
143
|
|
|
static::assertEquals(1.80, $team->getPlayers()[0]->getHeight()); |
144
|
|
|
static::assertTrue($team->getPlayers()[0]->isRegular()); |
145
|
|
|
|
146
|
|
|
static::assertEquals('Player 2', $team->getPlayers()[1]->getName()); |
147
|
|
|
static::assertEquals(2, $team->getPlayers()[1]->getNumber()); |
148
|
|
|
static::assertEquals(1.85, $team->getPlayers()[1]->getHeight()); |
149
|
|
|
static::assertFalse($team->getPlayers()[1]->isRegular()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testArray2ObjectWithNestingChildrenOnlyOne() |
153
|
|
|
{ |
154
|
|
|
//with only one children |
155
|
|
|
//https://github.com/rafrsr/lib-array2object/issues/1#issuecomment-228155603 |
156
|
|
|
$teamArray = [ |
157
|
|
|
'name' => 'Dream Team', |
158
|
|
|
'players' => [ |
159
|
|
|
'player' => [ |
160
|
|
|
'name' => 'Player 1', |
161
|
|
|
'number' => '1', |
162
|
|
|
'height' => '1.80', |
163
|
|
|
'regular' => 'true', |
164
|
|
|
], |
165
|
|
|
], |
166
|
|
|
]; |
167
|
|
|
|
168
|
|
|
$array2Object = Array2ObjectBuilder::create()->build(); |
169
|
|
|
|
170
|
|
|
/** @var Team $team */ |
171
|
|
|
$team = $array2Object->createObject(Team::class, $teamArray); |
172
|
|
|
static::assertEquals('Dream Team', $team->getName()); |
173
|
|
|
static::assertEquals('Player 1', $team->getPlayers()[0]->getName()); |
174
|
|
|
static::assertEquals(1, $team->getPlayers()[0]->getNumber()); |
175
|
|
|
static::assertEquals(1.80, $team->getPlayers()[0]->getHeight()); |
176
|
|
|
static::assertTrue($team->getPlayers()[0]->isRegular()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function testPopulateObjectError() |
180
|
|
|
{ |
181
|
|
|
static::setExpectedException('\InvalidArgumentException'); |
|
|
|
|
182
|
|
|
Array2ObjectBuilder::create()->build()->populate(Team::class, []); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function testCreateObjectError() |
186
|
|
|
{ |
187
|
|
|
static::setExpectedException('\InvalidArgumentException'); |
|
|
|
|
188
|
|
|
Array2ObjectBuilder::create()->build()->createObject(new Team(), []); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testSettingPropertyMatcher() |
192
|
|
|
{ |
193
|
|
|
$array2Object = Array2ObjectBuilder::create()->setMatcher( |
194
|
|
|
new MapMatcher( |
195
|
|
|
[ |
196
|
|
|
'name' => 'nombre', |
197
|
|
|
] |
198
|
|
|
) |
199
|
|
|
)->build(); |
200
|
|
|
/** @var Team $team */ |
201
|
|
|
$team = $array2Object->createObject(Team::class, ['nombre' => 'Team']); |
202
|
|
|
static::assertEquals('Team', $team->getName()); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function testDeserializeJson() |
206
|
|
|
{ |
207
|
|
|
$json = '{"name":"Big Team","players":[{"name":"Player 1"}]}'; |
208
|
|
|
/** @var Team $team */ |
209
|
|
|
$team = Array2ObjectBuilder::create()->build()->createObject(Team::class, json_decode($json, true)); |
210
|
|
|
static::assertEquals('Big Team', $team->getName()); |
211
|
|
|
static::assertEquals('Player 1', $team->getPlayers()[0]->getName()); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.