Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Eloquent Phpunit Laravel Package

erikgall/eloquent-phpunit

Eloquent PHPUnit adds convenient PHPUnit integration for Eloquent models, reducing boilerplate around test setup and database assertions. It helps you verify model behavior and persistence cleanly, making Laravel or standalone Eloquent test suites faster to write and easier to maintain.

View on GitHub
Deep Wiki
Context7

Getting Started

  • Install the package as a dev dependency: composer require --dev <vendor>/<package>. It is intended for test suites, not production runtime.
  • Start with the package README’s relationship assertion examples. In v1.0.7 the new hasOne test method is the first thing to check if you have one-to-one relationships that were previously untested.
  • Minimal first use case: add a relationship test to an existing model test. For example, if a User has one Profile, assert it with the new method:
public function test_user_has_one_profile(): void
{
    $this->assertHasOne(User::class, 'profile');
}
  • Run the relevant test file first (php artisan test --filter=UserTest or ./vendor/bin/phpunit --filter=UserTest) before expanding to the full suite.

Implementation Patterns

  • Use the package’s relationship assertions alongside your existing Eloquent model tests. A common pattern is to create one test class per model and group relationship assertions there.
  • Combine relationship assertions with factory-created records when you need to verify the relationship resolves to the correct related model:
$user = User::factory()->hasProfile()->create();

$this->assertHasOne(User::class, 'profile');
$this->assertTrue($user->profile->is($user->profile()->first()));
  • Use data providers to cover multiple relationships without duplicating test methods:
/**
 * @dataProvider relationshipProvider
 */
public function test_model_relationships(string $model, string $relationship): void
{
    $this->assertHasOne($model, $relationship);
}
  • Integrate into CI by running the relationship tests as part of your normal test suite. Because the package is a dev dependency, no production configuration changes are required.
  • For the new v1.0.7 hasOne support, add or update tests for models where the inverse relationship was already covered but the one-to-one side was missing. This is especially useful for User → Profile, Order → Invoice, and similar domain models.

Gotchas and Tips

  • Relationship assertions usually require the related table to exist and be migrated in the test database. Use RefreshDatabase or DatabaseMigrations and ensure both sides of the relationship are available.
  • Check the method signature in the package source or README before using the new hasOne method. Relationship helpers often accept either a relationship name or a related model class, and the argument order matters.
  • If an assertion fails, inspect the relationship definition directly:
dd($model->relationship()->toSql());

This quickly reveals missing foreign keys, incorrect model classes, or wrong relationship names.

  • Relationship tests verify the relationship definition, not necessarily business logic. Pair them with feature tests that exercise actual queries and constraints.
  • The v1.0.7 release is additive: it adds hasOne test support. No breaking changes are implied by the release note, but update your test coverage if you previously skipped one-to-one relationships because the helper was unavailable.
  • Use the package’s extension points, such as custom test macros or base test case traits, if you need project-specific assertions around relationship behavior.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
robertmarney/lara-hierarchial-collections
simonschaufi/rule-doc-generator
emissaoja/emissaoja
blackbricksoftware/gravityforms-civicrm-form-processor
al-rahabi/fancy-crud
jinas/dhivatar-component
mltstephane/laravel-quality
bernskiold/laravel-talentlms
bernskioldmedia/laravel-form-architect