d08272df50482e6f0bb45ae51efb4136daa98271
[yaffs-website] / serializer / Tests / Encoder / YamlEncoderTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Serializer\Tests\Encoder;
13
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;
19
20 /**
21  * @author Kévin Dunglas <dunglas@gmail.com>
22  */
23 class YamlEncoderTest extends TestCase
24 {
25     public function testEncode()
26     {
27         $encoder = new YamlEncoder();
28
29         $this->assertEquals('foo', $encoder->encode('foo', 'yaml'));
30         $this->assertEquals('{ foo: 1 }', $encoder->encode(array('foo' => 1), 'yaml'));
31     }
32
33     public function testSupportsEncoding()
34     {
35         $encoder = new YamlEncoder();
36
37         $this->assertTrue($encoder->supportsEncoding('yaml'));
38         $this->assertFalse($encoder->supportsEncoding('json'));
39     }
40
41     public function testDecode()
42     {
43         $encoder = new YamlEncoder();
44
45         $this->assertEquals('foo', $encoder->decode('foo', 'yaml'));
46         $this->assertEquals(array('foo' => 1), $encoder->decode('{ foo: 1 }', 'yaml'));
47     }
48
49     public function testSupportsDecoding()
50     {
51         $encoder = new YamlEncoder();
52
53         $this->assertTrue($encoder->supportsDecoding('yaml'));
54         $this->assertFalse($encoder->supportsDecoding('json'));
55     }
56
57     public function testContext()
58     {
59         $encoder = new YamlEncoder(new Dumper(), new Parser(), array('yaml_inline' => 1, 'yaml_indent' => 4, 'yaml_flags' => Yaml::DUMP_OBJECT | Yaml::PARSE_OBJECT));
60
61         $obj = new \stdClass();
62         $obj->bar = 2;
63
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)));
70     }
71 }