-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
Provide a Faker Enum Provider Bridge #30
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking care of this 😃
|
||
$reflectionEnum = new ReflectionClass($enumClass); | ||
|
||
if (!$reflectionEnum->implementsInterface(EnumInterface::class)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than using reflection, you can use is_a
with third argument set to true
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanx for the tip !!!
* | ||
* ``` | ||
* [ | ||
* 'civility' => Civility::class, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd use capitalized aliases: Civility
c5d4bb9
to
c220f08
Compare
} | ||
|
||
/** | ||
* @param string $enumValueShortcut As <ENUM_CLASS_ALIAS::ENUM_VALUE_CONSTANT> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<
and >
is specific to Alice. it should not appear here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eh eh, <
and >
is just a convention for generic template masks but as a matter of fact, it can be ambiguous in an Alice/Fixture context.
/** @var EnumInterface $class */ | ||
$class = $this->enumMapping[$classAlias]; | ||
|
||
$constants = explode('|', $constants); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we should check if the enum class is an instance of FlaggedEnum
to throw a nice exception otherwise
private function ensureEnumClass(string $enumClass) | ||
{ | ||
if (!class_exists($enumClass)) { | ||
throw new \InvalidArgumentException("$enumClass class does not exist"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can remove this check as is_a
below will already trigger autoload.
} | ||
|
||
if (!is_a($enumClass, EnumInterface::class, true)) { | ||
throw new \InvalidArgumentException("$enumClass is not a proper enum class"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use the one in the package (Elao\Enum\Exception\InvalidArgumentException
)
d29c349
to
d037be3
Compare
<?php | ||
|
||
/* | ||
* This file is part of the Jarvis - Elao website. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jarvis ;( >> I will update my IDE config for this project
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vendor/bin/php-cs-fixer fix
d037be3
to
5105219
Compare
86a627b
to
bc2542a
Compare
7713f4f
to
ebf6a64
Compare
$class = $this->enumMapping[$enumClassAlias]; | ||
|
||
$instances = $class::instances(); | ||
$randomRank = rand(0, count($instances) - 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you rather use mt_rand
?
$enumProvider = new EnumProvider($mapping); | ||
} | ||
|
||
public function provideErroneousEnumMapping(): array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
diff --git a/tests/Unit/Bridge/Faker/Provider/EnumProviderTest.php b/tests/Unit/Bridge/Faker/Provider/EnumProviderTest.php
index f0640fc..dcfa153 100644
--- a/tests/Unit/Bridge/Faker/Provider/EnumProviderTest.php
+++ b/tests/Unit/Bridge/Faker/Provider/EnumProviderTest.php
@@ -50,25 +50,22 @@ class EnumProviderTest extends \PHPUnit_Framework_TestCase
*/
public function testConstructorShouldFailWhenEnumClassIsIncorrect(array $mapping)
{
- $enumProvider = new EnumProvider($mapping);
+ new EnumProvider($mapping);
}
- public function provideErroneousEnumMapping(): array
+ public function provideErroneousEnumMapping()
{
- return [
+ yield 'EnumProvider constructor with a class that does not exist' => [
[
- // Pass to EnumProvider constructor a class that does not exist
- [
- 'Simple' => SimpleEnum::class,
- 'Not-a-class' => '\UnexistingClass',
- ],
+ 'Simple' => SimpleEnum::class,
+ 'Not-a-class' => '\UnexistingClass',
],
+ ];
+
+ yield 'EnumProvider constructor with a class that is not an Enum' => [
[
- // Pass to EnumProvider constructor a class that is not an Enum
- [
- 'Simple' => SimpleEnum::class,
- 'Not-an-enum' => \DateTime::class,
- ],
+ 'Simple' => SimpleEnum::class,
+ 'Not-an-enum' => \DateTime::class,
],
];
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a minor diff to apply, and I think we're good to go. Thank you :)
1bc8b63
to
4e96d48
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Nice
Fixes #29
enum(EnumAlias::VALUE)
using pipes for flagged enums, e.g: enum(Permissions::WRITE|READ)randomEnum(EnumAlias)
Integrations
part