Skip to content

Commit c8b4b52

Browse files
committed
[Config] Throw an exception when using cannotBeEmpty() on numeric or boolean nodes
1 parent 99f93d6 commit c8b4b52

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

Definition/Builder/BooleanNodeDefinition.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Config\Definition\Builder;
1313

1414
use Symfony\Component\Config\Definition\BooleanNode;
15+
use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;
1516

1617
/**
1718
* This class provides a fluent interface for defining a node.
@@ -39,4 +40,14 @@ protected function instantiateNode()
3940
{
4041
return new BooleanNode($this->name, $this->parent);
4142
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*
47+
* @throws InvalidDefinitionException
48+
*/
49+
public function cannotBeEmpty()
50+
{
51+
throw new InvalidDefinitionException('->cannotBeEmpty() is not applicable to BooleanNodeDefinition.');
52+
}
4253
}

Definition/Builder/NumericNodeDefinition.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Config\Definition\Builder;
1313

14+
use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;
15+
1416
/**
1517
* Abstract class that contains common code of integer and float node definitions.
1618
*
@@ -58,4 +60,14 @@ public function min($min)
5860

5961
return $this;
6062
}
63+
64+
/**
65+
* {@inheritdoc}
66+
*
67+
* @throws InvalidDefinitionException
68+
*/
69+
public function cannotBeEmpty()
70+
{
71+
throw new InvalidDefinitionException('->cannotBeEmpty() is not applicable to NumericNodeDefinition.');
72+
}
6173
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Config\Tests\Definition\Builder;
13+
14+
use Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition;
15+
16+
class BooleanNodeDefinitionTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidDefinitionException
20+
* @expectedExceptionMessage ->cannotBeEmpty() is not applicable to BooleanNodeDefinition.
21+
*/
22+
public function testCannotBeEmptyThrowsAnException()
23+
{
24+
$def = new BooleanNodeDefinition('foo');
25+
$def->cannotBeEmpty();
26+
}
27+
}

Tests/Definition/Builder/NumericNodeDefinitionTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,14 @@ public function testFloatValidMinMaxAssertion()
9090
$node = $def->min(3.0)->max(7e2)->getNode();
9191
$this->assertEquals(4.5, $node->finalize(4.5));
9292
}
93+
94+
/**
95+
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidDefinitionException
96+
* @expectedExceptionMessage ->cannotBeEmpty() is not applicable to NumericNodeDefinition.
97+
*/
98+
public function testCannotBeEmptyThrowsAnException()
99+
{
100+
$def = new NumericNodeDefinition('foo');
101+
$def->cannotBeEmpty();
102+
}
93103
}

0 commit comments

Comments
 (0)