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

[Feature request] Multiple Pest.php support #1261

Open
innocenzi opened this issue Sep 17, 2024 · 0 comments
Open

[Feature request] Multiple Pest.php support #1261

innocenzi opened this issue Sep 17, 2024 · 0 comments

Comments

@innocenzi
Copy link
Contributor

Problem

We are using a modules-based architecture and are colocating test files next to their corresponding classes.
It looks like this:

src/
└── Modules/
    └── BambooImporter/
        ├── BambooImporterServiceProvider.php
        ├── BambooImporterTest.php
        ├── Features/
        │   ├── SomeFeature.php
        │   └── SomeFeatureTest.php
        ├── config.php
        └── ...
tests/
├── TestCase.php
└── Pest.php

The gist of it is that we have a module per functionality, and any amount of nesting inside a module.

As the project grows, the amount of module-specific test configuration we have to put in Pest.php is growing:

// Global configuration
uses(LazilyRefreshDatabase::class, Tests\TestCase::class)
    ->beforeEach(function () {
        \Saloon\Config::preventStrayRequests();
        \Saloon\MockConfig::setFixturePath('tests/Fixtures');
    })
    ->afterEach(fn () => MockClient::destroyGlobal())
    ->in('../src');

// Module-specific configurations
uses()
    ->beforeEach(function () {
        Config::set('bamboo-importer.jwt_key', 'foo');
        Config::set('bamboo-importer.secret', 'foo-bar-baz-foo-bar-baz-foo-bar-baz-foo-bar-baz');
        Config::set('bamboo-importer.site_id', '1234');
    })
    ->in('../src/Modules/BambooImporter');

// other `uses` for each module

Requested solution

As we would like to keep module-specific code in each module, including testing code, it would be great to natively support Pest.php file at the directory level.

I'm not sure how doable it would be, but in our case, since we have a service provider per module, we could do something like that:

// tests/Pest.php
collect(require realpath(__DIR__ . '/../bootstrap/providers.php'))->each(function (string $fqcn) {
    if (!file_exists($path = (dirname((new ReflectionClass($fqcn))->getFileName()) . '/Pest.php'))) {
        return;
    }

    require $path;
});

While this is fine, an improved built-in support for non-Laravel-conventional architectures would be appreciated!

The feature would be something as simple as Pest detecting all Pest.php files and running them. In the example above, the src/Modules/BambooImporter/Pest.php file would still have the scoping responsibility:

uses()
    ->beforeEach(function () {
        Config::set('bamboo-importer.jwt_key', 'foo');
        Config::set('bamboo-importer.secret', 'foo-bar-baz-foo-bar-baz-foo-bar-baz-foo-bar-baz');
        Config::set('bamboo-importer.site_id', '1234');
    })
    ->in(__DIR__);
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

No branches or pull requests

1 participant