*/ 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 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 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), ''), array(new XmlSerializer(), new Shortcode('x', array('arg' => 'val'), null), 'val'), array(new XmlSerializer(), new Shortcode('x', array(), 'cnt'), 'cnt'), ); } /** * @dataProvider provideExceptions */ public function testSerializerExceptions(SerializerInterface $serializer, $value, $exceptionClass) { $this->willThrowException($exceptionClass); $serializer->unserialize($value); } public 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, '', 'InvalidArgumentException'), array($xml, 'sss', 'InvalidArgumentException'), array($xml, 'xxsss', 'InvalidArgumentException'), array($xml, 'xxsss', 'InvalidArgumentException'), array($xml, 'xxsss', 'InvalidArgumentException'), array($xml, '', 'InvalidArgumentException'), array($xml, '', 'InvalidArgumentException'), ); } }