Skip to content

Commit b8218d2

Browse files
Merge branch '4.4' into 5.4
* 4.4: [Serializer] Fix passing null to str_contains() [DependencyInjection] Don't reset env placeholders during compilation [HttpClient] Fix overriding default options with null [DependencyInjection] Clarify that using expressions in parameters is not allowed [GHA] Cancel running CI jobs when a PR is updated [Validator] Improve tests for the Image and File constraints [Validator][Tests] Fix AssertingContextualValidator not throwing on remaining expectations
2 parents f715649 + 71f9942 commit b8218d2

File tree

5 files changed

+108
-1
lines changed

5 files changed

+108
-1
lines changed

Test/ConstraintValidatorTestCase.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,17 @@ public function __construct(ExecutionContextInterface $context)
472472
$this->context = $context;
473473
}
474474

475+
public function __destruct()
476+
{
477+
if ($this->expectedAtPath) {
478+
throw new ExpectationFailedException('Some expected validation calls for paths were not done.');
479+
}
480+
481+
if ($this->expectedValidate) {
482+
throw new ExpectationFailedException('Some expected validation calls for values were not done.');
483+
}
484+
}
485+
475486
public function atPath(string $path)
476487
{
477488
}
@@ -487,7 +498,10 @@ public function doAtPath(string $path)
487498
throw new ExpectationFailedException(sprintf('Validation for property path "%s" was not expected.', $path));
488499
}
489500

490-
Assert::assertSame($this->expectedAtPath[$this->atPathCalls], $path);
501+
$expectedPath = $this->expectedAtPath[$this->atPathCalls];
502+
unset($this->expectedAtPath[$this->atPathCalls]);
503+
504+
Assert::assertSame($expectedPath, $path);
491505

492506
return $this;
493507
}
@@ -508,6 +522,7 @@ public function doValidate($value, $constraints = null, $groups = null)
508522
}
509523

510524
[$expectedValue, $expectedGroup, $expectedConstraints, $violation] = $this->expectedValidate[$this->validateCalls];
525+
unset($this->expectedValidate[$this->validateCalls]);
511526

512527
Assert::assertSame($expectedValue, $value);
513528
$expectedConstraints($constraints);

Tests/Constraints/FileValidatorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ public function testMaxSizeExceeded($bytesWritten, $limit, $sizeAsString, $limit
185185
public function provideMaxSizeNotExceededTests()
186186
{
187187
return [
188+
// 0 has no effect
189+
[100, 0],
190+
188191
// limit in bytes
189192
[1000, 1000],
190193
[1000000, 1000000],

Tests/Constraints/Fixtures/ccc.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foobar

Tests/Constraints/ImageValidatorTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ImageValidatorTest extends ConstraintValidatorTestCase
3434
protected $imagePortrait;
3535
protected $image4By3;
3636
protected $imageCorrupted;
37+
protected $notAnImage;
3738

3839
protected function createValidator()
3940
{
@@ -50,6 +51,7 @@ protected function setUp(): void
5051
$this->image4By3 = __DIR__.'/Fixtures/test_4by3.gif';
5152
$this->image16By9 = __DIR__.'/Fixtures/test_16by9.gif';
5253
$this->imageCorrupted = __DIR__.'/Fixtures/test_corrupted.gif';
54+
$this->notAnImage = __DIR__.'/Fixtures/ccc.txt';
5355
}
5456

5557
public function testNullIsValid()
@@ -560,6 +562,21 @@ public function testCorrupted(Image $constraint)
560562
->assertRaised();
561563
}
562564

565+
public function testInvalidMimeType()
566+
{
567+
$this->validator->validate($this->notAnImage, $constraint = new Image());
568+
569+
$this->assertSame('image/*', $constraint->mimeTypes);
570+
571+
$this->buildViolation('This file is not a valid image.')
572+
->setParameter('{{ file }}', sprintf('"%s"', $this->notAnImage))
573+
->setParameter('{{ type }}', '"text/plain"')
574+
->setParameter('{{ types }}', '"image/*"')
575+
->setParameter('{{ name }}', '"ccc.txt"')
576+
->setCode(Image::INVALID_MIME_TYPE_ERROR)
577+
->assertRaised();
578+
}
579+
563580
public function provideDetectCorruptedConstraints(): iterable
564581
{
565582
yield 'Doctrine style' => [new Image([
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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\Validator\Tests\Test;
13+
14+
use PHPUnit\Framework\ExpectationFailedException;
15+
use Symfony\Component\Validator\Constraint;
16+
use Symfony\Component\Validator\Constraints\DateTime;
17+
use Symfony\Component\Validator\Constraints\NotNull;
18+
use Symfony\Component\Validator\ConstraintValidator;
19+
use Symfony\Component\Validator\ConstraintValidatorInterface;
20+
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
21+
22+
class ConstraintValidatorTestCaseTest extends ConstraintValidatorTestCase
23+
{
24+
protected function createValidator(): ConstraintValidatorInterface
25+
{
26+
return new TestCustomValidator();
27+
}
28+
29+
public function testAssertingContextualValidatorRemainingExpectationsThrow()
30+
{
31+
$this->expectValidateValueAt(0, 'k1', 'ccc', [
32+
new NotNull(),
33+
]);
34+
$this->expectValidateValueAt(1, 'k2', 'ccc', [
35+
new DateTime(),
36+
]);
37+
38+
$this->validator->validate('ccc', $this->constraint);
39+
40+
$contextualValidator = $this->context->getValidator()->inContext($this->context);
41+
// Simulate __destruct to assert it throws
42+
try {
43+
$contextualValidator->__destruct();
44+
$this->fail();
45+
} catch (ExpectationFailedException $e) {
46+
}
47+
48+
// Actually fulfill expectations so real __destruct doesn't throw
49+
$contextualValidator
50+
->atPath('k2')
51+
->validate('ccc', [
52+
new DateTime(),
53+
]);
54+
}
55+
}
56+
57+
class TestCustomValidator extends ConstraintValidator
58+
{
59+
public function validate($value, Constraint $constraint)
60+
{
61+
$validator = $this->context
62+
->getValidator()
63+
->inContext($this->context);
64+
65+
$validator
66+
->atPath('k1')
67+
->validate($value, [
68+
new NotNull(),
69+
]);
70+
}
71+
}

0 commit comments

Comments
 (0)