From 75ae346236aec5826279ace808177dc3b2a23c08 Mon Sep 17 00:00:00 2001 From: Eldar Shahmaliyev Date: Fri, 10 Oct 2025 10:42:08 +0400 Subject: [PATCH 1/3] Test(BooleanType): Add unit tests for boolean validator --- .../Syntax/Constraints/BooleanTypeTest.php | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 tests/Unit/Service/Syntax/Constraints/BooleanTypeTest.php diff --git a/tests/Unit/Service/Syntax/Constraints/BooleanTypeTest.php b/tests/Unit/Service/Syntax/Constraints/BooleanTypeTest.php new file mode 100644 index 0000000..02cf605 --- /dev/null +++ b/tests/Unit/Service/Syntax/Constraints/BooleanTypeTest.php @@ -0,0 +1,104 @@ +validator->validate($value, $constraint); + + $this->assertNoViolation(); + } + + public static function validBooleanProvider(): array + { + return [ + [true], + [false], + ]; + } + + #[DataProvider('invalidBooleanProvider')] + public function test_invalid_boolean_values(mixed $invalidValue): void + { + $this->expectException(UnexpectedTypeException::class); + + $constraint = new BooleanType(); + $this->validator->validate($invalidValue, $constraint); + } + + #[Pure] + public static function invalidBooleanProvider(): array + { + return [ + ['string'], + [1.4], + [1], + [new stdClass()], + [[]], + [null], + ]; + } + + public function test_violation_when_const_not_matching_value(): void + { + $constraint = new BooleanType([ + 'type' => 'boolean', + 'const' => false, + ]); + + $this->validator->validate(true, $constraint); + + $this->buildViolation($constraint->message) + ->setParameter('{{ actual }}', 'true') + ->setParameter('{{ must }}', 'false') + ->assertRaised(); + } + + public function test_valid_when_value_matches_const(): void + { + $constraint = new BooleanType([ + 'type' => 'boolean', + 'const' => false, + ]); + + $this->validator->validate(false, $constraint); + + $this->assertNoViolation(); + } + + public function test_false_value_is_valid(): void + { + $constraint = new BooleanType(); + + $this->validator->validate(false, $constraint); + + $this->assertNoViolation(); + } + + public function test_true_value_is_valid(): void + { + $constraint = new BooleanType(); + + $this->validator->validate(true, $constraint); + + $this->assertNoViolation(); + } +} From a9605c24fc6ad5b6ad94c9dd7f41d7d576bcc4c6 Mon Sep 17 00:00:00 2001 From: Eldar Shahmaliyev Date: Fri, 10 Oct 2025 10:42:23 +0400 Subject: [PATCH 2/3] Feat(BooleanType): Implement BooleanType validator and constraint --- .../Constraints/BooleanType/BooleanType.php | 15 +++++++++ .../BooleanType/BooleanTypeValidator.php | 32 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/Service/Syntax/Constraints/BooleanType/BooleanType.php create mode 100644 src/Service/Syntax/Constraints/BooleanType/BooleanTypeValidator.php diff --git a/src/Service/Syntax/Constraints/BooleanType/BooleanType.php b/src/Service/Syntax/Constraints/BooleanType/BooleanType.php new file mode 100644 index 0000000..2a930aa --- /dev/null +++ b/src/Service/Syntax/Constraints/BooleanType/BooleanType.php @@ -0,0 +1,15 @@ +schema())); + + if (! is_null($const = $schema->const()) && $value !== $const) { + $this->context->buildViolation($constraint->message) + ->setParameter('{{ actual }}', $this->formatValue($value)) + ->setParameter('{{ must }}', $this->formatValue($const)) + ->addViolation(); + } + } +} From 4a96ddf606956362e8d70cc7dda8b6a97edfd063 Mon Sep 17 00:00:00 2001 From: Eldar Shahmaliyev Date: Fri, 10 Oct 2025 10:46:52 +0400 Subject: [PATCH 3/3] Delete Pure attribute --- tests/Unit/Service/Syntax/Constraints/BooleanTypeTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Unit/Service/Syntax/Constraints/BooleanTypeTest.php b/tests/Unit/Service/Syntax/Constraints/BooleanTypeTest.php index 02cf605..15695f4 100644 --- a/tests/Unit/Service/Syntax/Constraints/BooleanTypeTest.php +++ b/tests/Unit/Service/Syntax/Constraints/BooleanTypeTest.php @@ -4,7 +4,6 @@ use Blugen\Service\Syntax\Constraints\BooleanType\BooleanType; use Blugen\Service\Syntax\Constraints\BooleanType\BooleanTypeValidator; -use JetBrains\PhpStorm\Pure; use PHPUnit\Framework\Attributes\DataProvider; use stdClass; use Symfony\Component\Validator\Exception\UnexpectedTypeException; @@ -44,7 +43,6 @@ public function test_invalid_boolean_values(mixed $invalidValue): void $this->validator->validate($invalidValue, $constraint); } - #[Pure] public static function invalidBooleanProvider(): array { return [