-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Add PHP enum support in BaseHtml and RangeValidator
#20794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
WarLikeLaux
wants to merge
5
commits into
yiisoft:master
Choose a base branch
from
WarLikeLaux:extend-enum-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+361
−1
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9df5211
Fix #20318: Add PHP enum support in `BaseHtml` and `RangeValidator`
WarLikeLaux 1b75547
Fix PHPStan 7.4 baseline and improve enum test assertions
WarLikeLaux 2b151cd
Validate `enum` and `target` properties in `RangeValidator::init()`
WarLikeLaux 5ed1e23
Merge branch 'master' into extend-enum-support
WarLikeLaux 8356d80
Remove unreachable `enum_exists` from PHPStan 7.4 baseline
WarLikeLaux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @link https://www.yiiframework.com/ | ||
| * @copyright Copyright (c) 2008 Yii Software LLC | ||
| * @license https://www.yiiframework.com/license/ | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace yiiunit\framework\helpers; | ||
|
|
||
| use yii\base\DynamicModel; | ||
| use yii\helpers\Html; | ||
| use yiiunit\framework\validators\stubs\IntStatus; | ||
| use yiiunit\framework\validators\stubs\StringStatus; | ||
| use yiiunit\framework\validators\stubs\Suit; | ||
| use yiiunit\TestCase; | ||
|
|
||
| /** | ||
| * @group helpers | ||
| * @requires PHP >= 8.1 | ||
| */ | ||
| class HtmlEnumTest extends TestCase | ||
| { | ||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
| require_once __DIR__ . '/../validators/stubs/EnumStubs.php'; | ||
| $this->destroyApplication(); | ||
| } | ||
|
|
||
| public function testGetAttributeValueWithStringBackedEnum(): void | ||
| { | ||
| $model = new DynamicModel(['status' => StringStatus::Active]); | ||
| $this->assertSame('active', Html::getAttributeValue($model, 'status')); | ||
| } | ||
|
|
||
| public function testGetAttributeValueWithIntBackedEnum(): void | ||
| { | ||
| $model = new DynamicModel(['status' => IntStatus::On]); | ||
| $this->assertSame(1, Html::getAttributeValue($model, 'status')); | ||
| } | ||
|
|
||
| public function testGetAttributeValueWithUnitEnum(): void | ||
| { | ||
| $model = new DynamicModel(['suit' => Suit::Hearts]); | ||
| $this->assertSame('Hearts', Html::getAttributeValue($model, 'suit')); | ||
| } | ||
|
|
||
| public function testGetAttributeValueWithArrayOfEnums(): void | ||
| { | ||
| $model = new DynamicModel(['statuses' => [StringStatus::Active, StringStatus::Inactive]]); | ||
| $this->assertSame(['active', 'inactive'], Html::getAttributeValue($model, 'statuses')); | ||
| } | ||
|
|
||
| public function testGetAttributeValueWithArrayOfUnitEnums(): void | ||
| { | ||
| $model = new DynamicModel(['suits' => [Suit::Hearts, Suit::Spades]]); | ||
| $this->assertSame(['Hearts', 'Spades'], Html::getAttributeValue($model, 'suits')); | ||
| } | ||
|
|
||
| public function testGetAttributeValueWithMixedArray(): void | ||
| { | ||
| $model = new DynamicModel(['items' => [StringStatus::Active, 'plain', 42]]); | ||
| $result = Html::getAttributeValue($model, 'items'); | ||
| $this->assertSame(['active', 'plain', 42], $result); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @link https://www.yiiframework.com/ | ||
| * @copyright Copyright (c) 2008 Yii Software LLC | ||
| * @license https://www.yiiframework.com/license/ | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace yiiunit\framework\validators; | ||
|
|
||
| use yii\validators\RangeValidator; | ||
| use yiiunit\data\validators\models\FakedValidationModel; | ||
| use yiiunit\framework\validators\stubs\IntStatus; | ||
| use yiiunit\framework\validators\stubs\StringStatus; | ||
| use yiiunit\framework\validators\stubs\Suit; | ||
| use yiiunit\TestCase; | ||
|
|
||
| /** | ||
| * @group validators | ||
| * @requires PHP >= 8.1 | ||
| */ | ||
| class RangeValidatorEnumTest extends TestCase | ||
| { | ||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
| require_once __DIR__ . '/stubs/EnumStubs.php'; | ||
| $this->destroyApplication(); | ||
| } | ||
|
|
||
| public function testEnumWithStringBackedValues(): void | ||
| { | ||
| $val = new RangeValidator(['enum' => StringStatus::class]); | ||
| $this->assertTrue($val->validate('active')); | ||
| $this->assertTrue($val->validate('inactive')); | ||
| $this->assertFalse($val->validate('pending')); | ||
| $this->assertFalse($val->validate('')); | ||
| } | ||
|
|
||
| public function testEnumWithIntBackedValues(): void | ||
| { | ||
| $val = new RangeValidator(['enum' => IntStatus::class]); | ||
| $this->assertTrue($val->validate(1)); | ||
| $this->assertTrue($val->validate(0)); | ||
| $this->assertFalse($val->validate(2)); | ||
| } | ||
|
|
||
| public function testEnumWithNameTarget(): void | ||
| { | ||
| $val = new RangeValidator(['enum' => StringStatus::class, 'target' => 'name']); | ||
| $this->assertTrue($val->validate('Active')); | ||
| $this->assertTrue($val->validate('Inactive')); | ||
| $this->assertFalse($val->validate('active')); | ||
| } | ||
|
|
||
| public function testEnumUnitEnumWithNameTarget(): void | ||
| { | ||
| $val = new RangeValidator(['enum' => Suit::class, 'target' => 'name']); | ||
| $this->assertTrue($val->validate('Hearts')); | ||
| $this->assertTrue($val->validate('Spades')); | ||
| $this->assertFalse($val->validate('hearts')); | ||
| $this->assertFalse($val->validate('')); | ||
| } | ||
|
|
||
| public function testEnumUnitEnumWithValueTargetThrows(): void | ||
| { | ||
| $this->expectException('yii\base\InvalidConfigException'); | ||
| $this->expectExceptionMessage('The "value" target requires a backed enum'); | ||
WarLikeLaux marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| new RangeValidator(['enum' => Suit::class]); | ||
| } | ||
|
|
||
| public function testEnumInvalidClassThrows(): void | ||
| { | ||
| $this->expectException('yii\base\InvalidConfigException'); | ||
| $this->expectExceptionMessage('The "enum" property must be a valid enum class'); | ||
| new RangeValidator(['enum' => \stdClass::class]); | ||
| } | ||
|
|
||
| public function testEnumWithNotProperty(): void | ||
| { | ||
| $val = new RangeValidator(['enum' => StringStatus::class, 'not' => true]); | ||
| $this->assertFalse($val->validate('active')); | ||
| $this->assertTrue($val->validate('pending')); | ||
| } | ||
|
|
||
| public function testEnumWithStrictProperty(): void | ||
| { | ||
| $val = new RangeValidator(['enum' => IntStatus::class, 'strict' => true]); | ||
| $this->assertTrue($val->validate(1)); | ||
| $this->assertFalse($val->validate('1')); | ||
| } | ||
|
|
||
| public function testEnumValidateAttribute(): void | ||
| { | ||
| $val = new RangeValidator(['enum' => StringStatus::class]); | ||
| $m = FakedValidationModel::createWithAttributes(['attr_status' => 'active']); | ||
| $val->validateAttribute($m, 'attr_status'); | ||
| $this->assertFalse($m->hasErrors('attr_status')); | ||
|
|
||
| $m = FakedValidationModel::createWithAttributes(['attr_status' => 'bogus']); | ||
| $val->validateAttribute($m, 'attr_status'); | ||
| $this->assertTrue($m->hasErrors('attr_status')); | ||
| $this->assertSame(['attr_status is invalid.'], $m->getErrors('attr_status')); | ||
| } | ||
|
|
||
| public function testGetClientOptionsWithEnumCasesInRange(): void | ||
| { | ||
| $this->mockWebApplication(); | ||
| $val = new RangeValidator(['range' => StringStatus::cases()]); | ||
| $m = FakedValidationModel::createWithAttributes(['attr_status' => 'active']); | ||
| $options = $val->getClientOptions($m, 'attr_status'); | ||
| $this->assertSame(['active', 'inactive'], $options['range']); | ||
| } | ||
|
|
||
| public function testGetClientOptionsWithIntEnumCasesInRange(): void | ||
| { | ||
| $this->mockWebApplication(); | ||
| $val = new RangeValidator(['range' => IntStatus::cases()]); | ||
| $m = FakedValidationModel::createWithAttributes(['attr_status' => 1]); | ||
| $options = $val->getClientOptions($m, 'attr_status'); | ||
| $this->assertSame(['1', '0'], $options['range']); | ||
| } | ||
|
|
||
| public function testGetClientOptionsWithUnitEnumCasesInRange(): void | ||
| { | ||
| $this->mockWebApplication(); | ||
| $val = new RangeValidator(['range' => Suit::cases()]); | ||
| $m = FakedValidationModel::createWithAttributes(['attr_suit' => 'Hearts']); | ||
| $options = $val->getClientOptions($m, 'attr_suit'); | ||
| $this->assertSame(['Hearts', 'Diamonds', 'Clubs', 'Spades'], $options['range']); | ||
| } | ||
|
|
||
| public function testGetClientOptionsWithEnumProperty(): void | ||
| { | ||
| $this->mockWebApplication(); | ||
| $val = new RangeValidator(['enum' => StringStatus::class]); | ||
| $m = FakedValidationModel::createWithAttributes(['attr_status' => 'active']); | ||
| $options = $val->getClientOptions($m, 'attr_status'); | ||
| $this->assertSame(['active', 'inactive'], $options['range']); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @link https://www.yiiframework.com/ | ||
| * @copyright Copyright (c) 2008 Yii Software LLC | ||
| * @license https://www.yiiframework.com/license/ | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace yiiunit\framework\validators\stubs; | ||
|
|
||
| enum StringStatus: string | ||
| { | ||
| case Active = 'active'; | ||
| case Inactive = 'inactive'; | ||
| } | ||
|
|
||
| enum IntStatus: int | ||
| { | ||
| case On = 1; | ||
| case Off = 0; | ||
| } | ||
|
|
||
| enum Suit | ||
| { | ||
| case Hearts; | ||
| case Diamonds; | ||
| case Clubs; | ||
| case Spades; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.