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