-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[8.x] Adds --test
and --pest
options to various make
commands
#38997
Conversation
if it creates |
@peterfox by default, tests generated in the unit folder don't boot the Laravel app as they extend a different testcase. In most of my model tests, I'd want to be hitting the database to make sure I've set up relationships correctly, so I'd need it to be a feature test. Realised I'd accidentally omitted the 'feature' directory from the examples though, so I've updated 👍 |
@lukeraymonddowning sure that makes sense. That might be the only drawback I can see to this PR is it's open to interpretation. Such as for me I probably would want a unit test for a lot of things over a feature test. Equally I'm not a fan of feature tests being structured by class namespace. Not saying it's wrong, just a personal preference. I'm wondering if there's a better syntax rather than just |
I am not a big fan of the folder structure either, but could get used to it. Another alternative could be to create the file as Feature/ProductModelTest.php, Feature/OrderListenerTest.php etc. That way it is obvious what where testing while keeping the folder structure flat. |
I think it would make more sense to put the tests in the unit/feature folder depending on the test. Controller tests goes in the feature folder, and most else goes in the unit folder. I agree with @lukeraymonddowning that you probably want to touch the DB when testing a model, and that by default, that scaffold does not boot the app. It is however fixable by changing the stub file. I really hope this gets merged. I have written a package to solve this exact problem, would be awesome seeing it in core! |
Howdy all!
Hot off the back of #38966, this PR adds a new, simple method of adding matching tests to various
GeneratorCommand
s in the framework, such asmake:model
,make:controller
andmake:job
.Usage
To generate a matching test, simply use the
--test
option for PhpUnit or--pest
for Pest PHP. The test will be placed in thetests
folder under the same directory structure as the generated class. For example,will create a PhpUnit test at
tests/Feature/Models/ProductTest.php
.will create a Pest test at
tests/Feature/Jobs/Shop/ProcessSalesTest.php
Adding to custom/additional commands
If you want to add this functionality to a custom command, it's as easy as adding the
CreatesMatchingTest
trait to any command that extendsGeneratorCommand
.Currently, I've added this functionality to the following commands:
make:command
make:job
make:listener
make:mail
make:model
make:notification
make:controller
make:middleware
The benefit of generating tests this way is being able to quickly create a clean, matching folder structure, which many test suites end up lacking due to inconsistencies.
Let me know if there are other commands you think should have this functionality.
As always, thank you for all your hard work.
Kind Regards,
Luke