DumperTest.php (6554B)
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\Yaml\Tests; 13 14 use Symfony\Component\Yaml\Parser; 15 use Symfony\Component\Yaml\Dumper; 16 17 class DumperTest extends \PHPUnit_Framework_TestCase 18 { 19 protected $parser; 20 protected $dumper; 21 protected $path; 22 23 protected $array = array( 24 '' => 'bar', 25 'foo' => '#bar', 26 'foo\'bar' => array(), 27 'bar' => array(1, 'foo'), 28 'foobar' => array( 29 'foo' => 'bar', 30 'bar' => array(1, 'foo'), 31 'foobar' => array( 32 'foo' => 'bar', 33 'bar' => array(1, 'foo'), 34 ), 35 ), 36 ); 37 38 protected function setUp() 39 { 40 $this->parser = new Parser(); 41 $this->dumper = new Dumper(); 42 $this->path = __DIR__.'/Fixtures'; 43 } 44 45 protected function tearDown() 46 { 47 $this->parser = null; 48 $this->dumper = null; 49 $this->path = null; 50 $this->array = null; 51 } 52 53 public function testSetIndentation() 54 { 55 $this->dumper->setIndentation(7); 56 57 $expected = <<<EOF 58 '': bar 59 foo: '#bar' 60 'foo''bar': { } 61 bar: 62 - 1 63 - foo 64 foobar: 65 foo: bar 66 bar: 67 - 1 68 - foo 69 foobar: 70 foo: bar 71 bar: 72 - 1 73 - foo 74 75 EOF; 76 $this->assertEquals($expected, $this->dumper->dump($this->array, 4, 0)); 77 } 78 79 public function testSpecifications() 80 { 81 $files = $this->parser->parse(file_get_contents($this->path.'/index.yml')); 82 foreach ($files as $file) { 83 $yamls = file_get_contents($this->path.'/'.$file.'.yml'); 84 85 // split YAMLs documents 86 foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) { 87 if (!$yaml) { 88 continue; 89 } 90 91 $test = $this->parser->parse($yaml); 92 if (isset($test['dump_skip']) && $test['dump_skip']) { 93 continue; 94 } elseif (isset($test['todo']) && $test['todo']) { 95 // TODO 96 } else { 97 eval('$expected = '.trim($test['php']).';'); 98 $this->assertSame($expected, $this->parser->parse($this->dumper->dump($expected, 10)), $test['test']); 99 } 100 } 101 } 102 } 103 104 public function testInlineLevel() 105 { 106 $expected = <<<EOF 107 { '': bar, foo: '#bar', 'foo''bar': { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } } 108 EOF; 109 $this->assertEquals($expected, $this->dumper->dump($this->array, -10), '->dump() takes an inline level argument'); 110 $this->assertEquals($expected, $this->dumper->dump($this->array, 0), '->dump() takes an inline level argument'); 111 112 $expected = <<<EOF 113 '': bar 114 foo: '#bar' 115 'foo''bar': { } 116 bar: [1, foo] 117 foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } 118 119 EOF; 120 $this->assertEquals($expected, $this->dumper->dump($this->array, 1), '->dump() takes an inline level argument'); 121 122 $expected = <<<EOF 123 '': bar 124 foo: '#bar' 125 'foo''bar': { } 126 bar: 127 - 1 128 - foo 129 foobar: 130 foo: bar 131 bar: [1, foo] 132 foobar: { foo: bar, bar: [1, foo] } 133 134 EOF; 135 $this->assertEquals($expected, $this->dumper->dump($this->array, 2), '->dump() takes an inline level argument'); 136 137 $expected = <<<EOF 138 '': bar 139 foo: '#bar' 140 'foo''bar': { } 141 bar: 142 - 1 143 - foo 144 foobar: 145 foo: bar 146 bar: 147 - 1 148 - foo 149 foobar: 150 foo: bar 151 bar: [1, foo] 152 153 EOF; 154 $this->assertEquals($expected, $this->dumper->dump($this->array, 3), '->dump() takes an inline level argument'); 155 156 $expected = <<<EOF 157 '': bar 158 foo: '#bar' 159 'foo''bar': { } 160 bar: 161 - 1 162 - foo 163 foobar: 164 foo: bar 165 bar: 166 - 1 167 - foo 168 foobar: 169 foo: bar 170 bar: 171 - 1 172 - foo 173 174 EOF; 175 $this->assertEquals($expected, $this->dumper->dump($this->array, 4), '->dump() takes an inline level argument'); 176 $this->assertEquals($expected, $this->dumper->dump($this->array, 10), '->dump() takes an inline level argument'); 177 } 178 179 public function testObjectSupportEnabled() 180 { 181 $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, false, true); 182 183 $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'); 184 } 185 186 public function testObjectSupportDisabledButNoExceptions() 187 { 188 $dump = $this->dumper->dump(array('foo' => new A(), 'bar' => 1)); 189 190 $this->assertEquals('{ foo: null, bar: 1 }', $dump, '->dump() does not dump objects when disabled'); 191 } 192 193 /** 194 * @expectedException \Symfony\Component\Yaml\Exception\DumpException 195 */ 196 public function testObjectSupportDisabledWithExceptions() 197 { 198 $this->dumper->dump(array('foo' => new A(), 'bar' => 1), 0, 0, true, false); 199 } 200 201 /** 202 * @dataProvider getEscapeSequences 203 */ 204 public function testEscapedEscapeSequencesInQuotedScalar($input, $expected) 205 { 206 $this->assertEquals($expected, $this->dumper->dump($input)); 207 } 208 209 public function getEscapeSequences() 210 { 211 return array( 212 'null' => array("\t\\0", '"\t\\\\0"'), 213 'bell' => array("\t\\a", '"\t\\\\a"'), 214 'backspace' => array("\t\\b", '"\t\\\\b"'), 215 'horizontal-tab' => array("\t\\t", '"\t\\\\t"'), 216 'line-feed' => array("\t\\n", '"\t\\\\n"'), 217 'vertical-tab' => array("\t\\v", '"\t\\\\v"'), 218 'form-feed' => array("\t\\f", '"\t\\\\f"'), 219 'carriage-return' => array("\t\\r", '"\t\\\\r"'), 220 'escape' => array("\t\\e", '"\t\\\\e"'), 221 'space' => array("\t\\ ", '"\t\\\\ "'), 222 'double-quote' => array("\t\\\"", '"\t\\\\\\""'), 223 'slash' => array("\t\\/", '"\t\\\\/"'), 224 'backslash' => array("\t\\\\", '"\t\\\\\\\\"'), 225 'next-line' => array("\t\\N", '"\t\\\\N"'), 226 'non-breaking-space' => array("\t\\�", '"\t\\\\�"'), 227 'line-separator' => array("\t\\L", '"\t\\\\L"'), 228 'paragraph-separator' => array("\t\\P", '"\t\\\\P"'), 229 ); 230 } 231 } 232 233 class A 234 { 235 public $a = 'foo'; 236 }