1
|
|
|
<?php |
2
|
|
|
namespace Thunder\Serializard\Tests; |
3
|
|
|
use Thunder\Serializard\Format\ArrayFormat; |
4
|
|
|
use Thunder\Serializard\FormatContainer\FormatContainer; |
5
|
|
|
use Thunder\Serializard\HandlerContainer\HandlerContainer; |
6
|
|
|
use Thunder\Serializard\Serializard; |
7
|
|
|
use Thunder\Serializard\Tests\Fake\Interfaces\TypeMultiple; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class HandlerContainerTest extends \PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
public function testAlias() |
15
|
|
|
{ |
16
|
|
|
$handlers = new HandlerContainer(); |
17
|
|
|
$handlers->add('stdClass', 'std', function() { |
18
|
|
|
return 'value'; |
19
|
|
|
}); |
20
|
|
|
$handlers->addAlias('DateTime', 'stdClass'); |
21
|
|
|
|
22
|
|
|
$this->assertSame('value', call_user_func_array($handlers->getHandler('stdClass'), array())); |
23
|
|
|
$this->assertSame('value', call_user_func_array($handlers->getHandler('DateTime'), array())); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testInvalidClassOrInterfaceName() |
27
|
|
|
{ |
28
|
|
|
$this->setExpectedException('RuntimeException'); |
29
|
|
|
$handlers = new HandlerContainer(); |
30
|
|
|
$handlers->add('invalid', 'root', function() {}); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testExceptionOnMultipleInterfaces() |
34
|
|
|
{ |
35
|
|
|
$handlers = new HandlerContainer(); |
36
|
|
|
$handlers->add('Thunder\Serializard\Tests\Fake\Interfaces\TypeInterface', 'root', function() {}); |
37
|
|
|
$handlers->add('Thunder\Serializard\Tests\Fake\Interfaces\AnotherTypeInterface', 'root', function() {}); |
38
|
|
|
|
39
|
|
|
$normalizers = new HandlerContainer(); |
40
|
|
|
|
41
|
|
|
$formats = new FormatContainer(); |
42
|
|
|
$formats->add('array', new ArrayFormat()); |
43
|
|
|
|
44
|
|
|
$serializard = new Serializard($formats, $handlers, $normalizers); |
45
|
|
|
$this->setExpectedException('RuntimeException'); |
46
|
|
|
$serializard->serialize(new TypeMultiple(), 'array'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testAliasForInvalidClass() |
50
|
|
|
{ |
51
|
|
|
$this->setExpectedException('RuntimeException'); |
52
|
|
|
$handlers = new HandlerContainer(); |
53
|
|
|
$handlers->addAlias('stdClass', 'DateTime'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testInvalidHandler() |
57
|
|
|
{ |
58
|
|
|
$this->setExpectedException('RuntimeException'); |
59
|
|
|
$handlers = new HandlerContainer(); |
60
|
|
|
$handlers->add('stdClass', 'name', 'invalid'); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|