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
10 changes: 5 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ jobs:
include:
- php-version: '8.2'
symfony-version: '7.0.*'
- php-version: '8.3'
symfony-version: '7.1.*'
- php-version: '8.4'
symfony-version: '7.3.*'

steps:
- name: "Checkout"
Expand Down Expand Up @@ -55,7 +55,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
coverage: xdebug
php-version: '8.3'
php-version: '8.4'

- name: "Install dependencies with composer"
run: composer update --no-interaction --no-progress
Expand All @@ -81,7 +81,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
coverage: none
php-version: '8.3'
php-version: '8.4'

- name: "Install dependencies with composer"
run: composer update --no-interaction --no-progress
Expand All @@ -101,7 +101,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
coverage: none
php-version: '8.3'
php-version: '8.4'

- name: "Install dependencies with composer"
run: composer update --no-interaction --no-progress
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"phpunit/phpunit": "^9.6",
"symfony/form": "^7.0",
"symfony/http-kernel": "^7.0",
"symfony/phpunit-bridge": "^7.3",
"symfony/translation": "^7.0",
"symfony/twig-bundle": "^7.0",
"symfony/validator": "^7.0",
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
bootstrap="vendor/autoload.php">
<php>
<server name="KERNEL_CLASS" value="Yokai\EnumBundle\Tests\Integration\App\Kernel"/>
<server name="SYMFONY_DEPRECATIONS_HELPER" value="max[direct]=0"/>
</php>

