4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Tests\Encoder;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Serializer\Encoder\YamlEncoder;
16 use Symfony\Component\Yaml\Dumper;
17 use Symfony\Component\Yaml\Parser;
18 use Symfony\Component\Yaml\Yaml;
21 * @author Kévin Dunglas <dunglas@gmail.com>
23 class YamlEncoderTest extends TestCase
25 public function testEncode()
27 $encoder = new YamlEncoder();
29 $this->assertEquals('foo', $encoder->encode('foo', 'yaml'));
30 $this->assertEquals('{ foo: 1 }', $encoder->encode(array('foo' => 1), 'yaml'));
33 public function testSupportsEncoding()
35 $encoder = new YamlEncoder();
37 $this->assertTrue($encoder->supportsEncoding('yaml'));
38 $this->assertFalse($encoder->supportsEncoding('json'));
41 public function testDecode()
43 $encoder = new YamlEncoder();
45 $this->assertEquals('foo', $encoder->decode('foo', 'yaml'));
46 $this->assertEquals(array('foo' => 1), $encoder->decode('{ foo: 1 }', 'yaml'));
49 public function testSupportsDecoding()
51 $encoder = new YamlEncoder();
53 $this->assertTrue($encoder->supportsDecoding('yaml'));
54 $this->assertFalse($encoder->supportsDecoding('json'));
57 public function testContext()
59 $encoder = new YamlEncoder(new Dumper(), new Parser(), array('yaml_inline' => 1, 'yaml_indent' => 4, 'yaml_flags' => Yaml::DUMP_OBJECT | Yaml::PARSE_OBJECT));
61 $obj = new \stdClass();
64 $legacyTag = " foo: !php/object:O:8:\"stdClass\":1:{s:3:\"bar\";i:2;}\n";
65 $spacedTag = " foo: !php/object 'O:8:\"stdClass\":1:{s:3:\"bar\";i:2;}'\n";
66 $this->assertThat($encoder->encode(array('foo' => $obj), 'yaml'), $this->logicalOr($this->equalTo($legacyTag), $this->equalTo($spacedTag)));
67 $this->assertEquals(' { foo: null }', $encoder->encode(array('foo' => $obj), 'yaml', array('yaml_inline' => 0, 'yaml_indent' => 2, 'yaml_flags' => 0)));
68 $this->assertEquals(array('foo' => $obj), $encoder->decode("foo: !php/object 'O:8:\"stdClass\":1:{s:3:\"bar\";i:2;}'", 'yaml'));
69 $this->assertEquals(array('foo' => null), $encoder->decode("foo: !php/object 'O:8:\"stdClass\":1:{s:3:\"bar\";i:2;}'", 'yaml', array('yaml_flags' => 0)));