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 \PHPUnit_Framework_TestCase |
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
|
|
|
$this->assertSame($name, $s->getName()); |
27
|
|
|
$this->assertSame($args, $s->getParameters()); |
28
|
|
|
$this->assertSame($content, $s->getContent()); |
29
|
|
|
$this->assertSame($expected, $textSerializer->serialize($s)); |
30
|
|
|
$this->assertSame('arg', $s->getParameterAt(0)); |
31
|
|
|
$this->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
|
|
|
$this->assertTrue($shortcode->hasParameter('arg')); |
49
|
|
|
$this->assertFalse($shortcode->hasParameter('invalid')); |
50
|
|
|
$this->assertSame(null, $shortcode->getParameter('none')); |
51
|
|
|
$this->assertSame('value', $shortcode->getParameter('arg')); |
52
|
|
|
$this->assertSame('', $shortcode->getParameter('invalid', '')); |
53
|
|
|
$this->assertSame(42, $shortcode->getParameter('invalid', 42)); |
54
|
|
|
|
55
|
|
|
$this->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
|
|
|
$this->assertSame('code', $processed->getName()); |
77
|
|
|
$this->assertSame(array('arg' => 'val'), $processed->getParameters()); |
78
|
|
|
$this->assertSame('content', $processed->getContent()); |
79
|
|
|
|
80
|
|
|
$this->assertSame(20, $processed->getPosition()); |
81
|
|
|
$this->assertSame(10, $processed->getNamePosition()); |
82
|
|
|
$this->assertSame(' [code] ', $processed->getText()); |
83
|
|
|
$this->assertSame(1, $processed->getOffset()); |
84
|
|
|
$this->assertSame('[code]', $processed->getShortcodeText()); |
85
|
|
|
$this->assertSame(1, $processed->getIterationNumber()); |
86
|
|
|
$this->assertSame(0, $processed->getRecursionLevel()); |
87
|
|
|
$this->assertSame(null, $processed->getParent()); |
88
|
|
|
$this->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
|
|
|
$this->assertSame('p3', $p3->getName()); |
106
|
|
|
$this->assertSame('p2', $p3->getParent()->getName()); |
107
|
|
|
$this->assertSame('p1', $p3->getParent()->getParent()->getName()); |
|
|
|
|
108
|
|
|
$this->assertFalse($p1->hasAncestor('p3')); |
109
|
|
|
$this->assertFalse($p1->hasAncestor('p1')); |
110
|
|
|
$this->assertTrue($p2->hasAncestor('p1')); |
111
|
|
|
$this->assertFalse($p2->hasAncestor('p3')); |
112
|
|
|
$this->assertTrue($p3->hasAncestor('p1')); |
113
|
|
|
$this->assertTrue($p3->hasAncestor('p2')); |
114
|
|
|
$this->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
|
|
|
$this->assertSame('name', $shortcode->getName()); |
122
|
|
|
$this->assertSame(array('arg' => 'val'), $shortcode->getParameters()); |
123
|
|
|
$this->assertSame('content', $shortcode->getContent()); |
124
|
|
|
$this->assertSame('text', $shortcode->getText()); |
125
|
|
|
$this->assertSame(12, $shortcode->getOffset()); |
126
|
|
|
$this->assertSame(true, $shortcode->hasContent()); |
127
|
|
|
|
128
|
|
|
$this->assertSame(false, $shortcode->withContent(null)->hasContent()); |
129
|
|
|
$this->assertSame('another', $shortcode->withContent('another')->getContent()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testShortcodeEmptyNameException() |
133
|
|
|
{ |
134
|
|
|
$this->setExpectedException('InvalidArgumentException'); |
135
|
|
|
new Shortcode('', array(), null); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: