|
1
|
|
|
<?php |
|
2
|
|
|
namespace Thunder\Shortcode\Tests; |
|
3
|
|
|
|
|
4
|
|
|
use Thunder\Shortcode\Event\ReplaceShortcodesEvent; |
|
5
|
|
|
use Thunder\Shortcode\EventContainer\EventContainer; |
|
6
|
|
|
use Thunder\Shortcode\EventHandler\FilterRawEventHandler; |
|
7
|
|
|
use Thunder\Shortcode\EventHandler\ReplaceJoinEventHandler; |
|
8
|
|
|
use Thunder\Shortcode\Events; |
|
9
|
|
|
use Thunder\Shortcode\HandlerContainer\HandlerContainer; |
|
10
|
|
|
use Thunder\Shortcode\Parser\RegularParser; |
|
11
|
|
|
use Thunder\Shortcode\Processor\Processor; |
|
12
|
|
|
use Thunder\Shortcode\Shortcode\ReplacedShortcode; |
|
13
|
|
|
use Thunder\Shortcode\Shortcode\ProcessedShortcode; |
|
14
|
|
|
use Thunder\Shortcode\Shortcode\ShortcodeInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
final class EventsTest extends AbstractTestCase |
|
20
|
|
|
{ |
|
21
|
|
|
public function testRaw() |
|
22
|
|
|
{ |
|
23
|
|
|
$times = 0; |
|
24
|
|
|
$handlers = new HandlerContainer(); |
|
25
|
|
|
$handlers->add('raw', function(ShortcodeInterface $s) { return $s->getContent(); }); |
|
26
|
|
|
$handlers->add('n', function(ShortcodeInterface $s) use(&$times) { ++$times; return $s->getName(); }); |
|
27
|
|
|
$handlers->add('c', function(ShortcodeInterface $s) use(&$times) { ++$times; return $s->getContent(); }); |
|
28
|
|
|
|
|
29
|
|
|
$events = new EventContainer(); |
|
30
|
|
|
$events->addListener(Events::FILTER_SHORTCODES, new FilterRawEventHandler(array('raw'))); |
|
31
|
|
|
|
|
32
|
|
|
$processor = new Processor(new RegularParser(), $handlers); |
|
33
|
|
|
$processor = $processor->withEventContainer($events); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertSame(' [n] [c]cnt[/c] [/n] ', $processor->process('[raw] [n] [c]cnt[/c] [/n] [/raw]')); |
|
36
|
|
|
$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')); |
|
37
|
|
|
$this->assertEquals(2, $times); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testStripContentOutsideShortcodes() |
|
41
|
|
|
{ |
|
42
|
|
|
$handlers = new HandlerContainer(); |
|
43
|
|
|
$handlers->add('name', function(ShortcodeInterface $s) { return $s->getName(); }); |
|
44
|
|
|
$handlers->add('content', function(ShortcodeInterface $s) { return $s->getContent(); }); |
|
45
|
|
|
$handlers->add('root', function(ProcessedShortcode $s) { return 'root['.$s->getContent().']'; }); |
|
46
|
|
|
|
|
47
|
|
|
$events = new EventContainer(); |
|
48
|
|
|
$events->addListener(Events::REPLACE_SHORTCODES, new ReplaceJoinEventHandler(array('root'))); |
|
49
|
|
|
|
|
50
|
|
|
$processor = new Processor(new RegularParser(), $handlers); |
|
51
|
|
|
$processor = $processor->withEventContainer($events); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertSame('a root[name name name] b', $processor->process('a [root]x [name] c[content] [name /] [/content] y[name/][/root] b')); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testDefaultApplier() |
|
57
|
|
|
{ |
|
58
|
|
|
$handlers = new HandlerContainer(); |
|
59
|
|
|
$handlers->add('name', function(ShortcodeInterface $s) { return $s->getName(); }); |
|
60
|
|
|
$handlers->add('content', function(ShortcodeInterface $s) { return $s->getContent(); }); |
|
61
|
|
|
$handlers->add('root', function(ProcessedShortcode $s) { return 'root['.$s->getContent().']'; }); |
|
62
|
|
|
|
|
63
|
|
|
$events = new EventContainer(); |
|
64
|
|
|
$events->addListener(Events::REPLACE_SHORTCODES, function(ReplaceShortcodesEvent $event) { |
|
65
|
|
|
$event->setResult(array_reduce(array_reverse($event->getReplacements()), function($state, ReplacedShortcode $r) { |
|
66
|
|
|
$offset = $r->getOffset(); |
|
67
|
|
|
$length = mb_strlen($r->getText()); |
|
68
|
|
|
|
|
69
|
|
|
return mb_substr($state, 0, $offset).$r->getReplacement().mb_substr($state, $offset + $length); |
|
70
|
|
|
}, $event->getText())); |
|
71
|
|
|
}); |
|
72
|
|
|
|
|
73
|
|
|
$processor = new Processor(new RegularParser(), $handlers); |
|
74
|
|
|
$processor = $processor->withEventContainer($events); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertSame('a root[x name c name y] b', $processor->process('a [root]x [name] c[content] [name /] [/content] y[/root] b')); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testExceptionOnHandlerForUnknownEvent() |
|
80
|
|
|
{ |
|
81
|
|
|
$events = new EventContainer(); |
|
82
|
|
|
$this->willThrowException('InvalidArgumentException'); |
|
83
|
|
|
$events->addListener('invalid', function() {}); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testInvalidFilterRawShortcodesNames() |
|
87
|
|
|
{ |
|
88
|
|
|
$this->willThrowException('InvalidArgumentException'); |
|
89
|
|
|
new FilterRawEventHandler(array(new \stdClass())); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function testInvalidReplaceJoinNames() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->willThrowException('InvalidArgumentException'); |
|
95
|
|
|
new ReplaceJoinEventHandler(array(new \stdClass())); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|