Skip to content

Commit

Permalink
bug #17978 [Yaml] ensure dump indentation to be greather than zero (x…
Browse files Browse the repository at this point in the history
…abbuh)

This PR was merged into the 2.3 branch.

Discussion
----------

[Yaml] ensure dump indentation to be greather than zero

| Q             | A
| ------------- | ---
| Branch        | 2.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #17943 (comment), #17977
| License       | MIT
| Doc PR        |

Commits
-------

3464282 ensure dump indentation to be greather than zero
  • Loading branch information
fabpot committed Mar 2, 2016
2 parents b5a3a56 + 3464282 commit 81b59b9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Symfony/Component/Yaml/Dumper.php
Expand Up @@ -32,6 +32,10 @@ class Dumper
*/
public function setIndentation($num)
{
if ($num < 1) {
throw new \InvalidArgumentException('The indentation must be greater than zero.');
}

$this->indentation = (int) $num;
}

Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Yaml/Tests/DumperTest.php
Expand Up @@ -229,6 +229,24 @@ public function getEscapeSequences()
'paragraph-separator' => array("\t\\P", '"\t\\\\P"'),
);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The indentation must be greater than zero
*/
public function testZeroIndentationThrowsException()
{
$this->dumper->setIndentation(0);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The indentation must be greater than zero
*/
public function testNegativeIndentationThrowsException()
{
$this->dumper->setIndentation(-4);
}
}

class A
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Yaml/Tests/YamlTest.php
Expand Up @@ -28,4 +28,22 @@ public function testParseAndDump()
$parsedByContents = Yaml::parse($contents);
$this->assertEquals($parsedByFilename, $parsedByContents);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The indentation must be greater than zero
*/
public function testZeroIndentationThrowsException()
{
Yaml::dump(array('lorem' => 'ipsum', 'dolor' => 'sit'), 2, 0);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The indentation must be greater than zero
*/
public function testNegativeIndentationThrowsException()
{
Yaml::dump(array('lorem' => 'ipsum', 'dolor' => 'sit'), 2, -4);
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Component/Yaml/Yaml.php
Expand Up @@ -83,6 +83,10 @@ public static function parse($input, $exceptionOnInvalidType = false, $objectSup
*/
public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false)
{
if ($indent < 1) {
throw new \InvalidArgumentException('The indentation must be greater than zero.');
}

$yaml = new Dumper();
$yaml->setIndentation($indent);

Expand Down

0 comments on commit 81b59b9

Please sign in to comment.