From df3f6bd1ae8896b7053c2414f5752992ee1d719e Mon Sep 17 00:00:00 2001 From: TavoNiievez Date: Tue, 26 Mar 2024 14:39:44 -0500 Subject: [PATCH 1/2] Added Symfony validator assertions --- composer.json | 1 + src/Codeception/Module/Symfony.php | 2 + .../Symfony/ValidatorAssertionsTrait.php | 106 ++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 src/Codeception/Module/Symfony/ValidatorAssertionsTrait.php diff --git a/composer.json b/composer.json index 1421734d..93757670 100644 --- a/composer.json +++ b/composer.json @@ -49,6 +49,7 @@ "symfony/security-csrf": "^5.4 | ^6.4 | ^7.0", "symfony/security-http": "^5.4 | ^6.4 | ^7.0", "symfony/twig-bundle": "^5.4 | ^6.4 | ^7.0", + "symfony/validator": "^5.4 | ^6.4 | ^7.0", "symfony/var-exporter": "^5.4 | ^6.4 | ^7.0", "vlucas/phpdotenv": "^4.2 | ^5.4" }, diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index 13a9f6df..2e6f4d74 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -24,6 +24,7 @@ use Codeception\Module\Symfony\SessionAssertionsTrait; use Codeception\Module\Symfony\TimeAssertionsTrait; use Codeception\Module\Symfony\TwigAssertionsTrait; +use Codeception\Module\Symfony\ValidatorAssertionsTrait; use Codeception\TestInterface; use Doctrine\ORM\EntityManagerInterface; use Exception; @@ -145,6 +146,7 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule use SessionAssertionsTrait; use TimeAssertionsTrait; use TwigAssertionsTrait; + use ValidatorAssertionsTrait; public Kernel $kernel; diff --git a/src/Codeception/Module/Symfony/ValidatorAssertionsTrait.php b/src/Codeception/Module/Symfony/ValidatorAssertionsTrait.php new file mode 100644 index 00000000..4ca47863 --- /dev/null +++ b/src/Codeception/Module/Symfony/ValidatorAssertionsTrait.php @@ -0,0 +1,106 @@ +dontSeeViolatedConstraint($subject); + * $I->dontSeeViolatedConstraint($subject, 'propertyName'); + * $I->dontSeeViolatedConstraint($subject, 'propertyName', 'Symfony\Validator\ConstraintClass'); + * ``` + */ + public function dontSeeViolatedConstraint(mixed $subject, ?string $propertyPath = null, ?string $constraint = null): void + { + $violations = $this->getViolationsForSubject($subject, $propertyPath, $constraint); + $this->assertCount(0, $violations, 'Constraint violations found.'); + } + + /** + * Asserts that the given subject passes validation. + * This assertion does not concern the exact number of violations. + * + * ```php + * seeViolatedConstraint($subject); + * $I->seeViolatedConstraint($subject, 'propertyName'); + * $I->seeViolatedConstraint($subject, 'propertyName', 'Symfony\Validator\ConstraintClass'); + * ``` + */ + public function seeViolatedConstraint(mixed $subject, ?string $propertyPath = null, ?string $constraint = null): void + { + $violations = $this->getViolationsForSubject($subject, $propertyPath, $constraint); + $this->assertNotCount(0, $violations, 'No constraint violations found.'); + } + + /** + * Asserts the exact number of violations for the given subject. + * + * ```php + * seeViolatedConstraintCount(3, $subject); + * $I->seeViolatedConstraintCount(2, $subject, 'propertyName'); + * ``` + */ + public function seeViolatedConstraintCount(int $expected, mixed $subject, ?string $propertyPath = null, ?string $constraint = null): void + { + $violations = $this->getViolationsForSubject($subject, $propertyPath, $constraint); + $this->assertCount($expected, $violations); + } + + /** + * Asserts that a specific violation message is present in the subject's violations. + * + * ```php + * seeViolatedConstraintMessageContains('Violation message', $subject, 'propertyName'); + * ``` + */ + public function seeViolatedConstraintMessageContains(string $expected, mixed $subject, string $propertyPath): void + { + $violations = $this->getViolationsForSubject($subject, $propertyPath); + $containsExpected = false; + foreach ($violations as $violation) { + if ($violation->getPropertyPath() === $propertyPath && str_contains($violation->getMessage(), $expected)) { + $containsExpected = true; + break; + } + } + + $this->assertTrue($containsExpected, 'The violation messages do not contain: ' . $expected); + } + + /** @return ConstraintViolationInterface[] */ + protected function getViolationsForSubject(mixed $subject, ?string $propertyPath = null, ?string $constraint = null): array + { + $validator = $this->getValidatorService(); + $violations = $propertyPath ? $validator->validateProperty($subject, $propertyPath) : $validator->validate($subject); + + $violations = iterator_to_array($violations); + + if ($constraint !== null) { + return array_filter( + $violations, + static fn($violation): bool => $violation->getConstraint()::class === $constraint && + ($propertyPath === null || $violation->getPropertyPath() === $propertyPath) + ); + } + + return $violations; + } + + protected function getValidatorService(): ValidatorInterface + { + return $this->grabService(ValidatorInterface::class); + } +} From adb68afe8501af6940bfbb89810fb570358d480a Mon Sep 17 00:00:00 2001 From: TavoNiievez Date: Thu, 28 Mar 2024 12:46:19 -0500 Subject: [PATCH 2/2] Rename some validator assertions --- .../Module/Symfony/ValidatorAssertionsTrait.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Codeception/Module/Symfony/ValidatorAssertionsTrait.php b/src/Codeception/Module/Symfony/ValidatorAssertionsTrait.php index 4ca47863..ca82e196 100644 --- a/src/Codeception/Module/Symfony/ValidatorAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/ValidatorAssertionsTrait.php @@ -48,25 +48,25 @@ public function seeViolatedConstraint(mixed $subject, ?string $propertyPath = nu * * ```php * seeViolatedConstraintCount(3, $subject); - * $I->seeViolatedConstraintCount(2, $subject, 'propertyName'); + * $I->seeViolatedConstraintsCount(3, $subject); + * $I->seeViolatedConstraintsCount(2, $subject, 'propertyName'); * ``` */ - public function seeViolatedConstraintCount(int $expected, mixed $subject, ?string $propertyPath = null, ?string $constraint = null): void + public function seeViolatedConstraintsCount(int $expected, mixed $subject, ?string $propertyPath = null, ?string $constraint = null): void { $violations = $this->getViolationsForSubject($subject, $propertyPath, $constraint); $this->assertCount($expected, $violations); } /** - * Asserts that a specific violation message is present in the subject's violations. + * Asserts that a constraint violation message or a part of it is present in the subject's violations. * * ```php * seeViolatedConstraintMessageContains('Violation message', $subject, 'propertyName'); + * $I->seeViolatedConstraintMessage('too short', $user, 'address'); * ``` */ - public function seeViolatedConstraintMessageContains(string $expected, mixed $subject, string $propertyPath): void + public function seeViolatedConstraintMessage(string $expected, mixed $subject, string $propertyPath): void { $violations = $this->getViolationsForSubject($subject, $propertyPath); $containsExpected = false; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy