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.07k stars 315 forks source link

[Bug]: Looked in to the Code and Do not understand a line that throws TestCaseAlreadyInUse #1109

Open JesperHerrloff opened 4 months ago

JesperHerrloff commented 4 months ago

What Happened

When use in Laravel Application with custom Module Structure, I didnt manage to get uses function which calls UsesCall Class to work as expected.

If for example:

uses(
    Tests\TestCase::class,
    Illuminate\Foundation\Testing\RefreshDatabase::class,
)->beforeEach(fn () => $this->seed(TestSeeder::class))->in('Feature', __DIR__.'/../modules/*/tests');

It throws this error as the testCase Class is not PHPUnit\Framework\TestCase; It is Tests\TestCase Which is logic as it is set in the uses function. If I comment out the condition if ($testCase->class !== TestCase::class) {

What if the check would check the class not equals the current class and is instance of TestCase::class for example

if ($testCase->class !== $class && $class instanceof TestCase::class) { 
     $testCase->class = $class;
}

Then it works as expected.

But It might be me that not understand the context of Why throwing an TestCaseAlreadyInUse Exception when Classes not Equals.

How to Reproduce

Install a fresh Laravel app, create a modules folder in Root. Create some modules modules/User/tests/Feature modules/Products/tests/Feature

Add a section for Modules in phpunit.xml

<testsuite name="Modules">
            <directory suffix="Test.php">modules/*/tests/Unit</directory>
            <directory suffix="Test.php">modules/*/tests/Feature</directory>
            <directory suffix="Test.php">modules/*/tests/Browser</directory>
        </testsuite>

Try to use uses in Pest.php in Feature, DIR./../modules/*/tests Then it will throw TestCaseAlreadyInUse.

Then my Idea was to Move the uses function to the different TestFiles, but with the same Result.

Sample Repository

No response

Pest Version

v2.34.0

PHP Version

8.3.3

Operation System

macOS

Notes

No response