InterNACHI / modular

Modularize your Laravel application
MIT License
796 stars 65 forks source link

Feature Request: Autoload tests dir using autoload-dev #105

Open cobbles opened 4 weeks ago

cobbles commented 4 weeks ago

Heya :wave: Thanks for your hard working maintaining this package :smile:

Context

The default behavior for creating a new module is to include the tests directory in the autoload of the modules composer.json file.

    "autoload": {
        "psr-4": {
            "Modules\\Order\\": "src/",
            "Modules\\Order\\Tests\\": "tests/",
            "Modules\\Order\\Database\\Factories\\": "database/factories/",
            "Modules\\Order\\Database\\Seeders\\": "database/seeders/"
        }
    },

This makes it easy to run run tests as they get autoloaded as part of the modules package.

The Problem

Including the the tests dir as part of the autoload and not putting it in autoload-dev means that we cant make use of

composer dumpautoload --dev

and

composer dumpautoload --no-dev

Which is valuable when building a production image in order to keep tests out of production code as well as keeping the overall size (Mb) of the final built image as lean as possible.

Possible Solution

Since composer wont let you autoload the dev dependencies of packages in the vendor dir (or modules in our our case) we could instead do two things

  1. Split out the autoloading in the modules composer.json to follow the normal conventions

modules/order/composer.json

    "autoload": {
        "psr-4": {
            "Modules\\Order\\": "src/",
            "Modules\\Order\\Database\\Factories\\": "database/factories/",
            "Modules\\Order\\Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Modules\\Order\\Tests\\": "tests/"
        }
    },
  1. In the root composer.json file we add the tests dir to the autoload-dev section as well

composer.json

    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/",
            "Modules\\Order\\Tests\\": "modules/order/tests/"
        }
    },

This would allow us to still easily run tests after creating a new module but also allow us to exclude test files from production build images.

Lemme know your thoughts and if its something your keen to implement I would be happy to submit a PR

cobbles commented 1 week ago

Heya just touching base on this 🙂