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\Naming\UnderscoreNamingStrategy; |
14
|
|
|
use Rafrsr\LibArray2Object\Object2ArrayBuilder; |
15
|
|
|
use Rafrsr\LibArray2Object\Parser\CallableParser; |
16
|
|
|
use Rafrsr\LibArray2Object\Tests\Fixtures\NameSpace2\Manager; |
17
|
|
|
use Rafrsr\LibArray2Object\Tests\Fixtures\Player; |
18
|
|
|
use Rafrsr\LibArray2Object\Tests\Fixtures\Team; |
19
|
|
|
|
20
|
|
|
class Object2ArrayTest extends \PHPUnit_Framework_TestCase |
21
|
|
|
{ |
22
|
|
|
public function testCreateArray() |
23
|
|
|
{ |
24
|
|
|
$team = new Team('Dream Team'); |
25
|
|
|
$team->setCreatedAt(new \DateTime('2016-01-01')); |
26
|
|
|
|
27
|
|
|
$manager = new Manager(); |
28
|
|
|
$manager->setName('Big Manager'); |
29
|
|
|
$manager->setSalary('10000'); |
30
|
|
|
$team->setManager($manager); |
31
|
|
|
|
32
|
|
|
$player1 = new Player(); |
33
|
|
|
$player1->setName('Player 1'); |
34
|
|
|
$player1->setNumber('1'); |
35
|
|
|
$player1->setHeight('1.80'); |
36
|
|
|
$team->setPlayers([$player1]); |
37
|
|
|
|
38
|
|
|
//register custom parser |
39
|
|
|
$object2Array = Object2ArrayBuilder::create()->addParser( |
40
|
|
|
new CallableParser( |
41
|
|
|
function ($value, $type, \ReflectionProperty $property, $object) { |
|
|
|
|
42
|
|
|
if ($property->getName() === 'salary') { |
43
|
|
|
$value = '$'.$value; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $value; |
47
|
|
|
} |
48
|
|
|
) |
49
|
|
|
)->build(); |
50
|
|
|
|
51
|
|
|
$array = $object2Array->createArray($team); |
52
|
|
|
static::assertEquals($team->getName(), $array['name']); |
53
|
|
|
static::assertEquals('2016-01-01 00:00:00', $array['createdAt']); |
54
|
|
|
static::assertEquals($manager->getName(), $array['manager']['name']); |
55
|
|
|
static::assertEquals('$10000', $array['manager']['salary']); |
56
|
|
|
static::assertEquals('Player 1', $array['players'][0]['name']); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testCreateArrayWithUnderScoreNamingStrategy() |
60
|
|
|
{ |
61
|
|
|
$team = new Team('Dream Team'); |
62
|
|
|
$team->setCreatedAt(new \DateTime('2016-01-01')); |
63
|
|
|
|
64
|
|
|
//register custom parser |
65
|
|
|
$object2Array = Object2ArrayBuilder::create()->setNamingStrategy(new UnderscoreNamingStrategy())->build(); |
66
|
|
|
|
67
|
|
|
$array = $object2Array->createArray($team); |
68
|
|
|
static::assertEquals($team->getName(), $array['name']); |
69
|
|
|
static::assertEquals('2016-01-01 00:00:00', $array['created_at']); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testCreateArrayWithNulls() |
73
|
|
|
{ |
74
|
|
|
$team = new Team('Dream Team'); |
75
|
|
|
|
76
|
|
|
//register custom parser |
77
|
|
|
$object2Array = Object2ArrayBuilder::create() |
78
|
|
|
->setIgnoreNulls(false) |
79
|
|
|
->build(); |
80
|
|
|
|
81
|
|
|
$array = $object2Array->createArray($team); |
82
|
|
|
|
83
|
|
|
static::assertEquals($team->getName(), $array['name']); |
84
|
|
|
static::assertNull($array['id']); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testSerializeJson() |
88
|
|
|
{ |
89
|
|
|
$team = new Team('Dream Team'); |
90
|
|
|
$team->setPlayers([new Player('Player 1', 1)]); |
91
|
|
|
$array = Object2ArrayBuilder::create()->build()->createArray($team); |
92
|
|
|
static::assertEquals('{"name":"Dream Team","manager":[],"players":[{"name":"Player 1","number":1}]}', json_encode($array)); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.