Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom arch expectations #1319

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from

Conversation

olivernybroe
Copy link
Member

@olivernybroe olivernybroe commented Nov 20, 2024

What:

  • Bug Fix
  • New Feature

Description:

Allow adding custom arch expectations into pest.

A custom arch expectation needs to have typehinted the return type to the ArchExpectation for it to work with this implementation.

An example could be the following added as a custom expectation

expect()->extend('toHaveMethodsDocumentedMatching', function (string $pattern): ArchExpectation {
    return Targeted::make(
        $this, // @phpstan-ignore-line
        fn (ObjectDescription $object): bool => isset($object->reflectionClass) === false
            || (
                array_filter(
                Reflection::getMethodsFromReflectionClass($object->reflectionClass),
                fn(ReflectionMethod $method): bool => (enum_exists($object->name) === false || in_array($method->name, ['from', 'tryFrom', 'cases'], true) === false)
                    && realpath($method->getFileName() ?: '/') === realpath($object->path)
                    && !Str::isMatch($pattern, $method->getDocComment()),
                ) === []
            ),
        'to have methods with documentation / annotations matching pattern',
        FileLineFinder::where(fn (string $line): bool => str_contains($line, 'class'))
    );
});

Related:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant