-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renamed Multi into Subset and added a few methods to Subset
- Loading branch information
Showing
13 changed files
with
206 additions
and
88 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
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 was deleted.
Oops, something went wrong.
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,14 @@ | ||
<?php | ||
|
||
namespace Henzeb\Enumhancer\Concerns; | ||
|
||
use Henzeb\Enumhancer\Contracts\EnumSubset; | ||
use Henzeb\Enumhancer\Helpers\EnumSubsetMethods; | ||
|
||
trait Subset | ||
{ | ||
final public static function of(self ...$enums): EnumSubset | ||
{ | ||
return new EnumSubsetMethods(self::class, ...($enums ?:self::cases())); | ||
} | ||
} |
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 Henzeb\Enumhancer\Contracts; | ||
|
||
use Closure; | ||
use UnitEnum; | ||
use BackedEnum; | ||
|
||
interface EnumSubset | ||
{ | ||
public function do(Closure $callable): void; | ||
|
||
public function equals(BackedEnum|UnitEnum|string ...$enum): bool; | ||
|
||
public function names(): array; | ||
|
||
public function values(): array; | ||
} |
This file was deleted.
Oops, something went wrong.
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,102 @@ | ||
<?php | ||
|
||
namespace Unit\Helpers; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Henzeb\Enumhancer\Helpers\EnumSubsetMethods; | ||
use Henzeb\Enumhancer\Tests\Fixtures\EnhancedUnitEnum; | ||
use Henzeb\Enumhancer\Tests\Fixtures\IntBackedMakersEnum; | ||
use Henzeb\Enumhancer\Tests\Fixtures\StringBackedMakersEnum; | ||
|
||
class EnumSubsetMethodsTest extends TestCase | ||
{ | ||
|
||
public function testShouldThrowErrorWithWrongEnumType(): void | ||
{ | ||
$this->expectError(); | ||
(new EnumSubsetMethods(IntBackedMakersEnum::class, EnhancedUnitEnum::ENUM)); | ||
} | ||
|
||
|
||
public function testEqualsShouldReturnNullWhenNoEnumsPassed() | ||
{ | ||
$this->assertFalse( | ||
(new EnumSubsetMethods(IntBackedMakersEnum::class)) | ||
->equals(IntBackedMakersEnum::TEST) | ||
); | ||
} | ||
|
||
public function testEqualsShouldReturnTrue() | ||
{ | ||
$this->assertTrue( | ||
(new EnumSubsetMethods(IntBackedMakersEnum::class, IntBackedMakersEnum::TEST)) | ||
->equals(IntBackedMakersEnum::TEST) | ||
); | ||
} | ||
|
||
public function testEqualsMultiShouldReturnTrue() | ||
{ | ||
$this->assertTrue( | ||
(new EnumSubsetMethods(IntBackedMakersEnum::class, ...IntBackedMakersEnum::cases())) | ||
->equals(IntBackedMakersEnum::TEST) | ||
); | ||
} | ||
|
||
public function testNamesShouldReturnArrayOfNames() | ||
{ | ||
$this->assertEquals( | ||
$this->getNames(IntBackedMakersEnum::cases()), | ||
(new EnumSubsetMethods(IntBackedMakersEnum::class, ...IntBackedMakersEnum::cases())) | ||
->names() | ||
); | ||
} | ||
|
||
public function testValueShouldReturnArrayOfValuesStringBacked() | ||
{ | ||
$this->assertEquals( | ||
$this->getValues(StringBackedMakersEnum::cases()), | ||
(new EnumSubsetMethods(StringBackedMakersEnum::class, ...StringBackedMakersEnum::cases())) | ||
->values() | ||
); | ||
} | ||
|
||
public function testValueShouldReturnArrayOfValuesIntBacked() | ||
{ | ||
$this->assertEquals( | ||
$this->getValues(IntBackedMakersEnum::cases()), | ||
(new EnumSubsetMethods(IntBackedMakersEnum::class, ...IntBackedMakersEnum::cases())) | ||
->values() | ||
); | ||
} | ||
|
||
public function testValueShouldReturnArrayOfValuesUnitEnums() | ||
{ | ||
$this->assertEquals( | ||
$this->getValues(EnhancedUnitEnum::cases()), | ||
(new EnumSubsetMethods(EnhancedUnitEnum::class, ...EnhancedUnitEnum::cases())) | ||
->values() | ||
); | ||
} | ||
|
||
public function testShouldRunClosureOnArrayOfEnums() | ||
{ | ||
$enums = []; | ||
(new EnumSubsetMethods(EnhancedUnitEnum::class, ...EnhancedUnitEnum::cases())) | ||
->do( | ||
function (EnhancedUnitEnum $enum) use (&$enums) { | ||
$enums[] = $enum; | ||
} | ||
); | ||
$this->assertEquals(EnhancedUnitEnum::cases(), $enums); | ||
} | ||
|
||
private function getNames(array $cases): array | ||
{ | ||
return array_map(fn($enum) => $enum->name, $cases); | ||
} | ||
|
||
private function getValues(array $cases): array | ||
{ | ||
return array_map(fn($enum) => $enum->value ?? $enum->value(), $cases); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.