CRUD Testing
There are generators to help build up some basic tests for your Cruds.
Command
A new console command has been added to Enso to generate a set of basic tests for a specific crud. In order to use this, you must have already created and set up the crud properly. You will also need to ensure that a model factory exists for the Model that you are testing for. Ideally, this will generate a new instance of the model to meet the minimum requirements for saving (storing or updating) the model via the crud routes. If it does not, you may have to manually update tests once generated to accomodate this.
Once ready, you can run enso artisan make:crud-test crud_name [flags] to generate tests.
The crud_name should be the array key of your crud within the config/enso/crud.php file.
Flags
- a|all - Attempt to create all files
- c|crud - Attempt to create crud test files
- t|traits - Attempt to create additional test traits
- N|never-overwrite - Never overwrite existing files
- F|force - Always overwrite existing files
If you don not specify any flags, no files will be generated.
If you do not specify the N or F flags, you will be asked for each file that already exists whether you would like to overwrite them.
Traits
If you specify the -t flag, the generator will attempt to make:
tests/Traits/CreatesUsers.php. This is required for crud tests to work properly. It provides acreateAdminUserfunction call, which is expected to return a user who has full access to the CMS. The default value is just "a user", so if your CMS requires additional permissions to access, you will need to alter the generated trait. If you already have a trait with this name and location, just add your own createAdminUser function instead. Either way, you should not need to run this flag more than once.
Crud Tests
If you specify the -c flag, it will generate an underpinning trait for these test which contains common functionality.
tests/Traits/Admin/[MODEL_CLASS]CrudTrait.php
The generator will then attempt to make tests to test individual crud route functionality:
tests/Feature/Admin/[MODEL_CLASS]/CreateTest.phptests/Feature/Admin/[MODEL_CLASS]/DestroyTest.phptests/Feature/Admin/[MODEL_CLASS]/EditTest.phptests/Feature/Admin/[MODEL_CLASS]/IndexTest.phptests/Feature/Admin/[MODEL_CLASS]/IndexJsonTest.phptests/Feature/Admin/[MODEL_CLASS]/StoreTest.phptests/Feature/Admin/[MODEL_CLASS]/UpdateTest.php
If you need to generate the IndexJsonTest, StoreTest or UpdateTest, you will also be asked to provide the name of a text field on that model. If your model does not have a string|text column, or if the field has additional validation rules attached (such as email), you may need to alter these files to ensure your tests pass as the default generated values (Value, Initial Value and New Value) may not pass your validation requirements.
If your model factories do no return a instance that complies with all validation rules you have applied to your crud, you will need to update the datasets used in StoreTest and UpdateTest.