pestphp / pest

Pest is an elegant PHP testing Framework with a focus on simplicity, meticulously designed to bring back the joy of testing in PHP.
https://pestphp.com
MIT License
9.12k stars 319 forks source link

[Bug]: False positives for namespaces that start with a restricted namespace #1045

Open innocenzi opened 6 months ago

innocenzi commented 6 months ago

What Happened

When forbidding the usage of Carbon\Carbon, the architecture plugin also forbids the usage of Carbon\CarbonImmutable. This seems to be due to the check being a "starts with".

How to Reproduce

The following test fails if Carbon\CarbonImmutable is used:

test('non-immutable date instances should not be used')
    ->expect([
        'Carbon\\Carbon',
        'Carbon\\CarbonInterface',
        'Illuminate\\Support\\Carbon',
        'Illuminate\\Support\\Facades\\Date',
    ])
    ->not->toBeUsed();

Versions

Screenshots

CleanShot 2023-12-25 at 05 10 25

CleanShot 2023-12-25 at 05 10 47

Notes

Using ->ignoring('Carbon\\CarbonImmutable') works around the issue.

vintagesucks commented 1 month ago

Reverse problem with false negatives:

Suppose I have the models App\Models\Blog and App\Models\BlogPost, doing the following will not throw an error if App\Models\BlogPost implements the ExampleContract, even though it was not in the ignore list (because App\Models\BlogPost also starts with App\Models\Blog):

arch('models do not implement some contract', function () {
    expect('App\Models')
        ->not->toImplement(ExampleContract::class)
        ->ignoring([
            'App\Models\Blog',
        ]);
});

When testing with explicit classes instead of namespaces, this is rather unexpected behavior.


Versions