1
|
|
|
<?php |
2
|
|
|
namespace Thunder\Shortcode\Tests; |
3
|
|
|
|
4
|
|
|
use Thunder\Shortcode\HandlerContainer\HandlerContainer; |
5
|
|
|
use Thunder\Shortcode\HandlerContainer\ImmutableHandlerContainer; |
6
|
|
|
use Thunder\Shortcode\Shortcode\ShortcodeInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
final class HandlerContainerTest extends AbstractTestCase |
12
|
|
|
{ |
13
|
|
|
public function testExceptionOnDuplicateHandler() |
14
|
|
|
{ |
15
|
|
|
$handlers = new HandlerContainer(); |
16
|
|
|
$handlers->add('name', function () {}); |
17
|
|
|
$this->willThrowException('RuntimeException'); |
18
|
|
|
$handlers->add('name', function () {}); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testRemove() |
22
|
|
|
{ |
23
|
|
|
$handlers = new HandlerContainer(); |
24
|
|
|
static::assertFalse($handlers->has('code')); |
25
|
|
|
$handlers->add('code', function(ShortcodeInterface $s) {}); |
|
|
|
|
26
|
|
|
static::assertTrue($handlers->has('code')); |
27
|
|
|
$handlers->remove('code'); |
28
|
|
|
static::assertFalse($handlers->has('code')); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testRemoveException() |
32
|
|
|
{ |
33
|
|
|
$handlers = new HandlerContainer(); |
34
|
|
|
$this->willThrowException('RuntimeException'); |
35
|
|
|
$handlers->remove('code'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testNames() |
39
|
|
|
{ |
40
|
|
|
$handlers = new HandlerContainer(); |
41
|
|
|
static::assertEmpty($handlers->getNames()); |
42
|
|
|
$handlers->add('code', function(ShortcodeInterface $s) {}); |
|
|
|
|
43
|
|
|
static::assertSame(array('code'), $handlers->getNames()); |
44
|
|
|
$handlers->addAlias('c', 'code'); |
45
|
|
|
static::assertSame(array('code', 'c'), $handlers->getNames()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testHandlerContainer() |
49
|
|
|
{ |
50
|
|
|
$x = function () {}; |
51
|
|
|
|
52
|
|
|
$handler = new HandlerContainer(); |
53
|
|
|
$handler->add('x', $x); |
54
|
|
|
$handler->addAlias('y', 'x'); |
55
|
|
|
|
56
|
|
|
static::assertSame($x, $handler->get('x')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testInvalidHandler() |
60
|
|
|
{ |
61
|
|
|
$handlers = new HandlerContainer(); |
62
|
|
|
$this->willThrowException('RuntimeException'); |
63
|
|
|
$handlers->add('invalid', new \stdClass()); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testDefaultHandler() |
67
|
|
|
{ |
68
|
|
|
$handlers = new HandlerContainer(); |
69
|
|
|
static::assertNull($handlers->get('missing')); |
70
|
|
|
|
71
|
|
|
$handlers->setDefault(function () {}); |
72
|
|
|
static::assertNotNull($handlers->get('missing')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testExceptionIfAliasingNonExistentHandler() |
76
|
|
|
{ |
77
|
|
|
$handlers = new HandlerContainer(); |
78
|
|
|
$this->willThrowException('RuntimeException'); |
79
|
|
|
$handlers->addAlias('m', 'missing'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testImmutableHandlerContainer() |
83
|
|
|
{ |
84
|
|
|
$handlers = new HandlerContainer(); |
85
|
|
|
$handlers->add('code', function () {}); |
86
|
|
|
$handlers->addAlias('c', 'code'); |
87
|
|
|
$imHandlers = new ImmutableHandlerContainer($handlers); |
88
|
|
|
$handlers->add('not', function() {}); |
89
|
|
|
|
90
|
|
|
static::assertNull($imHandlers->get('missing')); |
91
|
|
|
static::assertNotNull($imHandlers->get('code')); |
92
|
|
|
static::assertNotNull($imHandlers->get('c')); |
93
|
|
|
static::assertNull($imHandlers->get('not')); |
94
|
|
|
|
95
|
|
|
$defaultHandlers = new HandlerContainer(); |
96
|
|
|
$defaultHandlers->setDefault(function () {}); |
97
|
|
|
$imDefaultHandlers = new ImmutableHandlerContainer($defaultHandlers); |
98
|
|
|
static::assertNotNull($imDefaultHandlers->get('missing')); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.