|
1
|
|
|
<?php |
|
2
|
|
|
namespace Thunder\Serializard\Tests; |
|
3
|
|
|
|
|
4
|
|
|
use Thunder\Serializard\Normalizer\CallbackNormalizer; |
|
5
|
|
|
use Thunder\Serializard\Normalizer\ClosureBindNormalizer; |
|
6
|
|
|
use Thunder\Serializard\Normalizer\GetObjectVarsNormalizer; |
|
7
|
|
|
use Thunder\Serializard\Normalizer\ReflectionNormalizer; |
|
8
|
|
|
use Thunder\Serializard\Tests\Fake\Inheritance\FakeClass; |
|
9
|
|
|
use Thunder\Serializard\Tests\Fake\FakeTag; |
|
10
|
|
|
use Thunder\Serializard\Tests\Fake\FakeUser; |
|
11
|
|
|
use Thunder\Serializard\Tests\Fake\PropertyVisibility; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
final class NormalizerTest extends AbstractTestCase |
|
17
|
|
|
{ |
|
18
|
|
|
public function testReflectionSkip() |
|
19
|
|
|
{ |
|
20
|
|
|
$normalizer = new ReflectionNormalizer(['tag', 'tags']); |
|
21
|
|
|
$object = new FakeUser(12, 'XXX', new FakeTag(144, 'YYY')); |
|
22
|
|
|
|
|
23
|
|
|
$this->assertSame(['id' => 12, 'name' => 'XXX'], $normalizer($object)); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function testReflectionInheritance() |
|
27
|
|
|
{ |
|
28
|
|
|
$normalizer = new ReflectionNormalizer(); |
|
29
|
|
|
|
|
30
|
|
|
$this->assertSame([ |
|
31
|
|
|
'property' => 'property', |
|
32
|
|
|
'parentProperty' => 'parent', |
|
33
|
|
|
'parentParentProperty' => 'parentParent', |
|
34
|
|
|
], $normalizer(new FakeClass('parentParent', 'parent', 'property'))); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testGetObjectVarsNormalizer() |
|
38
|
|
|
{ |
|
39
|
|
|
$normalizer = new GetObjectVarsNormalizer(); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertSame(['public' => 'public'], $normalizer(new PropertyVisibility())); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testCallbackNormalizer() |
|
45
|
|
|
{ |
|
46
|
|
|
$normalizer = new CallbackNormalizer(function(PropertyVisibility $pv) { |
|
47
|
|
|
return ['public' => $pv->public]; |
|
48
|
|
|
}); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertSame(['public' => 'public'], $normalizer(new PropertyVisibility())); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testClosureBindNormalizer() |
|
54
|
|
|
{ |
|
55
|
|
|
$normalizer = new ClosureBindNormalizer(function() { |
|
56
|
|
|
return [ |
|
57
|
|
|
'id' => $this->id, |
|
|
|
|
|
|
58
|
|
|
'name' => $this->name, |
|
|
|
|
|
|
59
|
|
|
'tag' => $this->tag, |
|
|
|
|
|
|
60
|
|
|
]; |
|
61
|
|
|
}); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertEquals([ |
|
64
|
|
|
'id' => 1, |
|
65
|
|
|
'name' => 'user', |
|
66
|
|
|
'tag' => new FakeTag(2, 'tag'), |
|
67
|
|
|
], $normalizer(new FakeUser(1, 'user', new FakeTag(2, 'tag')))); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: