flipjms / laravel-scenarios
An easier way to put your application into different states for easy debugging/development
Installs: 14 273
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- illuminate/contracts: ^9.0|^10.0|^11.0
- nunomaduro/termwind: ^2.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-10-16 15:30:52 UTC
README
This little package helps you set your Laravel application into a given state/scenario.
Let's say that you want to finally fix that pesky edge case bug and for that you need to have your application in a very specific state. Example:
- you need to have a user with role X
- your user needs to have bought two products
- the payment for the second product needs to have status Y
- Another condition
- Extra condition
- Last condition
With this package you can create a new Scenario, write the code needed to have the application in the state above, and run the command to set the scenario.
Voilá! Now you have a nice file descriptive file that sets your application into the right state so you can easily develop against and reset everytime you need it.
Not only that, but it also makes it easy for other people to quickly check which scenarios are available, and use them.
Installation
You can install the package via composer:
composer require flipjms/laravel-scenarios
Go to you composer.json
and add the following:
"autoload": { "psr-4": { "Database\\Scenarios\\": "database/scenarios/" } },
Usage
To create a new scenario:
php artisan make:scenario NewScenarioName
Be descriptive with your names. Do not be afraid of long names.
The file will be created at ./database/scenarios/
Set an alias and a nice description for it. Then on the execute
method add the necessary code to place your
application into the desired state. Usually you do that by running factories, seeders, etc...
The output
method allows you to output information to the console once the scenario has been set. This is useful to
display information that you might need, like a user login, or an url that you might need to visit.
All the Laravel Output IO commands are available apart from the
progress bar.
You can also use Termwind for a prettier output.
To set a scenario
php artisan scenarios:set or php artisan scenarios:set scenario-alias
More often than not, you want to clean your database. If that's the case, you can use the --fresh
option:
php artisan scenarios:set --fresh or php artisan scenarios:set scenario-alias --fresh
Questions
- Why not just use seeders?
- IMHO seeders are nice to put your application into a default state not specific ones.
- Why not use commands?
- You can definitely use commands. I've certainly done so. Nonetheless, I like to see commands as business logic, whereas scenarios is something you need while developing/debugging. This way I can keep my commands folder clean, and my scenarios neatly organized in their own folder.
Credits
License
The MIT License (MIT). Please see License File for more information.