Skip to content

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

@deniskorbakov

Description

@deniskorbakov

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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions