8000 Fixed dumpArray when passing an empty array · symfony/symfony@9920a58 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9920a58

Browse files
committed
Fixed dumpArray when passing an empty array
1 parent 751f2be commit 9920a58

File tree

4 files changed

+43
-29
lines changed

4 files changed

+43
-29
lines changed

src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ services:
1515
properties: { foo: bar, moo: '@foo.baz', qux: { '%foo%': 'foo is %foo%', foobar: '%foo%' } }
1616
calls:
1717
- [setBar, ['@bar']]
18-
- [initialize, { }]
18+
- [initialize, []]
1919

2020
configurator: sc_configure
2121
bar:

src/Symfony/Component/Yaml/Inline.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,14 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp
156156
*/
157157
private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport)
158158
{
159-
// array
160159
$keys = array_keys($value);
161160
$keysCount = count($keys);
161+
162+
if (0 === $keysCount) {
163+
return '[]';
164+
}
165+
166+
// sequence
162167
if ((1 === $keysCount && '0' == $keys[0])
163168
|| ($keysCount > 1 && array_reduce($keys, function ($v, $w) { return (int) $v + $w; }, 0) === $keysCount * ($keysCount - 1) / 2)
164169
) {

src/Symfony/Component/Yaml/Tests/DumperTest.php

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function testSetIndentation()
5757
$expected = <<<EOF
5858
'': bar
5959
foo: '#bar'
60-
'foo''bar': { }
60+
'foo''bar': []
6161
bar:
6262
- 1
6363
- foo
@@ -102,28 +102,38 @@ public function testSpecifications()
102102
}
103103
}
104104

105-
public function testInlineLevel()
105+
/**
106+
* @dataProvider getInlineLevelTests
107+
*/
108+
public function testInlineLevel($expected, $levels)
106109
{
107-
$expected = <<<EOF
108-
{ '': bar, foo: '#bar', 'foo''bar': { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
109-
EOF;
110-
$this->assertEquals($expected, $this->dumper->dump($this->array, -10), '->dump() takes an inline level argument');
111-
$this->assertEquals($expected, $this->dumper->dump($this->array, 0), '->dump() takes an inline level argument');
110+
$levels = (array) $levels;
112111

113-
$expected = <<<EOF
112+
foreach ($levels as $level) {
113+
$this->assertEquals($expected, $this->dumper->dump($this->array, $level), '->dump() takes an inline level argument');
114+
}
115+
}
116+
117+
public function getInlineLevelTests()
118+
{
119+
return array(
120+
array(<<<EOF
121+
{ '': bar, foo: '#bar', 'foo''bar': [], bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
122+
EOF
123+
, array(-10, 0)),
124+
array(<<<EOF
114125
'': bar
115126
foo: '#bar'
116-
'foo''bar': { }
127+
'foo''bar': []
117128
bar: [1, foo]
118129
foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }
119130
120-
EOF;
121-
$this->assertEquals($expected, $this->dumper->dump($this->array, 1), '->dump() takes an inline level argument');
122-
123-
$expected = <<<EOF
131+
EOF
132+
, 1),
133+
array(<<<EOF
124134
'': bar
125135
foo: '#bar'
126-
'foo''bar': { }
136+
'foo''bar': []
127137
bar:
128138
- 1
129139
- foo
@@ -132,13 +142,12 @@ public function testInlineLevel()
132142
bar: [1, foo]
133143
foobar: { foo: bar, bar: [1, foo] }
134144
135-
EOF;
136-
$this->assertEquals($expected, $this->dumper->dump($this->array, 2), '->dump() takes an inline level argument');
137-
138-
$expected = <<<EOF
145+
EOF
146+
, 2),
147+
array(<<<EOF
139148
'': bar
140149
foo: '#bar'
141-
'foo''bar': { }
150+
'foo''bar': []
142151
bar:
143152
- 1
144153
- foo
@@ -151,13 +160,12 @@ public function testInlineLevel()
151160
foo: bar
152161
bar: [1, foo]
153162
154-
EOF;
155-
$this->assertEquals($expected, $this->dumper->dump($this->array, 3), '->dump() takes an inline level argument');
156-
157-
$expected = <<<EOF
163+
EOF
164+
, 3),
165+
array(<<<EOF
158166
'': bar
159167
foo: '#bar'
160-
'foo''bar': { }
168+
'foo''bar': []
161169
bar:
162170
- 1
163171
- foo
@@ -172,9 +180,9 @@ public function testInlineLevel()
172180
- 1
173181
- foo
174182
175-
EOF;
176-
$this->assertEquals($expected, $this->dumper->dump($this->array, 4), '->dump() takes an inline level argument');
177-
$this->assertEquals($expected, $this->dumper->dump($this->array, 10), '->dump() takes an inline level argument');
183+
EOF
184+
, array(4, 10)),
185+
);
178186
}
179187

180188
public function testObjectSupportEnabled()

src/Symfony/Component/Yaml/Tests/InlineTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ protected function getTestsForDump()
278278
"'off'" => 'off',
279279

280280
// sequences
281+
'[]' => array(),
281282
'[foo, bar, false, null, 12]' => array('foo', 'bar', false, null, 12),
282283
'[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),
283284

0 commit comments

Comments
 (0)
0