Skip to content

Commit

Permalink
add support for native enums to make the migration easier
Browse files Browse the repository at this point in the history
  • Loading branch information
mhujer committed Sep 20, 2022
1 parent b8fb1b4 commit fd3dfc1
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 11 deletions.
35 changes: 24 additions & 11 deletions src/EnumAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,34 @@ final class EnumAssert
{

/**
* @param \Consistence\Enum\Enum|mixed $expectedEnum
* @param \Consistence\Enum\Enum|mixed $actualEnum
* @param \Consistence\Enum\Enum|\BackedEnum|mixed $expectedEnum
* @param \Consistence\Enum\Enum|\BackedEnum|mixed $actualEnum
*/
public static function assertSame($expectedEnum, $actualEnum): void // phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
{
Assert::assertInstanceOf(Enum::class, $expectedEnum);
Assert::assertInstanceOf(Enum::class, $actualEnum);
if ($expectedEnum instanceof Enum || $actualEnum instanceof Enum) {
Assert::assertInstanceOf(Enum::class, $expectedEnum);
Assert::assertInstanceOf(Enum::class, $actualEnum);

Assert::assertSame($expectedEnum, $actualEnum, sprintf(
'Expected "%s:%s", but got "%s:%s"',
get_class($expectedEnum),
$expectedEnum->getValue(),
get_class($actualEnum),
$actualEnum->getValue()
));
Assert::assertSame($expectedEnum, $actualEnum, sprintf(
'Expected "%s:%s", but got "%s:%s"',
get_class($expectedEnum),
$expectedEnum->getValue(),
get_class($actualEnum),
$actualEnum->getValue()
));
} else {
Assert::assertInstanceOf(\BackedEnum::class, $expectedEnum);
Assert::assertInstanceOf(\BackedEnum::class, $actualEnum);

Assert::assertSame($expectedEnum, $actualEnum, sprintf(
'Expected "%s:%s", but got "%s:%s"',
get_class($expectedEnum),
$expectedEnum->value,
get_class($actualEnum),
$actualEnum->value
));
}
}

}
49 changes: 49 additions & 0 deletions tests/EnumAssertNativeEnumTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types = 1);

namespace Mhujer\ConsistencePhpunit;

use Mhujer\ConsistencePhpunit\Fixtures\CardColor;
use Mhujer\ConsistencePhpunit\Fixtures\CardColorNative;

final class EnumAssertNativeEnumTest extends \PHPUnit\Framework\TestCase
{

public function testComparingTwoSimilarEnumValuesWork(): void
{
EnumAssert::assertSame(CardColorNative::RED, CardColorNative::RED);
}

public function testAssertSameEnumFailsIfActualValueIsNotAnEnum(): void
{
$this->expectException(\PHPUnit\Framework\ExpectationFailedException::class);
$this->expectExceptionMessage('Failed asserting that \'foobar\' is an instance of interface "BackedEnum".');

EnumAssert::assertSame(CardColorNative::RED, 'foobar');
}

public function testAssertSameEnumFailsIfExpectedValueIsNotAnEnum(): void
{
$this->expectException(\PHPUnit\Framework\ExpectationFailedException::class);
$this->expectExceptionMessage('Failed asserting that \'foobar2\' is an instance of interface "BackedEnum".');

EnumAssert::assertSame('foobar2', CardColorNative::BLACK);
}

public function testAssertSameEnumFailsIfTheEnumsAreNotSame(): void
{
$this->expectException(\PHPUnit\Framework\ExpectationFailedException::class);
$this->expectExceptionMessage('Expected "Mhujer\ConsistencePhpunit\Fixtures\CardColorNative:red", but got "Mhujer\ConsistencePhpunit\Fixtures\CardColorNative:black"');

EnumAssert::assertSame(CardColorNative::RED, CardColorNative::BLACK);
}


public function testComparingNativeEnumWithConsisteneEnum(): void
{
$this->expectException(\PHPUnit\Framework\ExpectationFailedException::class);
$this->expectExceptionMessage('Failed asserting that Mhujer\ConsistencePhpunit\Fixtures\CardColorNative Object (...) is an instance of class "Consistence\Enum\Enum".');

EnumAssert::assertSame(CardColorNative::RED, CardColor::get(CardColor::BLACK));
}

}
11 changes: 11 additions & 0 deletions tests/Fixtures/CardColorNative.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types = 1);

namespace Mhujer\ConsistencePhpunit\Fixtures;

enum CardColorNative: string
{

case BLACK = 'black';
case RED = 'red';

}

0 comments on commit fd3dfc1

Please sign in to comment.