mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
Merge pull request #891 from ash-jc-allen/feature/invokable-arch-expectation
Add `toBeInvokable` arch expectation
This commit is contained in:
@ -693,4 +693,17 @@ final class Expectation
|
|||||||
{
|
{
|
||||||
return ToBeUsedInNothing::make($this);
|
return ToBeUsedInNothing::make($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that the given expectation dependency is an invokable class.
|
||||||
|
*/
|
||||||
|
public function toBeInvokable(): ArchExpectation
|
||||||
|
{
|
||||||
|
return Targeted::make(
|
||||||
|
$this,
|
||||||
|
fn (ObjectDescription $object): bool => $object->reflectionClass->hasMethod('__invoke'),
|
||||||
|
'to be invokable',
|
||||||
|
FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class'))
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -365,6 +365,19 @@ final class OppositeExpectation
|
|||||||
throw InvalidExpectation::fromMethods(['not', 'toBeUsedInNothing']);
|
throw InvalidExpectation::fromMethods(['not', 'toBeUsedInNothing']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that the given expectation dependency is not an invokable class.
|
||||||
|
*/
|
||||||
|
public function toBeInvokable(): ArchExpectation
|
||||||
|
{
|
||||||
|
return Targeted::make(
|
||||||
|
$this->original,
|
||||||
|
fn (ObjectDescription $object): bool => ! $object->reflectionClass->hasMethod('__invoke'),
|
||||||
|
'to not be invokable',
|
||||||
|
FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class'))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle dynamic method calls into the original expectation.
|
* Handle dynamic method calls into the original expectation.
|
||||||
*
|
*
|
||||||
|
|||||||
29
tests/Features/Expect/toBeInvokable.php
Normal file
29
tests/Features/Expect/toBeInvokable.php
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Pest\Arch\Exceptions\ArchExpectationFailedException;
|
||||||
|
|
||||||
|
test('class is invokable')
|
||||||
|
->expect('Tests\\Fixtures\\Arch\\ToBeInvokable\\IsInvokable\\InvokableClass')
|
||||||
|
->toBeInvokable();
|
||||||
|
|
||||||
|
test('opposite class is invokable')
|
||||||
|
->throws(ArchExpectationFailedException::class)
|
||||||
|
->expect('Tests\\Fixtures\\Arch\\ToBeInvokable\\IsInvokable\\InvokableClass')
|
||||||
|
->not->toBeInvokable();
|
||||||
|
|
||||||
|
test('class is invokable via a parent class')
|
||||||
|
->expect('Tests\\Fixtures\\Arch\\ToBeInvokable\\IsInvokable\\InvokableClassViaParent')
|
||||||
|
->toBeInvokable();
|
||||||
|
|
||||||
|
test('class is invokable via a trait')
|
||||||
|
->expect('Tests\\Fixtures\\Arch\\ToBeInvokable\\IsInvokable\\InvokableClassViaTrait')
|
||||||
|
->toBeInvokable();
|
||||||
|
|
||||||
|
test('failure when the class is not invokable')
|
||||||
|
->throws(ArchExpectationFailedException::class)
|
||||||
|
->expect('Tests\\Fixtures\\Arch\\ToBeInvokable\\IsNotInvokable\\IsNotInvokableClass')
|
||||||
|
->toBeInvokable();
|
||||||
|
|
||||||
|
test('class is not invokable')
|
||||||
|
->expect('Tests\\Fixtures\\Arch\\ToBeInvokable\\IsNotInvokable\\IsNotInvokableClass')
|
||||||
|
->not->toBeInvokable();
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToBeInvokable\IsInvokable;
|
||||||
|
|
||||||
|
class InvokableClass
|
||||||
|
{
|
||||||
|
public function __invoke(): void
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToBeInvokable\IsInvokable;
|
||||||
|
|
||||||
|
class InvokableClassViaParent extends ParentInvokableClass
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToBeInvokable\IsInvokable;
|
||||||
|
|
||||||
|
class InvokableClassViaTrait
|
||||||
|
{
|
||||||
|
use InvokableTrait;
|
||||||
|
}
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToBeInvokable\IsInvokable;
|
||||||
|
|
||||||
|
trait InvokableTrait
|
||||||
|
{
|
||||||
|
public function __invoke(): void
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToBeInvokable\IsInvokable;
|
||||||
|
|
||||||
|
class ParentInvokableClass
|
||||||
|
{
|
||||||
|
public function __invoke(): void
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToBeInvokable\IsNotInvokable;
|
||||||
|
|
||||||
|
class IsNotInvokableClass
|
||||||
|
{
|
||||||
|
public function handle(): void
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user