<testsuites>
Expand Down
22 changes: 4 additions & 18 deletions src/Form/Type/EnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,16 @@ function (string $name): bool {
function (Options $options): array {
/** @var string $name */
$name = $options['enum'];
$choices = $this->enumRegistry->get($name)->getChoices();

if ($options['enum_choice_value'] === null) {
foreach ($choices as $value) {
if (!\is_scalar($value)) {
@\trigger_error(
'Not configuring the "enum_choice_value" option is deprecated.' .
' It will default to "true" in 5.0.',
\E_USER_DEPRECATED
);
break;
}
}
}

return $choices;
return $this->enumRegistry->get($name)->getChoices();
}
)
->setAllowedTypes('enum_choice_value', ['bool', 'null'])
->setDefault('enum_choice_value', null)
->setAllowedTypes('enum_choice_value', ['bool'])
->setDefault('enum_choice_value', true)
->setDefault(
'choice_value',
static function (Options $options) {
if ($options['enum_choice_value'] !== true) {
if (!$options['enum_choice_value']) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/MyCLabsEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class MyCLabsEnum extends Enum
{
public function __construct(string $enum, string $name = null)
public function __construct(string $enum, string|null $name = null)
{
if (!\is_a($enum, ActualMyCLabsEnum::class, true)) {
throw LogicException::invalidMyClabsEnumClass($enum);
Expand Down
2 changes: 1 addition & 1 deletion src/MyCLabsTranslatedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
TranslatorInterface $translator,
string $transPattern,
string $transDomain = 'messages',
string $name = null
string|null $name = null
) {
if (!\is_a($enum, ActualMyCLabsEnum::class, true)) {
throw LogicException::invalidMyClabsEnumClass($enum);
Expand Down
2 changes: 1 addition & 1 deletion src/NativeEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class NativeEnum extends Enum
{
public function __construct(string $enum, string $name = null)
public function __construct(string $enum, string|null $name = null)
{
if (!\is_a($enum, UnitEnum::class, true)) {
throw LogicException::invalidUnitEnum($enum);
Expand Down
2 changes: 1 addition & 1 deletion src/NativeTranslatedEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
TranslatorInterface $translator,
string $transPattern,
string $transDomain = 'messages',
string $name = null
string|null $name = null
) {
if (!\is_a($enum, UnitEnum::class, true)) {
throw LogicException::invalidUnitEnum($enum);
Expand Down
27 changes: 18 additions & 9 deletions src/Validator/Constraints/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,33 @@ public function __construct(
array $options = [],
string|null $enum = null,
callable|null|string $callback = null,
bool $multiple = null,
bool $strict = null,
int $min = null,
int $max = null,
string $message = null,
string $multipleMessage = null,
string $minMessage = null,
string $maxMessage = null,
bool|null $multiple = null,
bool|null $strict = null,
int|null $min = null,
int|null $max = null,
string|null $message = null,
string|null $multipleMessage = null,
string|null $minMessage = null,
string|null $maxMessage = null,
array|null $groups = null,
mixed $payload = null,
) {
if (\is_array($options) && $options !== []) {
\trigger_deprecation(
'symfony/validator',
'7.3',
'Passing an array of options to configure the "%s" constraint is deprecated, use named arguments instead.',
static::class,
);
}

if (\is_string($enum)) {
$this->enum = $enum;
}

// Since Symfony 5.3, first argument of Choice is $options
parent::__construct(
$options,
[],
null,
$callback,
$multiple,
Expand Down
6 changes: 3 additions & 3 deletions tests/Integration/tests/PullRequestFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ public function valid(): Generator
{
foreach ($this->classes() as [$class]) {
yield [
['status' => 0, 'labels' => ['bugfix', '1.x']],
['status' => 'opened', 'labels' => ['bugfix', '1.x']],
self::pullRequest($class),
self::pullRequest($class, 'opened', ['bugfix', '1.x']),
];

yield [
['status' => 2, 'labels' => ['bugfix', '2.x']],
['status' => 'closed', 'labels' => ['bugfix', '2.x']],
self::pullRequest($class, 'opened', ['bugfix', '1.x']),
self::pullRequest($class, 'closed', ['bugfix', '2.x']),
];
Expand Down Expand Up @@ -171,7 +171,7 @@ public function invalid(): Generator

private static function pullRequest(
string $class,
string $status = null,
string|null $status = null,
array $labels = [],
): MyCLabsPullRequest|NativeEnumPullRequest {
return (match ($class) {
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Form/Extension/EnumTypeGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ protected function setUp(): void
$this->enumRegistry->add(new StateEnum());

$metadata = new ClassMetadata(self::TEST_CLASS);
$metadata->addPropertyConstraint(self::TEST_PROPERTY_NONE, new Choice(['choices' => ['new', 'validated']]));
$metadata->addPropertyConstraint(self::TEST_PROPERTY_DIRECT, new Enum(['enum' => StateEnum::class]));
$metadata->addPropertyConstraint(self::TEST_PROPERTY_NONE, new Choice(choices: ['new', 'validated']));
$metadata->addPropertyConstraint(self::TEST_PROPERTY_DIRECT, new Enum(enum: StateEnum::class));
if (\class_exists(Compound::class)) {
$metadata->addPropertyConstraint(
self::TEST_PROPERTY_COMPOUND,
new class() extends Compound {
protected function getConstraints(array $options): array
{
return [new Enum(['enum' => StateEnum::class])];
return [new Enum(enum: StateEnum::class)];
}
}
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Form/TestExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TestExtension extends AbstractExtension

private MetadataFactoryInterface|null $metadataFactory;

public function __construct(EnumRegistry $enumRegistry, MetadataFactoryInterface $metadataFactory = null)
public function __construct(EnumRegistry $enumRegistry, MetadataFactoryInterface|null $metadataFactory = null)
{
$this->enumRegistry = $enumRegistry;
$this->metadataFactory = $metadataFactory;
Expand Down
20 changes: 10 additions & 10 deletions tests/Unit/Form/Type/EnumTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,65 +61,65 @@ public static function submit(): \Generator
{
yield [
StateEnum::class,
[],
['enum_choice_value' => false],
'new',
'new',
];
yield [
StateEnum::class,
['enum_choice_value' => true],
[],
'new',
'new',
];

yield [
ActionEnum::class,
[],
['enum_choice_value' => false],
1,
Action::EDIT(),
];
yield [
ActionEnum::class,
['enum_choice_value' => true],
[],
'edit',
Action::EDIT(),
];

yield [
Picture::class,
[],
['enum_choice_value' => false],
0,
Picture::Landscape,
];
yield [
Picture::class,
['enum_choice_value' => true],
[],
'Landscape',
Picture::Landscape,
];

yield [
HTTPMethod::class,
[],
['enum_choice_value' => false],
0,
HTTPMethod::GET,
];
yield [
HTTPMethod::class,
['enum_choice_value' => true],
[],
'get',
HTTPMethod::GET,
];

yield [
HTTPStatus::class,
['enum_choice_value' => true],
[],
200,
HTTPStatus::OK,
];
yield [
HTTPStatus::class,
[],
['enum_choice_value' => false],
0,
HTTPStatus::OK,
];
Expand Down
8 changes: 6 additions & 2 deletions tests/Unit/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ public function getLocale(): string
return 'fr';
}

public function trans(string $id, array $parameters = [], string $domain = null, string $locale = null): string
{
public function trans(
string $id,
array $parameters = [],
string|null $domain = null,
string|null $locale = null,
): string {
if ($domain !== $this->domain) {
return $id;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Validator/Constraints/EnumValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function testValidSingleEnum(): void

public function testInvalidSingleEnum(): void
{
$constraint = new Enum(['enum' => 'type', 'message' => 'myMessage']);
$constraint = new Enum(enum: 'type', message: 'myMessage');

$this->validator->validate('foo', $constraint);

Expand All @@ -78,7 +78,7 @@ public function testInvalidSingleEnum(): void

public function testValidMultipleEnum(): void
{
$constraint = new Enum(['enum' => 'type', 'multiple' => true]);
$constraint = new Enum(enum: 'type', multiple: true);

$this->validator->validate(['customer', 'prospect'], $constraint);

Expand All @@ -87,7 +87,7 @@ public function testValidMultipleEnum(): void

public function testInvalidMultipleEnum(): void
{
$constraint = new Enum(['enum' => 'type', 'multiple' => true, 'multipleMessage' => 'myMessage']);
$constraint = new Enum(enum: 'type', multiple: true, multipleMessage: 'myMessage');

$this->validator->validate(['customer', 'foo'], $constraint);

Expand Down