Skip to content

[Feature] Generate CRUD tests#5945

Open
pxpm wants to merge 19 commits intomainfrom
dusk-test
Open

[Feature] Generate CRUD tests#5945
pxpm wants to merge 19 commits intomainfrom
dusk-test

Conversation

@pxpm
Copy link
Contributor

@pxpm pxpm commented Feb 10, 2026

This PR adds the ability to generate tests for your crud operations. It comes with some helpers that aim to make developers life easier when generating tests for crud controllers.

See the full docs here: Laravel-Backpack/docs#656

pxpm and others added 12 commits January 21, 2026 10:19
Co-authored-by: StyleCI Bot <bot@styleci.io>
Co-authored-by: Cristian Tabacitu <cristian.tabacitu@digitallyhappy.com>
Co-authored-by: StyleCI Bot <bot@styleci.io>
Co-authored-by: Cristian Tabacitu <cristian.tabacitu@digitallyhappy.com>
Co-authored-by: Cristian Tăbăcitu <cristitabacitu@gmail.com>
[ci skip] [skip ci]
Comment on lines 32 to 35
public function setup()
{
// Default setup can be defined here if needed
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this.

* @param string $controllerClass
* @return void
*/
public static function applyConfiguration(string $controllerClass, $operation = 'list'): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this.

* @param string $method
* @return void
*/
public static function mockRoute(array $parameters, string $operation, string $uri = '/', string $method = 'GET'): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move to the BaseTest

}
}

public static function createTestEntry(string $model, array $attributes = [])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's dump the code in the file

Comment on lines 135 to 138
public function invalidInput()
{
return [];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's dump the code in the file

return $model::factory()->raw();
}

public function validUpdateInput($model)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's dump the code in the file

Comment on lines 72 to 75
public function getRouteParameters()
{
return [];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this a property on BaseTest

* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
public function createEntry(array $attributes = [])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's dump the code in the file

return $url;
}

public function createTestEntries(int $count = 5, array $attributes = [])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's dump the code in the file

Comment on lines +16 to +29
/*
|--------------------------------------------------------------------------
| Test Configurations
|--------------------------------------------------------------------------
|
| Here you can map your CrudControllers to a specific TestConfiguration class.
| This allows you to customize the data used for testing, mock dependencies,
| or define valid/invalid inputs for specific controllers.
|
| Example:
| App\Http\Controllers\Admin\ProductCrudController::class => Tests\Config\ProductConfig::class,
|
*/
'configurations' => [],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This goes away

pxpm and others added 3 commits February 17, 2026 16:30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My delete test failed, because my entity had SoftDeletes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove from stubs

Image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Translated columns need stubs updated

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: if CrudController instantiation fails, generate using conventions with comment in TestBase:

/**
 * TODO: This testbase was generated using conventions. Please double-check and fix the configuration below.
 */

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In all my CrudController setup() methods i have something like this:

    public function setup(): void
    {
        CRUD::setModel(\App\Models\City::class);
        CRUD::setRoute(config('backpack.base.route_prefix') . '/city');
        CRUD::setEntityNameStrings(__('common.city'), __('common.cities'));

        $this->checkPermissions();
    }

So when i run the tests, they all fail with:

Image

But it should be a permission error, not "on null".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we have to document the way to easily change actingAs() to be able to add permissions etc. Perhaps a good way would be in DefaultTestBase to be able to do:

public function actingAs() {
	// ..
}

Perhaps we already have there there the code, so they can CHANGE IT instead of having to create the method.

Also maybe we have the code that creates the roles/permissions, commented out, so they can only uncomment it or something.

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

Successfully merging this pull request may close these issues.

3 participants

Comments