-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathEventsTest.php
97 lines (80 loc) · 4.2 KB
/
EventsTest.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
<?php
namespace Thunder\Shortcode\Tests;
use Thunder\Shortcode\Event\ReplaceShortcodesEvent;
use Thunder\Shortcode\EventContainer\EventContainer;
use Thunder\Shortcode\EventHandler\FilterRawEventHandler;
use Thunder\Shortcode\EventHandler\ReplaceJoinEventHandler;
use Thunder\Shortcode\Events;
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
use Thunder\Shortcode\Parser\RegularParser;
use Thunder\Shortcode\Processor\Processor;
use Thunder\Shortcode\Shortcode\ReplacedShortcode;
use Thunder\Shortcode\Shortcode\ProcessedShortcode;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
/**
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
*/
final class EventsTest extends AbstractTestCase
{
public function testRaw()
{
$times = 0;
$handlers = new HandlerContainer();
$handlers->add('raw', function(ShortcodeInterface $s) { return $s->getContent(); });
$handlers->add('n', function(ShortcodeInterface $s) use(&$times) { ++$times; return $s->getName(); });
$handlers->add('c', function(ShortcodeInterface $s) use(&$times) { ++$times; return $s->getContent(); });
$events = new EventContainer();
$events->addListener(Events::FILTER_SHORTCODES, new FilterRawEventHandler(array('raw')));
$processor = new Processor(new RegularParser(), $handlers);
$processor = $processor->withEventContainer($events);
$this->assertSame(' [n] [c]cnt[/c] [/n] ', $processor->process('[raw] [n] [c]cnt[/c] [/n] [/raw]'));
$this->assertSame('x un [n] [c]cnt[/c] [/n] y', $processor->process('x [c]u[n][/c][raw] [n] [c]cnt[/c] [/n] [/raw] y'));
$this->assertEquals(2, $times);
}
public function testStripContentOutsideShortcodes()
{
$handlers = new HandlerContainer();
$handlers->add('name', function(ShortcodeInterface $s) { return $s->getName(); });
$handlers->add('content', function(ShortcodeInterface $s) { return $s->getContent(); });
$handlers->add('root', function(ProcessedShortcode $s) { return 'root['.$s->getContent().']'; });
$events = new EventContainer();
$events->addListener(Events::REPLACE_SHORTCODES, new ReplaceJoinEventHandler(array('root')));
$processor = new Processor(new RegularParser(), $handlers);
$processor = $processor->withEventContainer($events);
$this->assertSame('a root[name name name] b', $processor->process('a [root]x [name] c[content] [name /] [/content] y[name/][/root] b'));
}
public function testDefaultApplier()
{
$handlers = new HandlerContainer();
$handlers->add('name', function(ShortcodeInterface $s) { return $s->getName(); });
$handlers->add('content', function(ShortcodeInterface $s) { return $s->getContent(); });
$handlers->add('root', function(ProcessedShortcode $s) { return 'root['.$s->getContent().']'; });
$events = new EventContainer();
$events->addListener(Events::REPLACE_SHORTCODES, function(ReplaceShortcodesEvent $event) {
$event->setResult(array_reduce(array_reverse($event->getReplacements()), function($state, ReplacedShortcode $r) {
$offset = $r->getOffset();
$length = mb_strlen($r->getText());
return mb_substr($state, 0, $offset).$r->getReplacement().mb_substr($state, $offset + $length);
}, $event->getText()));
});
$processor = new Processor(new RegularParser(), $handlers);
$processor = $processor->withEventContainer($events);
$this->assertSame('a root[x name c name y] b', $processor->process('a [root]x [name] c[content] [name /] [/content] y[/root] b'));
}
public function testExceptionOnHandlerForUnknownEvent()
{
$events = new EventContainer();
$this->willThrowException('InvalidArgumentException');
$events->addListener('invalid', function() {});
}
public function testInvalidFilterRawShortcodesNames()
{
$this->willThrowException('InvalidArgumentException');
new FilterRawEventHandler(array(new \stdClass()));
}
public function testInvalidReplaceJoinNames()
{
$this->willThrowException('InvalidArgumentException');
new ReplaceJoinEventHandler(array(new \stdClass()));
}
}