1
|
|
|
<?php |
2
|
|
|
namespace Thunder\Serializard\Tests; |
3
|
|
|
|
4
|
|
|
use Thunder\Serializard\Exception\ClassNotFoundException; |
5
|
|
|
use Thunder\Serializard\Exception\NormalizerConflictException; |
6
|
|
|
use Thunder\Serializard\Exception\NormalizerNotFoundException; |
7
|
|
|
use Thunder\Serializard\NormalizerContainer\FallbackNormalizerContainer; |
8
|
|
|
use Thunder\Serializard\Tests\Fake\FakeUser; |
9
|
|
|
use Thunder\Serializard\Tests\Fake\FakeUserParent; |
10
|
|
|
use Thunder\Serializard\Tests\Fake\FakeUserParentParent; |
11
|
|
|
use Thunder\Serializard\Tests\Fake\Interfaces\AnotherTypeInterface; |
12
|
|
|
use Thunder\Serializard\Tests\Fake\Interfaces\TypeA; |
13
|
|
|
use Thunder\Serializard\Tests\Fake\Interfaces\TypeB; |
14
|
|
|
use Thunder\Serializard\Tests\Fake\Interfaces\TypeInterface; |
15
|
|
|
use Thunder\Serializard\Tests\Fake\Interfaces\TypeMultiple; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
final class NormalizerContainerTest extends AbstractTestCase |
21
|
|
|
{ |
22
|
|
View Code Duplication |
public function testAlias() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$normalizers = new FallbackNormalizerContainer(); |
25
|
|
|
$normalizers->add(\stdClass::class, function() { return 'value'; }); |
26
|
|
|
$normalizers->addAlias(\DateTime::class, \stdClass::class); |
27
|
|
|
|
28
|
|
|
$this->assertSame('value', call_user_func($normalizers->getHandler(\stdClass::class))); |
29
|
|
|
$this->assertSame('value', call_user_func($normalizers->getHandler(\DateTime::class))); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
View Code Duplication |
public function testInterface() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$normalizers = new FallbackNormalizerContainer(); |
35
|
|
|
$normalizers->add(TypeInterface::class, function() { return 'type'; }); |
36
|
|
|
|
37
|
|
|
$this->assertSame('type', call_user_func($normalizers->getHandler(TypeA::class))); |
38
|
|
|
$this->assertSame('type', call_user_func($normalizers->getHandler(TypeB::class))); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function testInheritance() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$normalizers = new FallbackNormalizerContainer(); |
44
|
|
|
$normalizers->add(FakeUserParentParent::class, function() { return 'ancestor'; }); |
45
|
|
|
|
46
|
|
|
$this->assertSame('ancestor', call_user_func($normalizers->getHandler(FakeUserParentParent::class))); |
47
|
|
|
$this->assertSame('ancestor', call_user_func($normalizers->getHandler(FakeUserParent::class))); |
48
|
|
|
$this->assertSame('ancestor', call_user_func($normalizers->getHandler(FakeUser::class))); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
View Code Duplication |
public function testMultipleInterfacesException() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$normalizers = new FallbackNormalizerContainer(); |
54
|
|
|
$normalizers->add(TypeInterface::class, function() { return 'multiple'; }); |
55
|
|
|
$normalizers->add(AnotherTypeInterface::class, function() { return 'multiple'; }); |
56
|
|
|
|
57
|
|
|
$this->expectExceptionClass(NormalizerConflictException::class); |
58
|
|
|
$normalizers->getHandler(TypeMultiple::class); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testNoDefaultHandler() |
62
|
|
|
{ |
63
|
|
|
$normalizers = new FallbackNormalizerContainer(); |
64
|
|
|
$this->assertFalse($normalizers->hasDefault()); |
65
|
|
|
$this->assertNull($normalizers->getDefault()); |
66
|
|
|
|
67
|
|
|
$handler = function() {}; |
68
|
|
|
$normalizers->setDefault($handler); |
69
|
|
|
$this->assertTrue($normalizers->hasDefault()); |
70
|
|
|
$this->assertSame($handler, $normalizers->getDefault()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testDefaultHandlerFallback() |
74
|
|
|
{ |
75
|
|
|
$direct = function() {}; |
76
|
|
|
$fallback = function() {}; |
77
|
|
|
$normalizers = new FallbackNormalizerContainer(); |
78
|
|
|
$normalizers->add(\DateTime::class, $direct); |
79
|
|
|
$normalizers->setDefault($fallback); |
80
|
|
|
|
81
|
|
|
$this->assertSame($direct, $normalizers->getHandler(\DateTime::class)); |
82
|
|
|
$this->assertSame($fallback, $normalizers->getHandler(\DateTimeImmutable::class)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testInvalidClassOrInterfaceName() |
86
|
|
|
{ |
87
|
|
|
$normalizers = new FallbackNormalizerContainer(); |
88
|
|
|
$this->expectExceptionClass(ClassNotFoundException::class); |
89
|
|
|
$normalizers->add('invalid', function() {}); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testAliasForInvalidClass() |
93
|
|
|
{ |
94
|
|
|
$normalizers = new FallbackNormalizerContainer(); |
95
|
|
|
$this->expectExceptionClass(NormalizerNotFoundException::class); |
96
|
|
|
$normalizers->addAlias(\stdClass::class, \DateTime::class); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.