pestphp / pestphp/pest

[Question] Checking for the presence of a suffix in a file name

Open
#1,608 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
11.7k
Forks
538
Avg merge
4d 11h
Merged PRs (30d)
8

Description

Technical information

  • php v8.4
  • laravel v12.42
  • pest v4.2

Now, in architectural tests, we can check suffixes in class names

Example:

test('Controllers should have Controller suffix')
    ->expect('App\Http\Controllers')
    ->classes()
    ->toHaveSuffix('Controller');

I want to check that tests contain the suffix “Test” so that they participate in testing, because sometimes we may simply forget to add the suffix “Test” and the file does not submit during testing

I couldn't find how to do this in the documentation

Wrote a custom test that works:

test('Tests should have Test suffix', function () {
    $testDirs = [
        'tests',
    ];

    $exceptions = [
        'TestCase.php',
        'Pest.php',
    ];

    $invalidFiles = collect($testDirs)
        ->filter(fn($dir) => is_dir($dir))
        ->flatMap(function ($dir) use ($exceptions) {
            return collect(
                new RecursiveIteratorIterator(
                    new RecursiveDirectoryIterator($dir)
                )
            )->filter(fn($file) => $file->isFile())
                ->filter(fn($file) => $file->getExtension() === 'php')
                ->filter(fn($file) => ! in_array($file->getFilename(), $exceptions, true))
                ->filter(fn($file) => ! str_ends_with($file->getFilename(), 'Test.php'))
                ->map(fn($file) => $file->getPathname())
                ->all();
        });

    expect($invalidFiles)->toBeEmpty(
        "Files not should have Test suffix:\n" . $invalidFiles->implode("\n")
    );
});

I want to know if it is possible to do the same with pest as implemented in the first example ?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the architectural test example using toHaveSuffix('Controller') and compare it with the custom recursive test shown in the issue. Check Pest's architectural testing documentation and existing suffix-related tests or entry points; done means determining whether the requested filename rule is already supported or defining the missing behavior clearly.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.