8000 minor #58359 [Validator] do not skip tests from data providers (xabbuh) · symfony/symfony@7e1efbe · GitHub
[go: up one dir, main page]

Skip to content

Commit 7e1efbe

Browse files
committed
minor #58359 [Validator] do not skip tests from data providers (xabbuh)
This PR was merged into the 7.2 branch. Discussion ---------- [Validator] do not skip tests from data providers | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | | License | MIT While this used to work in the past PHPUnit 11 will report and error when a data provider does not return a valid set of data to run tests for. Commits ------- dc3300f do not skip tests from data providers
2 parents 81da0f2 + dc3300f commit 7e1efbe

18 files changed

+363
-344
lines changed

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

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

1212
namespace Symfony\Component\Validator\Tests\Constraints;
1313

14-
use Symfony\Component\Intl\Util\IntlTestHelper;
1514
use Symfony\Component\Validator\Constraint;
16-
use Symfony\Component\Validator\Constraints\AbstractComparison;
1715
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1816
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
19-
use Symfony\Component\Validator\Tests\Constraints\Fixtures\TypedDummy;
2017

2118
class ComparisonTest_Class
2219
{
@@ -98,32 +95,6 @@ public function testThrowsConstraintExceptionIfBothValueAndPropertyPath()
9895
]);
9996
}
10097

101-
/**
102-
* @dataProvider provideAllValidComparisons
103-
*/
104-
public function testValidComparisonToValue($dirtyValue, $comparisonValue)
105-
{
106-
$constraint = $this->createConstraint(['value' => $comparisonValue]);
107-
108-
$this->validator->validate($dirtyValue, $constraint);
109-
110-
$this->assertNoViolation();
111-
}
112-
113-
public static function provideAllValidComparisons(): array
114-
{
115-
// The provider runs before setUp(), so we need to manually fix
116-
// the default timezone
117-
$timezone = date_default_timezone_get();
118-
date_default_timezone_set('UTC');
119-
120-
$comparisons = self::addPhp5Dot5Comparisons(static::provideValidComparisons());
121-
122-
date_default_timezone_set($timezone);
123-
124-
return $comparisons;
125-
}
126-
12798
/**
12899
* @dataProvider provideValidComparisonsToPropertyPath
129100
*/
@@ -169,144 +140,8 @@ abstract public static function provideValidComparisons(): array;
169140

170141
abstract public static function provideValidComparisonsToPropertyPath(): array;
171142

172-
/**
173-
* @dataProvider provideAllInvalidComparisons
174-
*/
175-
public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $comparedValue, $comparedValueString, $comparedValueType)
176-
{
177-
// Conversion of dates to string differs between ICU versions
178-
// Make sure we have the correct version loaded
179-
if ($dirtyValue instanceof \DateTimeInterface) {
180-
IntlTestHelper::requireIntl($this, '57.1');
181-
}
182-
183-
$constraint = $this->createConstraint(['value' => $comparedValue]);
184-
$constraint->message = 'Constraint Message';
185-
186-
$this->validator->validate($dirtyValue, $constraint);
187-
188-
$this->buildViolation('Constraint Message')
189-
->setParameter('{{ value }}', $dirtyValueAsString)
190-
->setParameter('{{ compared_value }}', $comparedValueString)
191-
->setParameter('{{ compared_value_type }}', $comparedValueType)
192-
->setCode($this->getErrorCode())
193-
->assertRaised();
194-
}
195-
196-
public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
197-
{
198-
[$dirtyValue, $dirtyValueAsString, $comparedValue, $comparedValueString, $comparedValueType] = current($this->provideAllInvalidComparisons());
199-
200-
$constraint = $this->createConstraint(['propertyPath' => 'value']);
201-
$constraint->message = 'Constraint Message';
202-
203-
$object = new ComparisonTest_Class($comparedValue);
204-
205-
$this->setObject($object);
206-
207-
$this->validator->validate($dirtyValue, $constraint);
208-
209-
$this->buildViolation('Constraint Message')
210-
->setParameter('{{ value }}', $dirtyValueAsString)
211-
->setParameter('{{ compared_value }}', $comparedValueString)
212-
->setParameter('{{ compared_value_path }}', 'value')
213-
->setParameter('{{ compared_value_type }}', $comparedValueType)
214-
->setCode($this->getErrorCode())
215-
->assertRaised();
216-
}
217-
218-
/**
219-
* @dataProvider throwsOnInvalidStringDatesProvider
220-
*/
221-
public function testThrowsOnInvalidStringDates(AbstractComparison $constraint, $expectedMessage, $value)
222-
{
223-
$this->expectException(ConstraintDefinitionException::class);
224-
$this->expectExceptionMessage($expectedMessage);
225-
226-
$this->validator->validate($value, $constraint);
227-
}
228-
229-
public static function throwsOnInvalidStringDatesProvider(): array
230-
{
231-
$constraint = static::createConstraint([
232-
'value' => 'foo',
233-
]);
234-
235-
return [
236-
[$constraint, \sprintf('The compared value "foo" could not be converted to a "DateTimeImmutable" instance in the "%s" constra 10000 int.', $constraint::class), new \DateTimeImmutable()],
237-
[$constraint, \sprintf('The compared value "foo" could not be converted to a "DateTime" instance in the "%s" constraint.', $constraint::class), new \DateTime()],
238-
];
239-
}
240-
241-
/**
242-
* @dataProvider provideComparisonsToNullValueAtPropertyPath
243-
*/
244-
public function testCompareWithNullValueAtPropertyAt($dirtyValue, $dirtyValueAsString, $isValid)
245-
{
246-
$constraint = $this->createConstraint(['propertyPath' => 'value']);
247-
$constraint->message = 'Constraint Message';
248-
249-
$object = new ComparisonTest_Class(null);
250-
$this->setObject($object);
251-
252-
$this->validator->validate($dirtyValue, $constraint);
253-
254-
if ($isValid) {
255-
$this->assertNoViolation();
256-
} else {
257-
$this->buildViolation('Constraint Message')
258-
->setParameter('{{ value }}', $dirtyValueAsString)
259-
->setParameter('{{ compared_value }}', 'null')
260-
->setParameter('{{ compared_value_type }}', 'null')
261-
->setParameter('{{ compared_value_path }}', 'value')
262-
->setCode($this->getErrorCode())
263-
->assertRaised();
264-
}
265-
}
266-
267-
/**
268-
* @dataProvider provideComparisonsToNullValueAtPropertyPath
269-
*/
270-
public function testCompareWithUninitializedPropertyAtPropertyPath($dirtyValue, $dirtyValueAsString, $isValid)
271-
{
272-
$this->setObject(new TypedDummy());
273-
274-
$this->validator->validate($dirtyValue, $this->createConstraint([
275-
'message' => 'Constraint Message',
276-
'propertyPath' => 'value',
277-
]));
278-
279-
if ($isValid) {
280-
$this->assertNoViolation();
281-
} else {
282-
$this->buildViolation('Constraint Message')
283-
->setParameter('{{ value }}', $dirtyValueAsString)
284-
->setParameter('{{ compared_value }}', 'null')
285-
->setParameter('{{ compared_value_type }}', 'null')
286-
->setParameter('{{ compared_value_path }}', 'value')
287-
->setCode($this->getErrorCode())
288-
->assertRaised();
289-
}
290-
}
291-
292-
public static function provideAllInvalidComparisons(): array
293-
{
294-
// The provider runs before setUp(), so we need to manually fix
295-
// the default timezone
296-
$timezone = date_default_timezone_get();
297-
date_default_timezone_set('UTC');
298-
299-
$comparisons = self::addPhp5Dot5Comparisons(static::provideInvalidComparisons());
300-
301-
date_default_timezone_set($timezone);
302-
303-
return $comparisons;
304-
}
305-
306143
abstract public static function provideInvalidComparisons(): array;
307144

308-
abstract public static function provideComparisonsToNullValueAtPropertyPath();
309-
310145
abstract protected static function createConstraint(?array $options = null): Constraint;
311146

312147
protected function getErrorCode(): ?string
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Validator\Tests\Constraints;
13+
14+
use Symfony\Component\Validator\Tests\Constraints\Fixtures\TypedDummy;
15+
16+
trait CompareWithNullValueAtPropertyAtTestTrait
17+
{
18+
public function testCompareWithNullValueAtPropertyAt()
19+
{
20+
$constraint = $this->createConstraint(['propertyPath' => 'value']);
21+
$constraint->message = 'Constraint Message';
22+
23+
$object = new ComparisonTest_Class(null);
24+
$this->setObject($object);
25+
26+
$this->validator->validate(5, $constraint);
27+
28+
$this->assertNoViolation();
29+
}
30+
31+
public function testCompareWithUninitializedPropertyAtPropertyPath()
32+
{
33+
$this->setObject(new TypedDummy());
34+
35+
$this->validator->validate(5, $this->createConstraint([
36+
'message' => 'Constraint Message',
37+
'propertyPath' => 'value',
38+
]));
39+
40+
$this->assertNoViolation();
41+
}
42+
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
*/
2222
class DivisibleByValidatorTest extends AbstractComparisonValidatorTestCase
2323
{
24+
use InvalidCompa 10000 risonToValueTestTrait;
25+
use ThrowsOnInvalidStringDatesTestTrait;
26+
use ValidComparisonToValueTrait;
27+
2428
protected function createValidator(): DivisibleByValidator
2529
{
2630
return new DivisibleByValidator();
@@ -102,9 +106,4 @@ public static function throwsOnNonNumericValuesProvider()
102106
[\ArrayIterator::class, new \ArrayIterator(), 12],
103107
];
104108
}
105-
106-
public static function provideComparisonsToNullValueAtPropertyPath()
107-
{
108-
self::markTestSkipped('DivisibleByValidator rejects null values.');
109-
}
110109
}

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

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\Constraints\EqualTo;
1616
use Symfony\Component\Validator\Constraints\EqualToValidator;
17+
use Symfony\Component\Validator\Tests\Constraints\Fixtures\TypedDummy;
1718
use Symfony\Component\Validator\Tests\IcuCompatibilityTrait;
1819

1920
/**
@@ -22,6 +23,9 @@
2223
class EqualToValidatorTest extends AbstractComparisonValidatorTestCase
2324
{
2425
use IcuCompatibilityTrait;
26+
use InvalidComparisonToValueTestTrait;
27+
use ThrowsOnInvalidStringDatesTestTrait;
28+
use ValidComparisonToValueTrait;
2529

2630
protected function createValidator(): EqualToValidator
2731
{
@@ -71,10 +75,40 @@ public static function provideInvalidComparisons(): array
7175
];
7276
}
7377

74-
public static function provideComparisonsToNullValueAtPropertyPath(): array
78+
public function testCompareWithNullValueAtPropertyAt()
7579
{
76-
return [
77-
[5, '5', false],
78-
];
80+
$constraint = $this->createConstraint(['propertyPath' => 'value']);
81+
$constraint->message = 'Constraint Message';
82+
83+
$object = new ComparisonTest_Class(null);
84+
$this->setObject($object);
85+
86+
$this->validator->validate(5, $constraint);
87+
88+
$this->buildViolation('Constraint Message')
89+
->setParameter('{{ value }}', '5')
90+
->setParameter('{{ compared_value }}', 'null')
91+
->setParameter('{{ compared_value_type }}', 'null')
92+
->setParameter('{{ compared_value_path }}', 'value')
93+
->setCode($this->getErrorCode())
94+
->assertRaised();
95+
}
96+
97+
public function testCompareWithUninitializedPropertyAtPropertyPath()
98+
{
99+
$this->setObject(new TypedDummy());
100+
101+
$this->validator->validate(5, $this->createConstraint([
102+
'message' => 'Constraint Message',
103+
'propertyPath' => 'value',
104+
]));
105+
106+
$this->buildViolation('Constraint Message')
107+
->setParameter('{{ value }}', '5')
108+
->setParameter('{{ compared_value }}', 'null')
109+
->setParameter('{{ compared_value_type }}', 'null')
110+
->setParameter('{{ compared_value_path }}', 'value')
111+
->setCode($this->getErrorCode())
112+
->assertRaised();
79113
}
80114
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
*/
2222
class GreaterThanOrEqualValidatorTest extends AbstractComparisonValidatorTestCase
2323
{
24+
use CompareWithNullValueAtPropertyAtTestTrait;
2425
use IcuCompatibilityTrait;
26+
use InvalidComparisonToValueTestTrait;
27+
use ThrowsOnInvalidStringDatesTestTrait;
28+
use ValidComparisonToValueTrait;
2529

2630
protected function createValidator(): GreaterThanOrEqualValidator
2731
{
@@ -73,11 +77,4 @@ public static function provideInvalidComparisons(): array
7377
['b', '"b"', 'c', '"c"', 'string'],
7478
];
7579
}
76-
77-
public static function provideComparisonsToNullValueAtPropertyPath(): array
78-
{
79-
return [
80-
[5, '5', true],
81-
];
82-
}
8380
}

0 commit comments

Comments
 (0)
0