X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;f=vendor%2Fsymfony%2Fyaml%2FTests%2FDumperTest.php;h=d84a14089be7f9a0f3952a232ed3211973f96259;hb=5b8bb166bfa98770daef9de5c127fc2e6ef02340;hp=6a1b3ac3d6059820f36fa72245b70d10a5f0ac38;hpb=eba34333e3c89f208d2f72fa91351ad019a71583;p=yaffs-website diff --git a/vendor/symfony/yaml/Tests/DumperTest.php b/vendor/symfony/yaml/Tests/DumperTest.php index 6a1b3ac3d..d84a14089 100644 --- a/vendor/symfony/yaml/Tests/DumperTest.php +++ b/vendor/symfony/yaml/Tests/DumperTest.php @@ -12,8 +12,9 @@ namespace Symfony\Component\Yaml\Tests; use PHPUnit\Framework\TestCase; -use Symfony\Component\Yaml\Parser; use Symfony\Component\Yaml\Dumper; +use Symfony\Component\Yaml\Parser; +use Symfony\Component\Yaml\Yaml; class DumperTest extends TestCase { @@ -51,6 +52,34 @@ class DumperTest extends TestCase $this->array = null; } + public function testIndentationInConstructor() + { + $dumper = new Dumper(7); + $expected = <<<'EOF' +'': bar +foo: '#bar' +'foo''bar': { } +bar: + - 1 + - foo +foobar: + foo: bar + bar: + - 1 + - foo + foobar: + foo: bar + bar: + - 1 + - foo + +EOF; + $this->assertEquals($expected, $dumper->dump($this->array, 4, 0)); + } + + /** + * @group legacy + */ public function testSetIndentation() { $this->dumper->setIndentation(7); @@ -178,10 +207,20 @@ EOF; } public function testObjectSupportEnabled() + { + $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_OBJECT); + + $this->assertEquals('{ foo: !php/object \'O:30:"Symfony\Component\Yaml\Tests\A":1:{s:1:"a";s:3:"foo";}\', bar: 1 }', $dump, '->dump() is able to dump objects'); + } + + /** + * @group legacy + */ + public function testObjectSupportEnabledPassingTrue() { $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true); - $this->assertEquals('{ foo: !php/object:O:30:"Symfony\Component\Yaml\Tests\A":1:{s:1:"a";s:3:"foo";}, bar: 1 }', $dump, '->dump() is able to dump objects'); + $this->assertEquals('{ foo: !php/object \'O:30:"Symfony\Component\Yaml\Tests\A":1:{s:1:"a";s:3:"foo";}\', bar: 1 }', $dump, '->dump() is able to dump objects'); } public function testObjectSupportDisabledButNoExceptions() @@ -196,7 +235,34 @@ EOF; */ public function testObjectSupportDisabledWithExceptions() { - $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true, false); + $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE); + } + + /** + * @group legacy + * @expectedException \Symfony\Component\Yaml\Exception\DumpException + */ + public function testObjectSupportDisabledWithExceptionsPassingTrue() + { + $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true); + } + + public function testEmptyArray() + { + $dump = $this->dumper->dump(array()); + $this->assertEquals('{ }', $dump); + + $dump = $this->dumper->dump(array(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE); + $this->assertEquals('[]', $dump); + + $dump = $this->dumper->dump(array(), 9, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE); + $this->assertEquals('[]', $dump); + + $dump = $this->dumper->dump(new \ArrayObject(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP); + $this->assertEquals('{ }', $dump); + + $dump = $this->dumper->dump(new \stdClass(), 0, 0, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP); + $this->assertEquals('{ }', $dump); } /** @@ -232,13 +298,185 @@ EOF; ); } + public function testBinaryDataIsDumpedBase64Encoded() + { + $binaryData = file_get_contents(__DIR__.'/Fixtures/arrow.gif'); + $expected = '{ data: !!binary '.base64_encode($binaryData).' }'; + + $this->assertSame($expected, $this->dumper->dump(array('data' => $binaryData))); + } + + public function testNonUtf8DataIsDumpedBase64Encoded() + { + // "für" (ISO-8859-1 encoded) + $this->assertSame('!!binary ZsM/cg==', $this->dumper->dump("f\xc3\x3fr")); + } + + /** + * @dataProvider objectAsMapProvider + */ + public function testDumpObjectAsMap($object, $expected) + { + $yaml = $this->dumper->dump($object, 0, 0, Yaml::DUMP_OBJECT_AS_MAP); + + $this->assertEquals($expected, Yaml::parse($yaml, Yaml::PARSE_OBJECT_FOR_MAP)); + } + + public function objectAsMapProvider() + { + $tests = array(); + + $bar = new \stdClass(); + $bar->class = 'classBar'; + $bar->args = array('bar'); + $zar = new \stdClass(); + $foo = new \stdClass(); + $foo->bar = $bar; + $foo->zar = $zar; + $object = new \stdClass(); + $object->foo = $foo; + $tests['stdClass'] = array($object, $object); + + $arrayObject = new \ArrayObject(); + $arrayObject['foo'] = 'bar'; + $arrayObject['baz'] = 'foobar'; + $parsedArrayObject = new \stdClass(); + $parsedArrayObject->foo = 'bar'; + $parsedArrayObject->baz = 'foobar'; + $tests['ArrayObject'] = array($arrayObject, $parsedArrayObject); + + $a = new A(); + $tests['arbitrary-object'] = array($a, null); + + return $tests; + } + + public function testDumpingArrayObjectInstancesRespectsInlineLevel() + { + $deep = new \ArrayObject(array('deep1' => 'd', 'deep2' => 'e')); + $inner = new \ArrayObject(array('inner1' => 'b', 'inner2' => 'c', 'inner3' => $deep)); + $outer = new \ArrayObject(array('outer1' => 'a', 'outer2' => $inner)); + + $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP); + + $expected = <<assertSame($expected, $yaml); + } + + public function testDumpingArrayObjectInstancesWithNumericKeysInlined() + { + $deep = new \ArrayObject(array('d', 'e')); + $inner = new \ArrayObject(array('b', 'c', $deep)); + $outer = new \ArrayObject(array('a', $inner)); + + $yaml = $this->dumper->dump($outer, 0, 0, Yaml::DUMP_OBJECT_AS_MAP); + $expected = <<assertSame($expected, $yaml); + } + + public function testDumpingArrayObjectInstancesWithNumericKeysRespectsInlineLevel() + { + $deep = new \ArrayObject(array('d', 'e')); + $inner = new \ArrayObject(array('b', 'c', $deep)); + $outer = new \ArrayObject(array('a', $inner)); + $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP); + $expected = <<assertEquals($expected, $yaml); + } + + public function testDumpEmptyArrayObjectInstanceAsMap() + { + $this->assertSame('{ }', $this->dumper->dump(new \ArrayObject(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP)); + } + + public function testDumpEmptyStdClassInstanceAsMap() + { + $this->assertSame('{ }', $this->dumper->dump(new \stdClass(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP)); + } + + public function testDumpingStdClassInstancesRespectsInlineLevel() + { + $deep = new \stdClass(); + $deep->deep1 = 'd'; + $deep->deep2 = 'e'; + + $inner = new \stdClass(); + $inner->inner1 = 'b'; + $inner->inner2 = 'c'; + $inner->inner3 = $deep; + + $outer = new \stdClass(); + $outer->outer1 = 'a'; + $outer->outer2 = $inner; + + $yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP); + + $expected = <<assertSame($expected, $yaml); + } + + public function testDumpMultiLineStringAsScalarBlock() + { + $data = array( + 'data' => array( + 'single_line' => 'foo bar baz', + 'multi_line' => "foo\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz", + 'multi_line_with_carriage_return' => "foo\nbar\r\nbaz", + 'nested_inlined_multi_line_string' => array( + 'inlined_multi_line' => "foo\nbar\r\nempty line:\n\nbaz", + ), + ), + ); + + $this->assertSame(file_get_contents(__DIR__.'/Fixtures/multiple_lines_as_literal_block.yml'), $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)); + } + + public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace() + { + $data = array( + 'data' => array( + 'multi_line' => " the first line has leading spaces\nThe second line does not.", + ), + ); + + $this->assertSame(file_get_contents(__DIR__.'/Fixtures/multiple_lines_as_literal_block_leading_space_in_first_line.yml'), $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)); + } + + public function testCarriageReturnIsMaintainedWhenDumpingAsMultiLineLiteralBlock() + { + $this->assertSame("- \"a\\r\\nb\\nc\"\n", $this->dumper->dump(array("a\r\nb\nc"), 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)); + } + /** * @expectedException \InvalidArgumentException * @expectedExceptionMessage The indentation must be greater than zero */ public function testZeroIndentationThrowsException() { - $this->dumper->setIndentation(0); + new Dumper(0); } /** @@ -247,7 +485,7 @@ EOF; */ public function testNegativeIndentationThrowsException() { - $this->dumper->setIndentation(-4); + new Dumper(-4); } }