add to be backed enum expectation

This commit is contained in:
JonPurvis
2023-11-18 03:31:35 +00:00
parent bd6b166a62
commit 2e01776272
8 changed files with 150 additions and 0 deletions

View File

@ -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');
}
}

View File

@ -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');
}
}

View File

@ -0,0 +1,9 @@
<?php
test('enum is backed by int')
->expect('Tests\Fixtures\Arch\ToBeIntBackedEnum\HasIntBacking')
->toBeIntBackedEnum();
test('enum is not backed by int')
->expect('Tests\Fixtures\Arch\ToBeIntBackedEnum\HasStringBacking')
->not->toBeIntBackedEnum();

View File

@ -0,0 +1,9 @@
<?php
test('enum is backed by string')
->expect('Tests\Fixtures\Arch\ToBeStringBackedEnum\HasStringBacking')
->toBeStringBackedEnum();
test('enum is not backed by string')
->expect('Tests\Fixtures\Arch\ToBeStringBackedEnum\HasIntBacking')
->not->toBeStringBackedEnum();

View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Tests\Fixtures\Arch\ToBeIntBackedEnum\HasIntBacking;
enum HasIntBackingEnum: int
{
case IntBacked = 1;
}

View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Tests\Fixtures\Arch\ToBeIntBackedEnum\HasStringBacking;
enum HasStringBackingEnum: string
{
case StringBacked = 'Testing';
}

View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Tests\Fixtures\Arch\ToBeStringBackedEnum\HasIntBacking;
enum HasIntBackingEnum: int
{
case IntBacked = 1;
}

View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Tests\Fixtures\Arch\ToBeStringBackedEnum\HasStringBacking;
enum HasStringBackingEnum: string
{
case StringBacked = 'Testing';
}