bug #17978 [Yaml] ensure dump indentation to be greather than zero (xabbuh)

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 | https://github.com/symfony/symfony/pull/17943#issuecomment-190881815, #17977
| License       | MIT
| Doc PR        |

Commits
-------

3464282 ensure dump indentation to be greather than zero
This commit is contained in:
Fabien Potencier 2016-03-02 11:08:25 +01:00
commit 81b59b9eca
4 changed files with 44 additions and 0 deletions

View File

@ -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;
}

View File

@ -229,6 +229,24 @@ EOF;
'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

View File

@ -28,4 +28,22 @@ class YamlTest extends \PHPUnit_Framework_TestCase
$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);
}
}

View File

@ -83,6 +83,10 @@ class Yaml
*/
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);