This repository has been archived by the owner on Jul 2, 2022. It is now read-only.
/
DumperExampleTest.php
102 lines (81 loc) · 2.35 KB
/
DumperExampleTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/*
* Alkahest
*/
namespace tests\Alkahest;
use Trismegiste\Alkahest\Transform\Delegation\MappingDirector;
use Trismegiste\Alkahest\Transform\Delegation\Stage\Neutral;
use Trismegiste\Alkahest\Transform\Transformer;
/**
* DumperExample gives example of the serializing process
*/
class DumperExampleTest extends \PHPUnit\Framework\TestCase {
protected $transform;
protected function setUp(): void {
$director = new MappingDirector();
$mappingChain = $director->create(new Neutral());
$this->transform = new Transformer($mappingChain);
}
/**
* Transforms a complex object with non-empty constructor to
* a recursive array
*/
public function testSerialize() {
$product = new LightSaber('red');
$product->setOwner(new Owner('vader'));
$dump = $this->transform->desegregate($product);
$this->assertEquals([
'-fqcn' => 'tests\\Alkahest\\LightSaber',
'color' => 'red',
'owner' => [
'-fqcn' => 'tests\\Alkahest\\Owner',
'name' => 'vader',
],
], $dump);
}
/**
* Creates a complex object with non-empty constructor from
* a recursive array
*/
public function testUnserialize() {
$dump = [
'-fqcn' => 'tests\\Alkahest\\LightSaber',
'color' => 'red',
'owner' => [
'-fqcn' => 'tests\\Alkahest\\Owner',
'name' => 'vader',
],
];
$product = $this->transform->create($dump);
$this->assertInstanceOf(__NAMESPACE__ . '\LightSaber', $product);
$this->assertEquals('red', $product->getColor());
$this->assertEquals('vader', $product->getOwnerName());
}
}
//////////////////////////////
// some example class
class LightSaber {
protected $color;
protected $owner;
public function __construct($c) {
$this->color = $c;
}
public function getColor() {
return $this->color;
}
public function setOwner(Owner $own) {
$this->owner = $own;
}
public function getOwnerName() {
return $this->owner->getName();
}
}
class Owner {
protected $name;
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}