Merge pull request #1006 from JonPurvis/to-be-backed-enum-expectation

[2.x] Add `toBeStringBackedEnum()` and `toBeIntBackedEnum()` Architecture Expectations
This commit is contained in:
Nuno Maduro
2024-01-25 17:09:23 +00:00
committed by GitHub
8 changed files with 150 additions and 0 deletions

View File

@ -882,4 +882,50 @@ final class Expectation
{ {
return $this->toHaveMethod('__destruct'); 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'); 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';
}