mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
Merge pull request #1006 from JonPurvis/to-be-backed-enum-expectation
[2.x] Add `toBeStringBackedEnum()` and `toBeIntBackedEnum()` Architecture Expectations
This commit is contained in:
@ -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');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
9
tests/Features/Expect/toBeIntBackedEnum.php
Normal file
9
tests/Features/Expect/toBeIntBackedEnum.php
Normal 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();
|
||||||
9
tests/Features/Expect/toBeStringBackedEnum.php
Normal file
9
tests/Features/Expect/toBeStringBackedEnum.php
Normal 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();
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToBeIntBackedEnum\HasIntBacking;
|
||||||
|
|
||||||
|
enum HasIntBackingEnum: int
|
||||||
|
{
|
||||||
|
case IntBacked = 1;
|
||||||
|
}
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToBeIntBackedEnum\HasStringBacking;
|
||||||
|
|
||||||
|
enum HasStringBackingEnum: string
|
||||||
|
{
|
||||||
|
case StringBacked = 'Testing';
|
||||||
|
}
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToBeStringBackedEnum\HasIntBacking;
|
||||||
|
|
||||||
|
enum HasIntBackingEnum: int
|
||||||
|
{
|
||||||
|
case IntBacked = 1;
|
||||||
|
}
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToBeStringBackedEnum\HasStringBacking;
|
||||||
|
|
||||||
|
enum HasStringBackingEnum: string
|
||||||
|
{
|
||||||
|
case StringBacked = 'Testing';
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user