|
1
|
|
|
<?php |
|
2
|
|
|
namespace Thunder\Shortcode\Tests; |
|
3
|
|
|
|
|
4
|
|
|
use Thunder\Shortcode\HandlerContainer\HandlerContainer; |
|
5
|
|
|
use Thunder\Shortcode\Parser\RegexParser; |
|
6
|
|
|
use Thunder\Shortcode\Processor\Processor; |
|
7
|
|
|
use Thunder\Shortcode\Processor\ProcessorContext; |
|
8
|
|
|
use Thunder\Shortcode\Serializer\TextSerializer; |
|
9
|
|
|
use Thunder\Shortcode\Shortcode\ParsedShortcode; |
|
10
|
|
|
use Thunder\Shortcode\Shortcode\ProcessedShortcode; |
|
11
|
|
|
use Thunder\Shortcode\Shortcode\Shortcode; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
final class ShortcodeTest extends AbstractTestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @dataProvider provideShortcodes |
|
20
|
|
|
*/ |
|
21
|
|
|
public function testShortcode($expected, $name, array $args, $content) |
|
22
|
|
|
{ |
|
23
|
|
|
$s = new Shortcode($name, $args, $content); |
|
24
|
|
|
$textSerializer = new TextSerializer(); |
|
25
|
|
|
|
|
26
|
|
|
static::assertSame($name, $s->getName()); |
|
27
|
|
|
static::assertSame($args, $s->getParameters()); |
|
28
|
|
|
static::assertSame($content, $s->getContent()); |
|
29
|
|
|
static::assertSame($expected, $textSerializer->serialize($s)); |
|
30
|
|
|
static::assertSame('arg', $s->getParameterAt(0)); |
|
31
|
|
|
static::assertTrue($s->hasParameters()); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function provideShortcodes() |
|
35
|
|
|
{ |
|
36
|
|
|
return array( |
|
37
|
|
|
array('[x arg=val /]', 'x', array('arg' => 'val'), null), |
|
38
|
|
|
array('[x arg=val][/x]', 'x', array('arg' => 'val'), ''), |
|
39
|
|
|
array('[x arg=val]inner[/x]', 'x', array('arg' => 'val'), 'inner'), |
|
40
|
|
|
array('[x arg="val val"]inner[/x]', 'x', array('arg' => 'val val'), 'inner'), |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testObject() |
|
45
|
|
|
{ |
|
46
|
|
|
$shortcode = new Shortcode('random', array('arg' => 'value', 'none' => null), 'something'); |
|
47
|
|
|
|
|
48
|
|
|
static::assertTrue($shortcode->hasParameter('arg')); |
|
49
|
|
|
static::assertFalse($shortcode->hasParameter('invalid')); |
|
50
|
|
|
static::assertNull($shortcode->getParameter('none')); |
|
51
|
|
|
static::assertSame('value', $shortcode->getParameter('arg')); |
|
52
|
|
|
static::assertSame('', $shortcode->getParameter('invalid', '')); |
|
53
|
|
|
static::assertSame(42, $shortcode->getParameter('invalid', 42)); |
|
54
|
|
|
|
|
55
|
|
|
static::assertNotSame($shortcode, $shortcode->withContent('x')); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testProcessedShortcode() |
|
59
|
|
|
{ |
|
60
|
|
|
$processor = new Processor(new RegexParser(), new HandlerContainer()); |
|
61
|
|
|
|
|
62
|
|
|
$context = new ProcessorContext(); |
|
63
|
|
|
$context->shortcode = new Shortcode('code', array('arg' => 'val'), 'content'); |
|
64
|
|
|
$context->processor = $processor; |
|
65
|
|
|
$context->position = 20; |
|
66
|
|
|
$context->namePosition = array('code' => 10); |
|
67
|
|
|
$context->text = ' [code] '; |
|
68
|
|
|
$context->shortcodeText = '[code]'; |
|
69
|
|
|
$context->offset = 1; |
|
70
|
|
|
$context->iterationNumber = 1; |
|
71
|
|
|
$context->recursionLevel = 0; |
|
72
|
|
|
$context->parent = null; |
|
73
|
|
|
|
|
74
|
|
|
$processed = ProcessedShortcode::createFromContext($context); |
|
75
|
|
|
|
|
76
|
|
|
static::assertSame('code', $processed->getName()); |
|
77
|
|
|
static::assertSame(array('arg' => 'val'), $processed->getParameters()); |
|
78
|
|
|
static::assertSame('content', $processed->getContent()); |
|
79
|
|
|
|
|
80
|
|
|
static::assertSame(20, $processed->getPosition()); |
|
81
|
|
|
static::assertSame(10, $processed->getNamePosition()); |
|
82
|
|
|
static::assertSame(' [code] ', $processed->getText()); |
|
83
|
|
|
static::assertSame(1, $processed->getOffset()); |
|
84
|
|
|
static::assertSame('[code]', $processed->getShortcodeText()); |
|
85
|
|
|
static::assertSame(1, $processed->getIterationNumber()); |
|
86
|
|
|
static::assertSame(0, $processed->getRecursionLevel()); |
|
87
|
|
|
static::assertSame(null, $processed->getParent()); |
|
88
|
|
|
static::assertSame($processor, $processed->getProcessor()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function testProcessedShortcodeParents() |
|
92
|
|
|
{ |
|
93
|
|
|
$context = new ProcessorContext(); |
|
94
|
|
|
$context->shortcode = new Shortcode('p1', array(), null); |
|
95
|
|
|
$context->parent = null; |
|
96
|
|
|
$context->namePosition = array('p1' => 0, 'p2' => 0, 'p3' => 0); |
|
97
|
|
|
$p1 = ProcessedShortcode::createFromContext($context); |
|
98
|
|
|
$context->shortcode = new Shortcode('p2', array(), null); |
|
99
|
|
|
$context->parent = $p1; |
|
100
|
|
|
$p2 = ProcessedShortcode::createFromContext($context); |
|
101
|
|
|
$context->shortcode = new Shortcode('p3', array(), null); |
|
102
|
|
|
$context->parent = $p2; |
|
103
|
|
|
$p3 = ProcessedShortcode::createFromContext($context); |
|
104
|
|
|
|
|
105
|
|
|
static::assertSame('p3', $p3->getName()); |
|
106
|
|
|
static::assertSame('p2', $p3->getParent()->getName()); |
|
107
|
|
|
static::assertSame('p1', $p3->getParent()->getParent()->getName()); |
|
108
|
|
|
static::assertFalse($p1->hasAncestor('p3')); |
|
109
|
|
|
static::assertFalse($p1->hasAncestor('p1')); |
|
110
|
|
|
static::assertTrue($p2->hasAncestor('p1')); |
|
111
|
|
|
static::assertFalse($p2->hasAncestor('p3')); |
|
112
|
|
|
static::assertTrue($p3->hasAncestor('p1')); |
|
113
|
|
|
static::assertTrue($p3->hasAncestor('p2')); |
|
114
|
|
|
static::assertFalse($p3->hasAncestor('p4')); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function testParsedShortcode() |
|
118
|
|
|
{ |
|
119
|
|
|
$shortcode = new ParsedShortcode(new Shortcode('name', array('arg' => 'val'), 'content'), 'text', 12); |
|
120
|
|
|
|
|
121
|
|
|
static::assertSame('name', $shortcode->getName()); |
|
122
|
|
|
static::assertSame(array('arg' => 'val'), $shortcode->getParameters()); |
|
123
|
|
|
static::assertSame('content', $shortcode->getContent()); |
|
124
|
|
|
static::assertSame('text', $shortcode->getText()); |
|
125
|
|
|
static::assertSame(12, $shortcode->getOffset()); |
|
126
|
|
|
static::assertTrue($shortcode->hasContent()); |
|
127
|
|
|
|
|
128
|
|
|
static::assertFalse($shortcode->withContent(null)->hasContent()); |
|
129
|
|
|
static::assertSame('another', $shortcode->withContent('another')->getContent()); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function testShortcodeEmptyNameException() |
|
133
|
|
|
{ |
|
134
|
|
|
$this->willThrowException('InvalidArgumentException'); |
|
135
|
|
|
new Shortcode('', array(), null); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|