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 ( d8e85b...9135f5 )
by Tomasz
02:28
created

ProcessorTest::provideBuiltInTests()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 22
rs 9.2
cc 1
eloc 19
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\SerializerHandler;
11
use Thunder\Shortcode\Handler\UrlHandler;
12
use Thunder\Shortcode\Handler\WrapHandler;
13
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
14
use Thunder\Shortcode\Parser\RegexParser;
15
use Thunder\Shortcode\Parser\RegularParser;
16
use Thunder\Shortcode\Processor\Processor;
17
use Thunder\Shortcode\Shortcode\ParsedShortcodeInterface;
18
use Thunder\Shortcode\Serializer\JsonSerializer;
19
use Thunder\Shortcode\Serializer\TextSerializer;
20
use Thunder\Shortcode\Shortcode\ProcessedShortcode;
21
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
22
use Thunder\Shortcode\Tests\Fake\ReverseShortcode;
23
24
/**
25
 * @author Tomasz Kowalczyk <[email protected]>
26
 */
27
final class ProcessorTest extends \PHPUnit_Framework_TestCase
28
{
29
    private function getHandlers()
30
    {
31
        $handlers = new HandlerContainer();
32
        $handlers
33
            ->add('name', function (ShortcodeInterface $s) { return $s->getName(); })
34
            ->add('content', function (ShortcodeInterface $s) { return $s->getContent(); })
35
            ->add('reverse', new ReverseShortcode())
36
            ->add('url', function(ShortcodeInterface $s) {
37
                $url = $s->getParameter('url', $s->getBbCode());
38
39
                return '<a href="'.$url.'">'.$url.'</a>';
40
            })
41
            ->addAlias('c', 'content')
42
            ->addAlias('n', 'name');
43
44
        return $handlers;
45
    }
46
47 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...
48
    {
49
        $text = ' [x value=" [name]yyy[/name] "] [name]yyy[/name] [/x] ';
50
        $result = ' [x value=" [name]yyy[/name] "] name [/x] ';
51
52
        $processor = new Processor(new RegexParser(), $this->getHandlers());
53
54
        $this->assertSame($result, $processor->process($text));
55
    }
56
57
    /**
58
     * @param string $text
59
     * @param string $result
60
     *
61
     * @dataProvider provideTexts
62
     */
63
    public function testProcessorProcess($text, $result)
64
    {
65
        $processor = new Processor(new RegexParser(), $this->getHandlers());
66
67
        $this->assertSame($result, $processor->process($text));
68
    }
69
70
    public function provideTexts()
71
    {
72
        return array(
73
            array('[name]', 'name'),
74
            array('[content]random[/content]', 'random'),
75
            array('[content]象形字[/content]', '象形字'),
76
            array('xxx [content]象形字[/content] yyy', 'xxx 象形字 yyy'),
77
            array('xxx [content]ąćęłńóśżź ąćęłńóśżź[/content] yyy', 'xxx ąćęłńóśżź ąćęłńóśżź yyy'),
78
            array('[name]random[/other]', 'namerandom[/other]'),
79
            array('[name][other]random[/other]', 'name[other]random[/other]'),
80
            array('[content]random-[name]-random[/content]', 'random-name-random'),
81
            array('random [content]other[/content] various', 'random other various'),
82
            array('x [content]a-[name]-b[/content] y', 'x a-name-b y'),
83
            array('x [c]a-[n][/n]-b[/c] y', 'x a-n-b y'),
84
            array('x [content]a-[c]v[/c]-b[/content] y', 'x a-v-b y'),
85
            array('x [html]bold[/html] z', 'x [html]bold[/html] z'),
86
            array('x [reverse]abc xyz[/reverse] z', 'x zyx cba z'),
87
            array('x [i /][i]i[/i][i /][i]i[/i][i /] z', 'x [i /][i]i[/i][i /][i]i[/i][i /] z'),
88
            array('x [url url="http://giggle.com/search" /] z', 'x <a href="http://giggle.com/search">http://giggle.com/search</a> z'),
89
            array('x [url="http://giggle.com/search"] z', 'x <a href="http://giggle.com/search">http://giggle.com/search</a> z'),
90
            );
91
    }
92
93
    public function testProcessorParentContext()
94
    {
95
        $handlers = new HandlerContainer();
96
        $handlers->add('outer', function (ProcessedShortcode $s) {
97
            $name = $s->getParent() ? $s->getParent()->getName() : 'root';
98
99
            return $name.'['.$s->getContent().']';
100
            });
101
        $handlers->addAlias('inner', 'outer');
102
        $handlers->addAlias('level', 'outer');
103
104
        $processor = new Processor(new RegexParser(), $handlers);
105
106
        $text = 'x [outer]a [inner]c [level]x[/level] d[/inner] b[/outer] y';
107
        $result = 'x root[a outer[c inner[x] d] b] y';
108
        $this->assertSame($result, $processor->process($text));
109
        $this->assertSame($result.$result, $processor->process($text.$text));
110
    }
111
112 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...
113
    {
114
        $processor = new Processor(new RegexParser(), $this->getHandlers());
115
        $text = 'x [content]a-[name][/name]-b[/content] y';
116
117
        $this->assertSame('x a-[name][/name]-b y', $processor->withRecursionDepth(0)->process($text));
118
    }
119
120
    public function testProcessContentIfHasChildHandlerButNotParent()
121
    {
122
        $handlers = new HandlerContainer();
123
        $handlers->add('valid', function (ShortcodeInterface $s) { return $s->getName(); });
124
125
        $text = 'x [invalid   ] [valid /] [/invalid] y';
126
        $processor = new Processor(new RegexParser(), $handlers);
127
128
        $this->assertSame('x [invalid   ] valid [/invalid] y', $processor->withAutoProcessContent(true)->process($text));
129
        $this->assertSame('x [invalid   ] [valid /] [/invalid] y', $processor->withAutoProcessContent(false)->process($text));
130
    }
131
132 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...
133
    {
134
        $processor = new Processor(new RegexParser(), $this->getHandlers());
135
        $text = 'x [content]a-[name][/name]-b[/content] y';
136
137
        $this->assertSame('x a-[name][/name]-b y', $processor->withAutoProcessContent(false)->process($text));
138
    }
139
140 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...
141
    {
142
        $handlers = new HandlerContainer();
143
        $handlers->add('p', function (ProcessedShortcode $s) { return $s->getPosition(); });
144
        $handlers->add('n', function (ProcessedShortcode $s) { return $s->getNamePosition(); });
145
        $processor = new Processor(new RegexParser(), $handlers);
146
147
        $this->assertSame('123', $processor->process('[n][n][n]'), '3n');
148
        $this->assertSame('123', $processor->process('[p][p][p]'), '3p');
149
        $this->assertSame('113253', $processor->process('[p][n][p][n][p][n]'), 'pnpnpn');
150
        $this->assertSame('1231567', $processor->process('[p][p][p][n][p][p][p]'), 'pppnppp');
151
    }
152
153
    /**
154
     * @dataProvider provideBuiltInTests
155
     */
156
    public function testBuiltInHandlers($text, $result)
157
    {
158
        $handlers = new HandlerContainer();
159
        $handlers
160
            ->add('content', new ContentHandler())
161
            ->add('name', new NameHandler())
162
            ->add('null', new NullHandler())
163
            ->add('json', new SerializerHandler(new JsonSerializer()))
164
            ->add('text', new SerializerHandler(new TextSerializer()))
165
            ->add('placeholder', new PlaceholderHandler())
166
            ->add('b', new WrapHandler('<b>', '</b>'))
167
            ->add('bb', WrapHandler::createBold())
168
            ->add('declare', new DeclareHandler($handlers))
169
            ->add('url', new UrlHandler())
170
            ->add('email', new EmailHandler());
171
        $processor = new Processor(new RegexParser(), $handlers);
172
173
        $this->assertSame($result, $processor->process($text));
174
    }
175
176
    public function provideBuiltInTests()
177
    {
178
        return array(
179
            array('[declare date]%year%.%month%.%day%[/declare][date year=2015 month=08 day=26]', '2015.08.26'),
180
            array('[declare sample]%param%[/declare][invalid param=value]', '[invalid param=value]'),
181
            array('[declare]%param%[/declare][invalid param=value]', '[invalid param=value]'),
182
            array('[url]http://kowalczyk.cc[/url]', '<a href="http://kowalczyk.cc">http://kowalczyk.cc</a>'),
183
            array('[url="http://kowalczyk.cc"]Visit![/url]', '<a href="http://kowalczyk.cc">Visit!</a>'),
184
            array('[email][email protected][/email]', '<a href="mailto:[email protected]">[email protected]</a>'),
185
            array('[email="[email protected]"]Send![/email]', '<a href="mailto:[email protected]">Send!</a>'),
186
            array('[email="[email protected]" /]', '<a href="mailto:[email protected]">[email protected]</a>'),
187
            array('[b]text[/b]', '<b>text</b>'),
188
            array('[bb]text[/bb]', '<b>text</b>'),
189
            array('[json arg=val]value[/json]', '{"name":"json","parameters":{"arg":"val"},"content":"value","bbCode":null}'),
190
            array('[text arg=val]value[/text]', '[text arg=val]value[/text]'),
191
            array('[null arg=val]value[/null]', ''),
192
            array('[name /]', 'name'),
193
            array('[content]cnt[/content]', 'cnt'),
194
            array('[placeholder param=val]%param%[/placeholder]', 'val'),
195
            array('[placeholder param=val]%param%[/placeholder]', 'val'),
196
        );
197
    }
198
199
    public function testProcessorDeclare()
200
    {
201
        $handlers = new HandlerContainer();
202
        $handlers->add('declare', function (ProcessedShortcode $s) use ($handlers) {
203
            $handlers->add($s->getParameterAt(0), function (ShortcodeInterface $x) use ($s) {
204
                $keys = array_map(function ($item) {
205
                    return '%'.$item.'%';
206
                    }, array_keys($x->getParameters()));
207
                $values = array_values($x->getParameters());
208
209
                return str_replace($keys, $values, $s->getContent());
210
                });
211
            });
212
        $processor = new Processor(new RegexParser(), $handlers);
213
214
        $this->assertSame('You are 18 years old.', trim($processor->process('
215
            [declare age]You are %age% years old.[/declare]
216
            [age age=18]
217
            ')));
218
    }
219
220
    public function testProcessorIterative()
221
    {
222
        $handlers = new HandlerContainer();
223
        $handlers
224
            ->add('name', function (ShortcodeInterface $s) { return $s->getName(); })
225
            ->add('content', function (ShortcodeInterface $s) { return $s->getContent(); })
226
            ->addAlias('c', 'content')
227
            ->addAlias('n', 'name')
228
            ->addAlias('d', 'c')
229
            ->addAlias('e', 'c');
230
        $processor = new Processor(new RegexParser(), $handlers);
231
232
        /** @var $processor Processor */
233
        $processor = $processor->withRecursionDepth(0)->withMaxIterations(2);
234
        $this->assertSame('x a y', $processor->process('x [c]a[/c] y'));
235
        $this->assertSame('x abc y', $processor->process('x [c]a[d]b[/d]c[/c] y'));
236
        $this->assertSame('x ab[e]c[/e]de y', $processor->process('x [c]a[d]b[e]c[/e]d[/d]e[/c] y'));
237
238
        $processor = $processor->withMaxIterations(null);
239
        $this->assertSame('x abcde y', $processor->process('x [c]a[d]b[e]c[/e]d[/d]e[/c] y'));
240
    }
241
242
    public function testExceptionOnInvalidRecursionDepth()
243
    {
244
        $processor = new Processor(new RegularParser(), new HandlerContainer());
245
        $this->setExpectedException('InvalidArgumentException');
246
        $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...
247
    }
248
249
    public function testExceptionOnInvalidMaxIterations()
250
    {
251
        $processor = new Processor(new RegularParser(), new HandlerContainer());
252
        $this->setExpectedException('InvalidArgumentException');
253
        $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...
254
    }
255
256
    public function testExceptionOnInvalidAutoProcessFlag()
257
    {
258
        $processor = new Processor(new RegularParser(), new HandlerContainer());
259
        $this->setExpectedException('InvalidArgumentException');
260
        $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...
261
    }
262
263
    public function testDefaultHandler()
264
    {
265
        $handlers = new HandlerContainer();
266
        $handlers->setDefault(function (ShortcodeInterface $s) { return $s->getName(); });
267
        $processor = new Processor(new RegexParser(), $handlers);
268
269
        $this->assertSame('namerandom', $processor->process('[name][other][/name][random]'));
270
    }
271
272
    public function testStripOuter()
273
    {
274
        $handlers = new HandlerContainer();
275
        $handlers->add('q', function(ShortcodeInterface $s) {
276
            return $s->getContent();
277
        });
278
        $handlers->add('p', function(ProcessedShortcode $s) use(&$handlers) {
279
            $parser = new RegexParser();
280
            $processor = new Processor($parser, $handlers);
281
            $shortcodes = $parser->parse($s->getTextContent());
282
283
            return array_reduce($shortcodes, function($result, ParsedShortcodeInterface $s) use($processor) {
284
                return $result.$processor->process($s->getText());
285
            }, '');
286
        });
287
        $processor = new Processor(new RegexParser(), $handlers);
288
289
        $this->assertSame('x ab y', $processor->process('x [p] [q]a[/q] [q]b[/q] [/p] y'));
290
        $this->assertSame('x ab c y', $processor->process('x [p] [q]a[/q] [q]b [q]c[/q][/q] [/p] y'));
291
    }
292
293
    public function testOriginalContent()
294
    {
295
        $handlers = new HandlerContainer();
296
        $handlers->add('p', function(ProcessedShortcode $s) { return $s->getTextContent(); });
297
        $handlers->addAlias('q', 'p');
298
        $processor = new Processor(new RegexParser(), $handlers);
299
300
        $this->assertSame('x  [q]a[/q] [q]b[/q]  y', $processor->process('x [p] [q]a[/q] [q]b[/q] [/p] y'));
301
    }
302
303
    public function testMultipleParent()
304
    {
305
        $parents = 0;
306
        $handlers = new HandlerContainer();
307
        $handlers->add('p', function(ProcessedShortcode $s) use(&$parents) { $parents += $s->getParent() ? 1 : 0; });
308
        $handlers->addAlias('q', 'p');
309
        $processor = new Processor(new RegexParser(), $handlers);
310
        $processor->process('x [p] [q]a[/q] [q]b[/q] [q]c[/q] [/p] y');
311
312
        $this->assertSame(3, $parents);
313
    }
314
315
    public function testPreventInfiniteLoop()
316
    {
317
        $handlers = new HandlerContainer();
318
        $handlers
319
            ->add('self', function () { return '[self]'; })
320
            ->add('other', function () { return '[self]'; })
321
            ->add('random', function () { return '[other]'; });
322
        $processor = new Processor(new RegexParser(), $handlers);
323
        $processor->withMaxIterations(null);
324
325
        $processor->process('[self]');
326
        $processor->process('[other]');
327
        $processor->process('[random]');
328
    }
329
330 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...
331
    {
332
        $handlers = new HandlerContainer();
333
        $handlers->add('name', function(ShortcodeInterface $s) { return $s->getName(); });
334
        $handlers->addAlias('n', 'name');
335
        $processor = new Processor(new RegexParser(), $handlers);
336
        $this->assertSame('n', $processor->process('[n]'));
337
        $this->assertSame('name', $processor->process('[name]'));
338
        $handlers->remove('name');
339
        $this->assertSame('n', $processor->process('[n]'));
340
        $this->assertSame('[name]', $processor->process('[name]'));
341
    }
342
}
343