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
Push — master ( c87b3e...4d176b )
by Tomasz
02:18
created

ProcessorTest::testBaseOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
namespace Thunder\Shortcode\Tests;
3
4
use Thunder\Shortcode\Handler\ContentHandler;
5
use Thunder\Shortcode\Handler\DeclareHandler;
6
use Thunder\Shortcode\Handler\EmailHandler;
7
use Thunder\Shortcode\Handler\NameHandler;
8
use Thunder\Shortcode\Handler\NullHandler;
9
use Thunder\Shortcode\Handler\PlaceholderHandler;
10
use Thunder\Shortcode\Handler\RawHandler;
11
use Thunder\Shortcode\Handler\SerializerHandler;
12
use Thunder\Shortcode\Handler\UrlHandler;
13
use Thunder\Shortcode\Handler\WrapHandler;
14
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
15
use Thunder\Shortcode\Parser\RegexParser;
16
use Thunder\Shortcode\Parser\RegularParser;
17
use Thunder\Shortcode\Processor\Processor;
18
use Thunder\Shortcode\Shortcode\ParsedShortcodeInterface;
19
use Thunder\Shortcode\Serializer\JsonSerializer;
20
use Thunder\Shortcode\Serializer\TextSerializer;
21
use Thunder\Shortcode\Shortcode\ProcessedShortcode;
22
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
23
use Thunder\Shortcode\Syntax\CommonSyntax;
24
use Thunder\Shortcode\Tests\Fake\ReverseShortcode;
25
26
/**
27
 * @author Tomasz Kowalczyk <[email protected]>
28
 */
