From 3678b71cbe679cd1d2a7ccf0fe9d4e3c4310db27 Mon Sep 17 00:00:00 2001 From: henzeb Date: Wed, 1 Jun 2022 21:16:34 +0200 Subject: [PATCH] fix where int backed enums could not use Comparison --- src/Concerns/Comparison.php | 2 +- src/Helpers/EnumSubsetMethods.php | 6 ++--- ...BackedMakersEnum.php => IntBackedEnum.php} | 5 ++-- tests/Unit/Concerns/ComparisonTest.php | 13 ++++++++++ tests/Unit/Concerns/MakersTest.php | 10 ++++---- tests/Unit/Helpers/EnumSubsetMethodsTest.php | 24 +++++++++---------- 6 files changed, 37 insertions(+), 23 deletions(-) rename tests/Fixtures/{IntBackedMakersEnum.php => IntBackedEnum.php} (59%) diff --git a/src/Concerns/Comparison.php b/src/Concerns/Comparison.php index 6fb17cf..93f0c69 100644 --- a/src/Concerns/Comparison.php +++ b/src/Concerns/Comparison.php @@ -10,7 +10,7 @@ trait Comparison /** * @mixin BackedEnum */ - final public function equals(self|string ...$equals): bool + final public function equals(self|string|int ...$equals): bool { return (new EnumSubsetMethods(self::class, $this)) ->equals(...$equals); diff --git a/src/Helpers/EnumSubsetMethods.php b/src/Helpers/EnumSubsetMethods.php index 256a86e..0140a53 100644 --- a/src/Helpers/EnumSubsetMethods.php +++ b/src/Helpers/EnumSubsetMethods.php @@ -25,7 +25,7 @@ public function do(Closure $callable): void } } - public function equals(string|UnitEnum|BackedEnum ...$equals): bool + public function equals(UnitEnum|string|int ...$equals): bool { EnumCheck::matches($this->enumType, ...$equals); @@ -39,7 +39,7 @@ public function equals(string|UnitEnum|BackedEnum ...$equals): bool return false; } - private function compare(UnitEnum|BackedEnum $enum, string|UnitEnum|BackedEnum ...$equals): bool + private function compare(UnitEnum $enum, UnitEnum|string|int ...$equals): bool { foreach ($equals as $equal) { if ($enum->name === $equal) { @@ -54,7 +54,7 @@ private function compare(UnitEnum|BackedEnum $enum, string|UnitEnum|BackedEnum . return true; } - if (property_exists($equal, 'name') && $enum->name === $equal->name) { + if ($equal instanceof UnitEnum && $enum->name === $equal->name) { return true; } } diff --git a/tests/Fixtures/IntBackedMakersEnum.php b/tests/Fixtures/IntBackedEnum.php similarity index 59% rename from tests/Fixtures/IntBackedMakersEnum.php rename to tests/Fixtures/IntBackedEnum.php index b41ee44..d70a0be 100644 --- a/tests/Fixtures/IntBackedMakersEnum.php +++ b/tests/Fixtures/IntBackedEnum.php @@ -5,11 +5,12 @@ use Henzeb\Enumhancer\Concerns\Makers; +use Henzeb\Enumhancer\Concerns\Comparison; -enum IntBackedMakersEnum: int +enum IntBackedEnum: int { - use Makers; + use Makers, Comparison; case TEST = 0; case TEST_2 = 1; diff --git a/tests/Unit/Concerns/ComparisonTest.php b/tests/Unit/Concerns/ComparisonTest.php index 52be7b7..393b380 100644 --- a/tests/Unit/Concerns/ComparisonTest.php +++ b/tests/Unit/Concerns/ComparisonTest.php @@ -4,6 +4,7 @@ use Henzeb\Enumhancer\Concerns\Comparison; use PHPUnit\Framework\TestCase; +use Henzeb\Enumhancer\Tests\Fixtures\IntBackedEnum; use Henzeb\Enumhancer\Tests\Fixtures\EnhancedUnitEnum; use Henzeb\Enumhancer\Tests\Fixtures\EnhancedBackedEnum; @@ -82,4 +83,16 @@ public function testShouldMatchWithUnitEnumValue() { EnhancedUnitEnum::ENUM->equals('enum') ); } + + public function testShouldMatchWithIntBackedEnumValue() { + $this->assertTrue( + IntBackedEnum::TEST->equals(0) + ); + } + + public function testShouldNotMatchWithIntBackedEnumValue() { + $this->assertFalse( + IntBackedEnum::TEST->equals(1) + ); + } } diff --git a/tests/Unit/Concerns/MakersTest.php b/tests/Unit/Concerns/MakersTest.php index d5ce89e..4572198 100644 --- a/tests/Unit/Concerns/MakersTest.php +++ b/tests/Unit/Concerns/MakersTest.php @@ -4,7 +4,7 @@ use Generator; -use Henzeb\Enumhancer\Tests\Fixtures\IntBackedMakersEnum; +use Henzeb\Enumhancer\Tests\Fixtures\IntBackedEnum; use Henzeb\Enumhancer\Tests\Fixtures\StringBackedMakersEnum; use PHPUnit\Framework\TestCase; @@ -58,8 +58,8 @@ public function testMakeByValueStrToUpper() public function testMakeByValueOnIntbackedEnum() { $this->assertEquals( - IntBackedMakersEnum::TEST, - IntBackedMakersEnum::make(0) + IntBackedEnum::TEST, + IntBackedEnum::make(0) ); } @@ -89,8 +89,8 @@ public function testTryMakeByValue() public function testTryMakeByValueOnIntbackedEnum() { $this->assertEquals( - IntBackedMakersEnum::TEST, - IntBackedMakersEnum::tryMake(0) + IntBackedEnum::TEST, + IntBackedEnum::tryMake(0) ); } diff --git a/tests/Unit/Helpers/EnumSubsetMethodsTest.php b/tests/Unit/Helpers/EnumSubsetMethodsTest.php index cdd918c..7aaa0a6 100644 --- a/tests/Unit/Helpers/EnumSubsetMethodsTest.php +++ b/tests/Unit/Helpers/EnumSubsetMethodsTest.php @@ -6,7 +6,7 @@ use Henzeb\Enumhancer\Helpers\EnumSubsetMethods; use Henzeb\Enumhancer\Tests\Fixtures\SubsetUnitEnum; use Henzeb\Enumhancer\Tests\Fixtures\EnhancedUnitEnum; -use Henzeb\Enumhancer\Tests\Fixtures\IntBackedMakersEnum; +use Henzeb\Enumhancer\Tests\Fixtures\IntBackedEnum; use Henzeb\Enumhancer\Tests\Fixtures\StringBackedMakersEnum; class EnumSubsetMethodsTest extends TestCase @@ -15,39 +15,39 @@ class EnumSubsetMethodsTest extends TestCase public function testShouldThrowErrorWithWrongEnumType(): void { $this->expectError(); - (new EnumSubsetMethods(IntBackedMakersEnum::class, EnhancedUnitEnum::ENUM)); + (new EnumSubsetMethods(IntBackedEnum::class, EnhancedUnitEnum::ENUM)); } public function testEqualsShouldReturnNullWhenNoEnumsPassed() { $this->assertFalse( - (new EnumSubsetMethods(IntBackedMakersEnum::class)) - ->equals(IntBackedMakersEnum::TEST) + (new EnumSubsetMethods(IntBackedEnum::class)) + ->equals(IntBackedEnum::TEST) ); } public function testEqualsShouldReturnTrue() { $this->assertTrue( - (new EnumSubsetMethods(IntBackedMakersEnum::class, IntBackedMakersEnum::TEST)) - ->equals(IntBackedMakersEnum::TEST) + (new EnumSubsetMethods(IntBackedEnum::class, IntBackedEnum::TEST)) + ->equals(IntBackedEnum::TEST) ); } public function testEqualsMultiShouldReturnTrue() { $this->assertTrue( - (new EnumSubsetMethods(IntBackedMakersEnum::class, ...IntBackedMakersEnum::cases())) - ->equals(IntBackedMakersEnum::TEST) + (new EnumSubsetMethods(IntBackedEnum::class, ...IntBackedEnum::cases())) + ->equals(IntBackedEnum::TEST) ); } public function testNamesShouldReturnArrayOfNames() { $this->assertEquals( - $this->getNames(IntBackedMakersEnum::cases()), - (new EnumSubsetMethods(IntBackedMakersEnum::class, ...IntBackedMakersEnum::cases())) + $this->getNames(IntBackedEnum::cases()), + (new EnumSubsetMethods(IntBackedEnum::class, ...IntBackedEnum::cases())) ->names() ); } @@ -64,8 +64,8 @@ public function testValueShouldReturnArrayOfValuesStringBacked() public function testValueShouldReturnArrayOfValuesIntBacked() { $this->assertEquals( - $this->getValues(IntBackedMakersEnum::cases()), - (new EnumSubsetMethods(IntBackedMakersEnum::class, ...IntBackedMakersEnum::cases())) + $this->getValues(IntBackedEnum::cases()), + (new EnumSubsetMethods(IntBackedEnum::class, ...IntBackedEnum::cases())) ->values() ); }