1
|
|
|
<?php |
2
|
|
|
namespace Thunder\Serializard\Tests; |
3
|
|
|
|
4
|
|
|
use Thunder\Serializard\Exception\HydratorConflictException; |
5
|
|
|
use Thunder\Serializard\Exception\HydratorNotFoundException; |
6
|
|
|
use Thunder\Serializard\Exception\ClassNotFoundException; |
7
|
|
|
use Thunder\Serializard\HydratorContainer\FallbackHydratorContainer; |
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 HydratorContainerTest extends AbstractTestCase |
21
|
|
|
{ |
22
|
|
View Code Duplication |
public function testAlias() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$handlers = new FallbackHydratorContainer(); |
25
|
|
|
$handlers->add(\stdClass::class, function() { return 'value'; }); |
26
|
|
|
$handlers->addAlias(\DateTime::class, \stdClass::class); |
27
|
|
|
|
28
|
|
|
$this->assertSame('value', call_user_func($handlers->getHandler(\stdClass::class))); |
29
|
|
|
$this->assertSame('value', call_user_func($handlers->getHandler(\DateTime::class))); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
View Code Duplication |
public function testInterface() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$hydrators = new FallbackHydratorContainer(); |
35
|
|
|
$hydrators->add(TypeInterface::class, function() { return 'type'; }); |
36
|
|
|
|
37
|
|
|
$this->assertSame('type', call_user_func($hydrators->getHandler(TypeA::class))); |
38
|
|
|
$this->assertSame('type', call_user_func($hydrators->getHandler(TypeB::class))); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function testInheritance() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$hydrators = new FallbackHydratorContainer(); |
44
|
|
|
$hydrators->add(FakeUserParentParent::class, function() { return 'ancestor'; }); |
45
|
|
|
|
46
|
|
|
$this->assertSame('ancestor', call_user_func($hydrators->getHandler(FakeUserParentParent::class))); |
47
|
|
|
$this->assertSame('ancestor', call_user_func($hydrators->getHandler(FakeUserParent::class))); |
48
|
|
|
$this->assertSame('ancestor', call_user_func($hydrators->getHandler(FakeUser::class))); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
View Code Duplication |
public function testMultipleInterfacesException() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$hydrators = new FallbackHydratorContainer(); |
54
|
|
|
$hydrators->add(TypeInterface::class, function() { return 'multiple'; }); |
55
|
|
|
$hydrators->add(AnotherTypeInterface::class, function() { return 'multiple'; }); |
56
|
|
|
|
57
|
|
|
$this->expectExceptionClass(HydratorConflictException::class); |
58
|
|
|
$hydrators->getHandler(TypeMultiple::class); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testInvalidClassOrInterfaceName() |
62
|
|
|
{ |
63
|
|
|
$handlers = new FallbackHydratorContainer(); |
64
|
|
|
$this->expectExceptionClass(ClassNotFoundException::class); |
65
|
|
|
$handlers->add('invalid', function() {}); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testAliasForInvalidClass() |
69
|
|
|
{ |
70
|
|
|
$handlers = new FallbackHydratorContainer(); |
71
|
|
|
$this->expectExceptionClass(HydratorNotFoundException::class); |
72
|
|
|
$handlers->addAlias(\stdClass::class, \DateTime::class); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
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.