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