-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDynamicPropertyTest.php
143 lines (116 loc) · 4.58 KB
/
DynamicPropertyTest.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
/*
* This file is part of rekalogika/mapper package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/
namespace Rekalogika\Mapper\Tests\IntegrationTest;
use Rekalogika\Mapper\Tests\Common\FrameworkTestCase;
use Rekalogika\Mapper\Tests\Fixtures\DynamicProperty\ObjectExtendingStdClass;
use Rekalogika\Mapper\Tests\Fixtures\Scalar\ObjectWithScalarProperties;
use Rekalogika\Mapper\Tests\Fixtures\ScalarDto\ObjectWithScalarPropertiesDto;
class DynamicPropertyTest extends FrameworkTestCase
{
// from stdclass to object
public function testStdClassToObject(): void
{
$source = new \stdClass();
$source->a = 1;
$source->b = 'string';
$source->c = true;
$source->d = 1.1;
$target = $this->mapper->map($source, ObjectWithScalarPropertiesDto::class);
$this->assertInstanceOf(ObjectWithScalarPropertiesDto::class, $target);
$this->assertSame(1, $target->a);
$this->assertSame('string', $target->b);
$this->assertTrue($target->c);
$this->assertSame(1.1, $target->d);
}
public function testObjectExtendingStdClassToObject(): void
{
$source = new ObjectExtendingStdClass();
/** @psalm-suppress UndefinedPropertyAssignment */
$source->a = 1;
/** @psalm-suppress UndefinedPropertyAssignment */
$source->b = 'string';
/** @psalm-suppress UndefinedPropertyAssignment */
$source->c = true;
/** @psalm-suppress UndefinedPropertyAssignment */
$source->d = 1.1;
$target = $this->mapper->map($source, ObjectWithScalarPropertiesDto::class);
$this->assertInstanceOf(ObjectWithScalarPropertiesDto::class, $target);
$this->assertSame(1, $target->a);
$this->assertSame('string', $target->b);
$this->assertTrue($target->c);
$this->assertSame(1.1, $target->d);
}
public function testArrayCastToObjectToObject(): void
{
$source = [
'a' => 1,
'b' => 'string',
'c' => true,
'd' => 1.1,
];
$target = $this->mapper->map((object) $source, ObjectWithScalarPropertiesDto::class);
$this->assertInstanceOf(ObjectWithScalarPropertiesDto::class, $target);
$this->assertSame(1, $target->a);
$this->assertSame('string', $target->b);
$this->assertTrue($target->c);
$this->assertSame(1.1, $target->d);
}
public function testStdClassWithoutPropertiesToObject(): void
{
$source = new \stdClass();
$target = $this->mapper->map($source, ObjectWithScalarPropertiesDto::class);
$this->assertInstanceOf(ObjectWithScalarPropertiesDto::class, $target);
$this->assertNull($target->a);
$this->assertNull($target->b);
$this->assertNull($target->c);
$this->assertNull($target->d);
}
// to stdClass
public function testObjectToStdClass(): void
{
$source = new ObjectWithScalarProperties();
$target = $this->mapper->map($source, \stdClass::class);
$this->assertInstanceOf(\stdClass::class, $target);
$this->assertSame(1, $target->a);
$this->assertSame('string', $target->b);
$this->assertTrue($target->c);
$this->assertSame(1.1, $target->d);
}
public function testObjectToObjectExtendingStdClass(): void
{
$source = new ObjectWithScalarProperties();
$target = $this->mapper->map($source, ObjectExtendingStdClass::class);
$this->assertInstanceOf(ObjectExtendingStdClass::class, $target);
/** @psalm-suppress UndefinedPropertyFetch */
$this->assertSame(1, $target->a);
/** @psalm-suppress UndefinedPropertyFetch */
$this->assertSame('string', $target->b);
/** @psalm-suppress UndefinedPropertyFetch */
$this->assertTrue($target->c);
/** @psalm-suppress UndefinedPropertyFetch */
$this->assertSame(1.1, $target->d);
}
// stdclass to stdclass
public function testStdClassToStdClass(): void
{
$source = new \stdClass();
$source->a = 1;
$source->b = 'string';
$source->c = true;
$source->d = 1.1;
$target = $this->mapper->map($source, \stdClass::class);
$this->assertInstanceOf(\stdClass::class, $target);
$this->assertSame(1, $target->a);
$this->assertSame('string', $target->b);
$this->assertTrue($target->c);
$this->assertSame(1.1, $target->d);
}
}