1
|
|
|
<?php |
2
|
|
|
namespace Thunder\Shortcode\Tests; |
3
|
|
|
|
4
|
|
|
use Thunder\Shortcode\Event\ApplyResultsEvent; |
5
|
|
|
use Thunder\Shortcode\EventContainer\EventContainer; |
6
|
|
|
use Thunder\Shortcode\Event\FilterShortcodesEvent; |
7
|
|
|
use Thunder\Shortcode\Events; |
8
|
|
|
use Thunder\Shortcode\HandlerContainer\HandlerContainer; |
9
|
|
|
use Thunder\Shortcode\Parser\RegularParser; |
10
|
|
|
use Thunder\Shortcode\Processor\Processor; |
11
|
|
|
use Thunder\Shortcode\Shortcode\ProcessedShortcode; |
12
|
|
|
use Thunder\Shortcode\Shortcode\ShortcodeInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
final class EventsTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
public function testFilterShortcodes() |
20
|
|
|
{ |
21
|
|
|
$handlers = new HandlerContainer(); |
22
|
|
|
$handlers->add('root', function(ShortcodeInterface $s) { return 'root['.$s->getContent().']'; }); |
23
|
|
|
$handlers->add('yes', function(ShortcodeInterface $s) { return 'yes['.$s->getContent().']'; }); |
24
|
|
|
$handlers->add('no', function(ShortcodeInterface $s) { return 'nope'; }); |
|
|
|
|
25
|
|
|
|
26
|
|
|
$events = new EventContainer(); |
27
|
|
|
$events->addListener(Events::FILTER_SHORTCODES, function(FilterShortcodesEvent $event) { |
28
|
|
|
$event->setShortcodes(array_filter($event->getShortcodes(), function(ShortcodeInterface $s) { |
29
|
|
|
return $s->getName() !== 'no'; |
30
|
|
|
})); |
31
|
|
|
}); |
32
|
|
|
|
33
|
|
|
$processor = new Processor(new RegularParser(), $handlers); |
34
|
|
|
$processor = $processor->withEventContainer($events); |
35
|
|
|
|
36
|
|
|
$this->assertSame('x root[ yes[ yes[] ] yes[ [no /] ] ] y', $processor->process('x [root] [yes] [yes/] [/yes] [yes] [no /] [/yes] [/root] y')); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testRaw() |
40
|
|
|
{ |
41
|
|
|
$times = 0; |
42
|
|
|
$handlers = new HandlerContainer(); |
43
|
|
|
$handlers->add('raw', function(ShortcodeInterface $s) { return $s->getContent(); }); |
44
|
|
|
$handlers->add('n', function(ShortcodeInterface $s) use(&$times) { ++$times; return $s->getName(); }); |
45
|
|
|
$handlers->add('c', function(ShortcodeInterface $s) use(&$times) { ++$times; return $s->getContent(); }); |
46
|
|
|
|
47
|
|
|
$events = new EventContainer(); |
48
|
|
|
$events->addListener(Events::FILTER_SHORTCODES, function(FilterShortcodesEvent $event) { |
49
|
|
|
if(!$event->getParent()) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
if($event->getParent()->getName() === 'raw' || $event->getParent()->hasAncestor('raw')) { |
53
|
|
|
$event->setShortcodes(array()); |
54
|
|
|
} |
55
|
|
|
}); |
56
|
|
|
|
57
|
|
|
$processor = new Processor(new RegularParser(), $handlers); |
58
|
|
|
$processor = $processor->withEventContainer($events); |
59
|
|
|
|
60
|
|
|
$this->assertSame(' [n] [c]cnt[/c] [/n] ', $processor->process('[raw] [n] [c]cnt[/c] [/n] [/raw]')); |
61
|
|
|
$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')); |
62
|
|
|
$this->assertEquals(2, $times); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testStripContentOutsideShortcodes() |
66
|
|
|
{ |
67
|
|
|
$handlers = new HandlerContainer(); |
68
|
|
|
$handlers->add('name', function(ShortcodeInterface $s) { return $s->getName(); }); |
69
|
|
|
$handlers->add('content', function(ShortcodeInterface $s) { return $s->getContent(); }); |
70
|
|
|
$handlers->add('root', function(ProcessedShortcode $s) { return 'root['.$s->getContent().']'; }); |
71
|
|
|
|
72
|
|
|
$events = new EventContainer(); |
73
|
|
|
$events->addListener(Events::APPLY_RESULTS, function(ApplyResultsEvent $event) { |
74
|
|
|
if(!$event->getShortcode()) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
if('root' === $event->getShortcode()->getName()) { |
78
|
|
|
$replaces = array(); |
79
|
|
|
foreach($event->getReplaces() as $replace) { |
80
|
|
|
$replaces[] = $replace[0]; |
81
|
|
|
} |
82
|
|
|
$event->setResult(implode('', $replaces)); |
83
|
|
|
} |
84
|
|
|
}); |
85
|
|
|
|
86
|
|
|
$processor = new Processor(new RegularParser(), $handlers); |
87
|
|
|
$processor = $processor->withEventContainer($events); |
88
|
|
|
|
89
|
|
|
$this->assertSame('a root[name name ] b', $processor->process('a [root]x [name] c[content] [name /] [/content] y[/root] b')); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testDefaultApplier() |
93
|
|
|
{ |
94
|
|
|
$handlers = new HandlerContainer(); |
95
|
|
|
$handlers->add('name', function(ShortcodeInterface $s) { return $s->getName(); }); |
96
|
|
|
$handlers->add('content', function(ShortcodeInterface $s) { return $s->getContent(); }); |
97
|
|
|
$handlers->add('root', function(ProcessedShortcode $s) { return 'root['.$s->getContent().']'; }); |
98
|
|
|
|
99
|
|
|
$events = new EventContainer(); |
100
|
|
|
$events->addListener(Events::APPLY_RESULTS, function(ApplyResultsEvent $event) { |
101
|
|
View Code Duplication |
$event->setResult(array_reduce(array_reverse($event->getReplaces()), function($state, array $item) { |
|
|
|
|
102
|
|
|
return mb_substr($state, 0, $item[1]).$item[0].mb_substr($state, $item[1] + $item[2]); |
103
|
|
|
}, $event->getText())); |
104
|
|
|
}); |
105
|
|
|
|
106
|
|
|
$processor = new Processor(new RegularParser(), $handlers); |
107
|
|
|
$processor = $processor->withEventContainer($events); |
108
|
|
|
|
109
|
|
|
$this->assertSame('a root[x name c name y] b', $processor->process('a [root]x [name] c[content] [name /] [/content] y[/root] b')); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testExceptionOnHandlerForUnknownEvent() |
113
|
|
|
{ |
114
|
|
|
$events = new EventContainer(); |
115
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
116
|
|
|
$events->addListener('invalid', function() {}); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.