codenco-dev/eloquent-model-tester
Laravel dev-only helper to test Eloquent models: verify table structure/columns, fillable vs guarded attributes, and model relationships. Works with PHPUnit and model factories, integrates easily in your model test classes.
Strengths:
TestCase and RefreshDatabase patterns.assertHasColumns()->assertHasHasOneRelation()) reduces boilerplate and improves readability.$fillable, $guarded).HasModelTester), avoiding monolithic test frameworks.Weaknesses:
Laravel Ecosystem Compatibility:
RefreshDatabase (for transactional tests).make:model -mf).assertDatabaseHas).laravel-shift/database-tests or spatie/laravel-test-factory. Requires clear scoping (e.g., use this for schema/relation validation, others for behavior).Non-Laravel Projects:
Low-Medium:
assertHasOnlyColumns() may fail due to temporary migrations or seeding.Mitigation:
Feature/Models test suites, not alongside behavioral tests.pestphp/pest or phpunit for runtime validation.Testing Strategy Alignment:
phpunit or as a separate stage)?Team Adoption:
Schema::hasTable())?assertDatabaseHas)?Edge Cases:
refreshRelation()) or polymorphic relations with custom keys?Performance:
Long-Term Viability:
new Eloquent Model syntax)?Primary Use Case:
Complementary Tools:
| Tool | Purpose | Integration |
|---|---|---|
spatie/laravel-test-factory |
Factory-based testing | Use together for data setup. |
laravel-shift/database-tests |
Database behavior testing | Use for runtime validation. |
mockery/mockery |
Mocking Eloquent models | Not needed; this package is schema-focused. |
pestphp/pest |
Fluent testing syntax | Can replace phpunit for cleaner syntax. |
Assessment Phase:
Schema::hasColumns() + assertHasRelation()).Pilot Implementation:
User, Order) to validate ROI.# Generate test + factory for a model
php artisan make:model Post -mf
php artisan make:test Feature/Models/PostTest
// tests/Feature/Models/PostTest.php
use CodencoDev\EloquentModelTester\HasModelTester;
class PostTest extends TestCase {
use RefreshDatabase, HasModelTester;
public function test_post_model_structure() {
$this->modelTestable(Post::class)
->assertHasOnlyColumns(['id', 'title', 'body', 'user_id', 'created_at', 'updated_at'])
->assertHasBelongsToRelation(User::class)
->assertHasHasManyRelation(Comment::class);
}
}
Gradual Rollout:
Schema::hasTable()).Tooling Integration:
phpunit or Pest test suites. Example GitHub Actions:
- name: Run Model Tests
run: php artisan test --filter "Feature\Models"
modelTestable() assertions.assertHasHasManyRelations vs. assertHasHasManyRelation).-mf flag).RefreshDatabase in TestCase.php (recommended by package).--parallel flag).Schema:: and assertHasRelation() calls.belongsTo with User").How can I help you explore Laravel packages today?