Add toHaveMethod arch expectation

This commit is contained in:
ludoguenet
2023-09-01 18:32:40 +02:00
parent 04fa6b6372
commit 9dabecacbf
10 changed files with 121 additions and 22 deletions

View File

@ -14,7 +14,7 @@ use Symfony\Component\Console\Output\OutputInterface;
/**
* @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.

View File

@ -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.
*/

View File

@ -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.
*/

View File

@ -1,28 +1,29 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
use Pest\Arch\Exceptions\ArchExpectationFailedException;
$object = new class
{
public function foo(): void
{
}
};
test('class has method')
->expect('Tests\Fixtures\Arch\ToHaveMethod\HasMethod\HasMethod')
->toHaveMethod('foo');
test('pass', function () use ($object) {
expect($object)->toHaveMethod('foo')
->and($object)->toHaveMethod('foo')
->and($object)->not->toHaveMethod('fooNull');
});
test('opposite class has method')
->throws(ArchExpectationFailedException::class)
->expect('Tests\Fixtures\Arch\ToHaveMethod\HasMethod\HasMethod')
->not->toHaveMethod('foo');
test('failures', function () use ($object) {
expect($object)->toHaveMethod('bar');
})->throws(ExpectationFailedException::class);
test('class has method via a parent class')
->expect('Tests\Fixtures\Arch\ToHaveMethod\HasMethod\HasMethodViaParent')
->toHaveMethod('foo');
test('failures with message', function () use ($object) {
expect($object)->toHaveMethod(name: 'bar', message: 'oh no!');
})->throws(ExpectationFailedException::class, 'oh no!');
test('class has method via a trait')
->expect('Tests\Fixtures\Arch\ToHaveMethod\HasMethod\HasMethodViaTrait')
->toHaveMethod('foo');
test('not failures', function () use ($object) {
expect($object)->not->toHaveMethod('foo');
})->throws(ExpectationFailedException::class);
test('failure when the class has no method')
->throws(ArchExpectationFailedException::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');

View File

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace Tests\Fixtures\Arch\ToHaveMethod\HasMethod;
class HasMethod
{
public function foo(): void
{
}
}

View File

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace Tests\Fixtures\Arch\ToHaveMethod\HasMethod;
trait HasMethodTrait
{
public function foo(): void
{
}
}

View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Tests\Fixtures\Arch\ToHaveMethod\HasMethod;
class HasMethodViaParent extends ParentHasMethodClass
{
//
}

View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Tests\Fixtures\Arch\ToHaveMethod\HasMethod;
class HasMethodViaTrait
{
use HasMethodTrait;
}

View File

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace Tests\Fixtures\Arch\ToHaveMethod\HasMethod;
class ParentHasMethodClass
{
public function foo(): void
{
}
}

View File

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace Tests\Fixtures\Arch\ToHaveMethod\HasNoMethod;
class HasNoMethodClass
{
public function bar(): void
{
}
}