33
44use Thunder \Shortcode \Event \ReplaceShortcodesEvent ;
55use Thunder \Shortcode \EventContainer \EventContainer ;
6- use Thunder \Shortcode \Event \FilterShortcodesEvent ;
6+ use Thunder \Shortcode \EventHandler \FilterRawEventHandler ;
7+ use Thunder \Shortcode \EventHandler \ReplaceJoinEventHandler ;
78use Thunder \Shortcode \Events ;
89use Thunder \Shortcode \HandlerContainer \HandlerContainer ;
910use Thunder \Shortcode \Parser \RegularParser ;
1718 */
1819final class EventsTest extends \PHPUnit_Framework_TestCase
1920{
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-
4021 public function testRaw ()
4122 {
4223 $ times = 0 ;
@@ -46,12 +27,7 @@ public function testRaw()
4627 $ handlers ->add ('c ' , function (ShortcodeInterface $ s ) use (&$ times ) { ++$ times ; return $ s ->getContent (); });
4728
4829 $ 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- });
30+ $ events ->addListener (Events::FILTER_SHORTCODES , new FilterRawEventHandler (array ('raw ' )));
5531
5632 $ processor = new Processor (new RegularParser (), $ handlers );
5733 $ processor = $ processor ->withEventContainer ($ events );
@@ -69,20 +45,12 @@ public function testStripContentOutsideShortcodes()
6945 $ handlers ->add ('root ' , function (ProcessedShortcode $ s ) { return 'root[ ' .$ s ->getContent ().'] ' ; });
7046
7147 $ 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- });
48+ $ events ->addListener (Events::REPLACE_SHORTCODES , new ReplaceJoinEventHandler (array ('root ' )));
8149
8250 $ processor = new Processor (new RegularParser (), $ handlers );
8351 $ processor = $ processor ->withEventContainer ($ events );
8452
85- $ this ->assertSame ('a root[name name ] b ' , $ processor ->process ('a [root]x [name] c[content] [name /] [/content] y[/root] b ' ));
53+ $ this ->assertSame ('a root[name name name ] b ' , $ processor ->process ('a [root]x [name] c[content] [name /] [/content] y[name/] [/root] b ' ));
8654 }
8755
8856 public function testDefaultApplier ()
@@ -114,4 +82,16 @@ public function testExceptionOnHandlerForUnknownEvent()
11482 $ this ->setExpectedException ('InvalidArgumentException ' );
11583 $ events ->addListener ('invalid ' , function () {});
11684 }
85+
86+ public function testInvalidFilterRawShortcodesNames ()
87+ {
88+ $ this ->setExpectedException ('InvalidArgumentException ' );
89+ new FilterRawEventHandler (array (new \stdClass ()));
90+ }
91+
92+ public function testInvalidReplaceJoinNames ()
93+ {
94+ $ this ->setExpectedException ('InvalidArgumentException ' );
95+ new ReplaceJoinEventHandler (array (new \stdClass ()));
96+ }
11797}
0 commit comments