- How do I install codenco-dev/eloquent-model-tester in my Laravel project?
- Run `composer require codenco-dev/eloquent-model-tester --dev` to install the package as a development dependency. Ensure your Laravel project uses PHPUnit or PestPHP for testing, as this package integrates with those frameworks.
- Does this package work with Laravel 10+ and its latest features?
- Yes, the package is designed to work with modern Laravel versions, including Laravel 10+. It supports features like model observers and integrates seamlessly with the latest testing conventions, though always verify compatibility with your specific Laravel version.
- Can I test custom foreign keys in relationships (e.g., author_id instead of user_id)?
- The package assumes Laravel’s default naming conventions for relationships (e.g., `user_id`). For custom foreign keys, you’ll need to manually override the assertion methods or extend the package to handle your specific naming patterns.
- Will this package slow down my CI/CD pipeline due to database-dependent tests?
- The package is lightweight (~100KB) and designed for CI/CD efficiency. Use Laravel’s `RefreshDatabase` trait to reset the database between tests, and consider parallelizing test execution to minimize runtime overhead.
- How do I test model relationships like hasMany, belongsTo, or polymorphic relations?
- Use fluent assertions like `assertHasHasManyRelation()`, `assertHasBelongsToRelation()`, or `assertHasMorphToRelation()`. Ensure you’ve set up factories for related models, as the package relies on them to populate test data for relationship validation.
- Does this package support testing soft deletes or custom soft-delete columns?
- Yes, it includes `assertHasSoftDeleteTimestampColumns()` to verify soft-delete functionality. By default, it checks for `deleted_at`, but you can customize the column name by overriding the assertion or extending the package.
- Can I use this package without Laravel factories?
- No, the package requires factories for testing relationships. If you haven’t set them up, generate them using `php artisan make:model ModelName -mf` or manually define factories in `database/factories`.
- How do I test fillable and guarded attributes in my Eloquent models?
- Use `assertCanOnlyFill()` to verify fillable attributes and `assertCanOnlyGuard()` for guarded attributes. Chain these methods with schema assertions for comprehensive validation, e.g., `$this->modelTestable(User::class)->assertHasColumns(['email'])->assertCanOnlyFill(['name', 'email'])`.
- Is there a way to test model scopes or query constraints?
- The package doesn’t directly test scopes or query constraints, as it focuses on structural validation. For scope testing, use Laravel’s native testing tools like `assertDatabaseHas()` or custom assertions in your test classes.
- What alternatives exist for testing Eloquent models in Laravel?
- Alternatives include Laravel’s built-in `Schema` facade (for manual column checks) or packages like `spatie/laravel-test-factories` for factory-based testing. However, `codenco-dev/eloquent-model-tester` uniquely combines schema, fillable/guarded, and relationship validation in a fluent, chainable API.