GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#66)
by Tomasz
01:49
created

ParserTest::testValueModeAggressive()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 8
Ratio 34.78 %

Importance

Changes 0
Metric Value
dl 8
loc 23
rs 9.552
c 0
b 0
f 0
cc 2
nc 2
nop 0
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 AbstractTestCase
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
        static::assertCount($count, $parsed, 'counts');
32 View Code Duplication
        for ($i = 0; $i < $count; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
33
            static::assertSame($tested[$i]->getName(), $parsed[$i]->getName(), 'name');
34
            static::assertSame($tested[$i]->getParameters(), $parsed[$i]->getParameters(), 'parameters');
35
            static::assertSame($tested[$i]->getContent(), $parsed[$i]->getContent(), 'content');
36
            static::assertSame($tested[$i]->getText(), $parsed[$i]->getText(), 'text');
37
            static::assertSame($tested[$i]->getOffset(), $parsed[$i]->getOffset(), 'offset');
38
            static::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),
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
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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
                new ParsedShortcode(new Shortcode('_', array(), null), '[_]', 0),
188
                new ParsedShortcode(new Shortcode('na_me', array(), null), '[na_me]', 3),
189
                new ParsedShortcode(new Shortcode('_name', array(), null), '[_name]', 10),
190
                new ParsedShortcode(new Shortcode('name_', array(), null), '[name_]', 17),
191
                new ParsedShortcode(new Shortcode('n_am_e', array(), null), '[n_am_e]', 24),
192
                new ParsedShortcode(new Shortcode('_n_', array(), null), '[_n_]', 32),
193
            )),
194
            array($s, '[x]/[/x] [x]"[/x] [x]=[/x] [x]][/x] [x] [/x] [x]x[/x]', array(
195
                new ParsedShortcode(new Shortcode('x', array(), '/'), '[x]/[/x]', 0),
196
                new ParsedShortcode(new Shortcode('x', array(), '"'), '[x]"[/x]', 9),
197
                new ParsedShortcode(new Shortcode('x', array(), '='), '[x]=[/x]', 18),
198
                new ParsedShortcode(new Shortcode('x', array(), ']'), '[x]][/x]', 27),
199
                new ParsedShortcode(new Shortcode('x', array(), ' '), '[x] [/x]', 36),
200
                new ParsedShortcode(new Shortcode('x', array(), 'x'), '[x]x[/x]', 45),
201
            )),
202
            array($s, '[a]0[/a]', array(
203
                new ParsedShortcode(new Shortcode('a', array(), '0'), '[a]0[/a]', 0),
204
            )),
205
            array($s, '[fa icon=fa-camera /] [fa icon=fa-camera extras=fa-4x /]', array(
206
                new ParsedShortcode(new Shortcode('fa', array('icon' => 'fa-camera'), null), '[fa icon=fa-camera /]', 0),
207
                new ParsedShortcode(new Shortcode('fa', array('icon' => 'fa-camera', 'extras' => 'fa-4x'), null), '[fa icon=fa-camera extras=fa-4x /]', 22),
208
            )),
209
            array($s, '[fa icon=fa-circle-o-notch extras=fa-spin,fa-3x /]', array(
210
                new ParsedShortcode(new Shortcode('fa', array('icon' => 'fa-circle-o-notch', 'extras' => 'fa-spin,fa-3x'), null), '[fa icon=fa-circle-o-notch extras=fa-spin,fa-3x /]', 0),
211
            )),
212
            array($s, '[z =]', array()),
213
            array($s, '[x=#F00 one=#F00 two="#F00"]', array(
214
                new ParsedShortcode(new Shortcode('x', array('one' => '#F00', 'two' => '#F00'), null, '#F00'), '[x=#F00 one=#F00 two="#F00"]', 0),
215
            )),
216
            array($s, '[*] [* xyz arg=val]', array(
217
                new ParsedShortcode(new Shortcode('*', array(), null, null), '[*]', 0),
218
                new ParsedShortcode(new Shortcode('*', array('xyz' => null, 'arg' => 'val'), null, null), '[* xyz arg=val]', 4),
219
            )),
220
            array($s, '[*=bb x=y]cnt[/*]', array(
221
                new ParsedShortcode(new Shortcode('*', array('x' => 'y'), 'cnt', 'bb'), '[*=bb x=y]cnt[/*]', 0),
222
            )),
223
            array($s, '[ [] ] [x] [ ] [/x] ] [] [ [y] ] [] [ [z] [/#] [/z] [ [] ] [/] [/y] ] [z] [ [/ [/] /] ] [/z]', array(
224
                new ParsedShortcode(new Shortcode('x', array(), ' [ ] ', null), '[x] [ ] [/x]', 7),
225
                new ParsedShortcode(new Shortcode('y', array(), ' ] [] [ [z] [/#] [/z] [ [] ] [/] ', null), '[y] ] [] [ [z] [/#] [/z] [ [] ] [/] [/y]', 27),
226
                new ParsedShortcode(new Shortcode('z', array(), ' [ [/ [/] /] ] ', null), '[z] [ [/ [/] /] ] [/z]', 70),
227
            )),
228
            array($s, '[x=/[/] [y a=/"//] [z=http://url/] [a=http://url ]', array(
229
                new ParsedShortcode(new Shortcode('x', array(), null, '/['), '[x=/[/]', 0),
230
                new ParsedShortcode(new Shortcode('y', array('a' => '/"/'), null, null), '[y a=/"//]', 8),
231
                new ParsedShortcode(new Shortcode('z', array(), null, 'http://url'), '[z=http://url/]', 19),
232
                new ParsedShortcode(new Shortcode('a', array(), null, 'http://url'), '[a=http://url ]', 35),
233
            )),
234
        );
235
236
        /**
237
         * WordPress can't handle:
238
         *   - incorrect shortcode opening tag (blindly matches everything
239
         *     between opening token and closing token)
240
         *   - spaces between shortcode open tag and its name ([  name]),
241
         *   - spaces around BBCode part ([name  = "bbcode"]),
242
         *   - escaped tokens anywhere in the arguments ([x arg=" \" "]),
243
         *   - configurable syntax (that's intended),
244
         *   - numbers in shortcode name.
245
         *
246
         * Tests cases from array above with identifiers in the array below must be skipped.
247
         */
248
        $wordpressSkip = array(3, 6, 16, 21, 22, 23, 25, 32, 33, 34, 46, 47, 49, 51);
249
        $result = array();
250
        foreach($tests as $key => $test) {
251
            $syntax = array_shift($test);
252
253
            $result[] = array_merge(array(new RegexParser($syntax)), $test);
254
            $result[] = array_merge(array(new RegularParser($syntax)), $test);
255
            if(!in_array($key, $wordpressSkip, true)) {
256
                $result[] = array_merge(array(new WordpressParser()), $test);
257
            }
258
        }
259
260
        return $result;
261
    }
262
263
    public function testValueModeAggressive()
264
    {
265
        $parser = new RegularParser(new CommonSyntax());
266
        $parser->valueMode = RegularParser::VALUE_AGGRESSIVE;
267
        $parsed = $parser->parse('[x=/[/] [y a=/"//] [z=http://url/] [a=http://url ]');
268
        $tested = array(
269
            new ParsedShortcode(new Shortcode('x', array(), null, '/[/'), '[x=/[/]', 0),
270
            new ParsedShortcode(new Shortcode('y', array('a' => '/"//'), null, null), '[y a=/"//]', 8),
271
            new ParsedShortcode(new Shortcode('z', array(), null, 'http://url/'), '[z=http://url/]', 19),
272
            new ParsedShortcode(new Shortcode('a', array(), null, 'http://url'), '[a=http://url ]', 35),
273
        );
274
275
        $count = count($tested);
276
        static::assertCount($count, $parsed, 'counts');
277 View Code Duplication
        for ($i = 0; $i < $count; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
278
            static::assertSame($tested[$i]->getName(), $parsed[$i]->getName(), 'name');
279
            static::assertSame($tested[$i]->getParameters(), $parsed[$i]->getParameters(), 'parameters');
280
            static::assertSame($tested[$i]->getContent(), $parsed[$i]->getContent(), 'content');
281
            static::assertSame($tested[$i]->getText(), $parsed[$i]->getText(), 'text');
282
            static::assertSame($tested[$i]->getOffset(), $parsed[$i]->getOffset(), 'offset');
283
            static::assertSame($tested[$i]->getBbCode(), $parsed[$i]->getBbCode(), 'bbCode');
284
        }
285
    }
286
287
    public function testWordPress()
288
    {
289
        $parser = new WordpressParser();
290
291
        $this->testParser($parser, '[code arg="<html" oth=\'val\']', array(
292
            new ParsedShortcode(new Shortcode('code', array('arg' => '', 'oth' => 'val'), null), '[code arg="<html" oth=\'val\']', 0)
293
        ));
294
        $this->testParser($parser, '[code "xxx"]', array(
295
            new ParsedShortcode(new Shortcode('code', array('xxx' => null), null, null), '[code "xxx"]', 0)
296
        ));
297
        $this->testParser($parser, '[code="xxx"] [code=yyy-aaa]', array(
298
            new ParsedShortcode(new Shortcode('code', array('="xxx"' => null), null), '[code="xxx"]', 0),
299
            new ParsedShortcode(new Shortcode('code', array('=yyy-aaa' => null), null), '[code=yyy-aaa]', 13)
300
        ));
301
302
        $handlers = new HandlerContainer();
303
        $handlers->add('_', function() {});
304
        $handlers->add('na_me', function() {});
305
        $handlers->add('_n_', function() {});
306
        $this->testParser(WordpressParser::createFromHandlers($handlers), '[_][na_me][_name][name_][n_am_e][_n_]', array(
307
            new ParsedShortcode(new Shortcode('_', array(), null), '[_]', 0),
308
            new ParsedShortcode(new Shortcode('na_me', array(), null), '[na_me]', 3),
309
            new ParsedShortcode(new Shortcode('_n_', array(), null), '[_n_]', 32),
310
        ));
311
        $this->testParser(WordpressParser::createFromNames(array('_', 'na_me', '_n_')), '[_][na_me][_name][name_][n_am_e][_n_]', array(
312
            new ParsedShortcode(new Shortcode('_', array(), null), '[_]', 0),
313
            new ParsedShortcode(new Shortcode('na_me', array(), null), '[na_me]', 3),
314
            new ParsedShortcode(new Shortcode('_n_', array(), null), '[_n_]', 32),
315
        ));
316
    }
317
318
    public function testWordpressInvalidNamesException()
319
    {
320
        $this->expectException('InvalidArgumentException');
321
        WordpressParser::createFromNames(array('string', new \stdClass()));
322
    }
323
324
    public function testInstances()
325
    {
326
        static::assertInstanceOf('Thunder\Shortcode\Parser\WordPressParser', new WordpressParser());
327
        static::assertInstanceOf('Thunder\Shortcode\Parser\RegularParser', new RegularParser());
328
    }
329
}
330