Skip to content

Commit

Permalink
fix where int backed enums could not use Comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
henzeb committed Jun 1, 2022
1 parent 1963fa4 commit 3678b71
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/Concerns/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/Helpers/EnumSubsetMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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) {
Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/Concerns/ComparisonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
);
}
}
10 changes: 5 additions & 5 deletions tests/Unit/Concerns/MakersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -58,8 +58,8 @@ public function testMakeByValueStrToUpper()
public function testMakeByValueOnIntbackedEnum()
{
$this->assertEquals(
IntBackedMakersEnum::TEST,
IntBackedMakersEnum::make(0)
IntBackedEnum::TEST,
IntBackedEnum::make(0)
);
}

Expand Down Expand Up @@ -89,8 +89,8 @@ public function testTryMakeByValue()
public function testTryMakeByValueOnIntbackedEnum()
{
$this->assertEquals(
IntBackedMakersEnum::TEST,
IntBackedMakersEnum::tryMake(0)
IntBackedEnum::TEST,
IntBackedEnum::tryMake(0)
);
}

Expand Down
24 changes: 12 additions & 12 deletions tests/Unit/Helpers/EnumSubsetMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
);
}
Expand All @@ -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()
);
}
Expand Down

0 comments on commit 3678b71

Please sign in to comment.