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\Object2ArrayBuilder; |
13
|
|
|
use Rafrsr\LibArray2Object\Parser\CallableParser; |
14
|
|
|
use Rafrsr\LibArray2Object\Tests\Fixtures\Manager; |
15
|
|
|
use Rafrsr\LibArray2Object\Tests\Fixtures\Player; |
16
|
|
|
use Rafrsr\LibArray2Object\Tests\Fixtures\Team; |
17
|
|
|
|
18
|
|
|
class Object2ArrayTest extends \PHPUnit_Framework_TestCase |
19
|
|
|
{ |
20
|
|
|
public function testCreateArray() |
21
|
|
|
{ |
22
|
|
|
$team = new Team('Dream Team'); |
23
|
|
|
$team->setCreatedAt(new \DateTime('2016-01-01')); |
24
|
|
|
|
25
|
|
|
$manager = new Manager(); |
26
|
|
|
$manager->setName('Big Manager'); |
27
|
|
|
$manager->setSalary('10000'); |
28
|
|
|
$team->setManager($manager); |
29
|
|
|
|
30
|
|
|
$player1 = new Player(); |
31
|
|
|
$player1->setName('Player 1'); |
32
|
|
|
$player1->setNumber('1'); |
33
|
|
|
$player1->setHeight('1.80'); |
34
|
|
|
$team->setPlayers([$player1]); |
35
|
|
|
|
36
|
|
|
//register custom parser |
37
|
|
|
$object2Array = Object2ArrayBuilder::create()->addParser( |
38
|
|
|
new CallableParser( |
39
|
|
|
function ($value, $type, \ReflectionProperty $property, $object) { |
|
|
|
|
40
|
|
|
if ($property->getName() === 'salary') { |
41
|
|
|
$value = '$' . $value; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $value; |
45
|
|
|
} |
46
|
|
|
) |
47
|
|
|
)->build(); |
48
|
|
|
|
49
|
|
|
$array = $object2Array->createArray($team); |
50
|
|
|
static::assertEquals($team->getName(), $array['name']); |
51
|
|
|
static::assertEquals('2016-01-01T00:00:00+01:00', $array['createdAt']); |
52
|
|
|
static::assertEquals($manager->getName(), $array['manager']['name']); |
53
|
|
|
static::assertEquals('$10000', $array['manager']['salary']); |
54
|
|
|
static::assertEquals('Player 1', $array['players'][0]['name']); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.