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