|
1
|
|
|
<?php |
|
2
|
|
|
namespace Thunder\Shortcode\Tests; |
|
3
|
|
|
|
|
4
|
|
|
use Thunder\Shortcode\Serializer\JsonSerializer; |
|
5
|
|
|
use Thunder\Shortcode\Serializer\SerializerInterface; |
|
6
|
|
|
use Thunder\Shortcode\Serializer\TextSerializer; |
|
7
|
|
|
use Thunder\Shortcode\Serializer\XmlSerializer; |
|
8
|
|
|
use Thunder\Shortcode\Serializer\YamlSerializer; |
|
9
|
|
|
use Thunder\Shortcode\Shortcode\ParsedShortcode; |
|
10
|
|
|
use Thunder\Shortcode\Shortcode\Shortcode; |
|
11
|
|
|
use Thunder\Shortcode\Shortcode\ShortcodeInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
final class SerializerTest extends AbstractTestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @dataProvider provideShortcodes |
|
20
|
|
|
*/ |
|
21
|
|
|
public function testSerializer(SerializerInterface $serializer, ShortcodeInterface $test) |
|
22
|
|
|
{ |
|
23
|
|
|
$result = $serializer->serialize($test); |
|
24
|
|
|
$tested = $serializer->unserialize($result); |
|
25
|
|
|
|
|
26
|
|
|
static::assertSame($test->getName(), $tested->getName(), 'name: '.$result); |
|
27
|
|
|
static::assertSame($test->getParameters(), $tested->getParameters(), 'parameters: '.$result); |
|
28
|
|
|
static::assertSame($test->getContent(), $tested->getContent(), 'content: '.$result); |
|
29
|
|
|
static::assertSame($test->getBbCode(), $tested->getBbCode(), 'bbCode: '.$result); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function provideShortcodes() |
|
33
|
|
|
{ |
|
34
|
|
|
$shortcodes = array( |
|
35
|
|
|
new Shortcode('x', array(), null), |
|
36
|
|
|
new Shortcode('x', array('arg' => 'val'), null), |
|
37
|
|
|
new Shortcode('x', array('arg' => null), null), |
|
38
|
|
|
new Shortcode('x', array('arg' => ''), null), |
|
39
|
|
|
new Shortcode('x', array('arg' => 'val'), 'cnt'), |
|
40
|
|
|
new ParsedShortcode(new Shortcode('self-closed', array(), null), '[self-closed /]', 0), |
|
41
|
|
|
new Shortcode('self-closed', array(), null, 'bb code'."\n".' value'), |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
$serializers = array( |
|
45
|
|
|
new TextSerializer(), |
|
46
|
|
|
new JsonSerializer(), |
|
47
|
|
|
new XmlSerializer(), |
|
48
|
|
|
new YamlSerializer(), |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
$tests = array(); |
|
52
|
|
|
foreach($shortcodes as $shortcode) { |
|
53
|
|
|
foreach($serializers as $serializer) { |
|
54
|
|
|
$tests[] = array($serializer, $shortcode); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $tests; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @dataProvider provideUnserialized |
|
63
|
|
|
*/ |
|
64
|
|
|
public function testUnserialize(SerializerInterface $serializer, ShortcodeInterface $test, $text) |
|
65
|
|
|
{ |
|
66
|
|
|
$tested = $serializer->unserialize($text); |
|
67
|
|
|
|
|
68
|
|
|
static::assertSame($test->getName(), $tested->getName(), 'name: '.$text); |
|
69
|
|
|
static::assertSame($test->getParameters(), $tested->getParameters(), 'parameters: '.$text); |
|
70
|
|
|
static::assertSame($test->getContent(), $tested->getContent(), 'content: '.$text); |
|
71
|
|
|
static::assertSame($test->getBbCode(), $tested->getBbCode(), 'bbCode: '.$text); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function provideUnserialized() |
|
75
|
|
|
{ |
|
76
|
|
|
return array( |
|
77
|
|
|
array(new JsonSerializer(), new Shortcode('x', array(), null), '{"name":"x"}'), |
|
78
|
|
|
array(new JsonSerializer(), new Shortcode('x', array('arg' => 'val'), null), '{"name":"x","parameters":{"arg":"val"}}'), |
|
79
|
|
|
array(new JsonSerializer(), new Shortcode('x', array(), 'cnt'), '{"name":"x","content":"cnt"}'), |
|
80
|
|
|
array(new YamlSerializer(), new Shortcode('x', array(), null), 'name: x'), |
|
81
|
|
|
array(new YamlSerializer(), new Shortcode('x', array('arg' => 'val'), null), 'name: x'."\n".'parameters:'."\n".' arg: val'), |
|
82
|
|
|
array(new YamlSerializer(), new Shortcode('x', array(), 'cnt'), 'name: x'."\n".'content: cnt'), |
|
83
|
|
|
array(new XmlSerializer(), new Shortcode('x', array(), null), '<shortcode name="x"></shortcode>'), |
|
84
|
|
|
array(new XmlSerializer(), new Shortcode('x', array('arg' => 'val'), null), '<shortcode name="x"><parameters><parameter name="arg">val</parameter></parameters></shortcode>'), |
|
85
|
|
|
array(new XmlSerializer(), new Shortcode('x', array(), 'cnt'), '<shortcode name="x"><content>cnt</content></shortcode>'), |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @dataProvider provideExceptions |
|
91
|
|
|
*/ |
|
92
|
|
|
public function testSerializerExceptions(SerializerInterface $serializer, $value, $exceptionClass) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->willThrowException($exceptionClass); |
|
95
|
|
|
$serializer->unserialize($value); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function provideExceptions() |
|
99
|
|
|
{ |
|
100
|
|
|
$xml = new XmlSerializer(); |
|
101
|
|
|
$yaml = new YamlSerializer(); |
|
102
|
|
|
$text = new TextSerializer(); |
|
103
|
|
|
$json = new JsonSerializer(); |
|
104
|
|
|
|
|
105
|
|
|
return array( |
|
106
|
|
|
array($text, '[sc /] c [xx]', 'InvalidArgumentException'), |
|
107
|
|
|
array($text, '[/sc]', 'InvalidArgumentException'), |
|
108
|
|
|
array($json, '{}', 'InvalidArgumentException'), |
|
109
|
|
|
array($json, '', 'InvalidArgumentException'), |
|
110
|
|
|
array($json, '{"name":"x","parameters":null}', 'InvalidArgumentException'), |
|
111
|
|
|
array($json, '{"name":"x","parameters":{"key":[]}}', 'InvalidArgumentException'), |
|
112
|
|
|
array($yaml, 'shortcode: ', 'InvalidArgumentException'), |
|
113
|
|
|
array($yaml, '', 'InvalidArgumentException'), |
|
114
|
|
|
array($yaml, 'name: x'."\n".'parameters: string', 'InvalidArgumentException'), |
|
115
|
|
|
array($xml, '<shortcode />', 'InvalidArgumentException'), |
|
116
|
|
|
array($xml, '<shortcode name=""><content>sss</content></shortcode>', 'InvalidArgumentException'), |
|
117
|
|
|
array($xml, '<shortcode name="x"><parameters><parameter>xx</parameter></parameters><content>sss</content></shortcode>', 'InvalidArgumentException'), |
|
118
|
|
|
array($xml, '<shortcode name="x"><parameters><parameter name="">xx</parameter></parameters><content>sss</content></shortcode>', 'InvalidArgumentException'), |
|
119
|
|
|
array($xml, '<shortcode name="x"><parameters><parameter name=>xx</parameter></parameters><content>sss</content></shortcode>', 'InvalidArgumentException'), |
|
120
|
|
|
array($xml, '<invalid />', 'InvalidArgumentException'), |
|
121
|
|
|
array($xml, '', 'InvalidArgumentException'), |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|