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