-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from MarkBaker/develop
New Trait for case name validation against an enum
- Loading branch information
Showing
11 changed files
with
142 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
File renamed without changes.
This file contains 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 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 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,18 @@ | ||
<?php | ||
|
||
namespace EnumHelper; | ||
|
||
trait EnumValidatableCase | ||
{ | ||
public static function isValidCase(string $name): bool | ||
{ | ||
$reflector = new \ReflectionEnum(self::class); | ||
try { | ||
$reflector->getCase($name); | ||
} catch (\ReflectionException $e) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains 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 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 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 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 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,32 @@ | ||
<?php | ||
|
||
namespace EnumHelper\Tests; | ||
|
||
use EnumHelper\TestData\BackedEnumSuit; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ValidateNameInBackedEnumSuitTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider providerBackedEnumSuit | ||
*/ | ||
public function testBackedEnumValidateName(string $caseName, bool $validity) | ||
{ | ||
$caseValidity = BackedEnumSuit::isValidCase($caseName); | ||
|
||
self::assertSame($validity, $caseValidity); | ||
} | ||
|
||
public function providerBackedEnumSuit(): array | ||
{ | ||
return [ | ||
[BackedEnumSuit::Clubs->name, true], | ||
[BackedEnumSuit::Diamonds->name, true], | ||
[BackedEnumSuit::Hearts->name, true], | ||
[BackedEnumSuit::Spades->name, true], | ||
[strtoupper(BackedEnumSuit::Hearts->name), false], | ||
['NoTrumps', false], | ||
['', false], | ||
]; | ||
} | ||
} |
This file contains 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,32 @@ | ||
<?php | ||
|
||
namespace EnumHelper\Tests; | ||
|
||
use EnumHelper\TestData\EnumSuit; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ValidateNameInEnumSuitTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider providerEnumSuit | ||
*/ | ||
public function testBackedEnumValidateName(string $caseName, bool $validity) | ||
{ | ||
$caseValidity = EnumSuit::isValidCase($caseName); | ||
|
||
self::assertSame($validity, $caseValidity); | ||
} | ||
|
||
public function providerEnumSuit(): array | ||
{ | ||
return [ | ||
[EnumSuit::Clubs->name, true], | ||
[EnumSuit::Diamonds->name, true], | ||
[EnumSuit::Hearts->name, true], | ||
[EnumSuit::Spades->name, true], | ||
[strtoupper(EnumSuit::Hearts->name), false], | ||
['NoTrumps', false], | ||
['', false], | ||
]; | ||
} | ||
} |