Skip to content

Commit 34e6cc8

Browse files
Merge branch '5.4' into 6.0
* 5.4: [Lock] Release Locks from Internal Store on Postgres waitAndSave* [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 f95ed76 + b8218d2 commit 34e6cc8

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
@@ -467,6 +467,17 @@ public function __construct(ExecutionContextInterface $context)
467467
$this->context = $context;
468468
}
469469

470+
public function __destruct()
471+
{
472+
if ($this->expectedAtPath) {
473+
throw new ExpectationFailedException('Some expected validation calls for paths were not done.');
474+
}
475+
476+
if ($this->expectedValidate) {
477+
throw new ExpectationFailedException('Some expected validation calls for values were not done.');
478+
}
479+
}
480+
470481
public function atPath(string $path): static
471482
{
472483
throw new \BadMethodCallException();
@@ -483,7 +494,10 @@ public function doAtPath(string $path): static
483494
throw new ExpectationFailedException(sprintf('Validation for property path "%s" was not expected.', $path));
484495
}
485496

486-
Assert::assertSame($this->expectedAtPath[$this->atPathCalls], $path);
497+
$expectedPath = $this->expectedAtPath[$this->atPathCalls];
498+
unset($this->expectedAtPath[$this->atPathCalls]);
499+
500+
Assert::assertSame($expectedPath, $path);
487501

488502
return $this;
489503
}
@@ -505,6 +519,7 @@ public function doValidate(mixed $value, Constraint|array $constraints = null, s
505519
}
506520

507521
[$expectedValue, $expectedGroup, $expectedConstraints, $violation] = $this->expectedValidate[$this->validateCalls];
522+
unset($this->expectedValidate[$this->validateCalls]);
508523

509524
Assert::assertSame($expectedValue, $value);
510525
$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()
@@ -524,6 +526,21 @@ public function testCorrupted(Image $constraint)
524526
->assertRaised();
525527
}
526528

529+
public function testInvalidMimeType()
530+
{
531+
$this->validator->validate($this->notAnImage, $constraint = new Image());
532+
533+
$this->assertSame('image/*', $constraint->mimeTypes);
534+
535+
$this->buildViolation('This file is not a valid image.')
536+
->setParameter('{{ file }}', sprintf('"%s"', $this->notAnImage))
537+
->setParameter('{{ type }}', '"text/plain"')
538+
->setParameter('{{ types }}', '"image/*"')
539+
->setParameter('{{ name }}', '"ccc.txt"')
540+
->setCode(Image::INVALID_MIME_TYPE_ERROR)
541+
->assertRaised();
542+
}
543+
527544
public function provideDetectCorruptedConstraints(): iterable
528545
{
529546
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)