1
|
|
|
<?php |
2
|
|
|
namespace Thunder\Shortcode\Tests; |
3
|
|
|
|
4
|
|
|
use Thunder\Shortcode\HandlerContainer\HandlerContainer; |
5
|
|
|
use Thunder\Shortcode\Parser\RegularParser; |
6
|
|
|
use Thunder\Shortcode\Parser\ParserInterface; |
7
|
|
|
use Thunder\Shortcode\Parser\RegexParser; |
8
|
|
|
use Thunder\Shortcode\Parser\WordpressParser; |
9
|
|
|
use Thunder\Shortcode\Shortcode\ParsedShortcode; |
10
|
|
|
use Thunder\Shortcode\Shortcode\Shortcode; |
11
|
|
|
use Thunder\Shortcode\Syntax\CommonSyntax; |
12
|
|
|
use Thunder\Shortcode\Syntax\Syntax; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
final class ParserTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param ParserInterface $parser |
21
|
|
|
* @param string $code |
22
|
|
|
* @param ParsedShortcode[] $tested |
23
|
|
|
* |
24
|
|
|
* @dataProvider provideShortcodes |
25
|
|
|
*/ |
26
|
|
|
public function testParser(ParserInterface $parser, $code, array $tested) |
27
|
|
|
{ |
28
|
|
|
$parsed = $parser->parse($code); |
29
|
|
|
|
30
|
|
|
$count = count($tested); |
31
|
|
|
$this->assertSame($count, count($parsed), 'counts'); |
32
|
|
|
for ($i = 0; $i < $count; $i++) { |
33
|
|
|
$this->assertSame($tested[$i]->getName(), $parsed[$i]->getName(), 'name'); |
34
|
|
|
$this->assertSame($tested[$i]->getParameters(), $parsed[$i]->getParameters(), 'parameters'); |
35
|
|
|
$this->assertSame($tested[$i]->getContent(), $parsed[$i]->getContent(), 'content'); |
36
|
|
|
$this->assertSame($tested[$i]->getText(), $parsed[$i]->getText(), 'text'); |
37
|
|
|
$this->assertSame($tested[$i]->getOffset(), $parsed[$i]->getOffset(), 'offset'); |
38
|
|
|
$this->assertSame($tested[$i]->getBbCode(), $parsed[$i]->getBbCode(), 'bbCode'); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function provideShortcodes() |
43
|
|
|
{ |
44
|
|
|
$s = new CommonSyntax(); |
45
|
|
|
|
46
|
|
|
$tests = array( |
47
|
|
|
// invalid |
48
|
|
|
array($s, '', array()), |
49
|
|
|
array($s, '[]', array()), |
50
|
|
|
array($s, '![](image.jpg)', array()), |
51
|
|
|
array($s, 'x html([a. title][, alt][, classes]) x', array()), |
52
|
|
|
array($s, '[/y]', array()), |
53
|
|
|
array($s, '[sc', array()), |
54
|
|
|
array($s, '[sc / [/sc]', array()), |
55
|
|
|
array($s, '[sc arg="val', array()), |
56
|
|
|
|
57
|
|
|
// single shortcodes |
58
|
|
|
array($s, '[sc]', array( |
59
|
|
|
new ParsedShortcode(new Shortcode('sc', array(), null), '[sc]', 0), |
60
|
|
|
)), |
61
|
|
|
array($s, '[sc arg=val]', array( |
62
|
|
|
new ParsedShortcode(new Shortcode('sc', array('arg' => 'val'), null), '[sc arg=val]', 0), |
63
|
|
|
)), |
64
|
|
|
array($s, '[sc novalue arg="complex value"]', array( |
65
|
|
|
new ParsedShortcode(new Shortcode('sc', array('novalue' => null, 'arg' => 'complex value'), null), '[sc novalue arg="complex value"]', 0), |
66
|
|
|
)), |
67
|
|
|
array($s, '[sc x="ąćęłńóśżź ĄĆĘŁŃÓŚŻŹ"]', array( |
68
|
|
|
new ParsedShortcode(new Shortcode('sc', array('x' => 'ąćęłńóśżź ĄĆĘŁŃÓŚŻŹ'), null), '[sc x="ąćęłńóśżź ĄĆĘŁŃÓŚŻŹ"]', 0), |
69
|
|
|
)), |
70
|
|
|
array($s, '[sc x="multi'."\n".'line"]', array( |
71
|
|
|
new ParsedShortcode(new Shortcode('sc', array('x' => 'multi'."\n".'line'), null), '[sc x="multi'."\n".'line"]', 0), |
72
|
|
|
)), |
73
|
|
|
array($s, '[sc noval x="val" y]content[/sc]', array( |
74
|
|
|
new ParsedShortcode(new Shortcode('sc', array('noval' => null, 'x' => 'val', 'y' => null), 'content'), '[sc noval x="val" y]content[/sc]', 0), |
75
|
|
|
)), |
76
|
|
|
array($s, '[sc x="{..}"]', array( |
77
|
|
|
new ParsedShortcode(new Shortcode('sc', array('x' => '{..}'), null), '[sc x="{..}"]', 0), |
78
|
|
|
)), |
79
|
|
|
array($s, '[sc a="x y" b="x" c=""]', array( |
80
|
|
|
new ParsedShortcode(new Shortcode('sc', array('a' => 'x y', 'b' => 'x', 'c' => ''), null), '[sc a="x y" b="x" c=""]', 0), |
81
|
|
|
)), |
82
|
|
|
array($s, '[sc a="a \"\" b"]', array( |
83
|
|
|
new ParsedShortcode(new Shortcode('sc', array('a' => 'a \"\" b'), null), '[sc a="a \"\" b"]', 0), |
84
|
|
|
)), |
85
|
|
|
array($s, '[sc/]', array( |
86
|
|
|
new ParsedShortcode(new Shortcode('sc', array(), null), '[sc/]', 0), |
87
|
|
|
)), |
88
|
|
|
array($s, '[sc /]', array( |
89
|
|
|
new ParsedShortcode(new Shortcode('sc', array(), null), '[sc /]', 0), |
90
|
|
|
)), |
91
|
|
|
array($s, '[sc arg=val cmp="a b"/]', array( |
92
|
|
|
new ParsedShortcode(new Shortcode('sc', array('arg' => 'val', 'cmp' => 'a b'), null), '[sc arg=val cmp="a b"/]', 0), |
93
|
|
|
)), |
94
|
|
|
array($s, '[sc x y /]', array( |
95
|
|
|
new ParsedShortcode(new Shortcode('sc', array('x' => null, 'y' => null), null), '[sc x y /]', 0), |
96
|
|
|
)), |
97
|
|
|
array($s, '[sc x="\ " /]', array( |
98
|
|
|
new ParsedShortcode(new Shortcode('sc', array('x' => '\ '), null), '[sc x="\ " /]', 0), |
99
|
|
|
)), |
100
|
|
|
array($s, '[ sc x = "\ " y = value z / ]', array( |
101
|
|
|
new ParsedShortcode(new Shortcode('sc', array('x' => '\ ', 'y' => 'value', 'z' => null), null), '[ sc x = "\ " y = value z / ]', 0), |
102
|
|
|
)), |
103
|
|
|
array($s, '[ sc x= "\ " y =value ] vv [ / sc ]', array( |
104
|
|
|
new ParsedShortcode(new Shortcode('sc', array('x' => '\ ', 'y' => 'value'), ' vv '), '[ sc x= "\ " y =value ] vv [ / sc ]', 0), |
105
|
|
|
)), |
106
|
|
|
array($s, '[sc url="http://giggle.com/search" /]', array( |
107
|
|
|
new ParsedShortcode(new Shortcode('sc', array('url' => 'http://giggle.com/search'), null), '[sc url="http://giggle.com/search" /]', 0), |
108
|
|
|
)), |
109
|
|
|
|
110
|
|
|
// bbcode |
111
|
|
|
array($s, '[sc = "http://giggle.com/search" /]', array( |
112
|
|
|
new ParsedShortcode(new Shortcode('sc', array(), null, 'http://giggle.com/search'), '[sc = "http://giggle.com/search" /]', 0), |
113
|
|
|
)), |
114
|
|
|
|
115
|
|
|
// multiple shortcodes |
116
|
|
|
array($s, 'Lorem [ipsum] random [code-code arg=val] which is here', array( |
117
|
|
|
new ParsedShortcode(new Shortcode('ipsum', array(), null), '[ipsum]', 6, array('name' => 1)), |
|
|
|
|
118
|
|
|
new ParsedShortcode(new Shortcode('code-code', array('arg' => 'val'), null), '[code-code arg=val]', 21), |
119
|
|
|
)), |
120
|
|
|
array($s, 'x [aa] x [aa] x', array( |
121
|
|
|
new ParsedShortcode(new Shortcode('aa', array(), null), '[aa]', 2), |
122
|
|
|
new ParsedShortcode(new Shortcode('aa', array(), null), '[aa]', 9), |
123
|
|
|
)), |
124
|
|
|
array($s, 'x [x]a[/x] x [x]a[/x] x', array( |
125
|
|
|
new ParsedShortcode(new Shortcode('x', array(), 'a'), '[x]a[/x]', 2), |
126
|
|
|
new ParsedShortcode(new Shortcode('x', array(), 'a'), '[x]a[/x]', 13), |
127
|
|
|
)), |
128
|
|
|
array($s, 'x [x x y=z a="b c"]a[/x] x [x x y=z a="b c"]a[/x] x', array( |
129
|
|
|
new ParsedShortcode(new Shortcode('x', array('x' => null, 'y' => 'z', 'a' => 'b c'), 'a'), '[x x y=z a="b c"]a[/x]', 2), |
130
|
|
|
new ParsedShortcode(new Shortcode('x', array('x' => null, 'y' => 'z', 'a' => 'b c'), 'a'), '[x x y=z a="b c"]a[/x]', 27), |
131
|
|
|
)), |
132
|
|
|
array($s, 'x [code /] y [code]z[/code] x [code] y [code/] a', array( |
133
|
|
|
new ParsedShortcode(new Shortcode('code', array(), null), '[code /]', 2), |
134
|
|
|
new ParsedShortcode(new Shortcode('code', array(), 'z'), '[code]z[/code]', 13), |
135
|
|
|
new ParsedShortcode(new Shortcode('code', array(), null), '[code]', 30), |
136
|
|
|
new ParsedShortcode(new Shortcode('code', array(), null), '[code/]', 39), |
137
|
|
|
)), |
138
|
|
|
array($s, 'x [code arg=val /] y [code cmp="xx"/] x [code x=y/] a', array( |
139
|
|
|
new ParsedShortcode(new Shortcode('code', array('arg' => 'val'), null), '[code arg=val /]', 2), |
140
|
|
|
new ParsedShortcode(new Shortcode('code', array('cmp' => 'xx'), null), '[code cmp="xx"/]', 21), |
141
|
|
|
new ParsedShortcode(new Shortcode('code', array('x' => 'y'), null), '[code x=y/]', 40), |
142
|
|
|
)), |
143
|
|
|
array($s, 'x [ code arg=val /]a[ code/]c[x / ] m [ y ] c [ / y]', array( |
144
|
|
|
new ParsedShortcode(new Shortcode('code', array('arg' => 'val'), null), '[ code arg=val /]', 2), |
145
|
|
|
new ParsedShortcode(new Shortcode('code', array(), null), '[ code/]', 23), |
146
|
|
|
new ParsedShortcode(new Shortcode('x', array(), null), '[x / ]', 32), |
147
|
|
|
new ParsedShortcode(new Shortcode('y', array(), ' c '), '[ y ] c [ / y]', 47), |
148
|
|
|
)), |
149
|
|
|
|
150
|
|
|
// other syntax |
151
|
|
|
array(new Syntax('[[', ']]', '//', '==', '""'), '[[code arg==""val oth""]]cont[[//code]]', array( |
152
|
|
|
new ParsedShortcode(new Shortcode('code', array('arg' => 'val oth'), 'cont'), '[[code arg==""val oth""]]cont[[//code]]', 0), |
153
|
|
|
)), |
154
|
|
|
array(new Syntax('^', '$', '&', '!!!', '@@'), '^code a!!!@@\"\"@@ b!!!@@x\"y@@ c$cnt^&code$', array( |
155
|
|
|
new ParsedShortcode(new Shortcode('code', array('a' => '\"\"', 'b' => 'x\"y', 'c' => null), 'cnt'), '^code a!!!@@\"\"@@ b!!!@@x\"y@@ c$cnt^&code$', 0), |
156
|
|
|
)), |
157
|
|
|
|
158
|
|
|
// UTF-8 sequences |
159
|
|
|
array($s, '’’’’[sc]’’[sc]', array( |
160
|
|
|
new ParsedShortcode(new Shortcode('sc', array(), null), '[sc]', 4), |
161
|
|
|
new ParsedShortcode(new Shortcode('sc', array(), null), '[sc]', 10), |
162
|
|
|
)), |
163
|
|
|
|
164
|
|
|
// performance |
|
|
|
|
165
|
|
|
// array($s, 'x [[aa]] y', array()), |
166
|
|
|
array($s, str_repeat('[a]', 20), array_map(function($offset) { // 20 |
167
|
|
|
return new ParsedShortcode(new Shortcode('a', array(), null), '[a]', $offset); |
168
|
|
|
}, range(0, 57, 3))), |
169
|
|
|
array($s, '[b][a]x[a][/a][/a][/b] [b][a][a][/a]y[/a][/b]', array( |
170
|
|
|
new ParsedShortcode(new Shortcode('b', array(), '[a]x[a][/a][/a]'), '[b][a]x[a][/a][/a][/b]', 0), |
171
|
|
|
new ParsedShortcode(new Shortcode('b', array(), '[a][a][/a]y[/a]'), '[b][a][a][/a]y[/a][/b]', 23), |
172
|
|
|
)), |
173
|
|
|
array($s, '[b] [a][a][a] [/b] [b] [a][a][a] [/b]', array( |
174
|
|
|
new ParsedShortcode(new Shortcode('b', array(), ' [a][a][a] '), '[b] [a][a][a] [/b]', 0), |
175
|
|
|
new ParsedShortcode(new Shortcode('b', array(), ' [a][a][a] '), '[b] [a][a][a] [/b]', 19), |
176
|
|
|
)), |
177
|
|
|
array($s, '[name]random[/other]', array( |
178
|
|
|
new ParsedShortcode(new Shortcode('name', array(), null), '[name]', 0), |
179
|
|
|
)), |
180
|
|
|
array($s, '[0][1][2][3]', array( |
181
|
|
|
new ParsedShortcode(new Shortcode('0', array(), null), '[0]', 0), |
182
|
|
|
new ParsedShortcode(new Shortcode('1', array(), null), '[1]', 3), |
183
|
|
|
new ParsedShortcode(new Shortcode('2', array(), null), '[2]', 6), |
184
|
|
|
new ParsedShortcode(new Shortcode('3', array(), null), '[3]', 9), |
185
|
|
|
)), |
186
|
|
|
array($s, '[_][na_me][_name][name_][n_am_e][_n_]', array()), |
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* WordPress can't handle: |
191
|
|
|
* - incorrect shortcode opening tag (blindly matches everything |
192
|
|
|
* between opening token and closing token) |
193
|
|
|
* - spaces between shortcode open tag and its name ([ name]), |
194
|
|
|
* - spaces around BBCode part ([name = "bbcode"]), |
195
|
|
|
* - escaped tokens anywhere in the arguments ([x arg=" \" "]), |
196
|
|
|
* - configurable syntax (that's intended), |
197
|
|
|
* - numbers in shortcode name. |
198
|
|
|
* |
199
|
|
|
* Tests cases from array above with identifiers in the array below must be skipped. |
200
|
|
|
*/ |
201
|
|
|
$wordpressSkip = array(3, 6, 16, 21, 22, 23, 25, 32, 33, 34, 40); |
202
|
|
|
$result = array(); |
203
|
|
|
foreach($tests as $key => $test) { |
204
|
|
|
$syntax = array_shift($test); |
205
|
|
|
|
206
|
|
|
$result[] = array_merge(array(new RegexParser($syntax)), $test); |
207
|
|
|
$result[] = array_merge(array(new RegularParser($syntax)), $test); |
208
|
|
|
if(!in_array($key, $wordpressSkip)) { |
209
|
|
|
$result[] = array_merge(array(new WordpressParser()), $test); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $result; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function testWordPress() |
217
|
|
|
{ |
218
|
|
|
$parser = new WordpressParser(); |
219
|
|
|
|
220
|
|
|
$this->testParser($parser, '[code arg="<html" oth=\'val\']', array( |
221
|
|
|
new ParsedShortcode(new Shortcode('code', array('arg' => '', 'oth' => 'val'), null), '[code arg="<html" oth=\'val\']', 0) |
222
|
|
|
)); |
223
|
|
|
$this->testParser($parser, '[code "xxx"]', array( |
224
|
|
|
new ParsedShortcode(new Shortcode('code', array('xxx' => null), null, null), '[code "xxx"]', 0) |
225
|
|
|
)); |
226
|
|
|
|
227
|
|
|
$handlers = new HandlerContainer(); |
228
|
|
|
$handlers->add('_', function() {}); |
229
|
|
|
$handlers->add('na_me', function() {}); |
230
|
|
|
$handlers->add('_n_', function() {}); |
231
|
|
|
$this->testParser(WordpressParser::createFromHandlers($handlers), '[_][na_me][_name][name_][n_am_e][_n_]', array( |
232
|
|
|
new ParsedShortcode(new Shortcode('_', array(), null), '[_]', 0), |
233
|
|
|
new ParsedShortcode(new Shortcode('na_me', array(), null), '[na_me]', 3), |
234
|
|
|
new ParsedShortcode(new Shortcode('_n_', array(), null), '[_n_]', 32), |
235
|
|
|
)); |
236
|
|
|
$this->testParser(WordpressParser::createFromNames(array('_', 'na_me', '_n_')), '[_][na_me][_name][name_][n_am_e][_n_]', array( |
237
|
|
|
new ParsedShortcode(new Shortcode('_', array(), null), '[_]', 0), |
238
|
|
|
new ParsedShortcode(new Shortcode('na_me', array(), null), '[na_me]', 3), |
239
|
|
|
new ParsedShortcode(new Shortcode('_n_', array(), null), '[_n_]', 32), |
240
|
|
|
)); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function testWordpressInvalidNamesException() |
244
|
|
|
{ |
245
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
246
|
|
|
WordpressParser::createFromNames(array('string', new \stdClass())); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function testInstances() |
250
|
|
|
{ |
251
|
|
|
$this->assertInstanceOf('Thunder\Shortcode\Parser\WordPressParser', new WordpressParser()); |
252
|
|
|
$this->assertInstanceOf('Thunder\Shortcode\Parser\RegularParser', new RegularParser()); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.