mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 15:57:21 +01:00
Add toHaveMethod arch expectation
This commit is contained in:
@ -14,7 +14,7 @@ use Symfony\Component\Console\Output\OutputInterface;
|
|||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
final class NoDirtyTestsFound extends InvalidArgumentException implements ExceptionInterface, RenderlessEditor, RenderlessTrace, Panicable
|
final class NoDirtyTestsFound extends InvalidArgumentException implements ExceptionInterface, Panicable, RenderlessEditor, RenderlessTrace
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Renders the panic on the given output.
|
* Renders the panic on the given output.
|
||||||
|
|||||||
@ -501,6 +501,19 @@ final class Expectation
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that the given expectation target have a specific method.
|
||||||
|
*/
|
||||||
|
public function toHaveMethod(string $method): ArchExpectation
|
||||||
|
{
|
||||||
|
return Targeted::make(
|
||||||
|
$this,
|
||||||
|
fn (ObjectDescription $object): bool => $object->reflectionClass->hasMethod($method),
|
||||||
|
'to have method',
|
||||||
|
FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asserts that the given expectation target is enum.
|
* Asserts that the given expectation target is enum.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -149,6 +149,19 @@ final class OppositeExpectation
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that the given expectation target does not have a specific method.
|
||||||
|
*/
|
||||||
|
public function toHaveMethod(string $method): ArchExpectation
|
||||||
|
{
|
||||||
|
return Targeted::make(
|
||||||
|
$this->original,
|
||||||
|
fn (ObjectDescription $object): bool => ! $object->reflectionClass->hasMethod($method),
|
||||||
|
'to not have method',
|
||||||
|
FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class')),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asserts that the given expectation target is not enum.
|
* Asserts that the given expectation target is not enum.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -1,28 +1,29 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use PHPUnit\Framework\ExpectationFailedException;
|
use Pest\Arch\Exceptions\ArchExpectationFailedException;
|
||||||
|
|
||||||
$object = new class
|
test('class has method')
|
||||||
{
|
->expect('Tests\Fixtures\Arch\ToHaveMethod\HasMethod\HasMethod')
|
||||||
public function foo(): void
|
->toHaveMethod('foo');
|
||||||
{
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
test('pass', function () use ($object) {
|
test('opposite class has method')
|
||||||
expect($object)->toHaveMethod('foo')
|
->throws(ArchExpectationFailedException::class)
|
||||||
->and($object)->toHaveMethod('foo')
|
->expect('Tests\Fixtures\Arch\ToHaveMethod\HasMethod\HasMethod')
|
||||||
->and($object)->not->toHaveMethod('fooNull');
|
->not->toHaveMethod('foo');
|
||||||
});
|
|
||||||
|
|
||||||
test('failures', function () use ($object) {
|
test('class has method via a parent class')
|
||||||
expect($object)->toHaveMethod('bar');
|
->expect('Tests\Fixtures\Arch\ToHaveMethod\HasMethod\HasMethodViaParent')
|
||||||
})->throws(ExpectationFailedException::class);
|
->toHaveMethod('foo');
|
||||||
|
|
||||||
test('failures with message', function () use ($object) {
|
test('class has method via a trait')
|
||||||
expect($object)->toHaveMethod(name: 'bar', message: 'oh no!');
|
->expect('Tests\Fixtures\Arch\ToHaveMethod\HasMethod\HasMethodViaTrait')
|
||||||
})->throws(ExpectationFailedException::class, 'oh no!');
|
->toHaveMethod('foo');
|
||||||
|
|
||||||
test('not failures', function () use ($object) {
|
test('failure when the class has no method')
|
||||||
expect($object)->not->toHaveMethod('foo');
|
->throws(ArchExpectationFailedException::class)
|
||||||
})->throws(ExpectationFailedException::class);
|
->expect('Tests\Fixtures\Arch\ToHaveMethod\HasNoMethod\HasNoMethodClass')
|
||||||
|
->toHaveMethod('foo');
|
||||||
|
|
||||||
|
test('class has no method')
|
||||||
|
->expect('Tests\Fixtures\Arch\ToHaveMethod\HasNoMethod\HasNoMethodClass')
|
||||||
|
->not->toHaveMethod('foo');
|
||||||
|
|||||||
13
tests/Fixtures/Arch/ToHaveMethod/HasMethod/HasMethod.php
Normal file
13
tests/Fixtures/Arch/ToHaveMethod/HasMethod/HasMethod.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToHaveMethod\HasMethod;
|
||||||
|
|
||||||
|
class HasMethod
|
||||||
|
{
|
||||||
|
public function foo(): void
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToHaveMethod\HasMethod;
|
||||||
|
|
||||||
|
trait HasMethodTrait
|
||||||
|
{
|
||||||
|
public function foo(): void
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToHaveMethod\HasMethod;
|
||||||
|
|
||||||
|
class HasMethodViaParent extends ParentHasMethodClass
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToHaveMethod\HasMethod;
|
||||||
|
|
||||||
|
class HasMethodViaTrait
|
||||||
|
{
|
||||||
|
use HasMethodTrait;
|
||||||
|
}
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToHaveMethod\HasMethod;
|
||||||
|
|
||||||
|
class ParentHasMethodClass
|
||||||
|
{
|
||||||
|
public function foo(): void
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Fixtures\Arch\ToHaveMethod\HasNoMethod;
|
||||||
|
|
||||||
|
class HasNoMethodClass
|
||||||
|
{
|
||||||
|
public function bar(): void
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user