-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* matcher for native backed enums * Matcher backedEnumValue method. Correct typing of BackedEnumValueMatcher constructor input. * Matcher backedEnumCase method. * CR resolutions * Moving test enums to subfolder.
- Loading branch information
1 parent
a0a66ed
commit 445a62d
Showing
10 changed files
with
238 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace BrandEmbassy\MockeryTools\Enum; | ||
|
||
use BackedEnum; | ||
use Mockery\Matcher\MatcherInterface; | ||
|
||
/** | ||
* @final | ||
*/ | ||
class BackedEnumCaseMatcher implements MatcherInterface | ||
{ | ||
protected BackedEnum $expected; | ||
|
||
|
||
public function __construct(BackedEnum $expected) | ||
{ | ||
$this->expected = $expected; | ||
} | ||
|
||
|
||
/** | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint | ||
* | ||
* @param mixed $actual | ||
*/ | ||
public function match(&$actual): bool | ||
{ | ||
return $actual === $this->expected; | ||
} | ||
|
||
|
||
public function __toString(): string | ||
{ | ||
return '<EnumValue:' . $this->expected->value . '>'; | ||
} | ||
} |
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,58 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace BrandEmbassy\MockeryTools\Enum; | ||
|
||
use BackedEnum; | ||
use BrandEmbassy\MockeryTools\Enum\__fixtures__\TestOnlyBackedIntEnum; | ||
use BrandEmbassy\MockeryTools\Enum\__fixtures__\TestOnlyBackedStringAnotherEnum; | ||
use BrandEmbassy\MockeryTools\Enum\__fixtures__\TestOnlyBackedStringEnum; | ||
use PHPUnit\Framework\Assert; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @final | ||
*/ | ||
class BackedEnumCaseMatcherTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider matchingDataProvider | ||
*/ | ||
public function testMatching(bool $expectedResult, BackedEnum $expectedEnumCase, BackedEnum $enumToMatch): void | ||
{ | ||
$matcher = new BackedEnumCaseMatcher($expectedEnumCase); | ||
|
||
$result = $matcher->match($enumToMatch); | ||
|
||
Assert::assertSame($expectedResult, $result); | ||
} | ||
|
||
|
||
/** | ||
* @return array<string, array<string, mixed>> | ||
*/ | ||
public static function matchingDataProvider(): array | ||
{ | ||
return [ | ||
'Matching case (value string)' => [ | ||
'expectedResult' => true, | ||
'expectedEnumValue' => TestOnlyBackedStringEnum::STRING_VALUE, | ||
'enumToMatch' => TestOnlyBackedStringEnum::STRING_VALUE, | ||
], | ||
'Matching case (integer case)' => [ | ||
'expectedResult' => true, | ||
'expectedEnumValue' => TestOnlyBackedIntEnum::INTEGER_VALUE, | ||
'enumToMatch' => TestOnlyBackedIntEnum::INTEGER_VALUE, | ||
], | ||
'Not matching - same string, different enum' => [ | ||
'expectedResult' => false, | ||
'expectedEnumValue' => TestOnlyBackedStringAnotherEnum::STRING_VALUE, | ||
'enumToMatch' => TestOnlyBackedStringEnum::STRING_VALUE, | ||
], | ||
'Not matching - string vs int, different enums' => [ | ||
'expectedResult' => false, | ||
'expectedEnumValue' => TestOnlyBackedIntEnum::INTEGER_VALUE, | ||
'enumToMatch' => TestOnlyBackedStringEnum::INTEGER_AS_STRING_VALUE, | ||
], | ||
]; | ||
} | ||
} |
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,42 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace BrandEmbassy\MockeryTools\Enum; | ||
|
||
use Mockery\Matcher\MatcherInterface; | ||
use ReflectionEnum; | ||
use function assert; | ||
|
||
/** | ||
* @final | ||
*/ | ||
class BackedEnumValueMatcher implements MatcherInterface | ||
{ | ||
protected int|string $expected; | ||
|
||
|
||
public function __construct(string|int $expected) | ||
{ | ||
$this->expected = $expected; | ||
} | ||
|
||
|
||
/** | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint | ||
* | ||
* @param mixed $actual | ||
*/ | ||
public function match(&$actual): bool | ||
{ | ||
$rEnum = new ReflectionEnum($actual); | ||
assert($rEnum->isBacked()); | ||
|
||
return $actual->value === $this->expected; | ||
} | ||
|
||
|
||
public function __toString(): string | ||
{ | ||
return '<EnumValue:' . $this->expected . '>'; | ||
} | ||
} |
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,57 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace BrandEmbassy\MockeryTools\Enum; | ||
|
||
use BackedEnum; | ||
use BrandEmbassy\MockeryTools\Enum\__fixtures__\TestOnlyBackedIntEnum; | ||
use BrandEmbassy\MockeryTools\Enum\__fixtures__\TestOnlyBackedStringEnum; | ||
use PHPUnit\Framework\Assert; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @final | ||
*/ | ||
class BackedEnumValueMatcherTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider uuidDataProvider | ||
*/ | ||
public function testMatching(bool $expectedResult, string|int $expectedEnumValue, BackedEnum $enumToMatch): void | ||
{ | ||
$matcher = new BackedEnumValueMatcher($expectedEnumValue); | ||
|
||
$result = $matcher->match($enumToMatch); | ||
|
||
Assert::assertSame($expectedResult, $result); | ||
} | ||
|
||
|
||
/** | ||
* @return array<string, array<string, mixed>> | ||
*/ | ||
public static function uuidDataProvider(): array | ||
{ | ||
return [ | ||
'Matching string value' => [ | ||
'expectedResult' => true, | ||
'expectedEnumValue' => TestOnlyBackedStringEnum::STRING_VALUE->value, | ||
'enumToMatch' => TestOnlyBackedStringEnum::STRING_VALUE, | ||
], | ||
'Matching integer value' => [ | ||
'expectedResult' => true, | ||
'expectedEnumValue' => TestOnlyBackedIntEnum::INTEGER_VALUE->value, | ||
'enumToMatch' => TestOnlyBackedIntEnum::INTEGER_VALUE, | ||
], | ||
'Not matching string values' => [ | ||
'expectedResult' => false, | ||
'expectedEnumValue' => 'another-string-value-1', | ||
'enumToMatch' => TestOnlyBackedStringEnum::STRING_VALUE, | ||
], | ||
'Not matching data types' => [ | ||
'expectedResult' => false, | ||
'expectedEnumValue' => TestOnlyBackedIntEnum::INTEGER_VALUE->value, | ||
'enumToMatch' => TestOnlyBackedStringEnum::INTEGER_AS_STRING_VALUE, | ||
], | ||
]; | ||
} | ||
} |
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,8 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace BrandEmbassy\MockeryTools\Enum\__fixtures__; | ||
|
||
enum TestOnlyBackedIntEnum: int | ||
{ | ||
case INTEGER_VALUE = 551; | ||
} |
9 changes: 9 additions & 0 deletions
9
src/MockeryTools/Enum/__fixtures__/TestOnlyBackedStringAnotherEnum.php
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,9 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace BrandEmbassy\MockeryTools\Enum\__fixtures__; | ||
|
||
enum TestOnlyBackedStringAnotherEnum: string | ||
{ | ||
case STRING_VALUE = 'string-value'; | ||
case INTEGER_AS_STRING_VALUE = '551'; | ||
} |
9 changes: 9 additions & 0 deletions
9
src/MockeryTools/Enum/__fixtures__/TestOnlyBackedStringEnum.php
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,9 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace BrandEmbassy\MockeryTools\Enum\__fixtures__; | ||
|
||
enum TestOnlyBackedStringEnum: string | ||
{ | ||
case STRING_VALUE = 'string-value'; | ||
case INTEGER_AS_STRING_VALUE = '551'; | ||
} |
2 changes: 1 addition & 1 deletion
2
src/MockeryTools/Enum/TestOnlyEnum.php → ...yTools/Enum/__fixtures__/TestOnlyEnum.php
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