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\EventHandler\RewriteWrapEventHandler; |
9
|
|
|
use Thunder\Shortcode\Events; |
10
|
|
|
use Thunder\Shortcode\HandlerContainer\HandlerContainer; |
11
|
|
|
use Thunder\Shortcode\Parser\RegularParser; |
12
|
|
|
use Thunder\Shortcode\Processor\Processor; |
13
|
|
|
use Thunder\Shortcode\Shortcode\ReplacedShortcode; |
14
|
|
|
use Thunder\Shortcode\Shortcode\ProcessedShortcode; |
15
|
|
|
use Thunder\Shortcode\Shortcode\ShortcodeInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
final class EventsTest extends AbstractTestCase |
21
|
|
|
{ |
22
|
|
|
public function testRaw() |
23
|
|
|
{ |
24
|
|
|
$times = 0; |
25
|
|
|
$handlers = new HandlerContainer(); |
26
|
|
|
$handlers->add('raw', function(ShortcodeInterface $s) { return $s->getContent(); }); |
27
|
|
|
$handlers->add('n', function(ShortcodeInterface $s) use(&$times) { ++$times; return $s->getName(); }); |
28
|
|
|
$handlers->add('c', function(ShortcodeInterface $s) use(&$times) { ++$times; return $s->getContent(); }); |
29
|
|
|
|
30
|
|
|
$events = new EventContainer(); |
31
|
|
|
$events->addListener(Events::FILTER_SHORTCODES, new FilterRawEventHandler(array('raw'))); |
32
|
|
|
|
33
|
|
|
$processor = new Processor(new RegularParser(), $handlers); |
34
|
|
|
$processor = $processor->withEventContainer($events); |
35
|
|
|
|
36
|
|
|
$this->assertSame(' [n] [c]cnt[/c] [/n] ', $processor->process('[raw] [n] [c]cnt[/c] [/n] [/raw]')); |
37
|
|
|
$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')); |
38
|
|
|
$this->assertEquals(2, $times); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function testStripContentOutsideShortcodes() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$handlers = new HandlerContainer(); |
44
|
|
|
$handlers->add('name', function(ShortcodeInterface $s) { return $s->getName(); }); |
45
|
|
|
$handlers->add('content', function(ShortcodeInterface $s) { return $s->getContent(); }); |
46
|
|
|
$handlers->add('root', function(ProcessedShortcode $s) { return 'root['.$s->getContent().']'; }); |
47
|
|
|
|
48
|
|
|
$events = new EventContainer(); |
49
|
|
|
$events->addListener(Events::REPLACE_SHORTCODES, new ReplaceJoinEventHandler(array('root'))); |
50
|
|
|
|
51
|
|
|
$processor = new Processor(new RegularParser(), $handlers); |
52
|
|
|
$processor = $processor->withEventContainer($events); |
53
|
|
|
|
54
|
|
|
$this->assertSame('a root[name name name] b', $processor->process('a [root]x [name] c[content] [name /] [/content] y[name/][/root] b')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testDefaultApplier() |
58
|
|
|
{ |
59
|
|
|
$handlers = new HandlerContainer(); |
60
|
|
|
$handlers->add('name', function(ShortcodeInterface $s) { return $s->getName(); }); |
61
|
|
|
$handlers->add('content', function(ShortcodeInterface $s) { return $s->getContent(); }); |
62
|
|
|
$handlers->add('root', function(ProcessedShortcode $s) { return 'root['.$s->getContent().']'; }); |
63
|
|
|
|
64
|
|
|
$events = new EventContainer(); |
65
|
|
|
$events->addListener(Events::REPLACE_SHORTCODES, function(ReplaceShortcodesEvent $event) { |
66
|
|
|
$event->setResult(array_reduce(array_reverse($event->getReplacements()), function($state, ReplacedShortcode $r) { |
67
|
|
|
$offset = $r->getOffset(); |
68
|
|
|
$length = mb_strlen($r->getText()); |
69
|
|
|
|
70
|
|
|
return mb_substr($state, 0, $offset).$r->getReplacement().mb_substr($state, $offset + $length); |
71
|
|
|
}, $event->getText())); |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
$processor = new Processor(new RegularParser(), $handlers); |
75
|
|
|
$processor = $processor->withEventContainer($events); |
76
|
|
|
|
77
|
|
|
$this->assertSame('a root[x name c name y] b', $processor->process('a [root]x [name] c[content] [name /] [/content] y[/root] b')); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
public function testRewriteReplacements() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$handlers = new HandlerContainer(); |
83
|
|
|
$handlers->add('name', function(ShortcodeInterface $s) { return $s->getName(); }); |
84
|
|
|
$handlers->add('content', function(ShortcodeInterface $s) { return $s->getContent(); }); |
85
|
|
|
$handlers->add('root', function(ProcessedShortcode $s) { return 'root['.$s->getContent().']'; }); |
86
|
|
|
|
87
|
|
|
$events = new EventContainer(); |
88
|
|
|
$events->addListener(Events::REWRITE_REPLACEMENTS, new RewriteWrapEventHandler('>', '<')); |
89
|
|
|
|
90
|
|
|
$processor = new Processor(new RegularParser(), $handlers); |
91
|
|
|
$processor = $processor->withEventContainer($events); |
92
|
|
|
|
93
|
|
|
$this->assertSame('a >root[x >name< c> >name< < y]< b', $processor->process('a [root]x [name] c[content] [name /] [/content] y[/root] b')); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testExceptionOnHandlerForUnknownEvent() |
97
|
|
|
{ |
98
|
|
|
$events = new EventContainer(); |
99
|
|
|
$this->willThrowException('InvalidArgumentException'); |
100
|
|
|
$events->addListener('invalid', function() {}); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testInvalidFilterRawShortcodesNames() |
104
|
|
|
{ |
105
|
|
|
$this->willThrowException('InvalidArgumentException'); |
106
|
|
|
new FilterRawEventHandler(array(new \stdClass())); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testInvalidReplaceJoinNames() |
110
|
|
|
{ |
111
|
|
|
$this->willThrowException('InvalidArgumentException'); |
112
|
|
|
new ReplaceJoinEventHandler(array(new \stdClass())); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.