8000 [VarDumper] Draft: PositiveValidator extends GreaterThanValidator · symfony/symfony@43aef65 · GitHub
[go: up one dir, main page]

Skip to content

Commit 43aef65

Browse files
committed
[VarDumper] Draft: PositiveValidator extends GreaterThanValidator
1 parent c4a20ab commit 43aef65

File tree

3 files changed

+48
-80
lines changed

3 files changed

+48
-80
lines changed

src/Symfony/Component/Validator/Constraints/Positive.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,18 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Validator\Constraint;
15-
1614
/**
1715
* @Annotation
1816
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
1917
*
2018
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
2119
*/
22-
class Positive extends Constraint
20+
class Positive extends AbstractComparison
2321
{
2422
const IS_NEGATIVE_ERROR = 'da93d4fe-371c-4bc2-99a3-9201efe7e1a4';
25-
const IS_ZERO_ERROR = '3a10c504-6039-424f-833e-f1f805da1d7a';
2623

2724
protected static $errorNames = array(
2825
self::IS_NEGATIVE_ERROR => 'IS_NEGATIVE_ERROR',
29-
self::IS_ZERO_ERROR => 'IS_ZERO_ERROR',
3026
);
3127

3228
public $message = 'This value should be positive.';

src/Symfony/Component/Validator/Constraints/PositiveValidator.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@
1010
*/
1111

1212
namespace Symfony\Component\Validator\Constraints;
13-
1413
use Symfony\Component\Validator\Constraint;
15-
use Symfony\Component\Validator\ConstraintValidator;
1614
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1715

1816
/**
19-
* Validates that values are positive.
17+
* Validates that values are positive (>0).
2018
*
2119
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
2220
*/
23-
class PositiveValidator extends ConstraintValidator
21+
class PositiveValidator extends GreaterThanValidator
2422
{
2523
/**
2624
* {@inheritdoc}
@@ -39,20 +37,22 @@ public function validate($value, Constraint $constraint)
3937
throw new UnexpectedTypeException($value, 'int, float or string');
4038
}
4139

42-
$value = (float) $value;
40+
parent::validate($value, $constraint);
41+
}
4342

44-
if (.0 === $value) {
45-
$this->context->buildViolation($constraint->message)
46-
->setParameter('{{ value }}', $this->formatValue($value))
47-
->setCode(Positive::IS_ZERO_ERROR)
48-
->addViolation();
49-
}
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
protected function compareValues($value1, $value2)
47+
{
48+
return $value1 > 0;
49+
}
5050

51-
if ($value < 0) {
52-
$this->context->buildViolation($constraint->message)
53-
->setParameter('{{ value }}', $this->formatValue($value))
54-
->setCode(Positive::IS_NEGATIVE_ERROR)
55-
->addViolation();
56-
}
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
protected function getErrorCode()
55+
{
56+
return Positive::IS_NEGATIVE_ERROR;
5757
}
5858
}

src/Symfony/Component/Validator/Tests/Constraints/PositiveValidatorTest.php

Lines changed: 30 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,96 +13,59 @@
1313

1414
use Symfony\Component\Validator\Constraints\Positive;
1515
use Symfony\Component\Validator\Constraints\PositiveValidator;
16-
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
1716

1817
/**
1918
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
2019
*/
21-
class PositiveValidatorTest extends ConstraintValidatorTestCase
20+
class PositiveValidatorTest extends AbstractComparisonValidatorTestCase
2221
{
2322
protected function createValidator()
2423
{
2524
return new PositiveValidator();
2625
}
2726

28-
/**
29-
* @dataProvider getValidValues
30-
*/
31-
public function testValidValues($value)
27+
protected function createConstraint(array $options = null)
3228
{
33-
$this->validator->validate($value, new Positive());
34-
35-
$this->assertNoViolation();
29+
return new Positive($options);
3630
}
3731

38-
public function getValidValues()
32+
protected function getErrorCode()
3933
{
40-
return array(
41-
array(1),
42-
array(1.1),
43-
array('1.1'),
44-
);
34+
return Positive::IS_NEGATIVE_ERROR;
4535
}
4636

4737
/**
48-
* @dataProvider getInvalidValues
38+
* {@inheritdoc}
4939
*/
50-
public function testInvalidValues($value)
51-
{
52-
$constraint = new Positive(array(
53-
'message' => 'myMessage',
54-
));
55-
56-
$this->validator->validate($value, $constraint);
57-
58-
$this->buildViolation('myMessage')
59-
->setParameter('{{ value }}', $value)
60-
->setCode(Positive::IS_NEGATIVE_ERROR)
61-
->assertRaised();
62-
}
63-
64-
public function getInvalidValues()
40+
public function provideValidComparisons()
6541
{
6642
return array(
67-
array(-1),
68-
array(-1.1),
69-
array('-1.1'),
43+
array(1, 0),
44+
array(1.1, 0),
45+
array('1.1', 0),
7046
);
7147
}
7248

7349
/**
74-
* @dataProvider getZeroValues
50+
* {@inheritdoc}
7551
*/
76-
public function testZeroIsInvalid($value)
77-
{
78-
$constraint = new Positive(array(
79-
'message' => 'myMessage',
80-
));
81-
82-
$this->validator->validate($value, $constraint);
83-
84-
$this->buildViolation('myMessage')
85-
->setParameter('{{ value }}', $value)
86-
->setCode(Positive::IS_ZERO_ERROR)
87-
->assertRaised();
88-
}
89-
90-
public function getZeroValues()
52+
public function provideValidComparisonsToPropertyPath()
9153
{
9254
return array(
93-
array(0),
94-
array(0.0),
95-
array(.0),
55+
array(4),
9656
);
9757
}
9858

9959
/**
100-
* @dataProvider getWrongTypeValues
101-
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
60+
* {@inheritdoc}
10261
*/
103-
public function testItShouldThrowExceptionForWrongType($value)
62+
public function provideInvalidComparisons()
10463
{
105-
$this->validator->validate($value, new Positive());
64+
return array(
65+
array(-1, '-1', 0, '0', 'integer'),
66+
array(-1.1, '-1.1', 0, '0', 'integer'),
67+
array('-1.1', '"-1.1"', '0', '"0"', 'string'),
68+
);
10669
}
10770

10871
public function getWrongTypeValues()
@@ -115,9 +78,18 @@ public function getWrongTypeValues()
11578
);
11679
}
11780

81+
/**
82+
* @dataProvider getWrongTypeValues
83+
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
84+
*/
85+
public function testItShouldThrowExceptionForWrongType($value)
86+
{
87+
$this->validator->validate($value, new Positive(array('value' => 0)));
88+
}
89+
11890
public function testItShouldDoNothingForNullValue()
11991
{
120-
$this->validator->validate(null, new Positive());
92+
$this->validator->validate(null, new Positive(array('value' => 0)));
12193

12294
$this->assertNoViolation();
12395
}

0 commit comments

Comments
 (0)
0