Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Service/Syntax/Constraints/BooleanType/BooleanType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Blugen\Service\Syntax\Constraints\BooleanType;

use Attribute;
use Blugen\Service\Syntax\Constraints\LexConstraint;
use Symfony\Component\Validator\Constraint;

#[Attribute(Attribute::TARGET_PROPERTY)]
class BooleanType extends Constraint implements LexConstraint
{
use \Blugen\Service\Syntax\Constraints\Constraint;

public string $message = 'Expected boolean value {{ must }}, but got {{ actual }}';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Blugen\Service\Syntax\Constraints\BooleanType;

use Blugen\Service\Lexicon\V1\Schema;
use Blugen\Service\Lexicon\V1\TypeSpecificSchema\Field\BooleanSchema;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

class BooleanTypeValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint)
{
if (! $constraint instanceof BooleanType) {
throw new UnexpectedTypeException($constraint, BooleanType::class);
}

if (! is_bool($value)) {
throw new UnexpectedTypeException($value, 'boolean');
}

$schema = new BooleanSchema(new Schema($constraint->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();
}
}
}
102 changes: 102 additions & 0 deletions tests/Unit/Service/Syntax/Constraints/BooleanTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Blugen\Tests\Unit\Service\Syntax\Constraints;

use Blugen\Service\Syntax\Constraints\BooleanType\BooleanType;
use Blugen\Service\Syntax\Constraints\BooleanType\BooleanTypeValidator;
use PHPUnit\Framework\Attributes\DataProvider;
use stdClass;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

class BooleanTypeTest extends ConstraintValidatorTestCase
{
protected function createValidator(): BooleanTypeValidator
{
return new BooleanTypeValidator();
}

#[DataProvider('validBooleanProvider')]
public function test_valid_boolean_values(bool $value): void
{
$constraint = new BooleanType();

$this->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);
}

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();
}
}