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