1
|
|
|
<?php |
2
|
|
|
namespace Thunder\Serializard\Tests; |
3
|
|
|
|
4
|
|
|
use Thunder\Serializard\NormalizerContainer\NormalizerContainer; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
8
|
|
|
*/ |
9
|
|
View Code Duplication |
class NormalizerContainerTest extends \PHPUnit_Framework_TestCase |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
public function testAlias() |
12
|
|
|
{ |
13
|
|
|
$handlers = new NormalizerContainer(); |
14
|
|
|
$handlers->add('stdClass', 'std', function() { return 'value'; }); |
15
|
|
|
$handlers->addAlias('DateTime', 'stdClass'); |
16
|
|
|
|
17
|
|
|
$this->assertSame('value', call_user_func_array($handlers->getHandler('stdClass'), array())); |
18
|
|
|
$this->assertSame('value', call_user_func_array($handlers->getHandler('DateTime'), array())); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testInterface() |
22
|
|
|
{ |
23
|
|
|
$hydrators = new NormalizerContainer(); |
24
|
|
|
$interfaceName = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeInterface'; |
25
|
|
|
$interfaceTypeA = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeA'; |
26
|
|
|
$interfaceTypeB = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeB'; |
27
|
|
|
$hydrators->add($interfaceName, 'type', function() { return 'type'; }); |
28
|
|
|
|
29
|
|
|
$this->assertSame('type', call_user_func_array($hydrators->getHandler($interfaceTypeA), array())); |
30
|
|
|
$this->assertSame('type', call_user_func_array($hydrators->getHandler($interfaceTypeB), array())); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testInheritance() |
34
|
|
|
{ |
35
|
|
|
$hydrators = new NormalizerContainer(); |
36
|
|
|
$ancestorName = 'Thunder\Serializard\Tests\Fake\FakeUserParentParent'; |
37
|
|
|
$parentName = 'Thunder\Serializard\Tests\Fake\FakeUserParent'; |
38
|
|
|
$userName = 'Thunder\Serializard\Tests\Fake\FakeUser'; |
39
|
|
|
$hydrators->add($ancestorName, 'type', function() { return 'ancestor'; }); |
40
|
|
|
|
41
|
|
|
$this->assertSame('ancestor', call_user_func_array($hydrators->getHandler($ancestorName), array())); |
42
|
|
|
$this->assertSame('ancestor', call_user_func_array($hydrators->getHandler($parentName), array())); |
43
|
|
|
$this->assertSame('ancestor', call_user_func_array($hydrators->getHandler($userName), array())); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testMultipleInterfacesException() |
47
|
|
|
{ |
48
|
|
|
$typeInterface = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeInterface'; |
49
|
|
|
$typeAnother = 'Thunder\Serializard\Tests\Fake\Interfaces\AnotherTypeInterface'; |
50
|
|
|
$typeMultiple = 'Thunder\Serializard\Tests\Fake\Interfaces\TypeMultiple'; |
51
|
|
|
|
52
|
|
|
$hydrators = new NormalizerContainer(); |
53
|
|
|
$hydrators->add($typeInterface, 'type', function() { return 'multiple'; }); |
54
|
|
|
$hydrators->add($typeAnother, 'type', function() { return 'multiple'; }); |
55
|
|
|
|
56
|
|
|
$this->setExpectedException('RuntimeException'); |
|
|
|
|
57
|
|
|
$hydrators->getHandler($typeMultiple); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testInvalidClassOrInterfaceName() |
61
|
|
|
{ |
62
|
|
|
$handlers = new NormalizerContainer(); |
63
|
|
|
$this->setExpectedException('RuntimeException'); |
|
|
|
|
64
|
|
|
$handlers->add('invalid', 'root', function() {}); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testAliasForInvalidClass() |
68
|
|
|
{ |
69
|
|
|
$handlers = new NormalizerContainer(); |
70
|
|
|
$this->setExpectedException('RuntimeException'); |
|
|
|
|
71
|
|
|
$handlers->addAlias('stdClass', 'DateTime'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testInvalidHandler() |
75
|
|
|
{ |
76
|
|
|
$handlers = new NormalizerContainer(); |
77
|
|
|
$this->setExpectedException('RuntimeException'); |
|
|
|
|
78
|
|
|
$handlers->add('stdClass', 'name', 'invalid'); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
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.