From 2e0177627259022bd8f820f0cb29f896b87fab3e Mon Sep 17 00:00:00 2001 From: JonPurvis Date: Sat, 18 Nov 2023 03:31:35 +0000 Subject: [PATCH] add to be backed enum expectation --- src/Expectation.php | 46 +++++++++++++++++++ src/Expectations/OppositeExpectation.php | 46 +++++++++++++++++++ tests/Features/Expect/toBeIntBackedEnum.php | 9 ++++ .../Features/Expect/toBeStringBackedEnum.php | 9 ++++ .../HasIntBacking/HasIntBackingEnum.php | 10 ++++ .../HasStringBacking/HasStringBackingEnum.php | 10 ++++ .../HasIntBacking/HasIntBackingEnum.php | 10 ++++ .../HasStringBacking/HasStringBackingEnum.php | 10 ++++ 8 files changed, 150 insertions(+) create mode 100644 tests/Features/Expect/toBeIntBackedEnum.php create mode 100644 tests/Features/Expect/toBeStringBackedEnum.php create mode 100644 tests/Fixtures/Arch/ToBeIntBackedEnum/HasIntBacking/HasIntBackingEnum.php create mode 100644 tests/Fixtures/Arch/ToBeIntBackedEnum/HasStringBacking/HasStringBackingEnum.php create mode 100644 tests/Fixtures/Arch/ToBeStringBackedEnum/HasIntBacking/HasIntBackingEnum.php create mode 100644 tests/Fixtures/Arch/ToBeStringBackedEnum/HasStringBacking/HasStringBackingEnum.php diff --git a/src/Expectation.php b/src/Expectation.php index 5885bfeb..470b702a 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -878,4 +878,50 @@ final class Expectation { return $this->toHaveMethod('__destruct'); } + + /** + * Asserts that the given expectation target is a backed enum of given type. + */ + private function toBeBackedEnum(string $backingType): ArchExpectation + { + return Targeted::make( + $this, + fn (ObjectDescription $object): bool => (new \ReflectionEnum($object->name))->isBacked() + && (string)(new \ReflectionEnum($object->name))->getBackingType() === $backingType, + 'to be ' . $backingType . ' backed enum', + FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')), + ); + } + + /** + * Asserts that the given expectation targets are string backed enums. + */ + public function toBeStringBackedEnums(): ArchExpectation + { + return $this->toBeStringBackedEnum(); + } + + /** + * Asserts that the given expectation targets are int backed enums. + */ + public function toBeIntBackedEnums(): ArchExpectation + { + return $this->toBeIntBackedEnum(); + } + + /** + * Asserts that the given expectation target is a string backed enum. + */ + public function toBeStringBackedEnum(): ArchExpectation + { + return $this->toBeBackedEnum('string'); + } + + /** + * Asserts that the given expectation target is an int backed enum. + */ + public function toBeIntBackedEnum(): ArchExpectation + { + return $this->toBeBackedEnum('int'); + } } diff --git a/src/Expectations/OppositeExpectation.php b/src/Expectations/OppositeExpectation.php index 66963677..9a3399c2 100644 --- a/src/Expectations/OppositeExpectation.php +++ b/src/Expectations/OppositeExpectation.php @@ -485,4 +485,50 @@ final class OppositeExpectation { return $this->toHaveMethod('__destruct'); } + + /** + * Asserts that the given expectation target is not a backed enum of given type. + */ + private function toBeBackedEnum(string $backingType): ArchExpectation + { + return Targeted::make( + $this->original, + fn (ObjectDescription $object): bool => (new \ReflectionEnum($object->name))->isBacked() + && (string)(new \ReflectionEnum($object->name))->getBackingType() !== $backingType, + 'not to be ' . $backingType . ' backed enum', + FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')), + ); + } + + /** + * Asserts that the given expectation targets are not string backed enums. + */ + public function toBeStringBackedEnums(): ArchExpectation + { + return $this->toBeStringBackedEnum(); + } + + /** + * Asserts that the given expectation targets are not int backed enums. + */ + public function toBeIntBackedEnums(): ArchExpectation + { + return $this->toBeIntBackedEnum(); + } + + /** + * Asserts that the given expectation target is not a string backed enum. + */ + public function toBeStringBackedEnum(): ArchExpectation + { + return $this->toBeBackedEnum('string'); + } + + /** + * Asserts that the given expectation target is not an int backed enum. + */ + public function toBeIntBackedEnum(): ArchExpectation + { + return $this->toBeBackedEnum('int'); + } } diff --git a/tests/Features/Expect/toBeIntBackedEnum.php b/tests/Features/Expect/toBeIntBackedEnum.php new file mode 100644 index 00000000..10ee86c7 --- /dev/null +++ b/tests/Features/Expect/toBeIntBackedEnum.php @@ -0,0 +1,9 @@ +expect('Tests\Fixtures\Arch\ToBeIntBackedEnum\HasIntBacking') + ->toBeIntBackedEnum(); + +test('enum is not backed by int') + ->expect('Tests\Fixtures\Arch\ToBeIntBackedEnum\HasStringBacking') + ->not->toBeIntBackedEnum(); diff --git a/tests/Features/Expect/toBeStringBackedEnum.php b/tests/Features/Expect/toBeStringBackedEnum.php new file mode 100644 index 00000000..4451185d --- /dev/null +++ b/tests/Features/Expect/toBeStringBackedEnum.php @@ -0,0 +1,9 @@ +expect('Tests\Fixtures\Arch\ToBeStringBackedEnum\HasStringBacking') + ->toBeStringBackedEnum(); + +test('enum is not backed by string') + ->expect('Tests\Fixtures\Arch\ToBeStringBackedEnum\HasIntBacking') + ->not->toBeStringBackedEnum(); diff --git a/tests/Fixtures/Arch/ToBeIntBackedEnum/HasIntBacking/HasIntBackingEnum.php b/tests/Fixtures/Arch/ToBeIntBackedEnum/HasIntBacking/HasIntBackingEnum.php new file mode 100644 index 00000000..eddac03f --- /dev/null +++ b/tests/Fixtures/Arch/ToBeIntBackedEnum/HasIntBacking/HasIntBackingEnum.php @@ -0,0 +1,10 @@ +