1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Papper\Tests; |
4
|
|
|
|
5
|
|
|
use Papper\Engine; |
6
|
|
|
|
7
|
|
|
class MappingEngineTest extends TestCaseBase |
8
|
|
|
{ |
9
|
|
|
public function testMapperShouldCreateInstanceOfDestinationClass() |
10
|
|
|
{ |
11
|
|
|
// arrange |
12
|
|
|
$engine = new Engine(); |
13
|
|
|
// act |
14
|
|
|
/** @var Destination $destination */ |
15
|
|
|
$destination = $engine->map(new Source())->toType(Destination::className()); |
16
|
|
|
// assert |
17
|
|
|
$this->assertInstanceOf(Destination::className(), $destination); |
18
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testMappingFromPropertyToProperties() |
22
|
|
|
{ |
23
|
|
|
// arrange |
24
|
|
|
$engine = new Engine(); |
25
|
|
|
$source = new Source(); |
26
|
|
|
$source->propertyToProperty = 'property to property'; |
27
|
|
|
$source->propertyToSetter = 'property to setter'; |
28
|
|
|
$source->setGetterToProperty('getter to property'); |
29
|
|
|
$source->setGetterToSetter('getter to setter'); |
30
|
|
|
// act |
31
|
|
|
/** @var Destination $destination */ |
32
|
|
|
$destination = $engine->map($source)->toType(Destination::className()); |
33
|
|
|
// assert |
34
|
|
|
$this->assertEquals('property to property', $destination->propertyToProperty); |
35
|
|
|
$this->assertEquals('property to setter', $destination->getPropertyToSetter()); |
36
|
|
|
$this->assertEquals('getter to property', $destination->getterToProperty); |
37
|
|
|
$this->assertEquals('getter to setter', $destination->getGetterToSetter()); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
class Source extends FixtureBase |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
public $propertyToProperty; |
44
|
|
|
public $propertyToSetter; |
45
|
|
|
private $getterToProperty; |
46
|
|
|
private $getterToSetter; |
47
|
|
|
|
48
|
|
|
public function getGetterToProperty() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
return $this->getterToProperty; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function setGetterToProperty($getterToProperty) |
54
|
|
|
{ |
55
|
|
|
$this->getterToProperty = $getterToProperty; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getGetterToSetter() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
return $this->getterToSetter; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function setGetterToSetter($getterToSetter) |
64
|
|
|
{ |
65
|
|
|
$this->getterToSetter = $getterToSetter; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
class Destination extends FixtureBase |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
public $propertyToProperty; |
72
|
|
|
private $propertyToSetter; |
73
|
|
|
public $getterToProperty; |
74
|
|
|
private $getterToSetter; |
75
|
|
|
|
76
|
|
|
public function getGetterToSetter() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
return $this->getterToSetter; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function setGetterToSetter($getterToSetter) |
82
|
|
|
{ |
83
|
|
|
$this->getterToSetter = $getterToSetter; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getPropertyToSetter() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
return $this->propertyToSetter; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function setPropertyToSetter($propertyToSetter) |
92
|
|
|
{ |
93
|
|
|
$this->propertyToSetter = $propertyToSetter; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.