Skip to content

Commit

Permalink
Merge pull request #1602 from katin-dev/master
Browse files Browse the repository at this point in the history
Fix generating example for Assert\Choice when choices are object not array
  • Loading branch information
GuilhemN authored Mar 31, 2020
2 parents b3c663d + 97530a6 commit ab014fe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public function updateProperty(\ReflectionProperty $reflectionProperty, Schema $
$property->setMinItems($annotation->min);
$property->setMaxItems($annotation->max);
} elseif ($annotation instanceof Assert\Choice) {
$property->setEnum($annotation->callback ? call_user_func(is_array($annotation->callback) ? $annotation->callback : [$reflectionProperty->class, $annotation->callback]) : $annotation->choices);
$values = $annotation->callback ? call_user_func(is_array($annotation->callback) ? $annotation->callback : [$reflectionProperty->class, $annotation->callback]) : $annotation->choices;
$property->setEnum(array_values($values));
} elseif ($annotation instanceof Assert\Expression) {
$this->appendPattern($property, $annotation->message);
} elseif ($annotation instanceof Assert\Range) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,31 @@ public function testUpdatePropertyFix1283()
// expect required to be numeric array with sequential keys (not [0 => ..., 2 => ...])
$this->assertEquals($schema->getRequired(), ['property1', 'property2']);
}

public function testAssertChoiceResultsInNumericArray()
{
define('TEST_ASSERT_CHOICE_STATUSES', [
1 => 'active',
2 => 'blocked',
]);

$entity = new class() {
/**
* @Assert\Length(min = 1)
* @Assert\Choice(choices=TEST_ASSERT_CHOICE_STATUSES)
*/
private $property1;
};

$schema = new Schema();
$schema->getProperties()->set('property1', new Schema());

$symfonyConstraintAnnotationReader = new SymfonyConstraintAnnotationReader(new AnnotationReader());
$symfonyConstraintAnnotationReader->setSchema($schema);

$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property1'), $schema->getProperties()->get('property1'));

// expect enum to be numeric array with sequential keys (not [1 => "active", 2 => "active"])
$this->assertEquals($schema->getProperties()->get('property1')->getEnum(), ['active', 'blocked']);
}
}

0 comments on commit ab014fe

Please sign in to comment.