29
final class ProcessorTest extends \PHPUnit_Framework_TestCase
30
{
31
    private function getHandlers()
32
    {
33
        $handlers = new HandlerContainer();
34
        $handlers
35
            ->add('name', function (ShortcodeInterface $s) { return $s->getName(); })
36
            ->add('content', function (ShortcodeInterface $s) { return $s->getContent(); })
37
            ->add('reverse', new ReverseShortcode())
38
            ->add('url', function(ShortcodeInterface $s) {
39
                $url = $s->getParameter('url', $s->getBbCode());
40
41
                return '<a href="'.$url.'">'.$url.'</a>';
42
            })
43
            ->addAlias('c', 'content')
44
            ->addAlias('n', 'name');
45
46
        return $handlers;
47
    }
48
49 View Code Duplication
    public function testReplaceWithoutContentOffset()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
50
    {
51
        $text = ' [x value=" [name]yyy[/name] "] [name]yyy[/name] [/x] ';
52
        $result = ' [x value=" [name]yyy[/name] "] name [/x] ';
53
54
        $processor = new Processor(new RegexParser(), $this->getHandlers());
55
56
        $this->assertSame($result, $processor->process($text));
57
    }
58
59
    /**
60
     * @param string $text
61
     * @param string $result
62
     *
63
     * @dataProvider provideTexts
64
     */
65
    public function testProcessorProcess($text, $result)
66
    {
67
        $processor = new Processor(new RegexParser(), $this->getHandlers());
68
69
        $this->assertSame($result, $processor->process($text));
70
    }
71
72
    public function provideTexts()
73
    {
74
        return array(
75
            array('[name]', 'name'),
76
            array('[content]random[/content]', 'random'),
77
            array('[content]象形字[/content]', '象形字'),
78
            array('xxx [content]象形字[/content] yyy', 'xxx 象形字 yyy'),
79
            array('xxx [content]ąćęłńóśżź ąćęłńóśżź[/content] yyy', 'xxx ąćęłńóśżź ąćęłńóśżź yyy'),
80
            array('[name]random[/other]', 'namerandom[/other]'),
81
            array('[name][other]random[/other]', 'name[other]random[/other]'),
82
            array('[content]random-[name]-random[/content]', 'random-name-random'),
83
            array('random [content]other[/content] various', 'random other various'),
84
            array('x [content]a-[name]-b[/content] y', 'x a-name-b y'),
85
            array('x [c]a-[n][/n]-b[/c] y', 'x a-n-b y'),
86
            array('x [content]a-[c]v[/c]-b[/content] y', 'x a-v-b y'),
87
            array('x [html]bold[/html] z', 'x [html]bold[/html] z'),
88
            array('x [reverse]abc xyz[/reverse] z', 'x zyx cba z'),
89
            array('x [i /][i]i[/i][i /][i]i[/i][i /] z', 'x [i /][i]i[/i][i /][i]i[/i][i /] z'),
90
            array('x [url url="http://giggle.com/search" /] z', 'x <a href="http://giggle.com/search">http://giggle.com/search</a> z'),
91
            array('x [url="http://giggle.com/search"] z', 'x <a href="http://giggle.com/search">http://giggle.com/search</a> z'),
92
            );
93
    }
94
95
    public function testProcessorParentContext()
96
    {
97
        $handlers = new HandlerContainer();
98
        $handlers->add('outer', function (ProcessedShortcode $s) {
99
            $name = $s->getParent() ? $s->getParent()->getName() : 'root';
100
101
            return $name.'['.$s->getContent().']';
102
            });
103
        $handlers->addAlias('inner', 'outer');
104
        $handlers->addAlias('level', 'outer');
105
106
        $processor = new Processor(new RegexParser(), $handlers);
107
108
        $text = 'x [outer]a [inner]c [level]x[/level] d[/inner] b[/outer] y';
109
        $result = 'x root[a outer[c inner[x] d] b] y';
110
        $this->assertSame($result, $processor->process($text));
111
        $this->assertSame($result.$result, $processor->process($text.$text));
112
    }
113
114 View Code Duplication
    public function testProcessorWithoutRecursion()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
115
    {
116
        $processor = new Processor(new RegexParser(), $this->getHandlers());
117
        $text = 'x [content]a-[name][/name]-b[/content] y';
118
119
        $this->assertSame('x a-[name][/name]-b y', $processor->withRecursionDepth(0)->process($text));
120
    }
121
122
    public function testProcessContentIfHasChildHandlerButNotParent()
123
    {
124
        $handlers = new HandlerContainer();
125
        $handlers->add('valid', function (ShortcodeInterface $s) { return $s->getName(); });
126
127
        $text = 'x [invalid   ] [valid /] [/invalid] y';
128
        $processor = new Processor(new RegexParser(), $handlers);
129
130
        $this->assertSame('x [invalid   ] valid [/invalid] y', $processor->withAutoProcessContent(true)->process($text));
131
        $this->assertSame('x [invalid   ] [valid /] [/invalid] y', $processor->withAutoProcessContent(false)->process($text));
132
    }
133
134 View Code Duplication
    public function testProcessorWithoutContentAutoProcessing()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
135
    {
136
        $processor = new Processor(new RegexParser(), $this->getHandlers());
137
        $text = 'x [content]a-[name][/name]-b[/content] y';
138
139
        $this->assertSame('x a-[name][/name]-b y', $processor->withAutoProcessContent(false)->process($text));
140
    }
141
142 View Code Duplication
    public function testProcessorShortcodePositions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
143
    {
144
        $handlers = new HandlerContainer();
145
        $handlers->add('p', function (ProcessedShortcode $s) { return $s->getPosition(); });
146
        $handlers->add('n', function (ProcessedShortcode $s) { return $s->getNamePosition(); });
147
        $processor = new Processor(new RegexParser(), $handlers);
148
149
        $this->assertSame('123', $processor->process('[n][n][n]'), '3n');
150
        $this->assertSame('123', $processor->process('[p][p][p]'), '3p');
151
        $this->assertSame('113253', $processor->process('[p][n][p][n][p][n]'), 'pnpnpn');
152
        $this->assertSame('1231567', $processor->process('[p][p][p][n][p][p][p]'), 'pppnppp');
153
    }
154
155
    /**
156
     * @dataProvider provideBuiltInTests
157
     */
158
    public function testBuiltInHandlers($text, $result)
159
    {
160
        $handlers = new HandlerContainer();
161
        $handlers
162
            ->add('content', new ContentHandler())
163
            ->add('name', new NameHandler())
164
            ->add('null', new NullHandler())
165
            ->add('json', new SerializerHandler(new JsonSerializer()))
166
            ->add('text', new SerializerHandler(new TextSerializer()))
167
            ->add('placeholder', new PlaceholderHandler())
168
            ->add('b', new WrapHandler('<b>', '</b>'))
169
            ->add('bb', WrapHandler::createBold())
170
            ->add('declare', new DeclareHandler($handlers))
171
            ->add('url', new UrlHandler())
172
            ->add('email', new EmailHandler())
173
            ->add('raw', new RawHandler());
174
        $processor = new Processor(new RegexParser(), $handlers);
175
176
        $this->assertSame($result, $processor->process($text));
177
    }
178
179
    public function provideBuiltInTests()
180
    {
181
        return array(
182
            array('[declare date]%year%.%month%.%day%[/declare][date year=2015 month=08 day=26]', '2015.08.26'),
183
            array('[declare sample]%param%[/declare][invalid param=value]', '[invalid param=value]'),
184
            array('[declare]%param%[/declare][invalid param=value]', '[invalid param=value]'),
185
            array('[url]http://kowalczyk.cc[/url]', '<a href="http://kowalczyk.cc">http://kowalczyk.cc</a>'),
186
            array('[url="http://kowalczyk.cc"]Visit![/url]', '<a href="http://kowalczyk.cc">Visit!</a>'),
187
            array('[email][email protected][/email]', '<a href="mailto:[email protected]">[email protected]</a>'),
188
            array('[email="[email protected]"]Send![/email]', '<a href="mailto:[email protected]">Send!</a>'),
189
            array('[email="[email protected]" /]', '<a href="mailto:[email protected]">[email protected]</a>'),
190
            array('[b]text[/b]', '<b>text</b>'),
191
            array('[bb]text[/bb]', '<b>text</b>'),
192
            array('[json arg=val]value[/json]', '{"name":"json","parameters":{"arg":"val"},"content":"value","bbCode":null}'),
193
            array('[text arg=val]value[/text]', '[text arg=val]value[/text]'),
194
            array('[null arg=val]value[/null]', ''),
195
            array('[name /]', 'name'),
196
            array('[content]cnt[/content]', 'cnt'),
197
            array('[placeholder param=val]%param%[/placeholder]', 'val'),
198
            array('[placeholder param=val]%param%[/placeholder]', 'val'),
199
            array('[raw][null][content]cnt[/content][name /][/raw]', '[null][content]cnt[/content][name /]'),
200
        );
201
    }
202
203
    public function testProcessorDeclare()
204
    {
205
        $handlers = new HandlerContainer();
206
        $handlers->add('declare', function (ProcessedShortcode $s) use ($handlers) {
207
            $handlers->add($s->getParameterAt(0), function (ShortcodeInterface $x) use ($s) {
208
                $keys = array_map(function ($item) {
209
                    return '%'.$item.'%';
210
                    }, array_keys($x->getParameters()));
211
                $values = array_values($x->getParameters());
212
213
                return str_replace($keys, $values, $s->getContent());
214
                });
215
            });
216
        $processor = new Processor(new RegexParser(), $handlers);
217
218
        $this->assertSame('You are 18 years old.', trim($processor->process('
219
            [declare age]You are %age% years old.[/declare]
220
            [age age=18]
221
            ')));
222
    }
223
224
    public function testBaseOffset()
225
    {
226
        $handlers = new HandlerContainer();
227
        $handlers->setDefault(function(ProcessedShortcode $s) {
228
            return '['.$s->getBaseOffset().']'.$s->getContent();
229
        });
230
        $processor = new Processor(new RegularParser(new CommonSyntax()), $handlers);
231
232
        $this->assertSame('[0][3] ’[8][11]’ [20]', $processor->process('[a][b] ’[c][d]’ [/b][e]'));
233
    }
234
235
    public function testProcessorIterative()
236
    {
237
        $handlers = new HandlerContainer();
238
        $handlers
239
            ->add('name', function (ShortcodeInterface $s) { return $s->getName(); })
240
            ->add('content', function (ShortcodeInterface $s) { return $s->getContent(); })
241
            ->addAlias('c', 'content')
242
            ->addAlias('n', 'name')
243
            ->addAlias('d', 'c')
244
            ->addAlias('e', 'c');
245
        $processor = new Processor(new RegexParser(), $handlers);
246
247
        /** @var $processor Processor */
248
        $processor = $processor->withRecursionDepth(0)->withMaxIterations(2);
249
        $this->assertSame('x a y', $processor->process('x [c]a[/c] y'));
250
        $this->assertSame('x abc y', $processor->process('x [c]a[d]b[/d]c[/c] y'));
251
        $this->assertSame('x ab[e]c[/e]de y', $processor->process('x [c]a[d]b[e]c[/e]d[/d]e[/c] y'));
252
253
        $processor = $processor->withMaxIterations(null);
254
        $this->assertSame('x abcde y', $processor->process('x [c]a[d]b[e]c[/e]d[/d]e[/c] y'));
255
    }
256
257
    public function testExceptionOnInvalidRecursionDepth()
258
    {
259
        $processor = new Processor(new RegularParser(), new HandlerContainer());
260
        $this->setExpectedException('InvalidArgumentException');
261
        $processor->withRecursionDepth(new \stdClass());
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a integer|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
262
    }
263
264
    public function testExceptionOnInvalidMaxIterations()
265
    {
266
        $processor = new Processor(new RegularParser(), new HandlerContainer());
267
        $this->setExpectedException('InvalidArgumentException');
268
        $processor->withMaxIterations(new \stdClass());
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a integer|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
269
    }
270
271
    public function testExceptionOnInvalidAutoProcessFlag()
272
    {
273
        $processor = new Processor(new RegularParser(), new HandlerContainer());
274
        $this->setExpectedException('InvalidArgumentException');
275
        $processor->withAutoProcessContent(new \stdClass());
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
276
    }
277
278
    public function testDefaultHandler()
279
    {
280
        $handlers = new HandlerContainer();
281
        $handlers->setDefault(function (ShortcodeInterface $s) { return $s->getName(); });
282
        $processor = new Processor(new RegexParser(), $handlers);
283
284
        $this->assertSame('namerandom', $processor->process('[name][other][/name][random]'));
285
    }
286
287
    public function testStripOuter()
288
    {
289
        $handlers = new HandlerContainer();
290
        $handlers->add('q', function(ShortcodeInterface $s) {
291
            return $s->getContent();
292
        });
293
        $handlers->add('p', function(ProcessedShortcode $s) use(&$handlers) {
294
            $parser = new RegexParser();
295
            $processor = new Processor($parser, $handlers);
296
            $shortcodes = $parser->parse($s->getTextContent());
297
298
            return array_reduce($shortcodes, function($result, ParsedShortcodeInterface $s) use($processor) {
299
                return $result.$processor->process($s->getText());
300
            }, '');
301
        });
302
        $processor = new Processor(new RegexParser(), $handlers);
303
304
        $this->assertSame('x ab y', $processor->process('x [p] [q]a[/q] [q]b[/q] [/p] y'));
305
        $this->assertSame('x ab c y', $processor->process('x [p] [q]a[/q] [q]b [q]c[/q][/q] [/p] y'));
306
    }
307
308
    public function testOriginalContent()
309
    {
310
        $handlers = new HandlerContainer();
311
        $handlers->add('p', function(ProcessedShortcode $s) { return $s->getTextContent(); });
312
        $handlers->addAlias('q', 'p');
313
        $processor = new Processor(new RegexParser(), $handlers);
314
315
        $this->assertSame('x  [q]a[/q] [q]b[/q]  y', $processor->process('x [p] [q]a[/q] [q]b[/q] [/p] y'));
316
    }
317
318
    public function testMultipleParent()
319
    {
320
        $parents = 0;
321
        $handlers = new HandlerContainer();
322
        $handlers->add('p', function(ProcessedShortcode $s) use(&$parents) { $parents += $s->getParent() ? 1 : 0; });
323
        $handlers->addAlias('q', 'p');
324
        $processor = new Processor(new RegexParser(), $handlers);
325
        $processor->process('x [p] [q]a[/q] [q]b[/q] [q]c[/q] [/p] y');
326
327
        $this->assertSame(3, $parents);
328
    }
329
330
    public function testPreventInfiniteLoop()
331
    {
332
        $handlers = new HandlerContainer();
333
        $handlers
334
            ->add('self', function () { return '[self]'; })
335
            ->add('other', function () { return '[self]'; })
336
            ->add('random', function () { return '[other]'; });
337
        $processor = new Processor(new RegexParser(), $handlers);
338
        $processor->withMaxIterations(null);
339
340
        $processor->process('[self]');
341
        $processor->process('[other]');
342
        $processor->process('[random]');
343
    }
344
345 View Code Duplication
    public function testValidProcessAfterHandlerRemoval()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
346
    {
347
        $handlers = new HandlerContainer();
348
        $handlers->add('name', function(ShortcodeInterface $s) { return $s->getName(); });
349
        $handlers->addAlias('n', 'name');
350
        $processor = new Processor(new RegexParser(), $handlers);
351
        $this->assertSame('n', $processor->process('[n]'));
352
        $this->assertSame('name', $processor->process('[name]'));
353
        $handlers->remove('name');
354
        $this->assertSame('n', $processor->process('[n]'));
355
        $this->assertSame('[name]', $processor->process('[name]'));
356
    }
357
}
358