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